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/gstmarshal.h>
149 #include <gst/gst-i18n-lib.h>
151 GST_DEBUG_CATEGORY_STATIC (gst_base_sink_debug);
152 #define GST_CAT_DEFAULT gst_base_sink_debug
154 #define GST_BASE_SINK_GET_PRIVATE(obj) \
155 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_BASE_SINK, GstBaseSinkPrivate))
157 #define GST_FLOW_STEP GST_FLOW_CUSTOM_ERROR
161 gboolean valid; /* if this info is valid */
162 guint32 seqnum; /* the seqnum of the STEP event */
163 GstFormat format; /* the format of the amount */
164 guint64 amount; /* the total amount of data to skip */
165 guint64 position; /* the position in the stepped data */
166 guint64 duration; /* the duration in time of the skipped data */
167 guint64 start; /* running_time of the start */
168 gdouble rate; /* rate of skipping */
169 gdouble start_rate; /* rate before skipping */
170 guint64 start_start; /* start position skipping */
171 guint64 start_stop; /* stop position skipping */
172 gboolean flush; /* if this was a flushing step */
173 gboolean intermediate; /* if this is an intermediate step */
174 gboolean need_preroll; /* if we need preroll after this step */
177 struct _GstBaseSinkPrivate
179 gint qos_enabled; /* ATOMIC */
180 gboolean async_enabled;
181 GstClockTimeDiff ts_offset;
182 GstClockTime render_delay;
184 /* start, stop of current buffer, stream time, used to report position */
185 GstClockTime current_sstart;
186 GstClockTime current_sstop;
188 /* start, stop and jitter of current buffer, running time */
189 GstClockTime current_rstart;
190 GstClockTime current_rstop;
191 GstClockTimeDiff current_jitter;
192 /* the running time of the previous buffer */
193 GstClockTime prev_rstart;
195 /* EOS sync time in running time */
196 GstClockTime eos_rtime;
198 /* last buffer that arrived in time, running time */
199 GstClockTime last_render_time;
200 /* when the last buffer left the sink, running time */
201 GstClockTime last_left;
203 /* running averages go here these are done on running time */
205 GstClockTime avg_duration;
207 GstClockTime avg_in_diff;
209 /* these are done on system time. avg_jitter and avg_render are
210 * compared to eachother to see if the rendering time takes a
211 * huge amount of the processing, If so we are flooded with
213 GstClockTime last_left_systime;
214 GstClockTime avg_jitter;
215 GstClockTime start, stop;
216 GstClockTime avg_render;
218 /* number of rendered and dropped frames */
223 GstClockTime latency;
225 /* if we already commited the state */
227 /* state change to playing ongoing */
230 /* when we received EOS */
231 gboolean received_eos;
233 /* when we are prerolled and able to report latency */
234 gboolean have_latency;
236 /* the last buffer we prerolled or rendered. Useful for making snapshots */
237 gint enable_last_sample; /* atomic */
238 GstBuffer *last_buffer;
241 /* negotiated caps */
244 /* blocksize for pulling */
249 /* seqnum of the stream */
252 gboolean call_preroll;
253 gboolean step_unlock;
255 /* we have a pending and a current step operation */
256 GstStepInfo current_step;
257 GstStepInfo pending_step;
259 /* Cached GstClockID */
260 GstClockID cached_clock_id;
262 /* for throttling and QoS */
263 GstClockTime earliest_in_time;
264 GstClockTime throttle_time;
269 #define DO_RUNNING_AVG(avg,val,size) (((val) + ((size)-1) * (avg)) / (size))
271 /* generic running average, this has a neutral window size */
272 #define UPDATE_RUNNING_AVG(avg,val) DO_RUNNING_AVG(avg,val,8)
274 /* the windows for these running averages are experimentally obtained.
275 * possitive values get averaged more while negative values use a small
276 * window so we can react faster to badness. */
277 #define UPDATE_RUNNING_AVG_P(avg,val) DO_RUNNING_AVG(avg,val,16)
278 #define UPDATE_RUNNING_AVG_N(avg,val) DO_RUNNING_AVG(avg,val,4)
280 /* BaseSink properties */
282 #define DEFAULT_CAN_ACTIVATE_PULL FALSE /* fixme: enable me */
283 #define DEFAULT_CAN_ACTIVATE_PUSH TRUE
285 #define DEFAULT_SYNC TRUE
286 #define DEFAULT_MAX_LATENESS -1
287 #define DEFAULT_QOS FALSE
288 #define DEFAULT_ASYNC TRUE
289 #define DEFAULT_TS_OFFSET 0
290 #define DEFAULT_BLOCKSIZE 4096
291 #define DEFAULT_RENDER_DELAY 0
292 #define DEFAULT_ENABLE_LAST_SAMPLE TRUE
293 #define DEFAULT_THROTTLE_TIME 0
303 PROP_ENABLE_LAST_SAMPLE,
311 static GstElementClass *parent_class = NULL;
313 static void gst_base_sink_class_init (GstBaseSinkClass * klass);
314 static void gst_base_sink_init (GstBaseSink * trans, gpointer g_class);
315 static void gst_base_sink_finalize (GObject * object);
318 gst_base_sink_get_type (void)
320 static volatile gsize base_sink_type = 0;
322 if (g_once_init_enter (&base_sink_type)) {
324 static const GTypeInfo base_sink_info = {
325 sizeof (GstBaseSinkClass),
328 (GClassInitFunc) gst_base_sink_class_init,
331 sizeof (GstBaseSink),
333 (GInstanceInitFunc) gst_base_sink_init,
336 _type = g_type_register_static (GST_TYPE_ELEMENT,
337 "GstBaseSink", &base_sink_info, G_TYPE_FLAG_ABSTRACT);
338 g_once_init_leave (&base_sink_type, _type);
340 return base_sink_type;
343 static void gst_base_sink_set_property (GObject * object, guint prop_id,
344 const GValue * value, GParamSpec * pspec);
345 static void gst_base_sink_get_property (GObject * object, guint prop_id,
346 GValue * value, GParamSpec * pspec);
348 static gboolean gst_base_sink_send_event (GstElement * element,
350 static gboolean default_element_query (GstElement * element, GstQuery * query);
352 static GstCaps *gst_base_sink_default_get_caps (GstBaseSink * sink,
354 static gboolean gst_base_sink_default_set_caps (GstBaseSink * sink,
356 static void gst_base_sink_default_get_times (GstBaseSink * basesink,
357 GstBuffer * buffer, GstClockTime * start, GstClockTime * end);
358 static gboolean gst_base_sink_set_flushing (GstBaseSink * basesink,
359 GstPad * pad, gboolean flushing);
360 static gboolean gst_base_sink_default_activate_pull (GstBaseSink * basesink,
362 static gboolean gst_base_sink_default_do_seek (GstBaseSink * sink,
363 GstSegment * segment);
364 static gboolean gst_base_sink_default_prepare_seek_segment (GstBaseSink * sink,
365 GstEvent * event, GstSegment * segment);
367 static GstStateChangeReturn gst_base_sink_change_state (GstElement * element,
368 GstStateChange transition);
370 static gboolean gst_base_sink_sink_query (GstPad * pad, GstObject * parent,
372 static GstFlowReturn gst_base_sink_chain (GstPad * pad, GstObject * parent,
374 static GstFlowReturn gst_base_sink_chain_list (GstPad * pad, GstObject * parent,
375 GstBufferList * list);
377 static void gst_base_sink_loop (GstPad * pad);
378 static gboolean gst_base_sink_pad_activate (GstPad * pad, GstObject * parent);
379 static gboolean gst_base_sink_pad_activate_mode (GstPad * pad,
380 GstObject * parent, GstPadMode mode, gboolean active);
381 static gboolean gst_base_sink_default_event (GstBaseSink * basesink,
383 static GstFlowReturn gst_base_sink_default_wait_eos (GstBaseSink * basesink,
385 static gboolean gst_base_sink_event (GstPad * pad, GstObject * parent,
388 static gboolean gst_base_sink_default_query (GstBaseSink * sink,
391 static gboolean gst_base_sink_negotiate_pull (GstBaseSink * basesink);
392 static void gst_base_sink_default_fixate (GstBaseSink * bsink, GstCaps * caps);
393 static void 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 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 bclass->fixate (bsink, caps);
616 gst_base_sink_init (GstBaseSink * basesink, gpointer g_class)
618 GstPadTemplate *pad_template;
619 GstBaseSinkPrivate *priv;
621 basesink->priv = priv = GST_BASE_SINK_GET_PRIVATE (basesink);
624 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (g_class), "sink");
625 g_return_if_fail (pad_template != NULL);
627 basesink->sinkpad = gst_pad_new_from_template (pad_template, "sink");
629 gst_pad_set_activate_function (basesink->sinkpad, gst_base_sink_pad_activate);
630 gst_pad_set_activatemode_function (basesink->sinkpad,
631 gst_base_sink_pad_activate_mode);
632 gst_pad_set_query_function (basesink->sinkpad, gst_base_sink_sink_query);
633 gst_pad_set_event_function (basesink->sinkpad, gst_base_sink_event);
634 gst_pad_set_chain_function (basesink->sinkpad, gst_base_sink_chain);
635 gst_pad_set_chain_list_function (basesink->sinkpad, gst_base_sink_chain_list);
636 gst_element_add_pad (GST_ELEMENT_CAST (basesink), basesink->sinkpad);
638 basesink->pad_mode = GST_PAD_MODE_NONE;
639 basesink->preroll_lock = g_mutex_new ();
640 basesink->preroll_cond = g_cond_new ();
641 priv->have_latency = FALSE;
643 basesink->can_activate_push = DEFAULT_CAN_ACTIVATE_PUSH;
644 basesink->can_activate_pull = DEFAULT_CAN_ACTIVATE_PULL;
646 basesink->sync = DEFAULT_SYNC;
647 basesink->max_lateness = DEFAULT_MAX_LATENESS;
648 g_atomic_int_set (&priv->qos_enabled, DEFAULT_QOS);
649 priv->async_enabled = DEFAULT_ASYNC;
650 priv->ts_offset = DEFAULT_TS_OFFSET;
651 priv->render_delay = DEFAULT_RENDER_DELAY;
652 priv->blocksize = DEFAULT_BLOCKSIZE;
653 priv->cached_clock_id = NULL;
654 g_atomic_int_set (&priv->enable_last_sample, DEFAULT_ENABLE_LAST_SAMPLE);
655 priv->throttle_time = DEFAULT_THROTTLE_TIME;
657 GST_OBJECT_FLAG_SET (basesink, GST_ELEMENT_FLAG_SINK);
661 gst_base_sink_finalize (GObject * object)
663 GstBaseSink *basesink;
665 basesink = GST_BASE_SINK (object);
667 g_mutex_free (basesink->preroll_lock);
668 g_cond_free (basesink->preroll_cond);
670 G_OBJECT_CLASS (parent_class)->finalize (object);
674 * gst_base_sink_set_sync:
676 * @sync: the new sync value.
678 * Configures @sink to synchronize on the clock or not. When
679 * @sync is FALSE, incoming samples will be played as fast as
680 * possible. If @sync is TRUE, the timestamps of the incomming
681 * buffers will be used to schedule the exact render time of its
687 gst_base_sink_set_sync (GstBaseSink * sink, gboolean sync)
689 g_return_if_fail (GST_IS_BASE_SINK (sink));
691 GST_OBJECT_LOCK (sink);
693 GST_OBJECT_UNLOCK (sink);
697 * gst_base_sink_get_sync:
700 * Checks if @sink is currently configured to synchronize against the
703 * Returns: TRUE if the sink is configured to synchronize against the clock.
708 gst_base_sink_get_sync (GstBaseSink * sink)
712 g_return_val_if_fail (GST_IS_BASE_SINK (sink), FALSE);
714 GST_OBJECT_LOCK (sink);
716 GST_OBJECT_UNLOCK (sink);
722 * gst_base_sink_set_max_lateness:
724 * @max_lateness: the new max lateness value.
726 * Sets the new max lateness value to @max_lateness. This value is
727 * used to decide if a buffer should be dropped or not based on the
728 * buffer timestamp and the current clock time. A value of -1 means
734 gst_base_sink_set_max_lateness (GstBaseSink * sink, gint64 max_lateness)
736 g_return_if_fail (GST_IS_BASE_SINK (sink));
738 GST_OBJECT_LOCK (sink);
739 sink->max_lateness = max_lateness;
740 GST_OBJECT_UNLOCK (sink);
744 * gst_base_sink_get_max_lateness:
747 * Gets the max lateness value. See gst_base_sink_set_max_lateness for
750 * Returns: The maximum time in nanoseconds that a buffer can be late
751 * before it is dropped and not rendered. A value of -1 means an
757 gst_base_sink_get_max_lateness (GstBaseSink * sink)
761 g_return_val_if_fail (GST_IS_BASE_SINK (sink), -1);
763 GST_OBJECT_LOCK (sink);
764 res = sink->max_lateness;
765 GST_OBJECT_UNLOCK (sink);
771 * gst_base_sink_set_qos_enabled:
773 * @enabled: the new qos value.
775 * Configures @sink to send Quality-of-Service events upstream.
780 gst_base_sink_set_qos_enabled (GstBaseSink * sink, gboolean enabled)
782 g_return_if_fail (GST_IS_BASE_SINK (sink));
784 g_atomic_int_set (&sink->priv->qos_enabled, enabled);
788 * gst_base_sink_is_qos_enabled:
791 * Checks if @sink is currently configured to send Quality-of-Service events
794 * Returns: TRUE if the sink is configured to perform Quality-of-Service.
799 gst_base_sink_is_qos_enabled (GstBaseSink * sink)
803 g_return_val_if_fail (GST_IS_BASE_SINK (sink), FALSE);
805 res = g_atomic_int_get (&sink->priv->qos_enabled);
811 * gst_base_sink_set_async_enabled:
813 * @enabled: the new async value.
815 * Configures @sink to perform all state changes asynchronusly. When async is
816 * disabled, the sink will immediately go to PAUSED instead of waiting for a
817 * preroll buffer. This feature is useful if the sink does not synchronize
818 * against the clock or when it is dealing with sparse streams.
823 gst_base_sink_set_async_enabled (GstBaseSink * sink, gboolean enabled)
825 g_return_if_fail (GST_IS_BASE_SINK (sink));
827 GST_BASE_SINK_PREROLL_LOCK (sink);
828 g_atomic_int_set (&sink->priv->async_enabled, enabled);
829 GST_LOG_OBJECT (sink, "set async enabled to %d", enabled);
830 GST_BASE_SINK_PREROLL_UNLOCK (sink);
834 * gst_base_sink_is_async_enabled:
837 * Checks if @sink is currently configured to perform asynchronous state
840 * Returns: TRUE if the sink is configured to perform asynchronous state
846 gst_base_sink_is_async_enabled (GstBaseSink * sink)
850 g_return_val_if_fail (GST_IS_BASE_SINK (sink), FALSE);
852 res = g_atomic_int_get (&sink->priv->async_enabled);
858 * gst_base_sink_set_ts_offset:
860 * @offset: the new offset
862 * Adjust the synchronisation of @sink with @offset. A negative value will
863 * render buffers earlier than their timestamp. A positive value will delay
864 * rendering. This function can be used to fix playback of badly timestamped
870 gst_base_sink_set_ts_offset (GstBaseSink * sink, GstClockTimeDiff offset)
872 g_return_if_fail (GST_IS_BASE_SINK (sink));
874 GST_OBJECT_LOCK (sink);
875 sink->priv->ts_offset = offset;
876 GST_LOG_OBJECT (sink, "set time offset to %" G_GINT64_FORMAT, offset);
877 GST_OBJECT_UNLOCK (sink);
881 * gst_base_sink_get_ts_offset:
884 * Get the synchronisation offset of @sink.
886 * Returns: The synchronisation offset.
891 gst_base_sink_get_ts_offset (GstBaseSink * sink)
893 GstClockTimeDiff res;
895 g_return_val_if_fail (GST_IS_BASE_SINK (sink), 0);
897 GST_OBJECT_LOCK (sink);
898 res = sink->priv->ts_offset;
899 GST_OBJECT_UNLOCK (sink);
905 * gst_base_sink_get_last_sample:
908 * Get the last sample that arrived in the sink and was used for preroll or for
909 * rendering. This property can be used to generate thumbnails.
911 * The #GstCaps on the sample can be used to determine the type of the buffer.
913 * Free-function: gst_sample_unref
915 * Returns: (transfer full): a #GstSample. gst_sample_unref() after usage.
916 * This function returns NULL when no buffer has arrived in the sink yet
917 * or when the sink is not in PAUSED or PLAYING.
922 gst_base_sink_get_last_sample (GstBaseSink * sink)
924 GstSample *res = NULL;
926 g_return_val_if_fail (GST_IS_BASE_SINK (sink), NULL);
928 GST_OBJECT_LOCK (sink);
929 if (sink->priv->last_buffer) {
930 res = gst_sample_new (sink->priv->last_buffer,
931 sink->priv->last_caps, &sink->segment, NULL);
933 GST_OBJECT_UNLOCK (sink);
938 /* with OBJECT_LOCK */
940 gst_base_sink_set_last_buffer_unlocked (GstBaseSink * sink, GstBuffer * buffer)
944 old = sink->priv->last_buffer;
945 if (G_LIKELY (old != buffer)) {
946 GST_DEBUG_OBJECT (sink, "setting last buffer to %p", buffer);
947 if (G_LIKELY (buffer))
948 gst_buffer_ref (buffer);
949 sink->priv->last_buffer = buffer;
951 /* copy over the caps */
952 gst_caps_replace (&sink->priv->last_caps, sink->priv->caps);
954 gst_caps_replace (&sink->priv->last_caps, NULL);
958 /* avoid unreffing with the lock because cleanup code might want to take the
960 if (G_LIKELY (old)) {
961 GST_OBJECT_UNLOCK (sink);
962 gst_buffer_unref (old);
963 GST_OBJECT_LOCK (sink);
968 gst_base_sink_set_last_buffer (GstBaseSink * sink, GstBuffer * buffer)
970 if (!g_atomic_int_get (&sink->priv->enable_last_sample))
973 GST_OBJECT_LOCK (sink);
974 gst_base_sink_set_last_buffer_unlocked (sink, buffer);
975 GST_OBJECT_UNLOCK (sink);
979 * gst_base_sink_set_last_sample_enabled:
981 * @enabled: the new enable-last-sample value.
983 * Configures @sink to store the last received sample in the last-sample
989 gst_base_sink_set_last_sample_enabled (GstBaseSink * sink, gboolean enabled)
991 g_return_if_fail (GST_IS_BASE_SINK (sink));
993 /* Only take lock if we change the value */
994 if (g_atomic_int_compare_and_exchange (&sink->priv->enable_last_sample,
995 !enabled, enabled) && !enabled) {
996 GST_OBJECT_LOCK (sink);
997 gst_base_sink_set_last_buffer_unlocked (sink, NULL);
998 GST_OBJECT_UNLOCK (sink);
1003 * gst_base_sink_is_last_sample_enabled:
1006 * Checks if @sink is currently configured to store the last received sample in
1007 * the last-sample property.
1009 * Returns: TRUE if the sink is configured to store the last received sample.
1014 gst_base_sink_is_last_sample_enabled (GstBaseSink * sink)
1016 g_return_val_if_fail (GST_IS_BASE_SINK (sink), FALSE);
1018 return g_atomic_int_get (&sink->priv->enable_last_sample);
1022 * gst_base_sink_get_latency:
1025 * Get the currently configured latency.
1027 * Returns: The configured latency.
1032 gst_base_sink_get_latency (GstBaseSink * sink)
1036 GST_OBJECT_LOCK (sink);
1037 res = sink->priv->latency;
1038 GST_OBJECT_UNLOCK (sink);
1044 * gst_base_sink_query_latency:
1046 * @live: (out) (allow-none): if the sink is live
1047 * @upstream_live: (out) (allow-none): if an upstream element is live
1048 * @min_latency: (out) (allow-none): the min latency of the upstream elements
1049 * @max_latency: (out) (allow-none): the max latency of the upstream elements
1051 * Query the sink for the latency parameters. The latency will be queried from
1052 * the upstream elements. @live will be TRUE if @sink is configured to
1053 * synchronize against the clock. @upstream_live will be TRUE if an upstream
1056 * If both @live and @upstream_live are TRUE, the sink will want to compensate
1057 * for the latency introduced by the upstream elements by setting the
1058 * @min_latency to a strictly possitive value.
1060 * This function is mostly used by subclasses.
1062 * Returns: TRUE if the query succeeded.
1067 gst_base_sink_query_latency (GstBaseSink * sink, gboolean * live,
1068 gboolean * upstream_live, GstClockTime * min_latency,
1069 GstClockTime * max_latency)
1071 gboolean l, us_live, res, have_latency;
1072 GstClockTime min, max, render_delay;
1074 GstClockTime us_min, us_max;
1076 /* we are live when we sync to the clock */
1077 GST_OBJECT_LOCK (sink);
1079 have_latency = sink->priv->have_latency;
1080 render_delay = sink->priv->render_delay;
1081 GST_OBJECT_UNLOCK (sink);
1083 /* assume no latency */
1089 GST_DEBUG_OBJECT (sink, "we are ready for LATENCY query");
1090 /* we are ready for a latency query this is when we preroll or when we are
1092 query = gst_query_new_latency ();
1094 /* ask the peer for the latency */
1095 if ((res = gst_pad_peer_query (sink->sinkpad, query))) {
1096 /* get upstream min and max latency */
1097 gst_query_parse_latency (query, &us_live, &us_min, &us_max);
1100 /* upstream live, use its latency, subclasses should use these
1101 * values to create the complete latency. */
1106 /* we need to add the render delay if we are live */
1108 min += render_delay;
1110 max += render_delay;
1113 gst_query_unref (query);
1115 GST_DEBUG_OBJECT (sink, "we are not yet ready for LATENCY query");
1119 /* not live, we tried to do the query, if it failed we return TRUE anyway */
1123 GST_DEBUG_OBJECT (sink, "latency query failed but we are not live");
1125 GST_DEBUG_OBJECT (sink, "latency query failed and we are live");
1130 GST_DEBUG_OBJECT (sink, "latency query: live: %d, have_latency %d,"
1131 " upstream: %d, min %" GST_TIME_FORMAT ", max %" GST_TIME_FORMAT, l,
1132 have_latency, us_live, GST_TIME_ARGS (min), GST_TIME_ARGS (max));
1137 *upstream_live = us_live;
1147 * gst_base_sink_set_render_delay:
1148 * @sink: a #GstBaseSink
1149 * @delay: the new delay
1151 * Set the render delay in @sink to @delay. The render delay is the time
1152 * between actual rendering of a buffer and its synchronisation time. Some
1153 * devices might delay media rendering which can be compensated for with this
1156 * After calling this function, this sink will report additional latency and
1157 * other sinks will adjust their latency to delay the rendering of their media.
1159 * This function is usually called by subclasses.
1164 gst_base_sink_set_render_delay (GstBaseSink * sink, GstClockTime delay)
1166 GstClockTime old_render_delay;
1168 g_return_if_fail (GST_IS_BASE_SINK (sink));
1170 GST_OBJECT_LOCK (sink);
1171 old_render_delay = sink->priv->render_delay;
1172 sink->priv->render_delay = delay;
1173 GST_LOG_OBJECT (sink, "set render delay to %" GST_TIME_FORMAT,
1174 GST_TIME_ARGS (delay));
1175 GST_OBJECT_UNLOCK (sink);
1177 if (delay != old_render_delay) {
1178 GST_DEBUG_OBJECT (sink, "posting latency changed");
1179 gst_element_post_message (GST_ELEMENT_CAST (sink),
1180 gst_message_new_latency (GST_OBJECT_CAST (sink)));
1185 * gst_base_sink_get_render_delay:
1186 * @sink: a #GstBaseSink
1188 * Get the render delay of @sink. see gst_base_sink_set_render_delay() for more
1189 * information about the render delay.
1191 * Returns: the render delay of @sink.
1196 gst_base_sink_get_render_delay (GstBaseSink * sink)
1198 GstClockTimeDiff res;
1200 g_return_val_if_fail (GST_IS_BASE_SINK (sink), 0);
1202 GST_OBJECT_LOCK (sink);
1203 res = sink->priv->render_delay;
1204 GST_OBJECT_UNLOCK (sink);
1210 * gst_base_sink_set_blocksize:
1211 * @sink: a #GstBaseSink
1212 * @blocksize: the blocksize in bytes
1214 * Set the number of bytes that the sink will pull when it is operating in pull
1219 /* FIXME 0.11: blocksize property should be int, otherwise min>max.. */
1221 gst_base_sink_set_blocksize (GstBaseSink * sink, guint blocksize)
1223 g_return_if_fail (GST_IS_BASE_SINK (sink));
1225 GST_OBJECT_LOCK (sink);
1226 sink->priv->blocksize = blocksize;
1227 GST_LOG_OBJECT (sink, "set blocksize to %u", blocksize);
1228 GST_OBJECT_UNLOCK (sink);
1232 * gst_base_sink_get_blocksize:
1233 * @sink: a #GstBaseSink
1235 * Get the number of bytes that the sink will pull when it is operating in pull
1238 * Returns: the number of bytes @sink will pull in pull mode.
1242 /* FIXME 0.11: blocksize property should be int, otherwise min>max.. */
1244 gst_base_sink_get_blocksize (GstBaseSink * sink)
1248 g_return_val_if_fail (GST_IS_BASE_SINK (sink), 0);
1250 GST_OBJECT_LOCK (sink);
1251 res = sink->priv->blocksize;
1252 GST_OBJECT_UNLOCK (sink);
1258 * gst_base_sink_set_throttle_time:
1259 * @sink: a #GstBaseSink
1260 * @throttle: the throttle time in nanoseconds
1262 * Set the time that will be inserted between rendered buffers. This
1263 * can be used to control the maximum buffers per second that the sink
1269 gst_base_sink_set_throttle_time (GstBaseSink * sink, guint64 throttle)
1271 g_return_if_fail (GST_IS_BASE_SINK (sink));
1273 GST_OBJECT_LOCK (sink);
1274 sink->priv->throttle_time = throttle;
1275 GST_LOG_OBJECT (sink, "set throttle_time to %" G_GUINT64_FORMAT, throttle);
1276 GST_OBJECT_UNLOCK (sink);
1280 * gst_base_sink_get_throttle_time:
1281 * @sink: a #GstBaseSink
1283 * Get the time that will be inserted between frames to control the
1284 * maximum buffers per second.
1286 * Returns: the number of nanoseconds @sink will put between frames.
1291 gst_base_sink_get_throttle_time (GstBaseSink * sink)
1295 g_return_val_if_fail (GST_IS_BASE_SINK (sink), 0);
1297 GST_OBJECT_LOCK (sink);
1298 res = sink->priv->throttle_time;
1299 GST_OBJECT_UNLOCK (sink);
1305 gst_base_sink_set_property (GObject * object, guint prop_id,
1306 const GValue * value, GParamSpec * pspec)
1308 GstBaseSink *sink = GST_BASE_SINK (object);
1312 gst_base_sink_set_sync (sink, g_value_get_boolean (value));
1314 case PROP_MAX_LATENESS:
1315 gst_base_sink_set_max_lateness (sink, g_value_get_int64 (value));
1318 gst_base_sink_set_qos_enabled (sink, g_value_get_boolean (value));
1321 gst_base_sink_set_async_enabled (sink, g_value_get_boolean (value));
1323 case PROP_TS_OFFSET:
1324 gst_base_sink_set_ts_offset (sink, g_value_get_int64 (value));
1326 case PROP_BLOCKSIZE:
1327 gst_base_sink_set_blocksize (sink, g_value_get_uint (value));
1329 case PROP_RENDER_DELAY:
1330 gst_base_sink_set_render_delay (sink, g_value_get_uint64 (value));
1332 case PROP_ENABLE_LAST_SAMPLE:
1333 gst_base_sink_set_last_sample_enabled (sink, g_value_get_boolean (value));
1335 case PROP_THROTTLE_TIME:
1336 gst_base_sink_set_throttle_time (sink, g_value_get_uint64 (value));
1339 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1345 gst_base_sink_get_property (GObject * object, guint prop_id, GValue * value,
1348 GstBaseSink *sink = GST_BASE_SINK (object);
1352 g_value_set_boolean (value, gst_base_sink_get_sync (sink));
1354 case PROP_MAX_LATENESS:
1355 g_value_set_int64 (value, gst_base_sink_get_max_lateness (sink));
1358 g_value_set_boolean (value, gst_base_sink_is_qos_enabled (sink));
1361 g_value_set_boolean (value, gst_base_sink_is_async_enabled (sink));
1363 case PROP_TS_OFFSET:
1364 g_value_set_int64 (value, gst_base_sink_get_ts_offset (sink));
1366 case PROP_LAST_SAMPLE:
1367 gst_value_take_buffer (value, gst_base_sink_get_last_sample (sink));
1369 case PROP_ENABLE_LAST_SAMPLE:
1370 g_value_set_boolean (value, gst_base_sink_is_last_sample_enabled (sink));
1372 case PROP_BLOCKSIZE:
1373 g_value_set_uint (value, gst_base_sink_get_blocksize (sink));
1375 case PROP_RENDER_DELAY:
1376 g_value_set_uint64 (value, gst_base_sink_get_render_delay (sink));
1378 case PROP_THROTTLE_TIME:
1379 g_value_set_uint64 (value, gst_base_sink_get_throttle_time (sink));
1382 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1389 gst_base_sink_default_get_caps (GstBaseSink * sink, GstCaps * filter)
1395 gst_base_sink_default_set_caps (GstBaseSink * sink, GstCaps * caps)
1400 /* with PREROLL_LOCK, STREAM_LOCK */
1402 gst_base_sink_commit_state (GstBaseSink * basesink)
1404 /* commit state and proceed to next pending state */
1405 GstState current, next, pending, post_pending;
1406 gboolean post_paused = FALSE;
1407 gboolean post_async_done = FALSE;
1408 gboolean post_playing = FALSE;
1409 gboolean reset_time;
1411 /* we are certainly not playing async anymore now */
1412 basesink->playing_async = FALSE;
1414 GST_OBJECT_LOCK (basesink);
1415 current = GST_STATE (basesink);
1416 next = GST_STATE_NEXT (basesink);
1417 pending = GST_STATE_PENDING (basesink);
1418 post_pending = pending;
1419 reset_time = basesink->priv->reset_time;
1420 basesink->priv->reset_time = FALSE;
1423 case GST_STATE_PLAYING:
1425 GST_DEBUG_OBJECT (basesink, "commiting state to PLAYING");
1427 basesink->need_preroll = FALSE;
1428 post_async_done = TRUE;
1429 basesink->priv->commited = TRUE;
1430 post_playing = TRUE;
1431 /* post PAUSED too when we were READY */
1432 if (current == GST_STATE_READY) {
1437 case GST_STATE_PAUSED:
1438 GST_DEBUG_OBJECT (basesink, "commiting state to PAUSED");
1440 post_async_done = TRUE;
1441 basesink->priv->commited = TRUE;
1442 post_pending = GST_STATE_VOID_PENDING;
1444 case GST_STATE_READY:
1445 case GST_STATE_NULL:
1447 case GST_STATE_VOID_PENDING:
1448 goto nothing_pending;
1453 /* we can report latency queries now */
1454 basesink->priv->have_latency = TRUE;
1456 GST_STATE (basesink) = pending;
1457 GST_STATE_NEXT (basesink) = GST_STATE_VOID_PENDING;
1458 GST_STATE_PENDING (basesink) = GST_STATE_VOID_PENDING;
1459 GST_STATE_RETURN (basesink) = GST_STATE_CHANGE_SUCCESS;
1460 GST_OBJECT_UNLOCK (basesink);
1463 GST_DEBUG_OBJECT (basesink, "posting PAUSED state change message");
1464 gst_element_post_message (GST_ELEMENT_CAST (basesink),
1465 gst_message_new_state_changed (GST_OBJECT_CAST (basesink),
1466 current, next, post_pending));
1468 if (post_async_done) {
1469 GST_DEBUG_OBJECT (basesink, "posting async-done message");
1470 gst_element_post_message (GST_ELEMENT_CAST (basesink),
1471 gst_message_new_async_done (GST_OBJECT_CAST (basesink), reset_time));
1474 GST_DEBUG_OBJECT (basesink, "posting PLAYING state change message");
1475 gst_element_post_message (GST_ELEMENT_CAST (basesink),
1476 gst_message_new_state_changed (GST_OBJECT_CAST (basesink),
1477 next, pending, GST_STATE_VOID_PENDING));
1480 GST_STATE_BROADCAST (basesink);
1486 /* Depending on the state, set our vars. We get in this situation when the
1487 * state change function got a change to update the state vars before the
1488 * streaming thread did. This is fine but we need to make sure that we
1489 * update the need_preroll var since it was TRUE when we got here and might
1490 * become FALSE if we got to PLAYING. */
1491 GST_DEBUG_OBJECT (basesink, "nothing to commit, now in %s",
1492 gst_element_state_get_name (current));
1494 case GST_STATE_PLAYING:
1495 basesink->need_preroll = FALSE;
1497 case GST_STATE_PAUSED:
1498 basesink->need_preroll = TRUE;
1501 basesink->need_preroll = FALSE;
1502 basesink->flushing = TRUE;
1505 /* we can report latency queries now */
1506 basesink->priv->have_latency = TRUE;
1507 GST_OBJECT_UNLOCK (basesink);
1512 /* app is going to READY */
1513 GST_DEBUG_OBJECT (basesink, "stopping");
1514 basesink->need_preroll = FALSE;
1515 basesink->flushing = TRUE;
1516 GST_OBJECT_UNLOCK (basesink);
1522 start_stepping (GstBaseSink * sink, GstSegment * segment,
1523 GstStepInfo * pending, GstStepInfo * current)
1526 GstMessage *message;
1528 GST_DEBUG_OBJECT (sink, "update pending step");
1530 GST_OBJECT_LOCK (sink);
1531 memcpy (current, pending, sizeof (GstStepInfo));
1532 pending->valid = FALSE;
1533 GST_OBJECT_UNLOCK (sink);
1535 /* post message first */
1537 gst_message_new_step_start (GST_OBJECT (sink), TRUE, current->format,
1538 current->amount, current->rate, current->flush, current->intermediate);
1539 gst_message_set_seqnum (message, current->seqnum);
1540 gst_element_post_message (GST_ELEMENT (sink), message);
1542 /* get the running time of where we paused and remember it */
1543 current->start = gst_element_get_start_time (GST_ELEMENT_CAST (sink));
1544 gst_segment_set_running_time (segment, GST_FORMAT_TIME, current->start);
1546 /* set the new rate for the remainder of the segment */
1547 current->start_rate = segment->rate;
1548 segment->rate *= current->rate;
1551 if (segment->rate > 0.0)
1552 current->start_stop = segment->stop;
1554 current->start_start = segment->start;
1556 if (current->format == GST_FORMAT_TIME) {
1557 end = current->start + current->amount;
1558 if (!current->flush) {
1559 /* update the segment clipping regions for non-flushing seeks */
1560 if (segment->rate > 0.0) {
1561 segment->stop = gst_segment_to_position (segment, GST_FORMAT_TIME, end);
1562 segment->position = segment->stop;
1566 position = gst_segment_to_position (segment, GST_FORMAT_TIME, end);
1567 segment->time = position;
1568 segment->start = position;
1569 segment->position = position;
1574 GST_DEBUG_OBJECT (sink, "segment now %" GST_SEGMENT_FORMAT, segment);
1575 GST_DEBUG_OBJECT (sink, "step started at running_time %" GST_TIME_FORMAT,
1576 GST_TIME_ARGS (current->start));
1578 if (current->amount == -1) {
1579 GST_DEBUG_OBJECT (sink, "step amount == -1, stop stepping");
1580 current->valid = FALSE;
1582 GST_DEBUG_OBJECT (sink, "step amount: %" G_GUINT64_FORMAT ", format: %s, "
1583 "rate: %f", current->amount, gst_format_get_name (current->format),
1589 stop_stepping (GstBaseSink * sink, GstSegment * segment,
1590 GstStepInfo * current, gint64 rstart, gint64 rstop, gboolean eos)
1592 gint64 stop, position;
1593 GstMessage *message;
1595 GST_DEBUG_OBJECT (sink, "step complete");
1597 if (segment->rate > 0.0)
1602 GST_DEBUG_OBJECT (sink,
1603 "step stop at running_time %" GST_TIME_FORMAT, GST_TIME_ARGS (stop));
1606 current->duration = current->position;
1608 current->duration = stop - current->start;
1610 GST_DEBUG_OBJECT (sink, "step elapsed running_time %" GST_TIME_FORMAT,
1611 GST_TIME_ARGS (current->duration));
1613 position = current->start + current->duration;
1615 /* now move the segment to the new running time */
1616 gst_segment_set_running_time (segment, GST_FORMAT_TIME, position);
1618 if (current->flush) {
1619 /* and remove the time we flushed, start time did not change */
1620 segment->base = current->start;
1622 /* start time is now the stepped position */
1623 gst_element_set_start_time (GST_ELEMENT_CAST (sink), position);
1626 /* restore the previous rate */
1627 segment->rate = current->start_rate;
1629 if (segment->rate > 0.0)
1630 segment->stop = current->start_stop;
1632 segment->start = current->start_start;
1634 /* post the step done when we know the stepped duration in TIME */
1636 gst_message_new_step_done (GST_OBJECT_CAST (sink), current->format,
1637 current->amount, current->rate, current->flush, current->intermediate,
1638 current->duration, eos);
1639 gst_message_set_seqnum (message, current->seqnum);
1640 gst_element_post_message (GST_ELEMENT_CAST (sink), message);
1642 if (!current->intermediate)
1643 sink->need_preroll = current->need_preroll;
1645 /* and the current step info finished and becomes invalid */
1646 current->valid = FALSE;
1650 handle_stepping (GstBaseSink * sink, GstSegment * segment,
1651 GstStepInfo * current, guint64 * cstart, guint64 * cstop, guint64 * rstart,
1654 gboolean step_end = FALSE;
1656 /* see if we need to skip this buffer because of stepping */
1657 switch (current->format) {
1658 case GST_FORMAT_TIME:
1661 guint64 first, last;
1664 if (segment->rate > 0.0) {
1665 if (segment->stop == *cstop)
1666 *rstop = *rstart + current->amount;
1671 if (segment->start == *cstart)
1672 *rstart = *rstop + current->amount;
1678 end = current->start + current->amount;
1679 current->position = first - current->start;
1681 abs_rate = ABS (segment->rate);
1682 if (G_UNLIKELY (abs_rate != 1.0))
1683 current->position /= abs_rate;
1685 GST_DEBUG_OBJECT (sink,
1686 "buffer: %" GST_TIME_FORMAT "-%" GST_TIME_FORMAT,
1687 GST_TIME_ARGS (first), GST_TIME_ARGS (last));
1688 GST_DEBUG_OBJECT (sink,
1689 "got time step %" GST_TIME_FORMAT "-%" GST_TIME_FORMAT "/%"
1690 GST_TIME_FORMAT, GST_TIME_ARGS (current->position),
1691 GST_TIME_ARGS (last - current->start),
1692 GST_TIME_ARGS (current->amount));
1694 if ((current->flush && current->position >= current->amount)
1696 GST_DEBUG_OBJECT (sink, "step ended, we need clipping");
1698 if (segment->rate > 0.0) {
1700 *cstart = gst_segment_to_position (segment, GST_FORMAT_TIME, end);
1703 *cstop = gst_segment_to_position (segment, GST_FORMAT_TIME, end);
1706 GST_DEBUG_OBJECT (sink,
1707 "cstart %" GST_TIME_FORMAT ", rstart %" GST_TIME_FORMAT,
1708 GST_TIME_ARGS (*cstart), GST_TIME_ARGS (*rstart));
1709 GST_DEBUG_OBJECT (sink,
1710 "cstop %" GST_TIME_FORMAT ", rstop %" GST_TIME_FORMAT,
1711 GST_TIME_ARGS (*cstop), GST_TIME_ARGS (*rstop));
1714 case GST_FORMAT_BUFFERS:
1715 GST_DEBUG_OBJECT (sink,
1716 "got default step %" G_GUINT64_FORMAT "/%" G_GUINT64_FORMAT,
1717 current->position, current->amount);
1719 if (current->position < current->amount) {
1720 current->position++;
1725 case GST_FORMAT_DEFAULT:
1727 GST_DEBUG_OBJECT (sink,
1728 "got unknown step %" G_GUINT64_FORMAT "/%" G_GUINT64_FORMAT,
1729 current->position, current->amount);
1735 /* with STREAM_LOCK, PREROLL_LOCK
1737 * Returns TRUE if the object needs synchronisation and takes therefore
1738 * part in prerolling.
1740 * rsstart/rsstop contain the start/stop in stream time.
1741 * rrstart/rrstop contain the start/stop in running time.
1744 gst_base_sink_get_sync_times (GstBaseSink * basesink, GstMiniObject * obj,
1745 GstClockTime * rsstart, GstClockTime * rsstop,
1746 GstClockTime * rrstart, GstClockTime * rrstop, gboolean * do_sync,
1747 gboolean * stepped, GstStepInfo * step, gboolean * step_end)
1749 GstBaseSinkClass *bclass;
1751 GstClockTime start, stop; /* raw start/stop timestamps */
1752 guint64 cstart, cstop; /* clipped raw timestamps */
1753 guint64 rstart, rstop; /* clipped timestamps converted to running time */
1754 GstClockTime sstart, sstop; /* clipped timestamps converted to stream time */
1756 GstBaseSinkPrivate *priv;
1757 GstSegment *segment;
1760 priv = basesink->priv;
1761 segment = &basesink->segment;
1763 /* start with nothing */
1764 start = stop = GST_CLOCK_TIME_NONE;
1766 if (G_UNLIKELY (GST_IS_EVENT (obj))) {
1767 GstEvent *event = GST_EVENT_CAST (obj);
1769 switch (GST_EVENT_TYPE (event)) {
1770 /* EOS event needs syncing */
1773 if (segment->rate >= 0.0) {
1774 sstart = sstop = priv->current_sstop;
1775 if (!GST_CLOCK_TIME_IS_VALID (sstart)) {
1776 /* we have not seen a buffer yet, use the segment values */
1777 sstart = sstop = gst_segment_to_stream_time (segment,
1778 segment->format, segment->stop);
1781 sstart = sstop = priv->current_sstart;
1782 if (!GST_CLOCK_TIME_IS_VALID (sstart)) {
1783 /* we have not seen a buffer yet, use the segment values */
1784 sstart = sstop = gst_segment_to_stream_time (segment,
1785 segment->format, segment->start);
1789 rstart = rstop = priv->eos_rtime;
1790 *do_sync = rstart != -1;
1791 GST_DEBUG_OBJECT (basesink, "sync times for EOS %" GST_TIME_FORMAT,
1792 GST_TIME_ARGS (rstart));
1793 /* if we are stepping, we end now */
1794 *step_end = step->valid;
1799 /* other events do not need syncing */
1807 /* else do buffer sync code */
1808 buffer = GST_BUFFER_CAST (obj);
1810 bclass = GST_BASE_SINK_GET_CLASS (basesink);
1812 /* just get the times to see if we need syncing, if the start returns -1 we
1814 if (bclass->get_times)
1815 bclass->get_times (basesink, buffer, &start, &stop);
1817 if (!GST_CLOCK_TIME_IS_VALID (start)) {
1818 /* we don't need to sync but we still want to get the timestamps for
1819 * tracking the position */
1820 gst_base_sink_default_get_times (basesink, buffer, &start, &stop);
1826 GST_DEBUG_OBJECT (basesink, "got times start: %" GST_TIME_FORMAT
1827 ", stop: %" GST_TIME_FORMAT ", do_sync %d", GST_TIME_ARGS (start),
1828 GST_TIME_ARGS (stop), *do_sync);
1830 /* collect segment and format for code clarity */
1831 format = segment->format;
1834 if (G_UNLIKELY (!gst_segment_clip (segment, format,
1835 start, stop, &cstart, &cstop))) {
1837 GST_DEBUG_OBJECT (basesink, "step out of segment");
1838 /* when we are stepping, pretend we're at the end of the segment */
1839 if (segment->rate > 0.0) {
1840 cstart = segment->stop;
1841 cstop = segment->stop;
1843 cstart = segment->start;
1844 cstop = segment->start;
1848 goto out_of_segment;
1851 if (G_UNLIKELY (start != cstart || stop != cstop)) {
1852 GST_DEBUG_OBJECT (basesink, "clipped to: start %" GST_TIME_FORMAT
1853 ", stop: %" GST_TIME_FORMAT, GST_TIME_ARGS (cstart),
1854 GST_TIME_ARGS (cstop));
1857 /* set last stop position */
1858 if (G_LIKELY (stop != GST_CLOCK_TIME_NONE && cstop != GST_CLOCK_TIME_NONE))
1859 segment->position = cstop;
1861 segment->position = cstart;
1864 rstart = gst_segment_to_running_time (segment, format, cstart);
1865 rstop = gst_segment_to_running_time (segment, format, cstop);
1867 if (G_UNLIKELY (step->valid)) {
1868 if (!(*step_end = handle_stepping (basesink, segment, step, &cstart, &cstop,
1869 &rstart, &rstop))) {
1870 /* step is still busy, we discard data when we are flushing */
1871 *stepped = step->flush;
1872 GST_DEBUG_OBJECT (basesink, "stepping busy");
1875 /* this can produce wrong values if we accumulated non-TIME segments. If this happens,
1876 * upstream is behaving very badly */
1877 sstart = gst_segment_to_stream_time (segment, format, cstart);
1878 sstop = gst_segment_to_stream_time (segment, format, cstop);
1881 /* eos_done label only called when doing EOS, we also stop stepping then */
1882 if (*step_end && step->flush) {
1883 GST_DEBUG_OBJECT (basesink, "flushing step ended");
1884 stop_stepping (basesink, segment, step, rstart, rstop, eos);
1886 /* re-determine running start times for adjusted segment
1887 * (which has a flushed amount of running/accumulated time removed) */
1888 if (!GST_IS_EVENT (obj)) {
1889 GST_DEBUG_OBJECT (basesink, "refresh sync times");
1900 /* buffers and EOS always need syncing and preroll */
1906 /* we usually clip in the chain function already but stepping could cause
1907 * the segment to be updated later. we return FALSE so that we don't try
1909 GST_LOG_OBJECT (basesink, "buffer skipped, not in segment");
1914 /* with STREAM_LOCK, PREROLL_LOCK, LOCK
1915 * adjust a timestamp with the latency and timestamp offset. This function does
1916 * not adjust for the render delay. */
1918 gst_base_sink_adjust_time (GstBaseSink * basesink, GstClockTime time)
1920 GstClockTimeDiff ts_offset;
1922 /* don't do anything funny with invalid timestamps */
1923 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (time)))
1926 time += basesink->priv->latency;
1928 /* apply offset, be carefull for underflows */
1929 ts_offset = basesink->priv->ts_offset;
1930 if (ts_offset < 0) {
1931 ts_offset = -ts_offset;
1932 if (ts_offset < time)
1939 /* subtract the render delay again, which was included in the latency */
1940 if (time > basesink->priv->render_delay)
1941 time -= basesink->priv->render_delay;
1949 * gst_base_sink_wait_clock:
1951 * @time: the running_time to be reached
1952 * @jitter: (out) (allow-none): the jitter to be filled with time diff, or NULL
1954 * This function will block until @time is reached. It is usually called by
1955 * subclasses that use their own internal synchronisation.
1957 * If @time is not valid, no sycnhronisation is done and #GST_CLOCK_BADTIME is
1958 * returned. Likewise, if synchronisation is disabled in the element or there
1959 * is no clock, no synchronisation is done and #GST_CLOCK_BADTIME is returned.
1961 * This function should only be called with the PREROLL_LOCK held, like when
1962 * receiving an EOS event in the #GstBaseSinkClass.event() vmethod or when
1963 * receiving a buffer in
1964 * the #GstBaseSinkClass.render() vmethod.
1966 * The @time argument should be the running_time of when this method should
1967 * return and is not adjusted with any latency or offset configured in the
1972 * Returns: #GstClockReturn
1975 gst_base_sink_wait_clock (GstBaseSink * sink, GstClockTime time,
1976 GstClockTimeDiff * jitter)
1980 GstClockTime base_time;
1982 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (time)))
1985 GST_OBJECT_LOCK (sink);
1986 if (G_UNLIKELY (!sink->sync))
1989 if (G_UNLIKELY ((clock = GST_ELEMENT_CLOCK (sink)) == NULL))
1992 base_time = GST_ELEMENT_CAST (sink)->base_time;
1993 GST_LOG_OBJECT (sink,
1994 "time %" GST_TIME_FORMAT ", base_time %" GST_TIME_FORMAT,
1995 GST_TIME_ARGS (time), GST_TIME_ARGS (base_time));
1997 /* add base_time to running_time to get the time against the clock */
2000 /* Re-use existing clockid if available */
2001 /* FIXME: Casting to GstClockEntry only works because the types
2003 if (G_LIKELY (sink->priv->cached_clock_id != NULL
2004 && GST_CLOCK_ENTRY_CLOCK ((GstClockEntry *) sink->
2005 priv->cached_clock_id) == clock)) {
2006 if (!gst_clock_single_shot_id_reinit (clock, sink->priv->cached_clock_id,
2008 gst_clock_id_unref (sink->priv->cached_clock_id);
2009 sink->priv->cached_clock_id = gst_clock_new_single_shot_id (clock, time);
2012 if (sink->priv->cached_clock_id != NULL)
2013 gst_clock_id_unref (sink->priv->cached_clock_id);
2014 sink->priv->cached_clock_id = gst_clock_new_single_shot_id (clock, time);
2016 GST_OBJECT_UNLOCK (sink);
2018 /* A blocking wait is performed on the clock. We save the ClockID
2019 * so we can unlock the entry at any time. While we are blocking, we
2020 * release the PREROLL_LOCK so that other threads can interrupt the
2022 sink->clock_id = sink->priv->cached_clock_id;
2023 /* release the preroll lock while waiting */
2024 GST_BASE_SINK_PREROLL_UNLOCK (sink);
2026 ret = gst_clock_id_wait (sink->priv->cached_clock_id, jitter);
2028 GST_BASE_SINK_PREROLL_LOCK (sink);
2029 sink->clock_id = NULL;
2033 /* no syncing needed */
2036 GST_DEBUG_OBJECT (sink, "time not valid, no sync needed");
2037 return GST_CLOCK_BADTIME;
2041 GST_DEBUG_OBJECT (sink, "sync disabled");
2042 GST_OBJECT_UNLOCK (sink);
2043 return GST_CLOCK_BADTIME;
2047 GST_DEBUG_OBJECT (sink, "no clock, can't sync");
2048 GST_OBJECT_UNLOCK (sink);
2049 return GST_CLOCK_BADTIME;
2054 * gst_base_sink_wait_preroll:
2057 * If the #GstBaseSinkClass.render() method performs its own synchronisation
2058 * against the clock it must unblock when going from PLAYING to the PAUSED state
2059 * and call this method before continuing to render the remaining data.
2061 * This function will block until a state change to PLAYING happens (in which
2062 * case this function returns #GST_FLOW_OK) or the processing must be stopped due
2063 * to a state change to READY or a FLUSH event (in which case this function
2064 * returns #GST_FLOW_WRONG_STATE).
2066 * This function should only be called with the PREROLL_LOCK held, like in the
2069 * Returns: #GST_FLOW_OK if the preroll completed and processing can
2070 * continue. Any other return value should be returned from the render vmethod.
2075 gst_base_sink_wait_preroll (GstBaseSink * sink)
2077 sink->have_preroll = TRUE;
2078 GST_DEBUG_OBJECT (sink, "waiting in preroll for flush or PLAYING");
2079 /* block until the state changes, or we get a flush, or something */
2080 GST_BASE_SINK_PREROLL_WAIT (sink);
2081 sink->have_preroll = FALSE;
2082 if (G_UNLIKELY (sink->flushing))
2084 if (G_UNLIKELY (sink->priv->step_unlock))
2086 GST_DEBUG_OBJECT (sink, "continue after preroll");
2093 GST_DEBUG_OBJECT (sink, "preroll interrupted because of flush");
2094 return GST_FLOW_WRONG_STATE;
2098 sink->priv->step_unlock = FALSE;
2099 GST_DEBUG_OBJECT (sink, "preroll interrupted because of step");
2100 return GST_FLOW_STEP;
2105 * gst_base_sink_do_preroll:
2107 * @obj: (transfer none): the mini object that caused the preroll
2109 * If the @sink spawns its own thread for pulling buffers from upstream it
2110 * should call this method after it has pulled a buffer. If the element needed
2111 * to preroll, this function will perform the preroll and will then block
2112 * until the element state is changed.
2114 * This function should be called with the PREROLL_LOCK held.
2116 * Returns: #GST_FLOW_OK if the preroll completed and processing can
2117 * continue. Any other return value should be returned from the render vmethod.
2122 gst_base_sink_do_preroll (GstBaseSink * sink, GstMiniObject * obj)
2126 while (G_UNLIKELY (sink->need_preroll)) {
2127 GST_DEBUG_OBJECT (sink, "prerolling object %p", obj);
2129 /* if it's a buffer, we need to call the preroll method */
2130 if (sink->priv->call_preroll) {
2131 GstBaseSinkClass *bclass;
2134 if (GST_IS_BUFFER_LIST (obj)) {
2135 buf = gst_buffer_list_get (GST_BUFFER_LIST_CAST (obj), 0);
2136 g_assert (NULL != buf);
2137 } else if (GST_IS_BUFFER (obj)) {
2138 buf = GST_BUFFER_CAST (obj);
2139 /* For buffer lists do not set last buffer for now */
2140 gst_base_sink_set_last_buffer (sink, buf);
2145 GST_DEBUG_OBJECT (sink, "preroll buffer %" GST_TIME_FORMAT,
2146 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)));
2148 bclass = GST_BASE_SINK_GET_CLASS (sink);
2149 if (bclass->preroll)
2150 if ((ret = bclass->preroll (sink, buf)) != GST_FLOW_OK)
2151 goto preroll_canceled;
2153 sink->priv->call_preroll = FALSE;
2158 if (G_LIKELY (sink->playing_async)) {
2159 if (G_UNLIKELY (!gst_base_sink_commit_state (sink)))
2163 /* need to recheck here because the commit state could have
2164 * made us not need the preroll anymore */
2165 if (G_LIKELY (sink->need_preroll)) {
2166 /* block until the state changes, or we get a flush, or something */
2167 ret = gst_base_sink_wait_preroll (sink);
2168 if ((ret != GST_FLOW_OK) && (ret != GST_FLOW_STEP))
2169 goto preroll_failed;
2177 GST_DEBUG_OBJECT (sink, "preroll failed, abort state");
2178 gst_element_abort_state (GST_ELEMENT_CAST (sink));
2183 GST_DEBUG_OBJECT (sink, "stopping while commiting state");
2184 return GST_FLOW_WRONG_STATE;
2188 GST_DEBUG_OBJECT (sink, "preroll failed: %s", gst_flow_get_name (ret));
2194 * gst_base_sink_wait_eos:
2196 * @time: the running_time to be reached
2197 * @jitter: (out) (allow-none): the jitter to be filled with time diff, or NULL
2199 * This function will block until @time is reached. It is usually called by
2200 * subclasses that use their own internal synchronisation but want to let the
2201 * EOS be handled by the base class.
2203 * This function should only be called with the PREROLL_LOCK held, like when
2204 * receiving an EOS event in the ::event vmethod.
2206 * The @time argument should be the running_time of when the EOS should happen
2207 * and will be adjusted with any latency and offset configured in the sink.
2209 * Returns: #GstFlowReturn
2214 gst_base_sink_wait_eos (GstBaseSink * sink, GstClockTime time,
2215 GstClockTimeDiff * jitter)
2217 GstClockReturn status;
2223 GST_DEBUG_OBJECT (sink, "checking preroll");
2225 /* first wait for the playing state before we can continue */
2226 while (G_UNLIKELY (sink->need_preroll)) {
2227 ret = gst_base_sink_wait_preroll (sink);
2228 if ((ret != GST_FLOW_OK) && (ret != GST_FLOW_STEP))
2232 /* preroll done, we can sync since we are in PLAYING now. */
2233 GST_DEBUG_OBJECT (sink, "possibly waiting for clock to reach %"
2234 GST_TIME_FORMAT, GST_TIME_ARGS (time));
2236 /* compensate for latency and ts_offset. We don't adjust for render delay
2237 * because we don't interact with the device on EOS normally. */
2238 stime = gst_base_sink_adjust_time (sink, time);
2240 /* wait for the clock, this can be interrupted because we got shut down or
2242 status = gst_base_sink_wait_clock (sink, stime, jitter);
2244 GST_DEBUG_OBJECT (sink, "clock returned %d", status);
2246 /* invalid time, no clock or sync disabled, just continue then */
2247 if (status == GST_CLOCK_BADTIME)
2250 /* waiting could have been interrupted and we can be flushing now */
2251 if (G_UNLIKELY (sink->flushing))
2254 /* retry if we got unscheduled, which means we did not reach the timeout
2255 * yet. if some other error occures, we continue. */
2256 } while (status == GST_CLOCK_UNSCHEDULED);
2258 GST_DEBUG_OBJECT (sink, "end of stream");
2265 GST_DEBUG_OBJECT (sink, "we are flushing");
2266 return GST_FLOW_WRONG_STATE;
2270 /* with STREAM_LOCK, PREROLL_LOCK
2272 * Make sure we are in PLAYING and synchronize an object to the clock.
2274 * If we need preroll, we are not in PLAYING. We try to commit the state
2275 * if needed and then block if we still are not PLAYING.
2277 * We start waiting on the clock in PLAYING. If we got interrupted, we
2278 * immediately try to re-preroll.
2280 * Some objects do not need synchronisation (most events) and so this function
2281 * immediately returns GST_FLOW_OK.
2283 * for objects that arrive later than max-lateness to be synchronized to the
2284 * clock have the @late boolean set to TRUE.
2286 * This function keeps a running average of the jitter (the diff between the
2287 * clock time and the requested sync time). The jitter is negative for
2288 * objects that arrive in time and positive for late buffers.
2290 * does not take ownership of obj.
2292 static GstFlowReturn
2293 gst_base_sink_do_sync (GstBaseSink * basesink,
2294 GstMiniObject * obj, gboolean * late, gboolean * step_end)
2296 GstClockTimeDiff jitter = 0;
2298 GstClockReturn status = GST_CLOCK_OK;
2299 GstClockTime rstart, rstop, sstart, sstop, stime;
2301 GstBaseSinkPrivate *priv;
2303 GstStepInfo *current, *pending;
2306 priv = basesink->priv;
2309 sstart = sstop = rstart = rstop = GST_CLOCK_TIME_NONE;
2313 priv->current_rstart = GST_CLOCK_TIME_NONE;
2315 /* get stepping info */
2316 current = &priv->current_step;
2317 pending = &priv->pending_step;
2319 /* get timing information for this object against the render segment */
2320 syncable = gst_base_sink_get_sync_times (basesink, obj,
2321 &sstart, &sstop, &rstart, &rstop, &do_sync, &stepped, current, step_end);
2323 if (G_UNLIKELY (stepped))
2326 /* a syncable object needs to participate in preroll and
2327 * clocking. All buffers and EOS are syncable. */
2328 if (G_UNLIKELY (!syncable))
2331 /* store timing info for current object */
2332 priv->current_rstart = rstart;
2333 priv->current_rstop = (GST_CLOCK_TIME_IS_VALID (rstop) ? rstop : rstart);
2335 /* save sync time for eos when the previous object needed sync */
2336 priv->eos_rtime = (do_sync ? priv->current_rstop : GST_CLOCK_TIME_NONE);
2338 /* calculate inter frame spacing */
2339 if (G_UNLIKELY (priv->prev_rstart != -1 && priv->prev_rstart < rstart)) {
2340 GstClockTime in_diff;
2342 in_diff = rstart - priv->prev_rstart;
2344 if (priv->avg_in_diff == -1)
2345 priv->avg_in_diff = in_diff;
2347 priv->avg_in_diff = UPDATE_RUNNING_AVG (priv->avg_in_diff, in_diff);
2349 GST_LOG_OBJECT (basesink, "avg frame diff %" GST_TIME_FORMAT,
2350 GST_TIME_ARGS (priv->avg_in_diff));
2353 priv->prev_rstart = rstart;
2355 if (G_UNLIKELY (priv->earliest_in_time != -1
2356 && rstart < priv->earliest_in_time))
2360 /* first do preroll, this makes sure we commit our state
2361 * to PAUSED and can continue to PLAYING. We cannot perform
2362 * any clock sync in PAUSED because there is no clock. */
2363 ret = gst_base_sink_do_preroll (basesink, obj);
2364 if (G_UNLIKELY (ret != GST_FLOW_OK))
2365 goto preroll_failed;
2367 /* update the segment with a pending step if the current one is invalid and we
2368 * have a new pending one. We only accept new step updates after a preroll */
2369 if (G_UNLIKELY (pending->valid && !current->valid)) {
2370 start_stepping (basesink, &basesink->segment, pending, current);
2374 /* After rendering we store the position of the last buffer so that we can use
2375 * it to report the position. We need to take the lock here. */
2376 GST_OBJECT_LOCK (basesink);
2377 priv->current_sstart = sstart;
2378 priv->current_sstop = (GST_CLOCK_TIME_IS_VALID (sstop) ? sstop : sstart);
2379 GST_OBJECT_UNLOCK (basesink);
2384 /* adjust for latency */
2385 stime = gst_base_sink_adjust_time (basesink, rstart);
2387 /* adjust for render-delay, avoid underflows */
2388 if (GST_CLOCK_TIME_IS_VALID (stime)) {
2389 if (stime > priv->render_delay)
2390 stime -= priv->render_delay;
2395 /* preroll done, we can sync since we are in PLAYING now. */
2396 GST_DEBUG_OBJECT (basesink, "possibly waiting for clock to reach %"
2397 GST_TIME_FORMAT ", adjusted %" GST_TIME_FORMAT,
2398 GST_TIME_ARGS (rstart), GST_TIME_ARGS (stime));
2400 /* This function will return immediately if start == -1, no clock
2401 * or sync is disabled with GST_CLOCK_BADTIME. */
2402 status = gst_base_sink_wait_clock (basesink, stime, &jitter);
2404 GST_DEBUG_OBJECT (basesink, "clock returned %d, jitter %c%" GST_TIME_FORMAT,
2405 status, (jitter < 0 ? '-' : ' '), GST_TIME_ARGS (ABS (jitter)));
2407 /* invalid time, no clock or sync disabled, just render */
2408 if (status == GST_CLOCK_BADTIME)
2411 /* waiting could have been interrupted and we can be flushing now */
2412 if (G_UNLIKELY (basesink->flushing))
2415 /* check for unlocked by a state change, we are not flushing so
2416 * we can try to preroll on the current buffer. */
2417 if (G_UNLIKELY (status == GST_CLOCK_UNSCHEDULED)) {
2418 GST_DEBUG_OBJECT (basesink, "unscheduled, waiting some more");
2419 priv->call_preroll = TRUE;
2423 /* successful syncing done, record observation */
2424 priv->current_jitter = jitter;
2426 /* check if the object should be dropped */
2427 *late = gst_base_sink_is_too_late (basesink, obj, rstart, rstop,
2436 GST_DEBUG_OBJECT (basesink, "skipped stepped object %p", obj);
2442 GST_DEBUG_OBJECT (basesink, "non syncable object %p", obj);
2447 GST_DEBUG_OBJECT (basesink, "dropped because of QoS %p", obj);
2453 GST_DEBUG_OBJECT (basesink, "we are flushing");
2454 return GST_FLOW_WRONG_STATE;
2458 GST_DEBUG_OBJECT (basesink, "preroll failed");
2465 gst_base_sink_send_qos (GstBaseSink * basesink, GstQOSType type,
2466 gdouble proportion, GstClockTime time, GstClockTimeDiff diff)
2471 /* generate Quality-of-Service event */
2472 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, basesink,
2473 "qos: type %d, proportion: %lf, diff %" G_GINT64_FORMAT ", timestamp %"
2474 GST_TIME_FORMAT, type, proportion, diff, GST_TIME_ARGS (time));
2476 event = gst_event_new_qos (type, proportion, diff, time);
2479 res = gst_pad_push_event (basesink->sinkpad, event);
2485 gst_base_sink_perform_qos (GstBaseSink * sink, gboolean dropped)
2487 GstBaseSinkPrivate *priv;
2488 GstClockTime start, stop;
2489 GstClockTimeDiff jitter;
2490 GstClockTime pt, entered, left;
2491 GstClockTime duration;
2496 start = priv->current_rstart;
2498 if (priv->current_step.valid)
2501 /* if Quality-of-Service disabled, do nothing */
2502 if (!g_atomic_int_get (&priv->qos_enabled) ||
2503 !GST_CLOCK_TIME_IS_VALID (start))
2506 stop = priv->current_rstop;
2507 jitter = priv->current_jitter;
2510 /* this is the time the buffer entered the sink */
2511 if (start < -jitter)
2514 entered = start + jitter;
2517 /* this is the time the buffer entered the sink */
2518 entered = start + jitter;
2519 /* this is the time the buffer left the sink */
2520 left = start + jitter;
2523 /* calculate duration of the buffer */
2524 if (GST_CLOCK_TIME_IS_VALID (stop) && stop != start)
2525 duration = stop - start;
2527 duration = priv->avg_in_diff;
2529 /* if we have the time when the last buffer left us, calculate
2530 * processing time */
2531 if (GST_CLOCK_TIME_IS_VALID (priv->last_left)) {
2532 if (entered > priv->last_left) {
2533 pt = entered - priv->last_left;
2541 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, sink, "start: %" GST_TIME_FORMAT
2542 ", stop %" GST_TIME_FORMAT ", entered %" GST_TIME_FORMAT ", left %"
2543 GST_TIME_FORMAT ", pt: %" GST_TIME_FORMAT ", duration %" GST_TIME_FORMAT
2544 ",jitter %" G_GINT64_FORMAT, GST_TIME_ARGS (start), GST_TIME_ARGS (stop),
2545 GST_TIME_ARGS (entered), GST_TIME_ARGS (left), GST_TIME_ARGS (pt),
2546 GST_TIME_ARGS (duration), jitter);
2548 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, sink, "avg_duration: %" GST_TIME_FORMAT
2549 ", avg_pt: %" GST_TIME_FORMAT ", avg_rate: %g",
2550 GST_TIME_ARGS (priv->avg_duration), GST_TIME_ARGS (priv->avg_pt),
2553 /* collect running averages. for first observations, we copy the
2555 if (!GST_CLOCK_TIME_IS_VALID (priv->avg_duration))
2556 priv->avg_duration = duration;
2558 priv->avg_duration = UPDATE_RUNNING_AVG (priv->avg_duration, duration);
2560 if (!GST_CLOCK_TIME_IS_VALID (priv->avg_pt))
2563 priv->avg_pt = UPDATE_RUNNING_AVG (priv->avg_pt, pt);
2565 if (priv->avg_duration != 0)
2567 gst_guint64_to_gdouble (priv->avg_pt) /
2568 gst_guint64_to_gdouble (priv->avg_duration);
2572 if (GST_CLOCK_TIME_IS_VALID (priv->last_left)) {
2573 if (dropped || priv->avg_rate < 0.0) {
2574 priv->avg_rate = rate;
2577 priv->avg_rate = UPDATE_RUNNING_AVG_N (priv->avg_rate, rate);
2579 priv->avg_rate = UPDATE_RUNNING_AVG_P (priv->avg_rate, rate);
2583 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, sink,
2584 "updated: avg_duration: %" GST_TIME_FORMAT ", avg_pt: %" GST_TIME_FORMAT
2585 ", avg_rate: %g", GST_TIME_ARGS (priv->avg_duration),
2586 GST_TIME_ARGS (priv->avg_pt), priv->avg_rate);
2589 if (priv->avg_rate >= 0.0) {
2591 GstClockTimeDiff diff;
2593 /* if we have a valid rate, start sending QoS messages */
2594 if (priv->current_jitter < 0) {
2595 /* make sure we never go below 0 when adding the jitter to the
2597 if (priv->current_rstart < -priv->current_jitter)
2598 priv->current_jitter = -priv->current_rstart;
2601 if (priv->throttle_time > 0) {
2602 diff = priv->throttle_time;
2603 type = GST_QOS_TYPE_THROTTLE;
2605 diff = priv->current_jitter;
2607 type = GST_QOS_TYPE_OVERFLOW;
2609 type = GST_QOS_TYPE_UNDERFLOW;
2612 gst_base_sink_send_qos (sink, type, priv->avg_rate, priv->current_rstart,
2616 /* record when this buffer will leave us */
2617 priv->last_left = left;
2620 /* reset all qos measuring */
2622 gst_base_sink_reset_qos (GstBaseSink * sink)
2624 GstBaseSinkPrivate *priv;
2628 priv->last_render_time = GST_CLOCK_TIME_NONE;
2629 priv->prev_rstart = GST_CLOCK_TIME_NONE;
2630 priv->earliest_in_time = GST_CLOCK_TIME_NONE;
2631 priv->last_left = GST_CLOCK_TIME_NONE;
2632 priv->avg_duration = GST_CLOCK_TIME_NONE;
2633 priv->avg_pt = GST_CLOCK_TIME_NONE;
2634 priv->avg_rate = -1.0;
2635 priv->avg_render = GST_CLOCK_TIME_NONE;
2636 priv->avg_in_diff = GST_CLOCK_TIME_NONE;
2642 /* Checks if the object was scheduled too late.
2644 * rstart/rstop contain the running_time start and stop values
2647 * status and jitter contain the return values from the clock wait.
2649 * returns TRUE if the buffer was too late.
2652 gst_base_sink_is_too_late (GstBaseSink * basesink, GstMiniObject * obj,
2653 GstClockTime rstart, GstClockTime rstop,
2654 GstClockReturn status, GstClockTimeDiff jitter)
2657 guint64 max_lateness;
2658 GstBaseSinkPrivate *priv;
2660 priv = basesink->priv;
2664 /* only for objects that were too late */
2665 if (G_LIKELY (status != GST_CLOCK_EARLY))
2668 max_lateness = basesink->max_lateness;
2670 /* check if frame dropping is enabled */
2671 if (max_lateness == -1)
2674 /* only check for buffers */
2675 if (G_UNLIKELY (!GST_IS_BUFFER (obj)))
2678 /* can't do check if we don't have a timestamp */
2679 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (rstart)))
2682 /* we can add a valid stop time */
2683 if (GST_CLOCK_TIME_IS_VALID (rstop))
2684 max_lateness += rstop;
2686 max_lateness += rstart;
2687 /* no stop time, use avg frame diff */
2688 if (priv->avg_in_diff != -1)
2689 max_lateness += priv->avg_in_diff;
2692 /* if the jitter bigger than duration and lateness we are too late */
2693 if ((late = rstart + jitter > max_lateness)) {
2694 GST_CAT_DEBUG_OBJECT (GST_CAT_PERFORMANCE, basesink,
2695 "buffer is too late %" GST_TIME_FORMAT
2696 " > %" GST_TIME_FORMAT, GST_TIME_ARGS (rstart + jitter),
2697 GST_TIME_ARGS (max_lateness));
2698 /* !!emergency!!, if we did not receive anything valid for more than a
2699 * second, render it anyway so the user sees something */
2700 if (GST_CLOCK_TIME_IS_VALID (priv->last_render_time) &&
2701 rstart - priv->last_render_time > GST_SECOND) {
2703 GST_ELEMENT_WARNING (basesink, CORE, CLOCK,
2704 (_("A lot of buffers are being dropped.")),
2705 ("There may be a timestamping problem, or this computer is too slow."));
2706 GST_CAT_DEBUG_OBJECT (GST_CAT_PERFORMANCE, basesink,
2707 "**emergency** last buffer at %" GST_TIME_FORMAT " > GST_SECOND",
2708 GST_TIME_ARGS (priv->last_render_time));
2713 if (!late || !GST_CLOCK_TIME_IS_VALID (priv->last_render_time)) {
2714 priv->last_render_time = rstart;
2715 /* the next allowed input timestamp */
2716 if (priv->throttle_time > 0)
2717 priv->earliest_in_time = rstart + priv->throttle_time;
2724 GST_DEBUG_OBJECT (basesink, "object was scheduled in time");
2729 GST_DEBUG_OBJECT (basesink, "frame dropping disabled");
2734 GST_DEBUG_OBJECT (basesink, "object is not a buffer");
2739 GST_DEBUG_OBJECT (basesink, "buffer has no timestamp");
2744 /* called before and after calling the render vmethod. It keeps track of how
2745 * much time was spent in the render method and is used to check if we are
2748 gst_base_sink_do_render_stats (GstBaseSink * basesink, gboolean start)
2750 GstBaseSinkPrivate *priv;
2752 priv = basesink->priv;
2755 priv->start = gst_util_get_timestamp ();
2757 GstClockTime elapsed;
2759 priv->stop = gst_util_get_timestamp ();
2761 elapsed = GST_CLOCK_DIFF (priv->start, priv->stop);
2763 if (!GST_CLOCK_TIME_IS_VALID (priv->avg_render))
2764 priv->avg_render = elapsed;
2766 priv->avg_render = UPDATE_RUNNING_AVG (priv->avg_render, elapsed);
2768 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, basesink,
2769 "avg_render: %" GST_TIME_FORMAT, GST_TIME_ARGS (priv->avg_render));
2774 gst_base_sink_flush_start (GstBaseSink * basesink, GstPad * pad)
2776 /* make sure we are not blocked on the clock also clear any pending
2778 gst_base_sink_set_flushing (basesink, pad, TRUE);
2780 /* we grab the stream lock but that is not needed since setting the
2781 * sink to flushing would make sure no state commit is being done
2783 GST_PAD_STREAM_LOCK (pad);
2784 gst_base_sink_reset_qos (basesink);
2785 /* and we need to commit our state again on the next
2786 * prerolled buffer */
2787 basesink->playing_async = TRUE;
2788 if (basesink->priv->async_enabled) {
2789 gst_element_lost_state (GST_ELEMENT_CAST (basesink));
2791 /* start time reset in above case as well;
2792 * arranges for a.o. proper position reporting when flushing in PAUSED */
2793 gst_element_set_start_time (GST_ELEMENT_CAST (basesink), 0);
2794 basesink->priv->have_latency = TRUE;
2796 gst_base_sink_set_last_buffer (basesink, NULL);
2797 GST_PAD_STREAM_UNLOCK (pad);
2801 gst_base_sink_flush_stop (GstBaseSink * basesink, GstPad * pad,
2802 gboolean reset_time)
2804 /* unset flushing so we can accept new data, this also flushes out any EOS
2806 gst_base_sink_set_flushing (basesink, pad, FALSE);
2808 /* for position reporting */
2809 GST_OBJECT_LOCK (basesink);
2810 basesink->priv->current_sstart = GST_CLOCK_TIME_NONE;
2811 basesink->priv->current_sstop = GST_CLOCK_TIME_NONE;
2812 basesink->priv->eos_rtime = GST_CLOCK_TIME_NONE;
2813 basesink->priv->call_preroll = TRUE;
2814 basesink->priv->current_step.valid = FALSE;
2815 basesink->priv->pending_step.valid = FALSE;
2816 if (basesink->pad_mode == GST_PAD_MODE_PUSH) {
2817 /* we need new segment info after the flush. */
2818 basesink->have_newsegment = FALSE;
2820 gst_segment_init (&basesink->segment, GST_FORMAT_UNDEFINED);
2823 basesink->priv->reset_time = reset_time;
2824 GST_OBJECT_UNLOCK (basesink);
2827 static GstFlowReturn
2828 gst_base_sink_default_wait_eos (GstBaseSink * basesink, GstEvent * event)
2831 gboolean late, step_end;
2833 ret = gst_base_sink_do_sync (basesink, GST_MINI_OBJECT_CAST (event),
2840 gst_base_sink_default_event (GstBaseSink * basesink, GstEvent * event)
2842 gboolean result = TRUE;
2843 GstBaseSinkClass *bclass;
2845 bclass = GST_BASE_SINK_GET_CLASS (basesink);
2847 switch (GST_EVENT_TYPE (event)) {
2848 case GST_EVENT_FLUSH_START:
2850 GST_DEBUG_OBJECT (basesink, "flush-start %p", event);
2851 gst_base_sink_flush_start (basesink, basesink->sinkpad);
2854 case GST_EVENT_FLUSH_STOP:
2856 gboolean reset_time;
2858 gst_event_parse_flush_stop (event, &reset_time);
2859 GST_DEBUG_OBJECT (basesink, "flush-stop %p, reset_time: %d", event,
2861 gst_base_sink_flush_stop (basesink, basesink->sinkpad, reset_time);
2866 GstMessage *message;
2869 /* we set the received EOS flag here so that we can use it when testing if
2870 * we are prerolled and to refuse more buffers. */
2871 basesink->priv->received_eos = TRUE;
2874 if (G_LIKELY (bclass->wait_eos)) {
2877 ret = bclass->wait_eos (basesink, event);
2878 if (G_UNLIKELY (ret != GST_FLOW_OK)) {
2884 /* the EOS event is completely handled so we mark
2885 * ourselves as being in the EOS state. eos is also
2886 * protected by the object lock so we can read it when
2887 * answering the POSITION query. */
2888 GST_OBJECT_LOCK (basesink);
2889 basesink->eos = TRUE;
2890 GST_OBJECT_UNLOCK (basesink);
2892 /* ok, now we can post the message */
2893 GST_DEBUG_OBJECT (basesink, "Now posting EOS");
2895 seqnum = basesink->priv->seqnum = gst_event_get_seqnum (event);
2896 GST_DEBUG_OBJECT (basesink, "Got seqnum #%" G_GUINT32_FORMAT, seqnum);
2898 message = gst_message_new_eos (GST_OBJECT_CAST (basesink));
2899 gst_message_set_seqnum (message, seqnum);
2900 gst_element_post_message (GST_ELEMENT_CAST (basesink), message);
2903 case GST_EVENT_CAPS:
2907 GST_DEBUG_OBJECT (basesink, "caps %p", event);
2909 gst_event_parse_caps (event, &caps);
2910 if (bclass->set_caps)
2911 result = bclass->set_caps (basesink, caps);
2914 GST_OBJECT_LOCK (basesink);
2915 gst_caps_replace (&basesink->priv->caps, caps);
2916 GST_OBJECT_UNLOCK (basesink);
2920 case GST_EVENT_SEGMENT:
2921 /* configure the segment */
2922 /* The segment is protected with both the STREAM_LOCK and the OBJECT_LOCK.
2923 * We protect with the OBJECT_LOCK so that we can use the values to
2924 * safely answer a POSITION query. */
2925 GST_OBJECT_LOCK (basesink);
2926 /* the newsegment event is needed to bring the buffer timestamps to the
2927 * stream time and to drop samples outside of the playback segment. */
2928 gst_event_copy_segment (event, &basesink->segment);
2929 GST_DEBUG_OBJECT (basesink, "configured SEGMENT %" GST_SEGMENT_FORMAT,
2930 &basesink->segment);
2931 basesink->have_newsegment = TRUE;
2932 GST_OBJECT_UNLOCK (basesink);
2936 GstTagList *taglist;
2938 gst_event_parse_tag (event, &taglist);
2940 gst_element_post_message (GST_ELEMENT_CAST (basesink),
2941 gst_message_new_tag (GST_OBJECT_CAST (basesink),
2942 gst_tag_list_copy (taglist)));
2945 case GST_EVENT_SINK_MESSAGE:
2947 GstMessage *msg = NULL;
2949 gst_event_parse_sink_message (event, &msg);
2951 gst_element_post_message (GST_ELEMENT_CAST (basesink), msg);
2958 gst_event_unref (event);
2964 gst_base_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
2966 GstBaseSink *basesink;
2967 gboolean result = TRUE;
2968 GstBaseSinkClass *bclass;
2970 basesink = GST_BASE_SINK_CAST (parent);
2971 bclass = GST_BASE_SINK_GET_CLASS (basesink);
2973 GST_DEBUG_OBJECT (basesink, "received event %p %" GST_PTR_FORMAT, event,
2976 switch (GST_EVENT_TYPE (event)) {
2977 case GST_EVENT_FLUSH_STOP:
2978 /* special case for this serialized event because we don't want to grab
2979 * the PREROLL lock or check if we were flushing */
2981 result = bclass->event (basesink, event);
2984 if (GST_EVENT_IS_SERIALIZED (event)) {
2985 GST_BASE_SINK_PREROLL_LOCK (basesink);
2986 if (G_UNLIKELY (basesink->flushing))
2989 if (G_UNLIKELY (basesink->priv->received_eos))
2993 result = bclass->event (basesink, event);
2995 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
2998 result = bclass->event (basesink, event);
3008 GST_DEBUG_OBJECT (basesink, "we are flushing");
3009 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
3010 gst_event_unref (event);
3017 GST_DEBUG_OBJECT (basesink, "Event received after EOS, dropping");
3018 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
3019 gst_event_unref (event);
3025 /* default implementation to calculate the start and end
3026 * timestamps on a buffer, subclasses can override
3029 gst_base_sink_default_get_times (GstBaseSink * basesink, GstBuffer * buffer,
3030 GstClockTime * start, GstClockTime * end)
3032 GstClockTime timestamp, duration;
3034 timestamp = GST_BUFFER_TIMESTAMP (buffer);
3035 if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
3037 /* get duration to calculate end time */
3038 duration = GST_BUFFER_DURATION (buffer);
3039 if (GST_CLOCK_TIME_IS_VALID (duration)) {
3040 *end = timestamp + duration;
3046 /* must be called with PREROLL_LOCK */
3048 gst_base_sink_needs_preroll (GstBaseSink * basesink)
3050 gboolean is_prerolled, res;
3052 /* we have 2 cases where the PREROLL_LOCK is released:
3053 * 1) we are blocking in the PREROLL_LOCK and thus are prerolled.
3054 * 2) we are syncing on the clock
3056 is_prerolled = basesink->have_preroll || basesink->priv->received_eos;
3057 res = !is_prerolled;
3059 GST_DEBUG_OBJECT (basesink, "have_preroll: %d, EOS: %d => needs preroll: %d",
3060 basesink->have_preroll, basesink->priv->received_eos, res);
3065 /* with STREAM_LOCK, PREROLL_LOCK
3067 * Takes a buffer and compare the timestamps with the last segment.
3068 * If the buffer falls outside of the segment boundaries, drop it.
3069 * Else send the buffer for preroll and rendering.
3071 * This function takes ownership of the buffer.
3073 static GstFlowReturn
3074 gst_base_sink_chain_unlocked (GstBaseSink * basesink, GstPad * pad,
3077 GstBaseSinkClass *bclass;
3078 GstBaseSinkPrivate *priv = basesink->priv;
3080 GstClockTime start = GST_CLOCK_TIME_NONE, end = GST_CLOCK_TIME_NONE;
3081 GstSegment *segment;
3082 GstBuffer *sync_buf;
3084 gboolean late, step_end;
3086 if (G_UNLIKELY (basesink->flushing))
3089 if (G_UNLIKELY (priv->received_eos))
3092 if (GST_IS_BUFFER_LIST (obj)) {
3093 sync_buf = gst_buffer_list_get (GST_BUFFER_LIST_CAST (obj), 0);
3094 g_assert (NULL != sync_buf);
3096 sync_buf = GST_BUFFER_CAST (obj);
3099 /* for code clarity */
3100 segment = &basesink->segment;
3102 if (G_UNLIKELY (!basesink->have_newsegment)) {
3105 sync = gst_base_sink_get_sync (basesink);
3107 GST_ELEMENT_WARNING (basesink, STREAM, FAILED,
3108 (_("Internal data flow problem.")),
3109 ("Received buffer without a new-segment. Assuming timestamps start from 0."));
3112 /* this means this sink will assume timestamps start from 0 */
3113 GST_OBJECT_LOCK (basesink);
3116 basesink->segment.start = 0;
3117 basesink->segment.stop = -1;
3118 basesink->have_newsegment = TRUE;
3119 GST_OBJECT_UNLOCK (basesink);
3122 bclass = GST_BASE_SINK_GET_CLASS (basesink);
3124 /* check if the buffer needs to be dropped, we first ask the subclass for the
3126 if (bclass->get_times)
3127 bclass->get_times (basesink, sync_buf, &start, &end);
3129 if (!GST_CLOCK_TIME_IS_VALID (start)) {
3130 /* if the subclass does not want sync, we use our own values so that we at
3131 * least clip the buffer to the segment */
3132 gst_base_sink_default_get_times (basesink, sync_buf, &start, &end);
3135 GST_DEBUG_OBJECT (basesink, "got times start: %" GST_TIME_FORMAT
3136 ", end: %" GST_TIME_FORMAT, GST_TIME_ARGS (start), GST_TIME_ARGS (end));
3138 /* a dropped buffer does not participate in anything */
3139 if (GST_CLOCK_TIME_IS_VALID (start) && (segment->format == GST_FORMAT_TIME)) {
3140 if (G_UNLIKELY (!gst_segment_clip (segment,
3141 GST_FORMAT_TIME, start, end, NULL, NULL)))
3142 goto out_of_segment;
3149 /* synchronize this object, non syncable objects return OK
3151 ret = gst_base_sink_do_sync (basesink, GST_MINI_OBJECT_CAST (sync_buf),
3153 if (G_UNLIKELY (ret != GST_FLOW_OK))
3156 /* drop late buffers unconditionally, let's hope it's unlikely */
3157 if (G_UNLIKELY (late))
3160 /* read once, to get same value before and after */
3161 do_qos = g_atomic_int_get (&priv->qos_enabled);
3163 GST_DEBUG_OBJECT (basesink, "rendering object %p", obj);
3165 /* record rendering time for QoS and stats */
3167 gst_base_sink_do_render_stats (basesink, TRUE);
3169 if (!GST_IS_BUFFER_LIST (obj)) {
3170 /* For buffer lists do not set last buffer for now. */
3171 gst_base_sink_set_last_buffer (basesink, GST_BUFFER_CAST (obj));
3174 ret = bclass->render (basesink, GST_BUFFER_CAST (obj));
3176 if (bclass->render_list)
3177 ret = bclass->render_list (basesink, GST_BUFFER_LIST_CAST (obj));
3181 gst_base_sink_do_render_stats (basesink, FALSE);
3183 if (ret == GST_FLOW_STEP)
3186 if (G_UNLIKELY (basesink->flushing))
3193 /* the step ended, check if we need to activate a new step */
3194 GST_DEBUG_OBJECT (basesink, "step ended");
3195 stop_stepping (basesink, &basesink->segment, &priv->current_step,
3196 priv->current_rstart, priv->current_rstop, basesink->eos);
3200 gst_base_sink_perform_qos (basesink, late);
3202 GST_DEBUG_OBJECT (basesink, "object unref after render %p", obj);
3203 gst_mini_object_unref (GST_MINI_OBJECT_CAST (obj));
3210 GST_DEBUG_OBJECT (basesink, "sink is flushing");
3211 gst_mini_object_unref (GST_MINI_OBJECT_CAST (obj));
3212 return GST_FLOW_WRONG_STATE;
3216 GST_DEBUG_OBJECT (basesink, "we are EOS, dropping object, return EOS");
3217 gst_mini_object_unref (GST_MINI_OBJECT_CAST (obj));
3218 return GST_FLOW_EOS;
3222 GST_DEBUG_OBJECT (basesink, "dropping buffer, out of clipping segment");
3223 gst_mini_object_unref (GST_MINI_OBJECT_CAST (obj));
3228 GST_DEBUG_OBJECT (basesink, "do_sync returned %s", gst_flow_get_name (ret));
3234 GST_DEBUG_OBJECT (basesink, "buffer late, dropping");
3236 if (g_atomic_int_get (&priv->qos_enabled)) {
3237 GstMessage *qos_msg;
3238 GstClockTime timestamp, duration;
3240 timestamp = GST_BUFFER_TIMESTAMP (GST_BUFFER_CAST (sync_buf));
3241 duration = GST_BUFFER_DURATION (GST_BUFFER_CAST (sync_buf));
3243 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, basesink,
3244 "qos: dropped buffer rt %" GST_TIME_FORMAT ", st %" GST_TIME_FORMAT
3245 ", ts %" GST_TIME_FORMAT ", dur %" GST_TIME_FORMAT,
3246 GST_TIME_ARGS (priv->current_rstart),
3247 GST_TIME_ARGS (priv->current_sstart), GST_TIME_ARGS (timestamp),
3248 GST_TIME_ARGS (duration));
3249 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, basesink,
3250 "qos: rendered %" G_GUINT64_FORMAT ", dropped %" G_GUINT64_FORMAT,
3251 priv->rendered, priv->dropped);
3254 gst_message_new_qos (GST_OBJECT_CAST (basesink), basesink->sync,
3255 priv->current_rstart, priv->current_sstart, timestamp, duration);
3256 gst_message_set_qos_values (qos_msg, priv->current_jitter, priv->avg_rate,
3258 gst_message_set_qos_stats (qos_msg, GST_FORMAT_BUFFERS, priv->rendered,
3260 gst_element_post_message (GST_ELEMENT_CAST (basesink), qos_msg);
3268 static GstFlowReturn
3269 gst_base_sink_chain_main (GstBaseSink * basesink, GstPad * pad, gpointer obj)
3271 GstFlowReturn result;
3273 if (G_UNLIKELY (basesink->pad_mode != GST_PAD_MODE_PUSH))
3276 GST_BASE_SINK_PREROLL_LOCK (basesink);
3277 result = gst_base_sink_chain_unlocked (basesink, pad, obj);
3278 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
3286 GST_OBJECT_LOCK (pad);
3287 GST_WARNING_OBJECT (basesink,
3288 "Push on pad %s:%s, but it was not activated in push mode",
3289 GST_DEBUG_PAD_NAME (pad));
3290 GST_OBJECT_UNLOCK (pad);
3291 gst_mini_object_unref (GST_MINI_OBJECT_CAST (obj));
3292 /* we don't post an error message this will signal to the peer
3293 * pushing that EOS is reached. */
3294 result = GST_FLOW_EOS;
3299 static GstFlowReturn
3300 gst_base_sink_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
3302 GstBaseSink *basesink;
3304 basesink = GST_BASE_SINK (parent);
3306 return gst_base_sink_chain_main (basesink, pad, buf);
3309 static GstFlowReturn
3310 gst_base_sink_chain_list (GstPad * pad, GstObject * parent,
3311 GstBufferList * list)
3313 GstBaseSink *basesink;
3314 GstBaseSinkClass *bclass;
3315 GstFlowReturn result;
3317 basesink = GST_BASE_SINK (parent);
3318 bclass = GST_BASE_SINK_GET_CLASS (basesink);
3320 if (G_LIKELY (bclass->render_list)) {
3321 result = gst_base_sink_chain_main (basesink, pad, list);
3326 GST_INFO_OBJECT (pad, "chaining each group in list as a merged buffer");
3328 len = gst_buffer_list_length (list);
3330 result = GST_FLOW_OK;
3331 for (i = 0; i < len; i++) {
3332 buffer = gst_buffer_list_get (list, 0);
3333 result = gst_base_sink_chain_main (basesink, pad,
3334 gst_buffer_ref (buffer));
3335 if (result != GST_FLOW_OK)
3338 gst_buffer_list_unref (list);
3345 gst_base_sink_default_do_seek (GstBaseSink * sink, GstSegment * segment)
3347 gboolean res = TRUE;
3349 /* update our offset if the start/stop position was updated */
3350 if (segment->format == GST_FORMAT_BYTES) {
3351 segment->time = segment->start;
3352 } else if (segment->start == 0) {
3353 /* seek to start, we can implement a default for this. */
3357 GST_INFO_OBJECT (sink, "Can't do a default seek");
3363 #define SEEK_TYPE_IS_RELATIVE(t) (((t) != GST_SEEK_TYPE_NONE) && ((t) != GST_SEEK_TYPE_SET))
3366 gst_base_sink_default_prepare_seek_segment (GstBaseSink * sink,
3367 GstEvent * event, GstSegment * segment)
3369 /* By default, we try one of 2 things:
3370 * - For absolute seek positions, convert the requested position to our
3371 * configured processing format and place it in the output segment \
3372 * - For relative seek positions, convert our current (input) values to the
3373 * seek format, adjust by the relative seek offset and then convert back to
3374 * the processing format
3376 GstSeekType cur_type, stop_type;
3379 GstFormat seek_format;
3382 gboolean res = TRUE;
3384 gst_event_parse_seek (event, &rate, &seek_format, &flags,
3385 &cur_type, &cur, &stop_type, &stop);
3387 if (seek_format == segment->format) {
3388 gst_segment_do_seek (segment, rate, seek_format, flags,
3389 cur_type, cur, stop_type, stop, &update);
3393 if (cur_type != GST_SEEK_TYPE_NONE) {
3394 /* FIXME: Handle seek_cur & seek_end by converting the input segment vals */
3396 gst_pad_query_convert (sink->sinkpad, seek_format, cur, segment->format,
3398 cur_type = GST_SEEK_TYPE_SET;
3401 if (res && stop_type != GST_SEEK_TYPE_NONE) {
3402 /* FIXME: Handle seek_cur & seek_end by converting the input segment vals */
3404 gst_pad_query_convert (sink->sinkpad, seek_format, stop,
3405 segment->format, &stop);
3406 stop_type = GST_SEEK_TYPE_SET;
3409 /* And finally, configure our output segment in the desired format */
3410 gst_segment_do_seek (segment, rate, segment->format, flags, cur_type, cur,
3411 stop_type, stop, &update);
3420 GST_DEBUG_OBJECT (sink, "undefined format given, seek aborted.");
3425 /* perform a seek, only executed in pull mode */
3427 gst_base_sink_perform_seek (GstBaseSink * sink, GstPad * pad, GstEvent * event)
3431 GstFormat seek_format, dest_format;
3433 GstSeekType cur_type, stop_type;
3434 gboolean seekseg_configured = FALSE;
3436 gboolean update, res = TRUE;
3437 GstSegment seeksegment;
3439 dest_format = sink->segment.format;
3442 GST_DEBUG_OBJECT (sink, "performing seek with event %p", event);
3443 gst_event_parse_seek (event, &rate, &seek_format, &flags,
3444 &cur_type, &cur, &stop_type, &stop);
3446 flush = flags & GST_SEEK_FLAG_FLUSH;
3448 GST_DEBUG_OBJECT (sink, "performing seek without event");
3453 GST_DEBUG_OBJECT (sink, "flushing upstream");
3454 gst_pad_push_event (pad, gst_event_new_flush_start ());
3455 gst_base_sink_flush_start (sink, pad);
3457 GST_DEBUG_OBJECT (sink, "pausing pulling thread");
3460 GST_PAD_STREAM_LOCK (pad);
3462 /* If we configured the seeksegment above, don't overwrite it now. Otherwise
3463 * copy the current segment info into the temp segment that we can actually
3464 * attempt the seek with. We only update the real segment if the seek succeeds. */
3465 if (!seekseg_configured) {
3466 memcpy (&seeksegment, &sink->segment, sizeof (GstSegment));
3468 /* now configure the final seek segment */
3470 if (sink->segment.format != seek_format) {
3471 /* OK, here's where we give the subclass a chance to convert the relative
3472 * seek into an absolute one in the processing format. We set up any
3473 * absolute seek above, before taking the stream lock. */
3474 if (!gst_base_sink_default_prepare_seek_segment (sink, event,
3476 GST_DEBUG_OBJECT (sink,
3477 "Preparing the seek failed after flushing. " "Aborting seek");
3481 /* The seek format matches our processing format, no need to ask the
3482 * the subclass to configure the segment. */
3483 gst_segment_do_seek (&seeksegment, rate, seek_format, flags,
3484 cur_type, cur, stop_type, stop, &update);
3487 /* Else, no seek event passed, so we're just (re)starting the
3492 GST_DEBUG_OBJECT (sink, "segment configured from %" G_GINT64_FORMAT
3493 " to %" G_GINT64_FORMAT ", position %" G_GINT64_FORMAT,
3494 seeksegment.start, seeksegment.stop, seeksegment.position);
3496 /* do the seek, segment.position contains the new position. */
3497 res = gst_base_sink_default_do_seek (sink, &seeksegment);
3502 GST_DEBUG_OBJECT (sink, "stop flushing upstream");
3503 gst_pad_push_event (pad, gst_event_new_flush_stop (TRUE));
3504 gst_base_sink_flush_stop (sink, pad, TRUE);
3505 } else if (res && sink->running) {
3506 /* we are running the current segment and doing a non-flushing seek,
3507 * close the segment first based on the position. */
3508 GST_DEBUG_OBJECT (sink, "closing running segment %" G_GINT64_FORMAT
3509 " to %" G_GINT64_FORMAT, sink->segment.start, sink->segment.position);
3512 /* The subclass must have converted the segment to the processing format
3514 if (res && seeksegment.format != dest_format) {
3515 GST_DEBUG_OBJECT (sink, "Subclass failed to prepare a seek segment "
3516 "in the correct format. Aborting seek.");
3520 /* if successful seek, we update our real segment and push
3521 * out the new segment. */
3523 gst_segment_copy_into (&seeksegment, &sink->segment);
3525 if (sink->segment.flags & GST_SEEK_FLAG_SEGMENT) {
3526 gst_element_post_message (GST_ELEMENT (sink),
3527 gst_message_new_segment_start (GST_OBJECT (sink),
3528 sink->segment.format, sink->segment.position));
3532 sink->priv->discont = TRUE;
3533 sink->running = TRUE;
3535 GST_PAD_STREAM_UNLOCK (pad);
3541 set_step_info (GstBaseSink * sink, GstStepInfo * current, GstStepInfo * pending,
3542 guint seqnum, GstFormat format, guint64 amount, gdouble rate,
3543 gboolean flush, gboolean intermediate)
3545 GST_OBJECT_LOCK (sink);
3546 pending->seqnum = seqnum;
3547 pending->format = format;
3548 pending->amount = amount;
3549 pending->position = 0;
3550 pending->rate = rate;
3551 pending->flush = flush;
3552 pending->intermediate = intermediate;
3553 pending->valid = TRUE;
3554 /* flush invalidates the current stepping segment */
3556 current->valid = FALSE;
3557 GST_OBJECT_UNLOCK (sink);
3561 gst_base_sink_perform_step (GstBaseSink * sink, GstPad * pad, GstEvent * event)
3563 GstBaseSinkPrivate *priv;
3564 GstBaseSinkClass *bclass;
3565 gboolean flush, intermediate;
3570 GstStepInfo *pending, *current;
3571 GstMessage *message;
3573 bclass = GST_BASE_SINK_GET_CLASS (sink);
3576 GST_DEBUG_OBJECT (sink, "performing step with event %p", event);
3578 gst_event_parse_step (event, &format, &amount, &rate, &flush, &intermediate);
3579 seqnum = gst_event_get_seqnum (event);
3581 pending = &priv->pending_step;
3582 current = &priv->current_step;
3584 /* post message first */
3585 message = gst_message_new_step_start (GST_OBJECT (sink), FALSE, format,
3586 amount, rate, flush, intermediate);
3587 gst_message_set_seqnum (message, seqnum);
3588 gst_element_post_message (GST_ELEMENT (sink), message);
3591 /* we need to call ::unlock before locking PREROLL_LOCK
3592 * since we lock it before going into ::render */
3594 bclass->unlock (sink);
3596 GST_BASE_SINK_PREROLL_LOCK (sink);
3597 /* now that we have the PREROLL lock, clear our unlock request */
3598 if (bclass->unlock_stop)
3599 bclass->unlock_stop (sink);
3601 /* update the stepinfo and make it valid */
3602 set_step_info (sink, current, pending, seqnum, format, amount, rate, flush,
3605 if (sink->priv->async_enabled) {
3606 /* and we need to commit our state again on the next
3607 * prerolled buffer */
3608 sink->playing_async = TRUE;
3609 priv->pending_step.need_preroll = TRUE;
3610 sink->need_preroll = FALSE;
3611 gst_element_lost_state (GST_ELEMENT_CAST (sink));
3613 sink->priv->have_latency = TRUE;
3614 sink->need_preroll = FALSE;
3616 priv->current_sstart = GST_CLOCK_TIME_NONE;
3617 priv->current_sstop = GST_CLOCK_TIME_NONE;
3618 priv->eos_rtime = GST_CLOCK_TIME_NONE;
3619 priv->call_preroll = TRUE;
3620 gst_base_sink_set_last_buffer (sink, NULL);
3621 gst_base_sink_reset_qos (sink);
3623 if (sink->clock_id) {
3624 gst_clock_id_unschedule (sink->clock_id);
3627 if (sink->have_preroll) {
3628 GST_DEBUG_OBJECT (sink, "signal waiter");
3629 priv->step_unlock = TRUE;
3630 GST_BASE_SINK_PREROLL_SIGNAL (sink);
3632 GST_BASE_SINK_PREROLL_UNLOCK (sink);
3634 /* update the stepinfo and make it valid */
3635 set_step_info (sink, current, pending, seqnum, format, amount, rate, flush,
3645 gst_base_sink_loop (GstPad * pad)
3648 GstBaseSink *basesink;
3649 GstBuffer *buf = NULL;
3650 GstFlowReturn result;
3654 parent = GST_OBJECT_PARENT (pad);
3655 basesink = GST_BASE_SINK (parent);
3657 g_assert (basesink->pad_mode == GST_PAD_MODE_PULL);
3659 if ((blocksize = basesink->priv->blocksize) == 0)
3662 offset = basesink->segment.position;
3664 GST_DEBUG_OBJECT (basesink, "pulling %" G_GUINT64_FORMAT ", %u",
3667 result = gst_pad_pull_range (pad, offset, blocksize, &buf);
3668 if (G_UNLIKELY (result != GST_FLOW_OK))
3671 if (G_UNLIKELY (buf == NULL))
3674 offset += gst_buffer_get_size (buf);
3676 basesink->segment.position = offset;
3678 GST_BASE_SINK_PREROLL_LOCK (basesink);
3679 result = gst_base_sink_chain_unlocked (basesink, pad, buf);
3680 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
3681 if (G_UNLIKELY (result != GST_FLOW_OK))
3689 GST_LOG_OBJECT (basesink, "pausing task, reason %s",
3690 gst_flow_get_name (result));
3691 gst_pad_pause_task (pad);
3692 if (result == GST_FLOW_EOS) {
3693 /* perform EOS logic */
3694 if (basesink->segment.flags & GST_SEEK_FLAG_SEGMENT) {
3695 gst_element_post_message (GST_ELEMENT_CAST (basesink),
3696 gst_message_new_segment_done (GST_OBJECT_CAST (basesink),
3697 basesink->segment.format, basesink->segment.position));
3699 gst_base_sink_event (pad, parent, gst_event_new_eos ());
3701 } else if (result == GST_FLOW_NOT_LINKED || result <= GST_FLOW_EOS) {
3702 /* for fatal errors we post an error message, post the error
3703 * first so the app knows about the error first.
3704 * wrong-state is not a fatal error because it happens due to
3705 * flushing and posting an error message in that case is the
3706 * wrong thing to do, e.g. when basesrc is doing a flushing
3708 GST_ELEMENT_ERROR (basesink, STREAM, FAILED,
3709 (_("Internal data stream error.")),
3710 ("stream stopped, reason %s", gst_flow_get_name (result)));
3711 gst_base_sink_event (pad, parent, gst_event_new_eos ());
3717 GST_LOG_OBJECT (basesink, "no buffer, pausing");
3718 GST_ELEMENT_ERROR (basesink, STREAM, FAILED,
3719 (_("Internal data flow error.")), ("element returned NULL buffer"));
3720 result = GST_FLOW_ERROR;
3726 gst_base_sink_set_flushing (GstBaseSink * basesink, GstPad * pad,
3729 GstBaseSinkClass *bclass;
3731 bclass = GST_BASE_SINK_GET_CLASS (basesink);
3734 /* unlock any subclasses, we need to do this before grabbing the
3735 * PREROLL_LOCK since we hold this lock before going into ::render. */
3737 bclass->unlock (basesink);
3740 GST_BASE_SINK_PREROLL_LOCK (basesink);
3741 basesink->flushing = flushing;
3743 /* step 1, now that we have the PREROLL lock, clear our unlock request */
3744 if (bclass->unlock_stop)
3745 bclass->unlock_stop (basesink);
3747 /* set need_preroll before we unblock the clock. If the clock is unblocked
3748 * before timing out, we can reuse the buffer for preroll. */
3749 basesink->need_preroll = TRUE;
3751 /* step 2, unblock clock sync (if any) or any other blocking thing */
3752 if (basesink->clock_id) {
3753 gst_clock_id_unschedule (basesink->clock_id);
3756 /* flush out the data thread if it's locked in finish_preroll, this will
3757 * also flush out the EOS state */
3758 GST_DEBUG_OBJECT (basesink,
3759 "flushing out data thread, need preroll to TRUE");
3761 /* we can't have EOS anymore now */
3762 basesink->eos = FALSE;
3763 basesink->priv->received_eos = FALSE;
3764 basesink->have_preroll = FALSE;
3765 basesink->priv->step_unlock = FALSE;
3766 /* can't report latency anymore until we preroll again */
3767 if (basesink->priv->async_enabled) {
3768 GST_OBJECT_LOCK (basesink);
3769 basesink->priv->have_latency = FALSE;
3770 GST_OBJECT_UNLOCK (basesink);
3772 /* and signal any waiters now */
3773 GST_BASE_SINK_PREROLL_SIGNAL (basesink);
3775 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
3781 gst_base_sink_default_activate_pull (GstBaseSink * basesink, gboolean active)
3787 result = gst_pad_start_task (basesink->sinkpad,
3788 (GstTaskFunction) gst_base_sink_loop, basesink->sinkpad);
3790 /* step 2, make sure streaming finishes */
3791 result = gst_pad_stop_task (basesink->sinkpad);
3798 gst_base_sink_pad_activate (GstPad * pad, GstObject * parent)
3800 gboolean result = FALSE;
3801 GstBaseSink *basesink;
3805 basesink = GST_BASE_SINK (parent);
3807 GST_DEBUG_OBJECT (basesink, "Trying pull mode first");
3809 gst_base_sink_set_flushing (basesink, pad, FALSE);
3811 /* we need to have the pull mode enabled */
3812 if (!basesink->can_activate_pull) {
3813 GST_DEBUG_OBJECT (basesink, "pull mode disabled");
3817 /* check if downstreams supports pull mode at all */
3818 query = gst_query_new_scheduling ();
3820 if (!gst_pad_peer_query (pad, query)) {
3821 gst_query_unref (query);
3822 GST_DEBUG_OBJECT (basesink, "peer query faild, no pull mode");
3826 /* parse result of the query */
3827 pull_mode = gst_query_has_scheduling_mode (query, GST_PAD_MODE_PULL);
3828 gst_query_unref (query);
3831 GST_DEBUG_OBJECT (basesink, "pull mode not supported");
3835 /* set the pad mode before starting the task so that it's in the
3836 * correct state for the new thread. also the sink set_caps and get_caps
3837 * function checks this */
3838 basesink->pad_mode = GST_PAD_MODE_PULL;
3840 /* we first try to negotiate a format so that when we try to activate
3841 * downstream, it knows about our format */
3842 if (!gst_base_sink_negotiate_pull (basesink)) {
3843 GST_DEBUG_OBJECT (basesink, "failed to negotiate in pull mode");
3847 /* ok activate now */
3848 if (!gst_pad_activate_mode (pad, GST_PAD_MODE_PULL, TRUE)) {
3849 /* clear any pending caps */
3850 GST_OBJECT_LOCK (basesink);
3851 gst_caps_replace (&basesink->priv->caps, NULL);
3852 GST_OBJECT_UNLOCK (basesink);
3853 GST_DEBUG_OBJECT (basesink, "failed to activate in pull mode");
3857 GST_DEBUG_OBJECT (basesink, "Success activating pull mode");
3861 /* push mode fallback */
3863 GST_DEBUG_OBJECT (basesink, "Falling back to push mode");
3864 if ((result = gst_pad_activate_mode (pad, GST_PAD_MODE_PUSH, TRUE))) {
3865 GST_DEBUG_OBJECT (basesink, "Success activating push mode");
3870 GST_WARNING_OBJECT (basesink, "Could not activate pad in either mode");
3871 gst_base_sink_set_flushing (basesink, pad, TRUE);
3878 gst_base_sink_pad_activate_push (GstPad * pad, GstObject * parent,
3882 GstBaseSink *basesink;
3884 basesink = GST_BASE_SINK (parent);
3887 if (!basesink->can_activate_push) {
3889 basesink->pad_mode = GST_PAD_MODE_NONE;
3892 basesink->pad_mode = GST_PAD_MODE_PUSH;
3895 if (G_UNLIKELY (basesink->pad_mode != GST_PAD_MODE_PUSH)) {
3896 g_warning ("Internal GStreamer activation error!!!");
3899 gst_base_sink_set_flushing (basesink, pad, TRUE);
3901 basesink->pad_mode = GST_PAD_MODE_NONE;
3909 gst_base_sink_negotiate_pull (GstBaseSink * basesink)
3916 /* this returns the intersection between our caps and the peer caps. If there
3917 * is no peer, it returns NULL and we can't operate in pull mode so we can
3918 * fail the negotiation. */
3919 caps = gst_pad_get_allowed_caps (GST_BASE_SINK_PAD (basesink));
3920 if (caps == NULL || gst_caps_is_empty (caps))
3921 goto no_caps_possible;
3923 GST_DEBUG_OBJECT (basesink, "allowed caps: %" GST_PTR_FORMAT, caps);
3925 if (gst_caps_is_any (caps)) {
3926 GST_DEBUG_OBJECT (basesink, "caps were ANY after fixating, "
3928 /* neither side has template caps in this case, so they are prepared for
3929 pull() without setcaps() */
3932 caps = gst_caps_make_writable (caps);
3934 gst_base_sink_fixate (basesink, caps);
3935 GST_DEBUG_OBJECT (basesink, "fixated to: %" GST_PTR_FORMAT, caps);
3937 if (gst_caps_is_fixed (caps)) {
3938 if (!gst_pad_send_event (GST_BASE_SINK_PAD (basesink),
3939 gst_event_new_caps (caps)))
3940 goto could_not_set_caps;
3946 gst_caps_unref (caps);
3952 GST_INFO_OBJECT (basesink, "Pipeline could not agree on caps");
3953 GST_DEBUG_OBJECT (basesink, "get_allowed_caps() returned EMPTY");
3955 gst_caps_unref (caps);
3960 GST_INFO_OBJECT (basesink, "Could not set caps: %" GST_PTR_FORMAT, caps);
3961 gst_caps_unref (caps);
3966 /* this won't get called until we implement an activate function */
3968 gst_base_sink_pad_activate_pull (GstPad * pad, GstObject * parent,
3971 gboolean result = FALSE;
3972 GstBaseSink *basesink;
3973 GstBaseSinkClass *bclass;
3975 basesink = GST_BASE_SINK (parent);
3976 bclass = GST_BASE_SINK_GET_CLASS (basesink);
3981 /* we mark we have a newsegment here because pull based
3982 * mode works just fine without having a newsegment before the
3984 gst_segment_init (&basesink->segment, GST_FORMAT_BYTES);
3985 GST_OBJECT_LOCK (basesink);
3986 basesink->have_newsegment = TRUE;
3987 GST_OBJECT_UNLOCK (basesink);
3989 /* get the peer duration in bytes */
3990 result = gst_pad_peer_query_duration (pad, GST_FORMAT_BYTES, &duration);
3992 GST_DEBUG_OBJECT (basesink,
3993 "setting duration in bytes to %" G_GINT64_FORMAT, duration);
3994 basesink->segment.duration = duration;
3996 GST_DEBUG_OBJECT (basesink, "unknown duration");
3999 if (bclass->activate_pull)
4000 result = bclass->activate_pull (basesink, TRUE);
4005 goto activate_failed;
4008 if (G_UNLIKELY (basesink->pad_mode != GST_PAD_MODE_PULL)) {
4009 g_warning ("Internal GStreamer activation error!!!");
4012 result = gst_base_sink_set_flushing (basesink, pad, TRUE);
4013 if (bclass->activate_pull)
4014 result &= bclass->activate_pull (basesink, FALSE);
4015 basesink->pad_mode = GST_PAD_MODE_NONE;
4024 /* reset, as starting the thread failed */
4025 basesink->pad_mode = GST_PAD_MODE_NONE;
4027 GST_ERROR_OBJECT (basesink, "subclass failed to activate in pull mode");
4033 gst_base_sink_pad_activate_mode (GstPad * pad, GstObject * parent,
4034 GstPadMode mode, gboolean active)
4039 case GST_PAD_MODE_PULL:
4040 res = gst_base_sink_pad_activate_pull (pad, parent, active);
4042 case GST_PAD_MODE_PUSH:
4043 res = gst_base_sink_pad_activate_push (pad, parent, active);
4046 GST_LOG_OBJECT (pad, "unknown activation mode %d", mode);
4053 /* send an event to our sinkpad peer. */
4055 gst_base_sink_send_event (GstElement * element, GstEvent * event)
4058 GstBaseSink *basesink = GST_BASE_SINK (element);
4059 gboolean forward, result = TRUE;
4062 GST_OBJECT_LOCK (element);
4063 /* get the pad and the scheduling mode */
4064 pad = gst_object_ref (basesink->sinkpad);
4065 mode = basesink->pad_mode;
4066 GST_OBJECT_UNLOCK (element);
4068 /* only push UPSTREAM events upstream */
4069 forward = GST_EVENT_IS_UPSTREAM (event);
4071 GST_DEBUG_OBJECT (basesink, "handling event %p %" GST_PTR_FORMAT, event,
4074 switch (GST_EVENT_TYPE (event)) {
4075 case GST_EVENT_LATENCY:
4077 GstClockTime latency;
4079 gst_event_parse_latency (event, &latency);
4081 /* store the latency. We use this to adjust the running_time before syncing
4082 * it to the clock. */
4083 GST_OBJECT_LOCK (element);
4084 basesink->priv->latency = latency;
4085 if (!basesink->priv->have_latency)
4087 GST_OBJECT_UNLOCK (element);
4088 GST_DEBUG_OBJECT (basesink, "latency set to %" GST_TIME_FORMAT,
4089 GST_TIME_ARGS (latency));
4091 /* We forward this event so that all elements know about the global pipeline
4092 * latency. This is interesting for an element when it wants to figure out
4093 * when a particular piece of data will be rendered. */
4096 case GST_EVENT_SEEK:
4097 /* in pull mode we will execute the seek */
4098 if (mode == GST_PAD_MODE_PULL)
4099 result = gst_base_sink_perform_seek (basesink, pad, event);
4101 case GST_EVENT_STEP:
4102 result = gst_base_sink_perform_step (basesink, pad, event);
4110 result = gst_pad_push_event (pad, event);
4112 /* not forwarded, unref the event */
4113 gst_event_unref (event);
4116 gst_object_unref (pad);
4118 GST_DEBUG_OBJECT (basesink, "handled event %p %" GST_PTR_FORMAT ": %d", event,
4125 gst_base_sink_get_position (GstBaseSink * basesink, GstFormat format,
4126 gint64 * cur, gboolean * upstream)
4128 GstClock *clock = NULL;
4129 gboolean res = FALSE;
4131 GstSegment *segment;
4132 GstClockTime now, latency;
4133 GstClockTimeDiff base_time;
4134 gint64 time, base, duration;
4137 gboolean last_seen, with_clock, in_paused;
4139 GST_OBJECT_LOCK (basesink);
4140 /* we can only get the segment when we are not NULL or READY */
4141 if (!basesink->have_newsegment)
4145 /* when not in PLAYING or when we're busy with a state change, we
4146 * cannot read from the clock so we report time based on the
4147 * last seen timestamp. */
4148 if (GST_STATE (basesink) != GST_STATE_PLAYING ||
4149 GST_STATE_PENDING (basesink) != GST_STATE_VOID_PENDING) {
4153 segment = &basesink->segment;
4155 /* get the format in the segment */
4156 oformat = segment->format;
4158 /* report with last seen position when EOS */
4159 last_seen = basesink->eos;
4161 /* assume we will use the clock for getting the current position */
4163 if (basesink->sync == FALSE)
4166 /* and we need a clock */
4167 if (G_UNLIKELY ((clock = GST_ELEMENT_CLOCK (basesink)) == NULL))
4170 gst_object_ref (clock);
4172 /* mainloop might be querying position when going to playing async,
4173 * while (audio) rendering might be quickly advancing stream position,
4174 * so use clock asap rather than last reported position */
4175 if (in_paused && with_clock && g_atomic_int_get (&basesink->priv->to_playing)) {
4176 GST_DEBUG_OBJECT (basesink, "going to PLAYING, so not PAUSED");
4180 /* collect all data we need holding the lock */
4181 if (GST_CLOCK_TIME_IS_VALID (segment->time))
4182 time = segment->time;
4186 if (GST_CLOCK_TIME_IS_VALID (segment->stop))
4187 duration = segment->stop - segment->start;
4191 base = segment->base;
4192 rate = segment->rate * segment->applied_rate;
4193 latency = basesink->priv->latency;
4195 if (oformat == GST_FORMAT_TIME) {
4198 start = basesink->priv->current_sstart;
4199 stop = basesink->priv->current_sstop;
4202 /* in paused we use the last position as a lower bound */
4203 if (stop == -1 || segment->rate > 0.0)
4208 /* in playing, use last stop time as upper bound */
4209 if (start == -1 || segment->rate > 0.0)
4215 /* convert last stop to stream time */
4216 last = gst_segment_to_stream_time (segment, oformat, segment->position);
4220 /* in paused, use start_time */
4221 base_time = GST_ELEMENT_START_TIME (basesink);
4222 GST_DEBUG_OBJECT (basesink, "in paused, using start time %" GST_TIME_FORMAT,
4223 GST_TIME_ARGS (base_time));
4224 } else if (with_clock) {
4225 /* else use clock when needed */
4226 base_time = GST_ELEMENT_CAST (basesink)->base_time;
4227 GST_DEBUG_OBJECT (basesink, "using clock and base time %" GST_TIME_FORMAT,
4228 GST_TIME_ARGS (base_time));
4230 /* else, no sync or clock -> no base time */
4231 GST_DEBUG_OBJECT (basesink, "no sync or no clock");
4235 /* no base_time, we can't calculate running_time, use last seem timestamp to report
4237 if (base_time == -1)
4240 /* need to release the object lock before we can get the time,
4241 * a clock might take the LOCK of the provider, which could be
4242 * a basesink subclass. */
4243 GST_OBJECT_UNLOCK (basesink);
4246 /* in EOS or when no valid stream_time, report the value of last seen
4249 /* no timestamp, we need to ask upstream */
4250 GST_DEBUG_OBJECT (basesink, "no last seen timestamp, asking upstream");
4255 GST_DEBUG_OBJECT (basesink, "using last seen timestamp %" GST_TIME_FORMAT,
4256 GST_TIME_ARGS (last));
4259 if (oformat != GST_FORMAT_TIME) {
4260 /* convert base, time and duration to time */
4261 if (!gst_pad_query_convert (basesink->sinkpad, oformat, base,
4262 GST_FORMAT_TIME, &base))
4263 goto convert_failed;
4264 if (!gst_pad_query_convert (basesink->sinkpad, oformat, duration,
4265 GST_FORMAT_TIME, &duration))
4266 goto convert_failed;
4267 if (!gst_pad_query_convert (basesink->sinkpad, oformat, time,
4268 GST_FORMAT_TIME, &time))
4269 goto convert_failed;
4270 if (!gst_pad_query_convert (basesink->sinkpad, oformat, last,
4271 GST_FORMAT_TIME, &last))
4272 goto convert_failed;
4274 /* assume time format from now on */
4275 oformat = GST_FORMAT_TIME;
4278 if (!in_paused && with_clock) {
4279 now = gst_clock_get_time (clock);
4285 /* subtract base time and base time from the clock time.
4286 * Make sure we don't go negative. This is the current time in
4287 * the segment which we need to scale with the combined
4288 * rate and applied rate. */
4290 base_time += latency;
4291 if (GST_CLOCK_DIFF (base_time, now) < 0)
4294 /* for negative rates we need to count back from the segment
4299 *cur = time + gst_guint64_to_gdouble (now - base_time) * rate;
4302 /* never report less than segment values in paused */
4304 *cur = MAX (last, *cur);
4306 /* never report more than last seen position in playing */
4308 *cur = MIN (last, *cur);
4311 GST_DEBUG_OBJECT (basesink,
4312 "now %" GST_TIME_FORMAT " - base_time %" GST_TIME_FORMAT " - base %"
4313 GST_TIME_FORMAT " + time %" GST_TIME_FORMAT " last %" GST_TIME_FORMAT,
4314 GST_TIME_ARGS (now), GST_TIME_ARGS (base_time), GST_TIME_ARGS (base),
4315 GST_TIME_ARGS (time), GST_TIME_ARGS (last));
4318 if (oformat != format) {
4319 /* convert to final format */
4320 if (!gst_pad_query_convert (basesink->sinkpad, oformat, *cur, format, cur))
4321 goto convert_failed;
4327 GST_DEBUG_OBJECT (basesink, "res: %d, POSITION: %" GST_TIME_FORMAT,
4328 res, GST_TIME_ARGS (*cur));
4331 gst_object_unref (clock);
4338 /* in NULL or READY we always return FALSE and -1 */
4339 GST_DEBUG_OBJECT (basesink, "position in wrong state, return -1");
4342 GST_OBJECT_UNLOCK (basesink);
4347 GST_DEBUG_OBJECT (basesink, "convert failed, try upstream");
4355 gst_base_sink_get_duration (GstBaseSink * basesink, GstFormat format,
4356 gint64 * dur, gboolean * upstream)
4358 gboolean res = FALSE;
4360 if (basesink->pad_mode == GST_PAD_MODE_PULL) {
4363 /* get the duration in bytes, in pull mode that's all we are sure to
4364 * know. We have to explicitly get this value from upstream instead of
4365 * using our cached value because it might change. Duration caching
4366 * should be done at a higher level. */
4368 gst_pad_peer_query_duration (basesink->sinkpad, GST_FORMAT_BYTES,
4371 basesink->segment.duration = uduration;
4372 if (format != GST_FORMAT_BYTES) {
4373 /* convert to the requested format */
4375 gst_pad_query_convert (basesink->sinkpad, GST_FORMAT_BYTES,
4376 uduration, format, dur);
4390 default_element_query (GstElement * element, GstQuery * query)
4392 gboolean res = FALSE;
4394 GstBaseSink *basesink = GST_BASE_SINK (element);
4396 switch (GST_QUERY_TYPE (query)) {
4397 case GST_QUERY_POSITION:
4401 gboolean upstream = FALSE;
4403 gst_query_parse_position (query, &format, NULL);
4405 GST_DEBUG_OBJECT (basesink, "position query in format %s",
4406 gst_format_get_name (format));
4408 /* first try to get the position based on the clock */
4410 gst_base_sink_get_position (basesink, format, &cur, &upstream))) {
4411 gst_query_set_position (query, format, cur);
4412 } else if (upstream) {
4413 /* fallback to peer query */
4414 res = gst_pad_peer_query (basesink->sinkpad, query);
4417 /* we can handle a few things if upstream failed */
4418 if (format == GST_FORMAT_PERCENT) {
4421 res = gst_base_sink_get_position (basesink, GST_FORMAT_TIME, &cur,
4423 if (!res && upstream) {
4425 gst_pad_peer_query_position (basesink->sinkpad, GST_FORMAT_TIME,
4429 res = gst_base_sink_get_duration (basesink, GST_FORMAT_TIME, &dur,
4431 if (!res && upstream) {
4433 gst_pad_peer_query_duration (basesink->sinkpad,
4434 GST_FORMAT_TIME, &dur);
4440 pos = gst_util_uint64_scale (100 * GST_FORMAT_PERCENT_SCALE, cur,
4442 gst_query_set_position (query, GST_FORMAT_PERCENT, pos);
4448 case GST_QUERY_DURATION:
4452 gboolean upstream = FALSE;
4454 gst_query_parse_duration (query, &format, NULL);
4456 GST_DEBUG_OBJECT (basesink, "duration query in format %s",
4457 gst_format_get_name (format));
4460 gst_base_sink_get_duration (basesink, format, &dur, &upstream))) {
4461 gst_query_set_duration (query, format, dur);
4462 } else if (upstream) {
4463 /* fallback to peer query */
4464 res = gst_pad_peer_query (basesink->sinkpad, query);
4467 /* we can handle a few things if upstream failed */
4468 if (format == GST_FORMAT_PERCENT) {
4469 gst_query_set_duration (query, GST_FORMAT_PERCENT,
4470 GST_FORMAT_PERCENT_MAX);
4476 case GST_QUERY_LATENCY:
4478 gboolean live, us_live;
4479 GstClockTime min, max;
4481 if ((res = gst_base_sink_query_latency (basesink, &live, &us_live, &min,
4483 gst_query_set_latency (query, live, min, max);
4487 case GST_QUERY_JITTER:
4489 case GST_QUERY_RATE:
4490 /* gst_query_set_rate (query, basesink->segment_rate); */
4493 case GST_QUERY_SEGMENT:
4495 if (basesink->pad_mode == GST_PAD_MODE_PULL) {
4496 gst_query_set_segment (query, basesink->segment.rate,
4497 GST_FORMAT_TIME, basesink->segment.start, basesink->segment.stop);
4500 res = gst_pad_peer_query (basesink->sinkpad, query);
4504 case GST_QUERY_SEEKING:
4505 case GST_QUERY_CONVERT:
4506 case GST_QUERY_FORMATS:
4508 res = gst_pad_peer_query (basesink->sinkpad, query);
4511 GST_DEBUG_OBJECT (basesink, "query %s returns %d",
4512 GST_QUERY_TYPE_NAME (query), res);
4518 gst_base_sink_default_query (GstBaseSink * basesink, GstQuery * query)
4521 GstBaseSinkClass *bclass;
4523 bclass = GST_BASE_SINK_GET_CLASS (basesink);
4525 switch (GST_QUERY_TYPE (query)) {
4526 case GST_QUERY_ALLOCATION:
4528 if (bclass->propose_allocation)
4529 res = bclass->propose_allocation (basesink, query);
4534 case GST_QUERY_CAPS:
4536 GstCaps *caps, *filter;
4538 gst_query_parse_caps (query, &filter);
4539 caps = gst_base_sink_query_caps (basesink, basesink->sinkpad, filter);
4540 gst_query_set_caps_result (query, caps);
4541 gst_caps_unref (caps);
4547 gst_pad_query_default (basesink->sinkpad, GST_OBJECT_CAST (basesink),
4555 gst_base_sink_sink_query (GstPad * pad, GstObject * parent, GstQuery * query)
4557 GstBaseSink *basesink;
4558 GstBaseSinkClass *bclass;
4561 basesink = GST_BASE_SINK_CAST (parent);
4562 bclass = GST_BASE_SINK_GET_CLASS (basesink);
4565 res = bclass->query (basesink, query);
4572 static GstStateChangeReturn
4573 gst_base_sink_change_state (GstElement * element, GstStateChange transition)
4575 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
4576 GstBaseSink *basesink = GST_BASE_SINK (element);
4577 GstBaseSinkClass *bclass;
4578 GstBaseSinkPrivate *priv;
4580 priv = basesink->priv;
4582 bclass = GST_BASE_SINK_GET_CLASS (basesink);
4584 switch (transition) {
4585 case GST_STATE_CHANGE_NULL_TO_READY:
4587 if (!bclass->start (basesink))
4590 case GST_STATE_CHANGE_READY_TO_PAUSED:
4591 /* need to complete preroll before this state change completes, there
4592 * is no data flow in READY so we can safely assume we need to preroll. */
4593 GST_BASE_SINK_PREROLL_LOCK (basesink);
4594 GST_DEBUG_OBJECT (basesink, "READY to PAUSED");
4595 basesink->have_newsegment = FALSE;
4596 gst_segment_init (&basesink->segment, GST_FORMAT_UNDEFINED);
4597 basesink->offset = 0;
4598 basesink->have_preroll = FALSE;
4599 priv->step_unlock = FALSE;
4600 basesink->need_preroll = TRUE;
4601 basesink->playing_async = TRUE;
4602 basesink->priv->reset_time = FALSE;
4603 priv->current_sstart = GST_CLOCK_TIME_NONE;
4604 priv->current_sstop = GST_CLOCK_TIME_NONE;
4605 priv->eos_rtime = GST_CLOCK_TIME_NONE;
4607 basesink->eos = FALSE;
4608 priv->received_eos = FALSE;
4609 gst_base_sink_reset_qos (basesink);
4610 priv->commited = FALSE;
4611 priv->call_preroll = TRUE;
4612 priv->current_step.valid = FALSE;
4613 priv->pending_step.valid = FALSE;
4614 if (priv->async_enabled) {
4615 GST_DEBUG_OBJECT (basesink, "doing async state change");
4616 /* when async enabled, post async-start message and return ASYNC from
4617 * the state change function */
4618 ret = GST_STATE_CHANGE_ASYNC;
4619 gst_element_post_message (GST_ELEMENT_CAST (basesink),
4620 gst_message_new_async_start (GST_OBJECT_CAST (basesink)));
4622 priv->have_latency = TRUE;
4624 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
4626 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
4627 GST_BASE_SINK_PREROLL_LOCK (basesink);
4628 g_atomic_int_set (&basesink->priv->to_playing, TRUE);
4629 if (!gst_base_sink_needs_preroll (basesink)) {
4630 GST_DEBUG_OBJECT (basesink, "PAUSED to PLAYING, don't need preroll");
4631 /* no preroll needed anymore now. */
4632 basesink->playing_async = FALSE;
4633 basesink->need_preroll = FALSE;
4634 if (basesink->eos) {
4635 GstMessage *message;
4637 /* need to post EOS message here */
4638 GST_DEBUG_OBJECT (basesink, "Now posting EOS");
4639 message = gst_message_new_eos (GST_OBJECT_CAST (basesink));
4640 gst_message_set_seqnum (message, basesink->priv->seqnum);
4641 gst_element_post_message (GST_ELEMENT_CAST (basesink), message);
4643 GST_DEBUG_OBJECT (basesink, "signal preroll");
4644 GST_BASE_SINK_PREROLL_SIGNAL (basesink);
4647 GST_DEBUG_OBJECT (basesink, "PAUSED to PLAYING, we are not prerolled");
4648 basesink->need_preroll = TRUE;
4649 basesink->playing_async = TRUE;
4650 priv->call_preroll = TRUE;
4651 priv->commited = FALSE;
4652 if (priv->async_enabled) {
4653 GST_DEBUG_OBJECT (basesink, "doing async state change");
4654 ret = GST_STATE_CHANGE_ASYNC;
4655 gst_element_post_message (GST_ELEMENT_CAST (basesink),
4656 gst_message_new_async_start (GST_OBJECT_CAST (basesink)));
4659 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
4666 GstStateChangeReturn bret;
4668 bret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
4669 if (G_UNLIKELY (bret == GST_STATE_CHANGE_FAILURE))
4670 goto activate_failed;
4673 switch (transition) {
4674 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
4675 /* completed transition, so need not be marked any longer
4676 * And it should be unmarked, since e.g. losing our position upon flush
4677 * does not really change state to PAUSED ... */
4678 g_atomic_int_set (&basesink->priv->to_playing, FALSE);
4680 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
4681 g_atomic_int_set (&basesink->priv->to_playing, FALSE);
4682 GST_DEBUG_OBJECT (basesink, "PLAYING to PAUSED");
4683 /* FIXME, make sure we cannot enter _render first */
4685 /* we need to call ::unlock before locking PREROLL_LOCK
4686 * since we lock it before going into ::render */
4688 bclass->unlock (basesink);
4690 GST_BASE_SINK_PREROLL_LOCK (basesink);
4691 GST_DEBUG_OBJECT (basesink, "got preroll lock");
4692 /* now that we have the PREROLL lock, clear our unlock request */
4693 if (bclass->unlock_stop)
4694 bclass->unlock_stop (basesink);
4696 /* we need preroll again and we set the flag before unlocking the clockid
4697 * because if the clockid is unlocked before a current buffer expired, we
4698 * can use that buffer to preroll with */
4699 basesink->need_preroll = TRUE;
4701 if (basesink->clock_id) {
4702 GST_DEBUG_OBJECT (basesink, "unschedule clock");
4703 gst_clock_id_unschedule (basesink->clock_id);
4706 /* if we don't have a preroll buffer we need to wait for a preroll and
4708 if (!gst_base_sink_needs_preroll (basesink)) {
4709 GST_DEBUG_OBJECT (basesink, "PLAYING to PAUSED, we are prerolled");
4710 basesink->playing_async = FALSE;
4712 if (GST_STATE_TARGET (GST_ELEMENT (basesink)) <= GST_STATE_READY) {
4713 GST_DEBUG_OBJECT (basesink, "element is <= READY");
4714 ret = GST_STATE_CHANGE_SUCCESS;
4716 GST_DEBUG_OBJECT (basesink,
4717 "PLAYING to PAUSED, we are not prerolled");
4718 basesink->playing_async = TRUE;
4719 priv->commited = FALSE;
4720 priv->call_preroll = TRUE;
4721 if (priv->async_enabled) {
4722 GST_DEBUG_OBJECT (basesink, "doing async state change");
4723 ret = GST_STATE_CHANGE_ASYNC;
4724 gst_element_post_message (GST_ELEMENT_CAST (basesink),
4725 gst_message_new_async_start (GST_OBJECT_CAST (basesink)));
4729 GST_DEBUG_OBJECT (basesink, "rendered: %" G_GUINT64_FORMAT
4730 ", dropped: %" G_GUINT64_FORMAT, priv->rendered, priv->dropped);
4732 gst_base_sink_reset_qos (basesink);
4733 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
4735 case GST_STATE_CHANGE_PAUSED_TO_READY:
4736 GST_BASE_SINK_PREROLL_LOCK (basesink);
4737 /* start by resetting our position state with the object lock so that the
4738 * position query gets the right idea. We do this before we post the
4739 * messages so that the message handlers pick this up. */
4740 GST_OBJECT_LOCK (basesink);
4741 basesink->have_newsegment = FALSE;
4742 priv->current_sstart = GST_CLOCK_TIME_NONE;
4743 priv->current_sstop = GST_CLOCK_TIME_NONE;
4744 priv->have_latency = FALSE;
4745 if (priv->cached_clock_id) {
4746 gst_clock_id_unref (priv->cached_clock_id);
4747 priv->cached_clock_id = NULL;
4749 gst_caps_replace (&basesink->priv->caps, NULL);
4750 GST_OBJECT_UNLOCK (basesink);
4752 gst_base_sink_set_last_buffer (basesink, NULL);
4753 priv->call_preroll = FALSE;
4755 if (!priv->commited) {
4756 if (priv->async_enabled) {
4757 GST_DEBUG_OBJECT (basesink, "PAUSED to READY, posting async-done");
4759 gst_element_post_message (GST_ELEMENT_CAST (basesink),
4760 gst_message_new_state_changed (GST_OBJECT_CAST (basesink),
4761 GST_STATE_PLAYING, GST_STATE_PAUSED, GST_STATE_READY));
4763 gst_element_post_message (GST_ELEMENT_CAST (basesink),
4764 gst_message_new_async_done (GST_OBJECT_CAST (basesink), FALSE));
4766 priv->commited = TRUE;
4768 GST_DEBUG_OBJECT (basesink, "PAUSED to READY, don't need_preroll");
4770 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
4772 case GST_STATE_CHANGE_READY_TO_NULL:
4774 if (!bclass->stop (basesink)) {
4775 GST_WARNING_OBJECT (basesink, "failed to stop");
4778 gst_base_sink_set_last_buffer (basesink, NULL);
4779 priv->call_preroll = FALSE;
4790 GST_DEBUG_OBJECT (basesink, "failed to start");
4791 return GST_STATE_CHANGE_FAILURE;
4795 GST_DEBUG_OBJECT (basesink,
4796 "element failed to change states -- activation problem?");
4797 return GST_STATE_CHANGE_FAILURE;