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)
282 _PR_IS_NOTHING = 1 << 0,
283 _PR_IS_BUFFER = 1 << 1,
284 _PR_IS_BUFFERLIST = 1 << 2,
285 _PR_IS_EVENT = 1 << 3
288 #define OBJ_IS_BUFFER(a) ((a) & _PR_IS_BUFFER)
289 #define OBJ_IS_BUFFERLIST(a) ((a) & _PR_IS_BUFFERLIST)
290 #define OBJ_IS_EVENT(a) ((a) & _PR_IS_EVENT)
291 #define OBJ_IS_BUFFERFULL(a) ((a) & (_PR_IS_BUFFER | _PR_IS_BUFFERLIST))
293 /* BaseSink properties */
295 #define DEFAULT_CAN_ACTIVATE_PULL FALSE /* fixme: enable me */
296 #define DEFAULT_CAN_ACTIVATE_PUSH TRUE
298 #define DEFAULT_SYNC TRUE
299 #define DEFAULT_MAX_LATENESS -1
300 #define DEFAULT_QOS FALSE
301 #define DEFAULT_ASYNC TRUE
302 #define DEFAULT_TS_OFFSET 0
303 #define DEFAULT_BLOCKSIZE 4096
304 #define DEFAULT_RENDER_DELAY 0
305 #define DEFAULT_ENABLE_LAST_SAMPLE TRUE
306 #define DEFAULT_THROTTLE_TIME 0
316 PROP_ENABLE_LAST_SAMPLE,
324 static GstElementClass *parent_class = NULL;
326 static void gst_base_sink_class_init (GstBaseSinkClass * klass);
327 static void gst_base_sink_init (GstBaseSink * trans, gpointer g_class);
328 static void gst_base_sink_finalize (GObject * object);
331 gst_base_sink_get_type (void)
333 static volatile gsize base_sink_type = 0;
335 if (g_once_init_enter (&base_sink_type)) {
337 static const GTypeInfo base_sink_info = {
338 sizeof (GstBaseSinkClass),
341 (GClassInitFunc) gst_base_sink_class_init,
344 sizeof (GstBaseSink),
346 (GInstanceInitFunc) gst_base_sink_init,
349 _type = g_type_register_static (GST_TYPE_ELEMENT,
350 "GstBaseSink", &base_sink_info, G_TYPE_FLAG_ABSTRACT);
351 g_once_init_leave (&base_sink_type, _type);
353 return base_sink_type;
356 static void gst_base_sink_set_property (GObject * object, guint prop_id,
357 const GValue * value, GParamSpec * pspec);
358 static void gst_base_sink_get_property (GObject * object, guint prop_id,
359 GValue * value, GParamSpec * pspec);
361 static gboolean gst_base_sink_send_event (GstElement * element,
363 static gboolean default_element_query (GstElement * element, GstQuery * query);
365 static GstCaps *gst_base_sink_get_caps (GstBaseSink * sink, GstCaps * caps);
366 static gboolean gst_base_sink_set_caps (GstBaseSink * sink, GstCaps * caps);
367 static void gst_base_sink_get_times (GstBaseSink * basesink, GstBuffer * buffer,
368 GstClockTime * start, GstClockTime * end);
369 static gboolean gst_base_sink_set_flushing (GstBaseSink * basesink,
370 GstPad * pad, gboolean flushing);
371 static gboolean gst_base_sink_default_activate_pull (GstBaseSink * basesink,
373 static gboolean gst_base_sink_default_do_seek (GstBaseSink * sink,
374 GstSegment * segment);
375 static gboolean gst_base_sink_default_prepare_seek_segment (GstBaseSink * sink,
376 GstEvent * event, GstSegment * segment);
378 static GstStateChangeReturn gst_base_sink_change_state (GstElement * element,
379 GstStateChange transition);
381 static gboolean gst_base_sink_sink_query (GstPad * pad, GstObject * parent,
383 static GstFlowReturn gst_base_sink_chain (GstPad * pad, GstObject * parent,
385 static GstFlowReturn gst_base_sink_chain_list (GstPad * pad, GstObject * parent,
386 GstBufferList * list);
388 static void gst_base_sink_loop (GstPad * pad);
389 static gboolean gst_base_sink_pad_activate (GstPad * pad, GstObject * parent);
390 static gboolean gst_base_sink_pad_activate_mode (GstPad * pad,
391 GstObject * parent, GstPadMode mode, gboolean active);
392 static gboolean gst_base_sink_default_event (GstBaseSink * basesink,
394 static GstFlowReturn gst_base_sink_default_wait_eos (GstBaseSink * basesink,
396 static gboolean gst_base_sink_event (GstPad * pad, GstObject * parent,
399 static gboolean default_sink_query (GstBaseSink * sink, GstQuery * query);
401 static gboolean gst_base_sink_negotiate_pull (GstBaseSink * basesink);
402 static void gst_base_sink_default_fixate (GstBaseSink * bsink, GstCaps * caps);
403 static void gst_base_sink_fixate (GstBaseSink * bsink, GstCaps * caps);
405 /* check if an object was too late */
406 static gboolean gst_base_sink_is_too_late (GstBaseSink * basesink,
407 GstMiniObject * obj, GstClockTime rstart, GstClockTime rstop,
408 GstClockReturn status, GstClockTimeDiff jitter);
409 static GstFlowReturn gst_base_sink_preroll_object (GstBaseSink * basesink,
410 guint8 obj_type, GstMiniObject * obj);
413 gst_base_sink_class_init (GstBaseSinkClass * klass)
415 GObjectClass *gobject_class;
416 GstElementClass *gstelement_class;
418 gobject_class = G_OBJECT_CLASS (klass);
419 gstelement_class = GST_ELEMENT_CLASS (klass);
421 GST_DEBUG_CATEGORY_INIT (gst_base_sink_debug, "basesink", 0,
424 g_type_class_add_private (klass, sizeof (GstBaseSinkPrivate));
426 parent_class = g_type_class_peek_parent (klass);
428 gobject_class->finalize = gst_base_sink_finalize;
429 gobject_class->set_property = gst_base_sink_set_property;
430 gobject_class->get_property = gst_base_sink_get_property;
432 g_object_class_install_property (gobject_class, PROP_SYNC,
433 g_param_spec_boolean ("sync", "Sync", "Sync on the clock", DEFAULT_SYNC,
434 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
436 g_object_class_install_property (gobject_class, PROP_MAX_LATENESS,
437 g_param_spec_int64 ("max-lateness", "Max Lateness",
438 "Maximum number of nanoseconds that a buffer can be late before it "
439 "is dropped (-1 unlimited)", -1, G_MAXINT64, DEFAULT_MAX_LATENESS,
440 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
442 g_object_class_install_property (gobject_class, PROP_QOS,
443 g_param_spec_boolean ("qos", "Qos",
444 "Generate Quality-of-Service events upstream", DEFAULT_QOS,
445 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
449 * If set to #TRUE, the basesink will perform asynchronous state changes.
450 * When set to #FALSE, the sink will not signal the parent when it prerolls.
451 * Use this option when dealing with sparse streams or when synchronisation is
456 g_object_class_install_property (gobject_class, PROP_ASYNC,
457 g_param_spec_boolean ("async", "Async",
458 "Go asynchronously to PAUSED", DEFAULT_ASYNC,
459 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
461 * GstBaseSink:ts-offset
463 * Controls the final synchronisation, a negative value will render the buffer
464 * earlier while a positive value delays playback. This property can be
465 * used to fix synchronisation in bad files.
469 g_object_class_install_property (gobject_class, PROP_TS_OFFSET,
470 g_param_spec_int64 ("ts-offset", "TS Offset",
471 "Timestamp offset in nanoseconds", G_MININT64, G_MAXINT64,
472 DEFAULT_TS_OFFSET, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
475 * GstBaseSink:enable-last-sample
477 * Enable the last-sample property. If FALSE, basesink doesn't keep a
478 * reference to the last buffer arrived and the last-sample property is always
479 * set to NULL. This can be useful if you need buffers to be released as soon
480 * as possible, eg. if you're using a buffer pool.
484 g_object_class_install_property (gobject_class, PROP_ENABLE_LAST_SAMPLE,
485 g_param_spec_boolean ("enable-last-sample", "Enable Last Buffer",
486 "Enable the last-sample property", DEFAULT_ENABLE_LAST_SAMPLE,
487 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
490 * GstBaseSink:last-sample
492 * The last buffer that arrived in the sink and was used for preroll or for
493 * rendering. This property can be used to generate thumbnails. This property
494 * can be NULL when the sink has not yet received a bufer.
498 g_object_class_install_property (gobject_class, PROP_LAST_SAMPLE,
499 g_param_spec_boxed ("last-sample", "Last Sample",
500 "The last sample received in the sink", GST_TYPE_SAMPLE,
501 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
503 * GstBaseSink:blocksize
505 * The amount of bytes to pull when operating in pull mode.
509 /* FIXME 0.11: blocksize property should be int, otherwise min>max.. */
510 g_object_class_install_property (gobject_class, PROP_BLOCKSIZE,
511 g_param_spec_uint ("blocksize", "Block size",
512 "Size in bytes to pull per buffer (0 = default)", 0, G_MAXUINT,
513 DEFAULT_BLOCKSIZE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
515 * GstBaseSink:render-delay
517 * The additional delay between synchronisation and actual rendering of the
518 * media. This property will add additional latency to the device in order to
519 * make other sinks compensate for the delay.
523 g_object_class_install_property (gobject_class, PROP_RENDER_DELAY,
524 g_param_spec_uint64 ("render-delay", "Render Delay",
525 "Additional render delay of the sink in nanoseconds", 0, G_MAXUINT64,
526 DEFAULT_RENDER_DELAY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
528 * GstBaseSink:throttle-time
530 * The time to insert between buffers. This property can be used to control
531 * the maximum amount of buffers per second to render. Setting this property
532 * to a value bigger than 0 will make the sink create THROTTLE QoS events.
536 g_object_class_install_property (gobject_class, PROP_THROTTLE_TIME,
537 g_param_spec_uint64 ("throttle-time", "Throttle time",
538 "The time to keep between rendered buffers (unused)", 0, G_MAXUINT64,
539 DEFAULT_THROTTLE_TIME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
541 gstelement_class->change_state =
542 GST_DEBUG_FUNCPTR (gst_base_sink_change_state);
543 gstelement_class->send_event = GST_DEBUG_FUNCPTR (gst_base_sink_send_event);
544 gstelement_class->query = GST_DEBUG_FUNCPTR (default_element_query);
546 klass->get_caps = GST_DEBUG_FUNCPTR (gst_base_sink_get_caps);
547 klass->set_caps = GST_DEBUG_FUNCPTR (gst_base_sink_set_caps);
548 klass->fixate = GST_DEBUG_FUNCPTR (gst_base_sink_default_fixate);
549 klass->activate_pull =
550 GST_DEBUG_FUNCPTR (gst_base_sink_default_activate_pull);
551 klass->get_times = GST_DEBUG_FUNCPTR (gst_base_sink_get_times);
552 klass->query = GST_DEBUG_FUNCPTR (default_sink_query);
553 klass->event = GST_DEBUG_FUNCPTR (gst_base_sink_default_event);
554 klass->wait_eos = GST_DEBUG_FUNCPTR (gst_base_sink_default_wait_eos);
556 /* Registering debug symbols for function pointers */
557 GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_fixate);
558 GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_pad_activate);
559 GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_pad_activate_mode);
560 GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_event);
561 GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_chain);
562 GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_chain_list);
563 GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_sink_query);
567 gst_base_sink_query_caps (GstBaseSink * bsink, GstPad * pad, GstCaps * filter)
569 GstBaseSinkClass *bclass;
570 GstCaps *caps = NULL;
573 bclass = GST_BASE_SINK_GET_CLASS (bsink);
574 fixed = GST_PAD_IS_FIXED_CAPS (pad);
576 if (fixed || bsink->pad_mode == GST_PAD_MODE_PULL) {
577 /* if we are operating in pull mode or fixed caps, we only accept the
578 * currently negotiated caps */
579 caps = gst_pad_get_current_caps (pad);
582 if (bclass->get_caps)
583 caps = bclass->get_caps (bsink, filter);
586 GstPadTemplate *pad_template;
589 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (bclass),
591 if (pad_template != NULL) {
592 caps = gst_pad_template_get_caps (pad_template);
595 GstCaps *intersection;
598 gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
599 gst_caps_unref (caps);
610 gst_base_sink_default_fixate (GstBaseSink * bsink, GstCaps * caps)
612 GST_DEBUG_OBJECT (bsink, "using default caps fixate function");
613 gst_caps_fixate (caps);
617 gst_base_sink_fixate (GstBaseSink * bsink, GstCaps * caps)
619 GstBaseSinkClass *bclass;
621 bclass = GST_BASE_SINK_GET_CLASS (bsink);
624 bclass->fixate (bsink, caps);
628 gst_base_sink_init (GstBaseSink * basesink, gpointer g_class)
630 GstPadTemplate *pad_template;
631 GstBaseSinkPrivate *priv;
633 basesink->priv = priv = GST_BASE_SINK_GET_PRIVATE (basesink);
636 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (g_class), "sink");
637 g_return_if_fail (pad_template != NULL);
639 basesink->sinkpad = gst_pad_new_from_template (pad_template, "sink");
641 gst_pad_set_activate_function (basesink->sinkpad, gst_base_sink_pad_activate);
642 gst_pad_set_activatemode_function (basesink->sinkpad,
643 gst_base_sink_pad_activate_mode);
644 gst_pad_set_query_function (basesink->sinkpad, gst_base_sink_sink_query);
645 gst_pad_set_event_function (basesink->sinkpad, gst_base_sink_event);
646 gst_pad_set_chain_function (basesink->sinkpad, gst_base_sink_chain);
647 gst_pad_set_chain_list_function (basesink->sinkpad, gst_base_sink_chain_list);
648 gst_element_add_pad (GST_ELEMENT_CAST (basesink), basesink->sinkpad);
650 basesink->pad_mode = GST_PAD_MODE_NONE;
651 basesink->preroll_lock = g_mutex_new ();
652 basesink->preroll_cond = g_cond_new ();
653 priv->have_latency = FALSE;
655 basesink->can_activate_push = DEFAULT_CAN_ACTIVATE_PUSH;
656 basesink->can_activate_pull = DEFAULT_CAN_ACTIVATE_PULL;
658 basesink->sync = DEFAULT_SYNC;
659 basesink->max_lateness = DEFAULT_MAX_LATENESS;
660 g_atomic_int_set (&priv->qos_enabled, DEFAULT_QOS);
661 priv->async_enabled = DEFAULT_ASYNC;
662 priv->ts_offset = DEFAULT_TS_OFFSET;
663 priv->render_delay = DEFAULT_RENDER_DELAY;
664 priv->blocksize = DEFAULT_BLOCKSIZE;
665 priv->cached_clock_id = NULL;
666 g_atomic_int_set (&priv->enable_last_sample, DEFAULT_ENABLE_LAST_SAMPLE);
667 priv->throttle_time = DEFAULT_THROTTLE_TIME;
669 GST_OBJECT_FLAG_SET (basesink, GST_ELEMENT_FLAG_SINK);
673 gst_base_sink_finalize (GObject * object)
675 GstBaseSink *basesink;
677 basesink = GST_BASE_SINK (object);
679 g_mutex_free (basesink->preroll_lock);
680 g_cond_free (basesink->preroll_cond);
682 G_OBJECT_CLASS (parent_class)->finalize (object);
686 * gst_base_sink_set_sync:
688 * @sync: the new sync value.
690 * Configures @sink to synchronize on the clock or not. When
691 * @sync is FALSE, incoming samples will be played as fast as
692 * possible. If @sync is TRUE, the timestamps of the incomming
693 * buffers will be used to schedule the exact render time of its
699 gst_base_sink_set_sync (GstBaseSink * sink, gboolean sync)
701 g_return_if_fail (GST_IS_BASE_SINK (sink));
703 GST_OBJECT_LOCK (sink);
705 GST_OBJECT_UNLOCK (sink);
709 * gst_base_sink_get_sync:
712 * Checks if @sink is currently configured to synchronize against the
715 * Returns: TRUE if the sink is configured to synchronize against the clock.
720 gst_base_sink_get_sync (GstBaseSink * sink)
724 g_return_val_if_fail (GST_IS_BASE_SINK (sink), FALSE);
726 GST_OBJECT_LOCK (sink);
728 GST_OBJECT_UNLOCK (sink);
734 * gst_base_sink_set_max_lateness:
736 * @max_lateness: the new max lateness value.
738 * Sets the new max lateness value to @max_lateness. This value is
739 * used to decide if a buffer should be dropped or not based on the
740 * buffer timestamp and the current clock time. A value of -1 means
746 gst_base_sink_set_max_lateness (GstBaseSink * sink, gint64 max_lateness)
748 g_return_if_fail (GST_IS_BASE_SINK (sink));
750 GST_OBJECT_LOCK (sink);
751 sink->max_lateness = max_lateness;
752 GST_OBJECT_UNLOCK (sink);
756 * gst_base_sink_get_max_lateness:
759 * Gets the max lateness value. See gst_base_sink_set_max_lateness for
762 * Returns: The maximum time in nanoseconds that a buffer can be late
763 * before it is dropped and not rendered. A value of -1 means an
769 gst_base_sink_get_max_lateness (GstBaseSink * sink)
773 g_return_val_if_fail (GST_IS_BASE_SINK (sink), -1);
775 GST_OBJECT_LOCK (sink);
776 res = sink->max_lateness;
777 GST_OBJECT_UNLOCK (sink);
783 * gst_base_sink_set_qos_enabled:
785 * @enabled: the new qos value.
787 * Configures @sink to send Quality-of-Service events upstream.
792 gst_base_sink_set_qos_enabled (GstBaseSink * sink, gboolean enabled)
794 g_return_if_fail (GST_IS_BASE_SINK (sink));
796 g_atomic_int_set (&sink->priv->qos_enabled, enabled);
800 * gst_base_sink_is_qos_enabled:
803 * Checks if @sink is currently configured to send Quality-of-Service events
806 * Returns: TRUE if the sink is configured to perform Quality-of-Service.
811 gst_base_sink_is_qos_enabled (GstBaseSink * sink)
815 g_return_val_if_fail (GST_IS_BASE_SINK (sink), FALSE);
817 res = g_atomic_int_get (&sink->priv->qos_enabled);
823 * gst_base_sink_set_async_enabled:
825 * @enabled: the new async value.
827 * Configures @sink to perform all state changes asynchronusly. When async is
828 * disabled, the sink will immediately go to PAUSED instead of waiting for a
829 * preroll buffer. This feature is useful if the sink does not synchronize
830 * against the clock or when it is dealing with sparse streams.
835 gst_base_sink_set_async_enabled (GstBaseSink * sink, gboolean enabled)
837 g_return_if_fail (GST_IS_BASE_SINK (sink));
839 GST_BASE_SINK_PREROLL_LOCK (sink);
840 g_atomic_int_set (&sink->priv->async_enabled, enabled);
841 GST_LOG_OBJECT (sink, "set async enabled to %d", enabled);
842 GST_BASE_SINK_PREROLL_UNLOCK (sink);
846 * gst_base_sink_is_async_enabled:
849 * Checks if @sink is currently configured to perform asynchronous state
852 * Returns: TRUE if the sink is configured to perform asynchronous state
858 gst_base_sink_is_async_enabled (GstBaseSink * sink)
862 g_return_val_if_fail (GST_IS_BASE_SINK (sink), FALSE);
864 res = g_atomic_int_get (&sink->priv->async_enabled);
870 * gst_base_sink_set_ts_offset:
872 * @offset: the new offset
874 * Adjust the synchronisation of @sink with @offset. A negative value will
875 * render buffers earlier than their timestamp. A positive value will delay
876 * rendering. This function can be used to fix playback of badly timestamped
882 gst_base_sink_set_ts_offset (GstBaseSink * sink, GstClockTimeDiff offset)
884 g_return_if_fail (GST_IS_BASE_SINK (sink));
886 GST_OBJECT_LOCK (sink);
887 sink->priv->ts_offset = offset;
888 GST_LOG_OBJECT (sink, "set time offset to %" G_GINT64_FORMAT, offset);
889 GST_OBJECT_UNLOCK (sink);
893 * gst_base_sink_get_ts_offset:
896 * Get the synchronisation offset of @sink.
898 * Returns: The synchronisation offset.
903 gst_base_sink_get_ts_offset (GstBaseSink * sink)
905 GstClockTimeDiff res;
907 g_return_val_if_fail (GST_IS_BASE_SINK (sink), 0);
909 GST_OBJECT_LOCK (sink);
910 res = sink->priv->ts_offset;
911 GST_OBJECT_UNLOCK (sink);
917 * gst_base_sink_get_last_sample:
920 * Get the last sample that arrived in the sink and was used for preroll or for
921 * rendering. This property can be used to generate thumbnails.
923 * The #GstCaps on the sample can be used to determine the type of the buffer.
925 * Free-function: gst_sample_unref
927 * Returns: (transfer full): a #GstSample. gst_sample_unref() after usage.
928 * This function returns NULL when no buffer has arrived in the sink yet
929 * or when the sink is not in PAUSED or PLAYING.
934 gst_base_sink_get_last_sample (GstBaseSink * sink)
936 GstSample *res = NULL;
938 g_return_val_if_fail (GST_IS_BASE_SINK (sink), NULL);
940 GST_OBJECT_LOCK (sink);
941 if (sink->priv->last_buffer) {
942 res = gst_sample_new (sink->priv->last_buffer,
943 sink->priv->last_caps, &sink->segment, NULL);
945 GST_OBJECT_UNLOCK (sink);
950 /* with OBJECT_LOCK */
952 gst_base_sink_set_last_buffer_unlocked (GstBaseSink * sink, GstBuffer * buffer)
956 old = sink->priv->last_buffer;
957 if (G_LIKELY (old != buffer)) {
958 GST_DEBUG_OBJECT (sink, "setting last buffer to %p", buffer);
959 if (G_LIKELY (buffer))
960 gst_buffer_ref (buffer);
961 sink->priv->last_buffer = buffer;
963 /* copy over the caps */
964 gst_caps_replace (&sink->priv->last_caps, sink->priv->caps);
966 gst_caps_replace (&sink->priv->last_caps, NULL);
970 /* avoid unreffing with the lock because cleanup code might want to take the
972 if (G_LIKELY (old)) {
973 GST_OBJECT_UNLOCK (sink);
974 gst_buffer_unref (old);
975 GST_OBJECT_LOCK (sink);
980 gst_base_sink_set_last_buffer (GstBaseSink * sink, GstBuffer * buffer)
982 if (!g_atomic_int_get (&sink->priv->enable_last_sample))
985 GST_OBJECT_LOCK (sink);
986 gst_base_sink_set_last_buffer_unlocked (sink, buffer);
987 GST_OBJECT_UNLOCK (sink);
991 * gst_base_sink_set_last_sample_enabled:
993 * @enabled: the new enable-last-sample value.
995 * Configures @sink to store the last received sample in the last-sample
1001 gst_base_sink_set_last_sample_enabled (GstBaseSink * sink, gboolean enabled)
1003 g_return_if_fail (GST_IS_BASE_SINK (sink));
1005 /* Only take lock if we change the value */
1006 if (g_atomic_int_compare_and_exchange (&sink->priv->enable_last_sample,
1007 !enabled, enabled) && !enabled) {
1008 GST_OBJECT_LOCK (sink);
1009 gst_base_sink_set_last_buffer_unlocked (sink, NULL);
1010 GST_OBJECT_UNLOCK (sink);
1015 * gst_base_sink_is_last_sample_enabled:
1018 * Checks if @sink is currently configured to store the last received sample in
1019 * the last-sample property.
1021 * Returns: TRUE if the sink is configured to store the last received sample.
1026 gst_base_sink_is_last_sample_enabled (GstBaseSink * sink)
1028 g_return_val_if_fail (GST_IS_BASE_SINK (sink), FALSE);
1030 return g_atomic_int_get (&sink->priv->enable_last_sample);
1034 * gst_base_sink_get_latency:
1037 * Get the currently configured latency.
1039 * Returns: The configured latency.
1044 gst_base_sink_get_latency (GstBaseSink * sink)
1048 GST_OBJECT_LOCK (sink);
1049 res = sink->priv->latency;
1050 GST_OBJECT_UNLOCK (sink);
1056 * gst_base_sink_query_latency:
1058 * @live: (out) (allow-none): if the sink is live
1059 * @upstream_live: (out) (allow-none): if an upstream element is live
1060 * @min_latency: (out) (allow-none): the min latency of the upstream elements
1061 * @max_latency: (out) (allow-none): the max latency of the upstream elements
1063 * Query the sink for the latency parameters. The latency will be queried from
1064 * the upstream elements. @live will be TRUE if @sink is configured to
1065 * synchronize against the clock. @upstream_live will be TRUE if an upstream
1068 * If both @live and @upstream_live are TRUE, the sink will want to compensate
1069 * for the latency introduced by the upstream elements by setting the
1070 * @min_latency to a strictly possitive value.
1072 * This function is mostly used by subclasses.
1074 * Returns: TRUE if the query succeeded.
1079 gst_base_sink_query_latency (GstBaseSink * sink, gboolean * live,
1080 gboolean * upstream_live, GstClockTime * min_latency,
1081 GstClockTime * max_latency)
1083 gboolean l, us_live, res, have_latency;
1084 GstClockTime min, max, render_delay;
1086 GstClockTime us_min, us_max;
1088 /* we are live when we sync to the clock */
1089 GST_OBJECT_LOCK (sink);
1091 have_latency = sink->priv->have_latency;
1092 render_delay = sink->priv->render_delay;
1093 GST_OBJECT_UNLOCK (sink);
1095 /* assume no latency */
1101 GST_DEBUG_OBJECT (sink, "we are ready for LATENCY query");
1102 /* we are ready for a latency query this is when we preroll or when we are
1104 query = gst_query_new_latency ();
1106 /* ask the peer for the latency */
1107 if ((res = gst_pad_peer_query (sink->sinkpad, query))) {
1108 /* get upstream min and max latency */
1109 gst_query_parse_latency (query, &us_live, &us_min, &us_max);
1112 /* upstream live, use its latency, subclasses should use these
1113 * values to create the complete latency. */
1118 /* we need to add the render delay if we are live */
1120 min += render_delay;
1122 max += render_delay;
1125 gst_query_unref (query);
1127 GST_DEBUG_OBJECT (sink, "we are not yet ready for LATENCY query");
1131 /* not live, we tried to do the query, if it failed we return TRUE anyway */
1135 GST_DEBUG_OBJECT (sink, "latency query failed but we are not live");
1137 GST_DEBUG_OBJECT (sink, "latency query failed and we are live");
1142 GST_DEBUG_OBJECT (sink, "latency query: live: %d, have_latency %d,"
1143 " upstream: %d, min %" GST_TIME_FORMAT ", max %" GST_TIME_FORMAT, l,
1144 have_latency, us_live, GST_TIME_ARGS (min), GST_TIME_ARGS (max));
1149 *upstream_live = us_live;
1159 * gst_base_sink_set_render_delay:
1160 * @sink: a #GstBaseSink
1161 * @delay: the new delay
1163 * Set the render delay in @sink to @delay. The render delay is the time
1164 * between actual rendering of a buffer and its synchronisation time. Some
1165 * devices might delay media rendering which can be compensated for with this
1168 * After calling this function, this sink will report additional latency and
1169 * other sinks will adjust their latency to delay the rendering of their media.
1171 * This function is usually called by subclasses.
1176 gst_base_sink_set_render_delay (GstBaseSink * sink, GstClockTime delay)
1178 GstClockTime old_render_delay;
1180 g_return_if_fail (GST_IS_BASE_SINK (sink));
1182 GST_OBJECT_LOCK (sink);
1183 old_render_delay = sink->priv->render_delay;
1184 sink->priv->render_delay = delay;
1185 GST_LOG_OBJECT (sink, "set render delay to %" GST_TIME_FORMAT,
1186 GST_TIME_ARGS (delay));
1187 GST_OBJECT_UNLOCK (sink);
1189 if (delay != old_render_delay) {
1190 GST_DEBUG_OBJECT (sink, "posting latency changed");
1191 gst_element_post_message (GST_ELEMENT_CAST (sink),
1192 gst_message_new_latency (GST_OBJECT_CAST (sink)));
1197 * gst_base_sink_get_render_delay:
1198 * @sink: a #GstBaseSink
1200 * Get the render delay of @sink. see gst_base_sink_set_render_delay() for more
1201 * information about the render delay.
1203 * Returns: the render delay of @sink.
1208 gst_base_sink_get_render_delay (GstBaseSink * sink)
1210 GstClockTimeDiff res;
1212 g_return_val_if_fail (GST_IS_BASE_SINK (sink), 0);
1214 GST_OBJECT_LOCK (sink);
1215 res = sink->priv->render_delay;
1216 GST_OBJECT_UNLOCK (sink);
1222 * gst_base_sink_set_blocksize:
1223 * @sink: a #GstBaseSink
1224 * @blocksize: the blocksize in bytes
1226 * Set the number of bytes that the sink will pull when it is operating in pull
1231 /* FIXME 0.11: blocksize property should be int, otherwise min>max.. */
1233 gst_base_sink_set_blocksize (GstBaseSink * sink, guint blocksize)
1235 g_return_if_fail (GST_IS_BASE_SINK (sink));
1237 GST_OBJECT_LOCK (sink);
1238 sink->priv->blocksize = blocksize;
1239 GST_LOG_OBJECT (sink, "set blocksize to %u", blocksize);
1240 GST_OBJECT_UNLOCK (sink);
1244 * gst_base_sink_get_blocksize:
1245 * @sink: a #GstBaseSink
1247 * Get the number of bytes that the sink will pull when it is operating in pull
1250 * Returns: the number of bytes @sink will pull in pull mode.
1254 /* FIXME 0.11: blocksize property should be int, otherwise min>max.. */
1256 gst_base_sink_get_blocksize (GstBaseSink * sink)
1260 g_return_val_if_fail (GST_IS_BASE_SINK (sink), 0);
1262 GST_OBJECT_LOCK (sink);
1263 res = sink->priv->blocksize;
1264 GST_OBJECT_UNLOCK (sink);
1270 * gst_base_sink_set_throttle_time:
1271 * @sink: a #GstBaseSink
1272 * @throttle: the throttle time in nanoseconds
1274 * Set the time that will be inserted between rendered buffers. This
1275 * can be used to control the maximum buffers per second that the sink
1281 gst_base_sink_set_throttle_time (GstBaseSink * sink, guint64 throttle)
1283 g_return_if_fail (GST_IS_BASE_SINK (sink));
1285 GST_OBJECT_LOCK (sink);
1286 sink->priv->throttle_time = throttle;
1287 GST_LOG_OBJECT (sink, "set throttle_time to %" G_GUINT64_FORMAT, throttle);
1288 GST_OBJECT_UNLOCK (sink);
1292 * gst_base_sink_get_throttle_time:
1293 * @sink: a #GstBaseSink
1295 * Get the time that will be inserted between frames to control the
1296 * maximum buffers per second.
1298 * Returns: the number of nanoseconds @sink will put between frames.
1303 gst_base_sink_get_throttle_time (GstBaseSink * sink)
1307 g_return_val_if_fail (GST_IS_BASE_SINK (sink), 0);
1309 GST_OBJECT_LOCK (sink);
1310 res = sink->priv->throttle_time;
1311 GST_OBJECT_UNLOCK (sink);
1317 gst_base_sink_set_property (GObject * object, guint prop_id,
1318 const GValue * value, GParamSpec * pspec)
1320 GstBaseSink *sink = GST_BASE_SINK (object);
1324 gst_base_sink_set_sync (sink, g_value_get_boolean (value));
1326 case PROP_MAX_LATENESS:
1327 gst_base_sink_set_max_lateness (sink, g_value_get_int64 (value));
1330 gst_base_sink_set_qos_enabled (sink, g_value_get_boolean (value));
1333 gst_base_sink_set_async_enabled (sink, g_value_get_boolean (value));
1335 case PROP_TS_OFFSET:
1336 gst_base_sink_set_ts_offset (sink, g_value_get_int64 (value));
1338 case PROP_BLOCKSIZE:
1339 gst_base_sink_set_blocksize (sink, g_value_get_uint (value));
1341 case PROP_RENDER_DELAY:
1342 gst_base_sink_set_render_delay (sink, g_value_get_uint64 (value));
1344 case PROP_ENABLE_LAST_SAMPLE:
1345 gst_base_sink_set_last_sample_enabled (sink, g_value_get_boolean (value));
1347 case PROP_THROTTLE_TIME:
1348 gst_base_sink_set_throttle_time (sink, g_value_get_uint64 (value));
1351 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1357 gst_base_sink_get_property (GObject * object, guint prop_id, GValue * value,
1360 GstBaseSink *sink = GST_BASE_SINK (object);
1364 g_value_set_boolean (value, gst_base_sink_get_sync (sink));
1366 case PROP_MAX_LATENESS:
1367 g_value_set_int64 (value, gst_base_sink_get_max_lateness (sink));
1370 g_value_set_boolean (value, gst_base_sink_is_qos_enabled (sink));
1373 g_value_set_boolean (value, gst_base_sink_is_async_enabled (sink));
1375 case PROP_TS_OFFSET:
1376 g_value_set_int64 (value, gst_base_sink_get_ts_offset (sink));
1378 case PROP_LAST_SAMPLE:
1379 gst_value_take_buffer (value, gst_base_sink_get_last_sample (sink));
1381 case PROP_ENABLE_LAST_SAMPLE:
1382 g_value_set_boolean (value, gst_base_sink_is_last_sample_enabled (sink));
1384 case PROP_BLOCKSIZE:
1385 g_value_set_uint (value, gst_base_sink_get_blocksize (sink));
1387 case PROP_RENDER_DELAY:
1388 g_value_set_uint64 (value, gst_base_sink_get_render_delay (sink));
1390 case PROP_THROTTLE_TIME:
1391 g_value_set_uint64 (value, gst_base_sink_get_throttle_time (sink));
1394 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1401 gst_base_sink_get_caps (GstBaseSink * sink, GstCaps * filter)
1407 gst_base_sink_set_caps (GstBaseSink * sink, GstCaps * caps)
1412 /* with PREROLL_LOCK, STREAM_LOCK */
1414 gst_base_sink_commit_state (GstBaseSink * basesink)
1416 /* commit state and proceed to next pending state */
1417 GstState current, next, pending, post_pending;
1418 gboolean post_paused = FALSE;
1419 gboolean post_async_done = FALSE;
1420 gboolean post_playing = FALSE;
1421 gboolean reset_time;
1423 /* we are certainly not playing async anymore now */
1424 basesink->playing_async = FALSE;
1426 GST_OBJECT_LOCK (basesink);
1427 current = GST_STATE (basesink);
1428 next = GST_STATE_NEXT (basesink);
1429 pending = GST_STATE_PENDING (basesink);
1430 post_pending = pending;
1431 reset_time = basesink->priv->reset_time;
1432 basesink->priv->reset_time = FALSE;
1435 case GST_STATE_PLAYING:
1437 GST_DEBUG_OBJECT (basesink, "commiting state to PLAYING");
1439 basesink->need_preroll = FALSE;
1440 post_async_done = TRUE;
1441 basesink->priv->commited = TRUE;
1442 post_playing = TRUE;
1443 /* post PAUSED too when we were READY */
1444 if (current == GST_STATE_READY) {
1449 case GST_STATE_PAUSED:
1450 GST_DEBUG_OBJECT (basesink, "commiting state to PAUSED");
1452 post_async_done = TRUE;
1453 basesink->priv->commited = TRUE;
1454 post_pending = GST_STATE_VOID_PENDING;
1456 case GST_STATE_READY:
1457 case GST_STATE_NULL:
1459 case GST_STATE_VOID_PENDING:
1460 goto nothing_pending;
1465 /* we can report latency queries now */
1466 basesink->priv->have_latency = TRUE;
1468 GST_STATE (basesink) = pending;
1469 GST_STATE_NEXT (basesink) = GST_STATE_VOID_PENDING;
1470 GST_STATE_PENDING (basesink) = GST_STATE_VOID_PENDING;
1471 GST_STATE_RETURN (basesink) = GST_STATE_CHANGE_SUCCESS;
1472 GST_OBJECT_UNLOCK (basesink);
1475 GST_DEBUG_OBJECT (basesink, "posting PAUSED state change message");
1476 gst_element_post_message (GST_ELEMENT_CAST (basesink),
1477 gst_message_new_state_changed (GST_OBJECT_CAST (basesink),
1478 current, next, post_pending));
1480 if (post_async_done) {
1481 GST_DEBUG_OBJECT (basesink, "posting async-done message");
1482 gst_element_post_message (GST_ELEMENT_CAST (basesink),
1483 gst_message_new_async_done (GST_OBJECT_CAST (basesink), reset_time));
1486 GST_DEBUG_OBJECT (basesink, "posting PLAYING state change message");
1487 gst_element_post_message (GST_ELEMENT_CAST (basesink),
1488 gst_message_new_state_changed (GST_OBJECT_CAST (basesink),
1489 next, pending, GST_STATE_VOID_PENDING));
1492 GST_STATE_BROADCAST (basesink);
1498 /* Depending on the state, set our vars. We get in this situation when the
1499 * state change function got a change to update the state vars before the
1500 * streaming thread did. This is fine but we need to make sure that we
1501 * update the need_preroll var since it was TRUE when we got here and might
1502 * become FALSE if we got to PLAYING. */
1503 GST_DEBUG_OBJECT (basesink, "nothing to commit, now in %s",
1504 gst_element_state_get_name (current));
1506 case GST_STATE_PLAYING:
1507 basesink->need_preroll = FALSE;
1509 case GST_STATE_PAUSED:
1510 basesink->need_preroll = TRUE;
1513 basesink->need_preroll = FALSE;
1514 basesink->flushing = TRUE;
1517 /* we can report latency queries now */
1518 basesink->priv->have_latency = TRUE;
1519 GST_OBJECT_UNLOCK (basesink);
1524 /* app is going to READY */
1525 GST_DEBUG_OBJECT (basesink, "stopping");
1526 basesink->need_preroll = FALSE;
1527 basesink->flushing = TRUE;
1528 GST_OBJECT_UNLOCK (basesink);
1534 start_stepping (GstBaseSink * sink, GstSegment * segment,
1535 GstStepInfo * pending, GstStepInfo * current)
1538 GstMessage *message;
1540 GST_DEBUG_OBJECT (sink, "update pending step");
1542 GST_OBJECT_LOCK (sink);
1543 memcpy (current, pending, sizeof (GstStepInfo));
1544 pending->valid = FALSE;
1545 GST_OBJECT_UNLOCK (sink);
1547 /* post message first */
1549 gst_message_new_step_start (GST_OBJECT (sink), TRUE, current->format,
1550 current->amount, current->rate, current->flush, current->intermediate);
1551 gst_message_set_seqnum (message, current->seqnum);
1552 gst_element_post_message (GST_ELEMENT (sink), message);
1554 /* get the running time of where we paused and remember it */
1555 current->start = gst_element_get_start_time (GST_ELEMENT_CAST (sink));
1556 gst_segment_set_running_time (segment, GST_FORMAT_TIME, current->start);
1558 /* set the new rate for the remainder of the segment */
1559 current->start_rate = segment->rate;
1560 segment->rate *= current->rate;
1563 if (segment->rate > 0.0)
1564 current->start_stop = segment->stop;
1566 current->start_start = segment->start;
1568 if (current->format == GST_FORMAT_TIME) {
1569 end = current->start + current->amount;
1570 if (!current->flush) {
1571 /* update the segment clipping regions for non-flushing seeks */
1572 if (segment->rate > 0.0) {
1573 segment->stop = gst_segment_to_position (segment, GST_FORMAT_TIME, end);
1574 segment->position = segment->stop;
1578 position = gst_segment_to_position (segment, GST_FORMAT_TIME, end);
1579 segment->time = position;
1580 segment->start = position;
1581 segment->position = position;
1586 GST_DEBUG_OBJECT (sink, "segment now %" GST_SEGMENT_FORMAT, segment);
1587 GST_DEBUG_OBJECT (sink, "step started at running_time %" GST_TIME_FORMAT,
1588 GST_TIME_ARGS (current->start));
1590 if (current->amount == -1) {
1591 GST_DEBUG_OBJECT (sink, "step amount == -1, stop stepping");
1592 current->valid = FALSE;
1594 GST_DEBUG_OBJECT (sink, "step amount: %" G_GUINT64_FORMAT ", format: %s, "
1595 "rate: %f", current->amount, gst_format_get_name (current->format),
1601 stop_stepping (GstBaseSink * sink, GstSegment * segment,
1602 GstStepInfo * current, gint64 rstart, gint64 rstop, gboolean eos)
1604 gint64 stop, position;
1605 GstMessage *message;
1607 GST_DEBUG_OBJECT (sink, "step complete");
1609 if (segment->rate > 0.0)
1614 GST_DEBUG_OBJECT (sink,
1615 "step stop at running_time %" GST_TIME_FORMAT, GST_TIME_ARGS (stop));
1618 current->duration = current->position;
1620 current->duration = stop - current->start;
1622 GST_DEBUG_OBJECT (sink, "step elapsed running_time %" GST_TIME_FORMAT,
1623 GST_TIME_ARGS (current->duration));
1625 position = current->start + current->duration;
1627 /* now move the segment to the new running time */
1628 gst_segment_set_running_time (segment, GST_FORMAT_TIME, position);
1630 if (current->flush) {
1631 /* and remove the time we flushed, start time did not change */
1632 segment->base = current->start;
1634 /* start time is now the stepped position */
1635 gst_element_set_start_time (GST_ELEMENT_CAST (sink), position);
1638 /* restore the previous rate */
1639 segment->rate = current->start_rate;
1641 if (segment->rate > 0.0)
1642 segment->stop = current->start_stop;
1644 segment->start = current->start_start;
1646 /* post the step done when we know the stepped duration in TIME */
1648 gst_message_new_step_done (GST_OBJECT_CAST (sink), current->format,
1649 current->amount, current->rate, current->flush, current->intermediate,
1650 current->duration, eos);
1651 gst_message_set_seqnum (message, current->seqnum);
1652 gst_element_post_message (GST_ELEMENT_CAST (sink), message);
1654 if (!current->intermediate)
1655 sink->need_preroll = current->need_preroll;
1657 /* and the current step info finished and becomes invalid */
1658 current->valid = FALSE;
1662 handle_stepping (GstBaseSink * sink, GstSegment * segment,
1663 GstStepInfo * current, guint64 * cstart, guint64 * cstop, guint64 * rstart,
1666 gboolean step_end = FALSE;
1668 /* see if we need to skip this buffer because of stepping */
1669 switch (current->format) {
1670 case GST_FORMAT_TIME:
1673 guint64 first, last;
1676 if (segment->rate > 0.0) {
1677 if (segment->stop == *cstop)
1678 *rstop = *rstart + current->amount;
1683 if (segment->start == *cstart)
1684 *rstart = *rstop + current->amount;
1690 end = current->start + current->amount;
1691 current->position = first - current->start;
1693 abs_rate = ABS (segment->rate);
1694 if (G_UNLIKELY (abs_rate != 1.0))
1695 current->position /= abs_rate;
1697 GST_DEBUG_OBJECT (sink,
1698 "buffer: %" GST_TIME_FORMAT "-%" GST_TIME_FORMAT,
1699 GST_TIME_ARGS (first), GST_TIME_ARGS (last));
1700 GST_DEBUG_OBJECT (sink,
1701 "got time step %" GST_TIME_FORMAT "-%" GST_TIME_FORMAT "/%"
1702 GST_TIME_FORMAT, GST_TIME_ARGS (current->position),
1703 GST_TIME_ARGS (last - current->start),
1704 GST_TIME_ARGS (current->amount));
1706 if ((current->flush && current->position >= current->amount)
1708 GST_DEBUG_OBJECT (sink, "step ended, we need clipping");
1710 if (segment->rate > 0.0) {
1712 *cstart = gst_segment_to_position (segment, GST_FORMAT_TIME, end);
1715 *cstop = gst_segment_to_position (segment, GST_FORMAT_TIME, end);
1718 GST_DEBUG_OBJECT (sink,
1719 "cstart %" GST_TIME_FORMAT ", rstart %" GST_TIME_FORMAT,
1720 GST_TIME_ARGS (*cstart), GST_TIME_ARGS (*rstart));
1721 GST_DEBUG_OBJECT (sink,
1722 "cstop %" GST_TIME_FORMAT ", rstop %" GST_TIME_FORMAT,
1723 GST_TIME_ARGS (*cstop), GST_TIME_ARGS (*rstop));
1726 case GST_FORMAT_BUFFERS:
1727 GST_DEBUG_OBJECT (sink,
1728 "got default step %" G_GUINT64_FORMAT "/%" G_GUINT64_FORMAT,
1729 current->position, current->amount);
1731 if (current->position < current->amount) {
1732 current->position++;
1737 case GST_FORMAT_DEFAULT:
1739 GST_DEBUG_OBJECT (sink,
1740 "got unknown step %" G_GUINT64_FORMAT "/%" G_GUINT64_FORMAT,
1741 current->position, current->amount);
1747 /* with STREAM_LOCK, PREROLL_LOCK
1749 * Returns TRUE if the object needs synchronisation and takes therefore
1750 * part in prerolling.
1752 * rsstart/rsstop contain the start/stop in stream time.
1753 * rrstart/rrstop contain the start/stop in running time.
1756 gst_base_sink_get_sync_times (GstBaseSink * basesink, GstMiniObject * obj,
1757 GstClockTime * rsstart, GstClockTime * rsstop,
1758 GstClockTime * rrstart, GstClockTime * rrstop, gboolean * do_sync,
1759 gboolean * stepped, GstStepInfo * step,
1760 gboolean * step_end, guint8 obj_type)
1762 GstBaseSinkClass *bclass;
1764 GstClockTime start, stop; /* raw start/stop timestamps */
1765 guint64 cstart, cstop; /* clipped raw timestamps */
1766 guint64 rstart, rstop; /* clipped timestamps converted to running time */
1767 GstClockTime sstart, sstop; /* clipped timestamps converted to stream time */
1769 GstBaseSinkPrivate *priv;
1770 GstSegment *segment;
1773 priv = basesink->priv;
1774 segment = &basesink->segment;
1776 /* start with nothing */
1777 start = stop = GST_CLOCK_TIME_NONE;
1779 if (G_UNLIKELY (OBJ_IS_EVENT (obj_type))) {
1780 GstEvent *event = GST_EVENT_CAST (obj);
1782 switch (GST_EVENT_TYPE (event)) {
1783 /* EOS event needs syncing */
1786 if (segment->rate >= 0.0) {
1787 sstart = sstop = priv->current_sstop;
1788 if (!GST_CLOCK_TIME_IS_VALID (sstart)) {
1789 /* we have not seen a buffer yet, use the segment values */
1790 sstart = sstop = gst_segment_to_stream_time (segment,
1791 segment->format, segment->stop);
1794 sstart = sstop = priv->current_sstart;
1795 if (!GST_CLOCK_TIME_IS_VALID (sstart)) {
1796 /* we have not seen a buffer yet, use the segment values */
1797 sstart = sstop = gst_segment_to_stream_time (segment,
1798 segment->format, segment->start);
1802 rstart = rstop = priv->eos_rtime;
1803 *do_sync = rstart != -1;
1804 GST_DEBUG_OBJECT (basesink, "sync times for EOS %" GST_TIME_FORMAT,
1805 GST_TIME_ARGS (rstart));
1806 /* if we are stepping, we end now */
1807 *step_end = step->valid;
1812 /* other events do not need syncing */
1820 /* else do buffer sync code */
1821 buffer = GST_BUFFER_CAST (obj);
1823 bclass = GST_BASE_SINK_GET_CLASS (basesink);
1825 /* just get the times to see if we need syncing, if the start returns -1 we
1827 if (bclass->get_times)
1828 bclass->get_times (basesink, buffer, &start, &stop);
1830 if (!GST_CLOCK_TIME_IS_VALID (start)) {
1831 /* we don't need to sync but we still want to get the timestamps for
1832 * tracking the position */
1833 gst_base_sink_get_times (basesink, buffer, &start, &stop);
1839 GST_DEBUG_OBJECT (basesink, "got times start: %" GST_TIME_FORMAT
1840 ", stop: %" GST_TIME_FORMAT ", do_sync %d", GST_TIME_ARGS (start),
1841 GST_TIME_ARGS (stop), *do_sync);
1843 /* collect segment and format for code clarity */
1844 format = segment->format;
1847 if (G_UNLIKELY (!gst_segment_clip (segment, format,
1848 start, stop, &cstart, &cstop))) {
1850 GST_DEBUG_OBJECT (basesink, "step out of segment");
1851 /* when we are stepping, pretend we're at the end of the segment */
1852 if (segment->rate > 0.0) {
1853 cstart = segment->stop;
1854 cstop = segment->stop;
1856 cstart = segment->start;
1857 cstop = segment->start;
1861 goto out_of_segment;
1864 if (G_UNLIKELY (start != cstart || stop != cstop)) {
1865 GST_DEBUG_OBJECT (basesink, "clipped to: start %" GST_TIME_FORMAT
1866 ", stop: %" GST_TIME_FORMAT, GST_TIME_ARGS (cstart),
1867 GST_TIME_ARGS (cstop));
1870 /* set last stop position */
1871 if (G_LIKELY (stop != GST_CLOCK_TIME_NONE && cstop != GST_CLOCK_TIME_NONE))
1872 segment->position = cstop;
1874 segment->position = cstart;
1877 rstart = gst_segment_to_running_time (segment, format, cstart);
1878 rstop = gst_segment_to_running_time (segment, format, cstop);
1880 if (G_UNLIKELY (step->valid)) {
1881 if (!(*step_end = handle_stepping (basesink, segment, step, &cstart, &cstop,
1882 &rstart, &rstop))) {
1883 /* step is still busy, we discard data when we are flushing */
1884 *stepped = step->flush;
1885 GST_DEBUG_OBJECT (basesink, "stepping busy");
1888 /* this can produce wrong values if we accumulated non-TIME segments. If this happens,
1889 * upstream is behaving very badly */
1890 sstart = gst_segment_to_stream_time (segment, format, cstart);
1891 sstop = gst_segment_to_stream_time (segment, format, cstop);
1894 /* eos_done label only called when doing EOS, we also stop stepping then */
1895 if (*step_end && step->flush) {
1896 GST_DEBUG_OBJECT (basesink, "flushing step ended");
1897 stop_stepping (basesink, segment, step, rstart, rstop, eos);
1899 /* re-determine running start times for adjusted segment
1900 * (which has a flushed amount of running/accumulated time removed) */
1901 if (!GST_IS_EVENT (obj)) {
1902 GST_DEBUG_OBJECT (basesink, "refresh sync times");
1913 /* buffers and EOS always need syncing and preroll */
1919 /* we usually clip in the chain function already but stepping could cause
1920 * the segment to be updated later. we return FALSE so that we don't try
1922 GST_LOG_OBJECT (basesink, "buffer skipped, not in segment");
1927 /* with STREAM_LOCK, PREROLL_LOCK, LOCK
1928 * adjust a timestamp with the latency and timestamp offset. This function does
1929 * not adjust for the render delay. */
1931 gst_base_sink_adjust_time (GstBaseSink * basesink, GstClockTime time)
1933 GstClockTimeDiff ts_offset;
1935 /* don't do anything funny with invalid timestamps */
1936 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (time)))
1939 time += basesink->priv->latency;
1941 /* apply offset, be carefull for underflows */
1942 ts_offset = basesink->priv->ts_offset;
1943 if (ts_offset < 0) {
1944 ts_offset = -ts_offset;
1945 if (ts_offset < time)
1952 /* subtract the render delay again, which was included in the latency */
1953 if (time > basesink->priv->render_delay)
1954 time -= basesink->priv->render_delay;
1962 * gst_base_sink_wait_clock:
1964 * @time: the running_time to be reached
1965 * @jitter: (out) (allow-none): the jitter to be filled with time diff, or NULL
1967 * This function will block until @time is reached. It is usually called by
1968 * subclasses that use their own internal synchronisation.
1970 * If @time is not valid, no sycnhronisation is done and #GST_CLOCK_BADTIME is
1971 * returned. Likewise, if synchronisation is disabled in the element or there
1972 * is no clock, no synchronisation is done and #GST_CLOCK_BADTIME is returned.
1974 * This function should only be called with the PREROLL_LOCK held, like when
1975 * receiving an EOS event in the #GstBaseSinkClass.event() vmethod or when
1976 * receiving a buffer in
1977 * the #GstBaseSinkClass.render() vmethod.
1979 * The @time argument should be the running_time of when this method should
1980 * return and is not adjusted with any latency or offset configured in the
1985 * Returns: #GstClockReturn
1988 gst_base_sink_wait_clock (GstBaseSink * sink, GstClockTime time,
1989 GstClockTimeDiff * jitter)
1993 GstClockTime base_time;
1995 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (time)))
1998 GST_OBJECT_LOCK (sink);
1999 if (G_UNLIKELY (!sink->sync))
2002 if (G_UNLIKELY ((clock = GST_ELEMENT_CLOCK (sink)) == NULL))
2005 base_time = GST_ELEMENT_CAST (sink)->base_time;
2006 GST_LOG_OBJECT (sink,
2007 "time %" GST_TIME_FORMAT ", base_time %" GST_TIME_FORMAT,
2008 GST_TIME_ARGS (time), GST_TIME_ARGS (base_time));
2010 /* add base_time to running_time to get the time against the clock */
2013 /* Re-use existing clockid if available */
2014 /* FIXME: Casting to GstClockEntry only works because the types
2016 if (G_LIKELY (sink->priv->cached_clock_id != NULL
2017 && GST_CLOCK_ENTRY_CLOCK ((GstClockEntry *) sink->priv->
2018 cached_clock_id) == clock)) {
2019 if (!gst_clock_single_shot_id_reinit (clock, sink->priv->cached_clock_id,
2021 gst_clock_id_unref (sink->priv->cached_clock_id);
2022 sink->priv->cached_clock_id = gst_clock_new_single_shot_id (clock, time);
2025 if (sink->priv->cached_clock_id != NULL)
2026 gst_clock_id_unref (sink->priv->cached_clock_id);
2027 sink->priv->cached_clock_id = gst_clock_new_single_shot_id (clock, time);
2029 GST_OBJECT_UNLOCK (sink);
2031 /* A blocking wait is performed on the clock. We save the ClockID
2032 * so we can unlock the entry at any time. While we are blocking, we
2033 * release the PREROLL_LOCK so that other threads can interrupt the
2035 sink->clock_id = sink->priv->cached_clock_id;
2036 /* release the preroll lock while waiting */
2037 GST_BASE_SINK_PREROLL_UNLOCK (sink);
2039 ret = gst_clock_id_wait (sink->priv->cached_clock_id, jitter);
2041 GST_BASE_SINK_PREROLL_LOCK (sink);
2042 sink->clock_id = NULL;
2046 /* no syncing needed */
2049 GST_DEBUG_OBJECT (sink, "time not valid, no sync needed");
2050 return GST_CLOCK_BADTIME;
2054 GST_DEBUG_OBJECT (sink, "sync disabled");
2055 GST_OBJECT_UNLOCK (sink);
2056 return GST_CLOCK_BADTIME;
2060 GST_DEBUG_OBJECT (sink, "no clock, can't sync");
2061 GST_OBJECT_UNLOCK (sink);
2062 return GST_CLOCK_BADTIME;
2067 * gst_base_sink_wait_preroll:
2070 * If the #GstBaseSinkClass.render() method performs its own synchronisation
2071 * against the clock it must unblock when going from PLAYING to the PAUSED state
2072 * and call this method before continuing to render the remaining data.
2074 * This function will block until a state change to PLAYING happens (in which
2075 * case this function returns #GST_FLOW_OK) or the processing must be stopped due
2076 * to a state change to READY or a FLUSH event (in which case this function
2077 * returns #GST_FLOW_WRONG_STATE).
2079 * This function should only be called with the PREROLL_LOCK held, like in the
2082 * Returns: #GST_FLOW_OK if the preroll completed and processing can
2083 * continue. Any other return value should be returned from the render vmethod.
2088 gst_base_sink_wait_preroll (GstBaseSink * sink)
2090 sink->have_preroll = TRUE;
2091 GST_DEBUG_OBJECT (sink, "waiting in preroll for flush or PLAYING");
2092 /* block until the state changes, or we get a flush, or something */
2093 GST_BASE_SINK_PREROLL_WAIT (sink);
2094 sink->have_preroll = FALSE;
2095 if (G_UNLIKELY (sink->flushing))
2097 if (G_UNLIKELY (sink->priv->step_unlock))
2099 GST_DEBUG_OBJECT (sink, "continue after preroll");
2106 GST_DEBUG_OBJECT (sink, "preroll interrupted because of flush");
2107 return GST_FLOW_WRONG_STATE;
2111 sink->priv->step_unlock = FALSE;
2112 GST_DEBUG_OBJECT (sink, "preroll interrupted because of step");
2113 return GST_FLOW_STEP;
2117 static inline guint8
2118 get_object_type (GstMiniObject * obj)
2122 if (G_LIKELY (GST_IS_BUFFER (obj)))
2123 obj_type = _PR_IS_BUFFER;
2124 else if (GST_IS_EVENT (obj))
2125 obj_type = _PR_IS_EVENT;
2126 else if (GST_IS_BUFFER_LIST (obj))
2127 obj_type = _PR_IS_BUFFERLIST;
2129 obj_type = _PR_IS_NOTHING;
2135 * gst_base_sink_do_preroll:
2137 * @obj: (transfer none): the mini object that caused the preroll
2139 * If the @sink spawns its own thread for pulling buffers from upstream it
2140 * should call this method after it has pulled a buffer. If the element needed
2141 * to preroll, this function will perform the preroll and will then block
2142 * until the element state is changed.
2144 * This function should be called with the PREROLL_LOCK held.
2146 * Returns: #GST_FLOW_OK if the preroll completed and processing can
2147 * continue. Any other return value should be returned from the render vmethod.
2152 gst_base_sink_do_preroll (GstBaseSink * sink, GstMiniObject * obj)
2156 while (G_UNLIKELY (sink->need_preroll)) {
2158 GST_DEBUG_OBJECT (sink, "prerolling object %p", obj);
2160 obj_type = get_object_type (obj);
2162 ret = gst_base_sink_preroll_object (sink, obj_type, obj);
2163 if (ret != GST_FLOW_OK)
2164 goto preroll_failed;
2166 /* need to recheck here because the commit state could have
2167 * made us not need the preroll anymore */
2168 if (G_LIKELY (sink->need_preroll)) {
2169 /* block until the state changes, or we get a flush, or something */
2170 ret = gst_base_sink_wait_preroll (sink);
2171 if ((ret != GST_FLOW_OK) && (ret != GST_FLOW_STEP))
2172 goto preroll_failed;
2180 GST_DEBUG_OBJECT (sink, "preroll failed: %s", gst_flow_get_name (ret));
2186 * gst_base_sink_wait_eos:
2188 * @time: the running_time to be reached
2189 * @jitter: (out) (allow-none): the jitter to be filled with time diff, or NULL
2191 * This function will block until @time is reached. It is usually called by
2192 * subclasses that use their own internal synchronisation but want to let the
2193 * EOS be handled by the base class.
2195 * This function should only be called with the PREROLL_LOCK held, like when
2196 * receiving an EOS event in the ::event vmethod.
2198 * The @time argument should be the running_time of when the EOS should happen
2199 * and will be adjusted with any latency and offset configured in the sink.
2201 * Returns: #GstFlowReturn
2206 gst_base_sink_wait_eos (GstBaseSink * sink, GstClockTime time,
2207 GstClockTimeDiff * jitter)
2209 GstClockReturn status;
2215 GST_DEBUG_OBJECT (sink, "checking preroll");
2217 /* first wait for the playing state before we can continue */
2218 while (G_UNLIKELY (sink->need_preroll)) {
2219 ret = gst_base_sink_wait_preroll (sink);
2220 if ((ret != GST_FLOW_OK) && (ret != GST_FLOW_STEP))
2224 /* preroll done, we can sync since we are in PLAYING now. */
2225 GST_DEBUG_OBJECT (sink, "possibly waiting for clock to reach %"
2226 GST_TIME_FORMAT, GST_TIME_ARGS (time));
2228 /* compensate for latency and ts_offset. We don't adjust for render delay
2229 * because we don't interact with the device on EOS normally. */
2230 stime = gst_base_sink_adjust_time (sink, time);
2232 /* wait for the clock, this can be interrupted because we got shut down or
2234 status = gst_base_sink_wait_clock (sink, stime, jitter);
2236 GST_DEBUG_OBJECT (sink, "clock returned %d", status);
2238 /* invalid time, no clock or sync disabled, just continue then */
2239 if (status == GST_CLOCK_BADTIME)
2242 /* waiting could have been interrupted and we can be flushing now */
2243 if (G_UNLIKELY (sink->flushing))
2246 /* retry if we got unscheduled, which means we did not reach the timeout
2247 * yet. if some other error occures, we continue. */
2248 } while (status == GST_CLOCK_UNSCHEDULED);
2250 GST_DEBUG_OBJECT (sink, "end of stream");
2257 GST_DEBUG_OBJECT (sink, "we are flushing");
2258 return GST_FLOW_WRONG_STATE;
2262 /* with STREAM_LOCK, PREROLL_LOCK
2264 * Make sure we are in PLAYING and synchronize an object to the clock.
2266 * If we need preroll, we are not in PLAYING. We try to commit the state
2267 * if needed and then block if we still are not PLAYING.
2269 * We start waiting on the clock in PLAYING. If we got interrupted, we
2270 * immediately try to re-preroll.
2272 * Some objects do not need synchronisation (most events) and so this function
2273 * immediately returns GST_FLOW_OK.
2275 * for objects that arrive later than max-lateness to be synchronized to the
2276 * clock have the @late boolean set to TRUE.
2278 * This function keeps a running average of the jitter (the diff between the
2279 * clock time and the requested sync time). The jitter is negative for
2280 * objects that arrive in time and positive for late buffers.
2282 * does not take ownership of obj.
2284 static GstFlowReturn
2285 gst_base_sink_do_sync (GstBaseSink * basesink,
2286 GstMiniObject * obj, gboolean * late, gboolean * step_end, guint8 obj_type)
2288 GstClockTimeDiff jitter = 0;
2290 GstClockReturn status = GST_CLOCK_OK;
2291 GstClockTime rstart, rstop, sstart, sstop, stime;
2293 GstBaseSinkPrivate *priv;
2295 GstStepInfo *current, *pending;
2298 priv = basesink->priv;
2301 sstart = sstop = rstart = rstop = GST_CLOCK_TIME_NONE;
2305 priv->current_rstart = GST_CLOCK_TIME_NONE;
2307 /* get stepping info */
2308 current = &priv->current_step;
2309 pending = &priv->pending_step;
2311 /* get timing information for this object against the render segment */
2312 syncable = gst_base_sink_get_sync_times (basesink, obj,
2313 &sstart, &sstop, &rstart, &rstop, &do_sync, &stepped,
2314 current, step_end, obj_type);
2316 if (G_UNLIKELY (stepped))
2319 /* a syncable object needs to participate in preroll and
2320 * clocking. All buffers and EOS are syncable. */
2321 if (G_UNLIKELY (!syncable))
2324 /* store timing info for current object */
2325 priv->current_rstart = rstart;
2326 priv->current_rstop = (GST_CLOCK_TIME_IS_VALID (rstop) ? rstop : rstart);
2328 /* save sync time for eos when the previous object needed sync */
2329 priv->eos_rtime = (do_sync ? priv->current_rstop : GST_CLOCK_TIME_NONE);
2331 /* calculate inter frame spacing */
2332 if (G_UNLIKELY (priv->prev_rstart != -1 && priv->prev_rstart < rstart)) {
2333 GstClockTime in_diff;
2335 in_diff = rstart - priv->prev_rstart;
2337 if (priv->avg_in_diff == -1)
2338 priv->avg_in_diff = in_diff;
2340 priv->avg_in_diff = UPDATE_RUNNING_AVG (priv->avg_in_diff, in_diff);
2342 GST_LOG_OBJECT (basesink, "avg frame diff %" GST_TIME_FORMAT,
2343 GST_TIME_ARGS (priv->avg_in_diff));
2346 priv->prev_rstart = rstart;
2348 if (G_UNLIKELY (priv->earliest_in_time != -1
2349 && rstart < priv->earliest_in_time))
2353 /* first do preroll, this makes sure we commit our state
2354 * to PAUSED and can continue to PLAYING. We cannot perform
2355 * any clock sync in PAUSED because there is no clock. */
2356 ret = gst_base_sink_do_preroll (basesink, obj);
2357 if (G_UNLIKELY (ret != GST_FLOW_OK))
2358 goto preroll_failed;
2360 /* update the segment with a pending step if the current one is invalid and we
2361 * have a new pending one. We only accept new step updates after a preroll */
2362 if (G_UNLIKELY (pending->valid && !current->valid)) {
2363 start_stepping (basesink, &basesink->segment, pending, current);
2367 /* After rendering we store the position of the last buffer so that we can use
2368 * it to report the position. We need to take the lock here. */
2369 GST_OBJECT_LOCK (basesink);
2370 priv->current_sstart = sstart;
2371 priv->current_sstop = (GST_CLOCK_TIME_IS_VALID (sstop) ? sstop : sstart);
2372 GST_OBJECT_UNLOCK (basesink);
2377 /* adjust for latency */
2378 stime = gst_base_sink_adjust_time (basesink, rstart);
2380 /* adjust for render-delay, avoid underflows */
2381 if (GST_CLOCK_TIME_IS_VALID (stime)) {
2382 if (stime > priv->render_delay)
2383 stime -= priv->render_delay;
2388 /* preroll done, we can sync since we are in PLAYING now. */
2389 GST_DEBUG_OBJECT (basesink, "possibly waiting for clock to reach %"
2390 GST_TIME_FORMAT ", adjusted %" GST_TIME_FORMAT,
2391 GST_TIME_ARGS (rstart), GST_TIME_ARGS (stime));
2393 /* This function will return immediately if start == -1, no clock
2394 * or sync is disabled with GST_CLOCK_BADTIME. */
2395 status = gst_base_sink_wait_clock (basesink, stime, &jitter);
2397 GST_DEBUG_OBJECT (basesink, "clock returned %d, jitter %c%" GST_TIME_FORMAT,
2398 status, (jitter < 0 ? '-' : ' '), GST_TIME_ARGS (ABS (jitter)));
2400 /* invalid time, no clock or sync disabled, just render */
2401 if (status == GST_CLOCK_BADTIME)
2404 /* waiting could have been interrupted and we can be flushing now */
2405 if (G_UNLIKELY (basesink->flushing))
2408 /* check for unlocked by a state change, we are not flushing so
2409 * we can try to preroll on the current buffer. */
2410 if (G_UNLIKELY (status == GST_CLOCK_UNSCHEDULED)) {
2411 GST_DEBUG_OBJECT (basesink, "unscheduled, waiting some more");
2412 priv->call_preroll = TRUE;
2416 /* successful syncing done, record observation */
2417 priv->current_jitter = jitter;
2419 /* check if the object should be dropped */
2420 *late = gst_base_sink_is_too_late (basesink, obj, rstart, rstop,
2429 GST_DEBUG_OBJECT (basesink, "skipped stepped object %p", obj);
2435 GST_DEBUG_OBJECT (basesink, "non syncable object %p", obj);
2440 GST_DEBUG_OBJECT (basesink, "dropped because of QoS %p", obj);
2446 GST_DEBUG_OBJECT (basesink, "we are flushing");
2447 return GST_FLOW_WRONG_STATE;
2451 GST_DEBUG_OBJECT (basesink, "preroll failed");
2458 gst_base_sink_send_qos (GstBaseSink * basesink, GstQOSType type,
2459 gdouble proportion, GstClockTime time, GstClockTimeDiff diff)
2464 /* generate Quality-of-Service event */
2465 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, basesink,
2466 "qos: type %d, proportion: %lf, diff %" G_GINT64_FORMAT ", timestamp %"
2467 GST_TIME_FORMAT, type, proportion, diff, GST_TIME_ARGS (time));
2469 event = gst_event_new_qos (type, proportion, diff, time);
2472 res = gst_pad_push_event (basesink->sinkpad, event);
2478 gst_base_sink_perform_qos (GstBaseSink * sink, gboolean dropped)
2480 GstBaseSinkPrivate *priv;
2481 GstClockTime start, stop;
2482 GstClockTimeDiff jitter;
2483 GstClockTime pt, entered, left;
2484 GstClockTime duration;
2489 start = priv->current_rstart;
2491 if (priv->current_step.valid)
2494 /* if Quality-of-Service disabled, do nothing */
2495 if (!g_atomic_int_get (&priv->qos_enabled) ||
2496 !GST_CLOCK_TIME_IS_VALID (start))
2499 stop = priv->current_rstop;
2500 jitter = priv->current_jitter;
2503 /* this is the time the buffer entered the sink */
2504 if (start < -jitter)
2507 entered = start + jitter;
2510 /* this is the time the buffer entered the sink */
2511 entered = start + jitter;
2512 /* this is the time the buffer left the sink */
2513 left = start + jitter;
2516 /* calculate duration of the buffer */
2517 if (GST_CLOCK_TIME_IS_VALID (stop) && stop != start)
2518 duration = stop - start;
2520 duration = priv->avg_in_diff;
2522 /* if we have the time when the last buffer left us, calculate
2523 * processing time */
2524 if (GST_CLOCK_TIME_IS_VALID (priv->last_left)) {
2525 if (entered > priv->last_left) {
2526 pt = entered - priv->last_left;
2534 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, sink, "start: %" GST_TIME_FORMAT
2535 ", stop %" GST_TIME_FORMAT ", entered %" GST_TIME_FORMAT ", left %"
2536 GST_TIME_FORMAT ", pt: %" GST_TIME_FORMAT ", duration %" GST_TIME_FORMAT
2537 ",jitter %" G_GINT64_FORMAT, GST_TIME_ARGS (start), GST_TIME_ARGS (stop),
2538 GST_TIME_ARGS (entered), GST_TIME_ARGS (left), GST_TIME_ARGS (pt),
2539 GST_TIME_ARGS (duration), jitter);
2541 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, sink, "avg_duration: %" GST_TIME_FORMAT
2542 ", avg_pt: %" GST_TIME_FORMAT ", avg_rate: %g",
2543 GST_TIME_ARGS (priv->avg_duration), GST_TIME_ARGS (priv->avg_pt),
2546 /* collect running averages. for first observations, we copy the
2548 if (!GST_CLOCK_TIME_IS_VALID (priv->avg_duration))
2549 priv->avg_duration = duration;
2551 priv->avg_duration = UPDATE_RUNNING_AVG (priv->avg_duration, duration);
2553 if (!GST_CLOCK_TIME_IS_VALID (priv->avg_pt))
2556 priv->avg_pt = UPDATE_RUNNING_AVG (priv->avg_pt, pt);
2558 if (priv->avg_duration != 0)
2560 gst_guint64_to_gdouble (priv->avg_pt) /
2561 gst_guint64_to_gdouble (priv->avg_duration);
2565 if (GST_CLOCK_TIME_IS_VALID (priv->last_left)) {
2566 if (dropped || priv->avg_rate < 0.0) {
2567 priv->avg_rate = rate;
2570 priv->avg_rate = UPDATE_RUNNING_AVG_N (priv->avg_rate, rate);
2572 priv->avg_rate = UPDATE_RUNNING_AVG_P (priv->avg_rate, rate);
2576 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, sink,
2577 "updated: avg_duration: %" GST_TIME_FORMAT ", avg_pt: %" GST_TIME_FORMAT
2578 ", avg_rate: %g", GST_TIME_ARGS (priv->avg_duration),
2579 GST_TIME_ARGS (priv->avg_pt), priv->avg_rate);
2582 if (priv->avg_rate >= 0.0) {
2584 GstClockTimeDiff diff;
2586 /* if we have a valid rate, start sending QoS messages */
2587 if (priv->current_jitter < 0) {
2588 /* make sure we never go below 0 when adding the jitter to the
2590 if (priv->current_rstart < -priv->current_jitter)
2591 priv->current_jitter = -priv->current_rstart;
2594 if (priv->throttle_time > 0) {
2595 diff = priv->throttle_time;
2596 type = GST_QOS_TYPE_THROTTLE;
2598 diff = priv->current_jitter;
2600 type = GST_QOS_TYPE_OVERFLOW;
2602 type = GST_QOS_TYPE_UNDERFLOW;
2605 gst_base_sink_send_qos (sink, type, priv->avg_rate, priv->current_rstart,
2609 /* record when this buffer will leave us */
2610 priv->last_left = left;
2613 /* reset all qos measuring */
2615 gst_base_sink_reset_qos (GstBaseSink * sink)
2617 GstBaseSinkPrivate *priv;
2621 priv->last_render_time = GST_CLOCK_TIME_NONE;
2622 priv->prev_rstart = GST_CLOCK_TIME_NONE;
2623 priv->earliest_in_time = GST_CLOCK_TIME_NONE;
2624 priv->last_left = GST_CLOCK_TIME_NONE;
2625 priv->avg_duration = GST_CLOCK_TIME_NONE;
2626 priv->avg_pt = GST_CLOCK_TIME_NONE;
2627 priv->avg_rate = -1.0;
2628 priv->avg_render = GST_CLOCK_TIME_NONE;
2629 priv->avg_in_diff = GST_CLOCK_TIME_NONE;
2635 /* Checks if the object was scheduled too late.
2637 * rstart/rstop contain the running_time start and stop values
2640 * status and jitter contain the return values from the clock wait.
2642 * returns TRUE if the buffer was too late.
2645 gst_base_sink_is_too_late (GstBaseSink * basesink, GstMiniObject * obj,
2646 GstClockTime rstart, GstClockTime rstop,
2647 GstClockReturn status, GstClockTimeDiff jitter)
2650 guint64 max_lateness;
2651 GstBaseSinkPrivate *priv;
2653 priv = basesink->priv;
2657 /* only for objects that were too late */
2658 if (G_LIKELY (status != GST_CLOCK_EARLY))
2661 max_lateness = basesink->max_lateness;
2663 /* check if frame dropping is enabled */
2664 if (max_lateness == -1)
2667 /* only check for buffers */
2668 if (G_UNLIKELY (!GST_IS_BUFFER (obj)))
2671 /* can't do check if we don't have a timestamp */
2672 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (rstart)))
2675 /* we can add a valid stop time */
2676 if (GST_CLOCK_TIME_IS_VALID (rstop))
2677 max_lateness += rstop;
2679 max_lateness += rstart;
2680 /* no stop time, use avg frame diff */
2681 if (priv->avg_in_diff != -1)
2682 max_lateness += priv->avg_in_diff;
2685 /* if the jitter bigger than duration and lateness we are too late */
2686 if ((late = rstart + jitter > max_lateness)) {
2687 GST_CAT_DEBUG_OBJECT (GST_CAT_PERFORMANCE, basesink,
2688 "buffer is too late %" GST_TIME_FORMAT
2689 " > %" GST_TIME_FORMAT, GST_TIME_ARGS (rstart + jitter),
2690 GST_TIME_ARGS (max_lateness));
2691 /* !!emergency!!, if we did not receive anything valid for more than a
2692 * second, render it anyway so the user sees something */
2693 if (GST_CLOCK_TIME_IS_VALID (priv->last_render_time) &&
2694 rstart - priv->last_render_time > GST_SECOND) {
2696 GST_ELEMENT_WARNING (basesink, CORE, CLOCK,
2697 (_("A lot of buffers are being dropped.")),
2698 ("There may be a timestamping problem, or this computer is too slow."));
2699 GST_CAT_DEBUG_OBJECT (GST_CAT_PERFORMANCE, basesink,
2700 "**emergency** last buffer at %" GST_TIME_FORMAT " > GST_SECOND",
2701 GST_TIME_ARGS (priv->last_render_time));
2706 if (!late || !GST_CLOCK_TIME_IS_VALID (priv->last_render_time)) {
2707 priv->last_render_time = rstart;
2708 /* the next allowed input timestamp */
2709 if (priv->throttle_time > 0)
2710 priv->earliest_in_time = rstart + priv->throttle_time;
2717 GST_DEBUG_OBJECT (basesink, "object was scheduled in time");
2722 GST_DEBUG_OBJECT (basesink, "frame dropping disabled");
2727 GST_DEBUG_OBJECT (basesink, "object is not a buffer");
2732 GST_DEBUG_OBJECT (basesink, "buffer has no timestamp");
2737 /* called before and after calling the render vmethod. It keeps track of how
2738 * much time was spent in the render method and is used to check if we are
2741 gst_base_sink_do_render_stats (GstBaseSink * basesink, gboolean start)
2743 GstBaseSinkPrivate *priv;
2745 priv = basesink->priv;
2748 priv->start = gst_util_get_timestamp ();
2750 GstClockTime elapsed;
2752 priv->stop = gst_util_get_timestamp ();
2754 elapsed = GST_CLOCK_DIFF (priv->start, priv->stop);
2756 if (!GST_CLOCK_TIME_IS_VALID (priv->avg_render))
2757 priv->avg_render = elapsed;
2759 priv->avg_render = UPDATE_RUNNING_AVG (priv->avg_render, elapsed);
2761 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, basesink,
2762 "avg_render: %" GST_TIME_FORMAT, GST_TIME_ARGS (priv->avg_render));
2766 /* with STREAM_LOCK, PREROLL_LOCK,
2768 * Synchronize the object on the clock and then render it.
2770 * takes ownership of obj.
2772 static GstFlowReturn
2773 gst_base_sink_render_object (GstBaseSink * basesink,
2774 guint8 obj_type, gpointer obj)
2777 GstBaseSinkClass *bclass;
2779 gboolean late, step_end;
2781 GstBaseSinkPrivate *priv;
2783 priv = basesink->priv;
2785 if (OBJ_IS_BUFFERLIST (obj_type)) {
2786 /* If buffer list, use the first group buffer within the list
2788 sync_obj = gst_buffer_list_get (GST_BUFFER_LIST_CAST (obj), 0);
2789 g_assert (NULL != sync_obj);
2798 /* synchronize this object, non syncable objects return OK
2800 ret = gst_base_sink_do_sync (basesink, sync_obj, &late, &step_end, obj_type);
2801 if (G_UNLIKELY (ret != GST_FLOW_OK))
2804 /* drop late buffers unconditionally, let's hope it's unlikely */
2805 if (G_UNLIKELY (late))
2808 bclass = GST_BASE_SINK_GET_CLASS (basesink);
2810 /* read once, to get same value before and after */
2811 do_qos = g_atomic_int_get (&priv->qos_enabled);
2813 GST_DEBUG_OBJECT (basesink, "rendering object %p", obj);
2815 /* record rendering time for QoS and stats */
2817 gst_base_sink_do_render_stats (basesink, TRUE);
2819 if (!OBJ_IS_BUFFERLIST (obj_type)) {
2822 /* For buffer lists do not set last buffer. Creating buffer
2823 * with meaningful data can be done only with memcpy which will
2824 * significantly affect performance */
2825 buf = GST_BUFFER_CAST (obj);
2826 gst_base_sink_set_last_buffer (basesink, buf);
2829 ret = bclass->render (basesink, buf);
2831 GstBufferList *buflist;
2833 buflist = GST_BUFFER_LIST_CAST (obj);
2835 if (bclass->render_list)
2836 ret = bclass->render_list (basesink, buflist);
2840 gst_base_sink_do_render_stats (basesink, FALSE);
2842 if (ret == GST_FLOW_STEP)
2845 if (G_UNLIKELY (basesink->flushing))
2852 /* the step ended, check if we need to activate a new step */
2853 GST_DEBUG_OBJECT (basesink, "step ended");
2854 stop_stepping (basesink, &basesink->segment, &priv->current_step,
2855 priv->current_rstart, priv->current_rstop, basesink->eos);
2859 gst_base_sink_perform_qos (basesink, late);
2861 GST_DEBUG_OBJECT (basesink, "object unref after render %p", obj);
2862 gst_mini_object_unref (GST_MINI_OBJECT_CAST (obj));
2869 GST_DEBUG_OBJECT (basesink, "do_sync returned %s", gst_flow_get_name (ret));
2875 GST_DEBUG_OBJECT (basesink, "buffer late, dropping");
2877 if (g_atomic_int_get (&priv->qos_enabled)) {
2878 GstMessage *qos_msg;
2879 GstClockTime timestamp, duration;
2881 timestamp = GST_BUFFER_TIMESTAMP (GST_BUFFER_CAST (sync_obj));
2882 duration = GST_BUFFER_DURATION (GST_BUFFER_CAST (sync_obj));
2884 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, basesink,
2885 "qos: dropped buffer rt %" GST_TIME_FORMAT ", st %" GST_TIME_FORMAT
2886 ", ts %" GST_TIME_FORMAT ", dur %" GST_TIME_FORMAT,
2887 GST_TIME_ARGS (priv->current_rstart),
2888 GST_TIME_ARGS (priv->current_sstart), GST_TIME_ARGS (timestamp),
2889 GST_TIME_ARGS (duration));
2890 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, basesink,
2891 "qos: rendered %" G_GUINT64_FORMAT ", dropped %" G_GUINT64_FORMAT,
2892 priv->rendered, priv->dropped);
2895 gst_message_new_qos (GST_OBJECT_CAST (basesink), basesink->sync,
2896 priv->current_rstart, priv->current_sstart, timestamp, duration);
2897 gst_message_set_qos_values (qos_msg, priv->current_jitter, priv->avg_rate,
2899 gst_message_set_qos_stats (qos_msg, GST_FORMAT_BUFFERS, priv->rendered,
2901 gst_element_post_message (GST_ELEMENT_CAST (basesink), qos_msg);
2907 GST_DEBUG_OBJECT (basesink, "we are flushing, ignore object");
2908 gst_mini_object_unref (obj);
2909 return GST_FLOW_WRONG_STATE;
2913 /* with STREAM_LOCK, PREROLL_LOCK
2915 * Perform preroll on the given object. For buffers this means
2916 * calling the preroll subclass method.
2917 * If that succeeds, the state will be commited.
2919 * function does not take ownership of obj.
2921 static GstFlowReturn
2922 gst_base_sink_preroll_object (GstBaseSink * basesink, guint8 obj_type,
2923 GstMiniObject * obj)
2927 GST_DEBUG_OBJECT (basesink, "prerolling object %p", obj);
2929 /* if it's a buffer, we need to call the preroll method */
2930 if (G_LIKELY (OBJ_IS_BUFFERFULL (obj_type) && basesink->priv->call_preroll)) {
2931 GstBaseSinkClass *bclass;
2934 if (OBJ_IS_BUFFERLIST (obj_type)) {
2935 buf = gst_buffer_list_get (GST_BUFFER_LIST_CAST (obj), 0);
2936 g_assert (NULL != buf);
2938 buf = GST_BUFFER_CAST (obj);
2941 GST_DEBUG_OBJECT (basesink, "preroll buffer %" GST_TIME_FORMAT,
2942 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)));
2945 * For buffer lists do not set last buffer. Creating buffer
2946 * with meaningful data can be done only with memcpy which will
2947 * significantly affect performance
2949 if (!OBJ_IS_BUFFERLIST (obj_type)) {
2950 gst_base_sink_set_last_buffer (basesink, buf);
2953 bclass = GST_BASE_SINK_GET_CLASS (basesink);
2954 if (bclass->preroll)
2955 if ((ret = bclass->preroll (basesink, buf)) != GST_FLOW_OK)
2956 goto preroll_failed;
2958 basesink->priv->call_preroll = FALSE;
2962 if (G_LIKELY (basesink->playing_async)) {
2963 if (G_UNLIKELY (!gst_base_sink_commit_state (basesink)))
2972 GST_DEBUG_OBJECT (basesink, "preroll failed, abort state");
2973 gst_element_abort_state (GST_ELEMENT_CAST (basesink));
2978 GST_DEBUG_OBJECT (basesink, "stopping while commiting state");
2979 return GST_FLOW_WRONG_STATE;
2984 gst_base_sink_flush_start (GstBaseSink * basesink, GstPad * pad)
2986 /* make sure we are not blocked on the clock also clear any pending
2988 gst_base_sink_set_flushing (basesink, pad, TRUE);
2990 /* we grab the stream lock but that is not needed since setting the
2991 * sink to flushing would make sure no state commit is being done
2993 GST_PAD_STREAM_LOCK (pad);
2994 gst_base_sink_reset_qos (basesink);
2995 /* and we need to commit our state again on the next
2996 * prerolled buffer */
2997 basesink->playing_async = TRUE;
2998 if (basesink->priv->async_enabled) {
2999 gst_element_lost_state (GST_ELEMENT_CAST (basesink));
3001 /* start time reset in above case as well;
3002 * arranges for a.o. proper position reporting when flushing in PAUSED */
3003 gst_element_set_start_time (GST_ELEMENT_CAST (basesink), 0);
3004 basesink->priv->have_latency = TRUE;
3006 gst_base_sink_set_last_buffer (basesink, NULL);
3007 GST_PAD_STREAM_UNLOCK (pad);
3011 gst_base_sink_flush_stop (GstBaseSink * basesink, GstPad * pad,
3012 gboolean reset_time)
3014 /* unset flushing so we can accept new data, this also flushes out any EOS
3016 gst_base_sink_set_flushing (basesink, pad, FALSE);
3018 /* for position reporting */
3019 GST_OBJECT_LOCK (basesink);
3020 basesink->priv->current_sstart = GST_CLOCK_TIME_NONE;
3021 basesink->priv->current_sstop = GST_CLOCK_TIME_NONE;
3022 basesink->priv->eos_rtime = GST_CLOCK_TIME_NONE;
3023 basesink->priv->call_preroll = TRUE;
3024 basesink->priv->current_step.valid = FALSE;
3025 basesink->priv->pending_step.valid = FALSE;
3026 if (basesink->pad_mode == GST_PAD_MODE_PUSH) {
3027 /* we need new segment info after the flush. */
3028 basesink->have_newsegment = FALSE;
3030 gst_segment_init (&basesink->segment, GST_FORMAT_UNDEFINED);
3033 basesink->priv->reset_time = reset_time;
3034 GST_OBJECT_UNLOCK (basesink);
3037 static GstFlowReturn
3038 gst_base_sink_default_wait_eos (GstBaseSink * basesink, GstEvent * event)
3041 gboolean late, step_end;
3043 ret = gst_base_sink_do_sync (basesink, GST_MINI_OBJECT_CAST (event),
3044 &late, &step_end, _PR_IS_EVENT);
3050 gst_base_sink_default_event (GstBaseSink * basesink, GstEvent * event)
3052 gboolean result = TRUE;
3053 GstBaseSinkClass *bclass;
3055 bclass = GST_BASE_SINK_GET_CLASS (basesink);
3057 switch (GST_EVENT_TYPE (event)) {
3058 case GST_EVENT_FLUSH_START:
3060 GST_DEBUG_OBJECT (basesink, "flush-start %p", event);
3061 gst_base_sink_flush_start (basesink, basesink->sinkpad);
3064 case GST_EVENT_FLUSH_STOP:
3066 gboolean reset_time;
3068 gst_event_parse_flush_stop (event, &reset_time);
3069 GST_DEBUG_OBJECT (basesink, "flush-stop %p, reset_time: %d", event,
3071 gst_base_sink_flush_stop (basesink, basesink->sinkpad, reset_time);
3076 GstMessage *message;
3079 /* we set the received EOS flag here so that we can use it when testing if
3080 * we are prerolled and to refuse more buffers. */
3081 basesink->priv->received_eos = TRUE;
3084 if (G_LIKELY (bclass->wait_eos)) {
3087 ret = bclass->wait_eos (basesink, event);
3088 if (G_UNLIKELY (ret != GST_FLOW_OK)) {
3094 /* the EOS event is completely handled so we mark
3095 * ourselves as being in the EOS state. eos is also
3096 * protected by the object lock so we can read it when
3097 * answering the POSITION query. */
3098 GST_OBJECT_LOCK (basesink);
3099 basesink->eos = TRUE;
3100 GST_OBJECT_UNLOCK (basesink);
3102 /* ok, now we can post the message */
3103 GST_DEBUG_OBJECT (basesink, "Now posting EOS");
3105 seqnum = basesink->priv->seqnum = gst_event_get_seqnum (event);
3106 GST_DEBUG_OBJECT (basesink, "Got seqnum #%" G_GUINT32_FORMAT, seqnum);
3108 message = gst_message_new_eos (GST_OBJECT_CAST (basesink));
3109 gst_message_set_seqnum (message, seqnum);
3110 gst_element_post_message (GST_ELEMENT_CAST (basesink), message);
3113 case GST_EVENT_CAPS:
3117 GST_DEBUG_OBJECT (basesink, "caps %p", event);
3119 gst_event_parse_caps (event, &caps);
3120 if (bclass->set_caps)
3121 result = bclass->set_caps (basesink, caps);
3124 GST_OBJECT_LOCK (basesink);
3125 gst_caps_replace (&basesink->priv->caps, caps);
3126 GST_OBJECT_UNLOCK (basesink);
3130 case GST_EVENT_SEGMENT:
3131 /* configure the segment */
3132 /* The segment is protected with both the STREAM_LOCK and the OBJECT_LOCK.
3133 * We protect with the OBJECT_LOCK so that we can use the values to
3134 * safely answer a POSITION query. */
3135 GST_OBJECT_LOCK (basesink);
3136 /* the newsegment event is needed to bring the buffer timestamps to the
3137 * stream time and to drop samples outside of the playback segment. */
3138 gst_event_copy_segment (event, &basesink->segment);
3139 GST_DEBUG_OBJECT (basesink, "configured SEGMENT %" GST_SEGMENT_FORMAT,
3140 &basesink->segment);
3141 basesink->have_newsegment = TRUE;
3142 GST_OBJECT_UNLOCK (basesink);
3146 GstTagList *taglist;
3148 gst_event_parse_tag (event, &taglist);
3150 gst_element_post_message (GST_ELEMENT_CAST (basesink),
3151 gst_message_new_tag (GST_OBJECT_CAST (basesink),
3152 gst_tag_list_copy (taglist)));
3155 case GST_EVENT_SINK_MESSAGE:
3157 GstMessage *msg = NULL;
3159 gst_event_parse_sink_message (event, &msg);
3161 gst_element_post_message (GST_ELEMENT_CAST (basesink), msg);
3168 gst_event_unref (event);
3174 gst_base_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
3176 GstBaseSink *basesink;
3177 gboolean result = TRUE;
3178 GstBaseSinkClass *bclass;
3180 basesink = GST_BASE_SINK_CAST (parent);
3181 bclass = GST_BASE_SINK_GET_CLASS (basesink);
3183 GST_DEBUG_OBJECT (basesink, "received event %p %" GST_PTR_FORMAT, event,
3186 switch (GST_EVENT_TYPE (event)) {
3187 case GST_EVENT_FLUSH_STOP:
3188 /* special case for this serialized event because we don't want to grab
3189 * the PREROLL lock or check if we were flushing */
3191 result = bclass->event (basesink, event);
3194 if (GST_EVENT_IS_SERIALIZED (event)) {
3195 GST_BASE_SINK_PREROLL_LOCK (basesink);
3196 if (G_UNLIKELY (basesink->flushing))
3199 if (G_UNLIKELY (basesink->priv->received_eos))
3203 result = bclass->event (basesink, event);
3205 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
3208 result = bclass->event (basesink, event);
3218 GST_DEBUG_OBJECT (basesink, "we are flushing");
3219 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
3220 gst_event_unref (event);
3227 GST_DEBUG_OBJECT (basesink, "Event received after EOS, dropping");
3228 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
3229 gst_event_unref (event);
3235 /* default implementation to calculate the start and end
3236 * timestamps on a buffer, subclasses can override
3239 gst_base_sink_get_times (GstBaseSink * basesink, GstBuffer * buffer,
3240 GstClockTime * start, GstClockTime * end)
3242 GstClockTime timestamp, duration;
3244 timestamp = GST_BUFFER_TIMESTAMP (buffer);
3245 if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
3247 /* get duration to calculate end time */
3248 duration = GST_BUFFER_DURATION (buffer);
3249 if (GST_CLOCK_TIME_IS_VALID (duration)) {
3250 *end = timestamp + duration;
3256 /* must be called with PREROLL_LOCK */
3258 gst_base_sink_needs_preroll (GstBaseSink * basesink)
3260 gboolean is_prerolled, res;
3262 /* we have 2 cases where the PREROLL_LOCK is released:
3263 * 1) we are blocking in the PREROLL_LOCK and thus are prerolled.
3264 * 2) we are syncing on the clock
3266 is_prerolled = basesink->have_preroll || basesink->priv->received_eos;
3267 res = !is_prerolled;
3269 GST_DEBUG_OBJECT (basesink, "have_preroll: %d, EOS: %d => needs preroll: %d",
3270 basesink->have_preroll, basesink->priv->received_eos, res);
3275 /* with STREAM_LOCK, PREROLL_LOCK
3277 * Takes a buffer and compare the timestamps with the last segment.
3278 * If the buffer falls outside of the segment boundaries, drop it.
3279 * Else queue the buffer for preroll and rendering.
3281 * This function takes ownership of the buffer.
3283 static GstFlowReturn
3284 gst_base_sink_chain_unlocked (GstBaseSink * basesink, GstPad * pad,
3285 guint8 obj_type, gpointer obj)
3287 GstBaseSinkClass *bclass;
3288 GstFlowReturn result;
3289 GstClockTime start = GST_CLOCK_TIME_NONE, end = GST_CLOCK_TIME_NONE;
3290 GstSegment *segment;
3291 GstBuffer *time_buf;
3293 if (G_UNLIKELY (basesink->flushing))
3296 if (G_UNLIKELY (basesink->priv->received_eos))
3299 if (OBJ_IS_BUFFERLIST (obj_type)) {
3300 time_buf = gst_buffer_list_get (GST_BUFFER_LIST_CAST (obj), 0);
3301 g_assert (NULL != time_buf);
3303 time_buf = GST_BUFFER_CAST (obj);
3306 /* for code clarity */
3307 segment = &basesink->segment;
3309 if (G_UNLIKELY (!basesink->have_newsegment)) {
3312 sync = gst_base_sink_get_sync (basesink);
3314 GST_ELEMENT_WARNING (basesink, STREAM, FAILED,
3315 (_("Internal data flow problem.")),
3316 ("Received buffer without a new-segment. Assuming timestamps start from 0."));
3319 /* this means this sink will assume timestamps start from 0 */
3320 GST_OBJECT_LOCK (basesink);
3323 basesink->segment.start = 0;
3324 basesink->segment.stop = -1;
3325 basesink->have_newsegment = TRUE;
3326 GST_OBJECT_UNLOCK (basesink);
3329 bclass = GST_BASE_SINK_GET_CLASS (basesink);
3331 /* check if the buffer needs to be dropped, we first ask the subclass for the
3333 if (bclass->get_times)
3334 bclass->get_times (basesink, time_buf, &start, &end);
3336 if (!GST_CLOCK_TIME_IS_VALID (start)) {
3337 /* if the subclass does not want sync, we use our own values so that we at
3338 * least clip the buffer to the segment */
3339 gst_base_sink_get_times (basesink, time_buf, &start, &end);
3342 GST_DEBUG_OBJECT (basesink, "got times start: %" GST_TIME_FORMAT
3343 ", end: %" GST_TIME_FORMAT, GST_TIME_ARGS (start), GST_TIME_ARGS (end));
3345 /* a dropped buffer does not participate in anything */
3346 if (GST_CLOCK_TIME_IS_VALID (start) && (segment->format == GST_FORMAT_TIME)) {
3347 if (G_UNLIKELY (!gst_segment_clip (segment,
3348 GST_FORMAT_TIME, start, end, NULL, NULL)))
3349 goto out_of_segment;
3352 /* now we can process the buffer in the queue, this function takes ownership
3354 result = gst_base_sink_render_object (basesink, obj_type, obj);
3361 GST_DEBUG_OBJECT (basesink, "sink is flushing");
3362 gst_mini_object_unref (GST_MINI_OBJECT_CAST (obj));
3363 return GST_FLOW_WRONG_STATE;
3367 GST_DEBUG_OBJECT (basesink, "we are EOS, dropping object, return EOS");
3368 gst_mini_object_unref (GST_MINI_OBJECT_CAST (obj));
3369 return GST_FLOW_EOS;
3373 GST_DEBUG_OBJECT (basesink, "dropping buffer, out of clipping segment");
3374 gst_mini_object_unref (GST_MINI_OBJECT_CAST (obj));
3381 static GstFlowReturn
3382 gst_base_sink_chain_main (GstBaseSink * basesink, GstPad * pad,
3383 guint8 obj_type, gpointer obj)
3385 GstFlowReturn result;
3387 if (G_UNLIKELY (basesink->pad_mode != GST_PAD_MODE_PUSH))
3390 GST_BASE_SINK_PREROLL_LOCK (basesink);
3391 result = gst_base_sink_chain_unlocked (basesink, pad, obj_type, obj);
3392 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
3400 GST_OBJECT_LOCK (pad);
3401 GST_WARNING_OBJECT (basesink,
3402 "Push on pad %s:%s, but it was not activated in push mode",
3403 GST_DEBUG_PAD_NAME (pad));
3404 GST_OBJECT_UNLOCK (pad);
3405 gst_mini_object_unref (GST_MINI_OBJECT_CAST (obj));
3406 /* we don't post an error message this will signal to the peer
3407 * pushing that EOS is reached. */
3408 result = GST_FLOW_EOS;
3413 static GstFlowReturn
3414 gst_base_sink_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
3416 GstBaseSink *basesink;
3418 basesink = GST_BASE_SINK (parent);
3420 return gst_base_sink_chain_main (basesink, pad, _PR_IS_BUFFER, buf);
3423 static GstFlowReturn
3424 gst_base_sink_chain_list (GstPad * pad, GstObject * parent,
3425 GstBufferList * list)
3427 GstBaseSink *basesink;
3428 GstBaseSinkClass *bclass;
3429 GstFlowReturn result;
3431 basesink = GST_BASE_SINK (parent);
3432 bclass = GST_BASE_SINK_GET_CLASS (basesink);
3434 if (G_LIKELY (bclass->render_list)) {
3435 result = gst_base_sink_chain_main (basesink, pad, _PR_IS_BUFFERLIST, list);
3440 GST_INFO_OBJECT (pad, "chaining each group in list as a merged buffer");
3442 len = gst_buffer_list_length (list);
3444 result = GST_FLOW_OK;
3445 for (i = 0; i < len; i++) {
3446 buffer = gst_buffer_list_get (list, 0);
3447 result = gst_base_sink_chain_main (basesink, pad, _PR_IS_BUFFER,
3448 gst_buffer_ref (buffer));
3449 if (result != GST_FLOW_OK)
3452 gst_buffer_list_unref (list);
3459 gst_base_sink_default_do_seek (GstBaseSink * sink, GstSegment * segment)
3461 gboolean res = TRUE;
3463 /* update our offset if the start/stop position was updated */
3464 if (segment->format == GST_FORMAT_BYTES) {
3465 segment->time = segment->start;
3466 } else if (segment->start == 0) {
3467 /* seek to start, we can implement a default for this. */
3471 GST_INFO_OBJECT (sink, "Can't do a default seek");
3477 #define SEEK_TYPE_IS_RELATIVE(t) (((t) != GST_SEEK_TYPE_NONE) && ((t) != GST_SEEK_TYPE_SET))
3480 gst_base_sink_default_prepare_seek_segment (GstBaseSink * sink,
3481 GstEvent * event, GstSegment * segment)
3483 /* By default, we try one of 2 things:
3484 * - For absolute seek positions, convert the requested position to our
3485 * configured processing format and place it in the output segment \
3486 * - For relative seek positions, convert our current (input) values to the
3487 * seek format, adjust by the relative seek offset and then convert back to
3488 * the processing format
3490 GstSeekType cur_type, stop_type;
3493 GstFormat seek_format;
3496 gboolean res = TRUE;
3498 gst_event_parse_seek (event, &rate, &seek_format, &flags,
3499 &cur_type, &cur, &stop_type, &stop);
3501 if (seek_format == segment->format) {
3502 gst_segment_do_seek (segment, rate, seek_format, flags,
3503 cur_type, cur, stop_type, stop, &update);
3507 if (cur_type != GST_SEEK_TYPE_NONE) {
3508 /* FIXME: Handle seek_cur & seek_end by converting the input segment vals */
3510 gst_pad_query_convert (sink->sinkpad, seek_format, cur, segment->format,
3512 cur_type = GST_SEEK_TYPE_SET;
3515 if (res && stop_type != GST_SEEK_TYPE_NONE) {
3516 /* FIXME: Handle seek_cur & seek_end by converting the input segment vals */
3518 gst_pad_query_convert (sink->sinkpad, seek_format, stop,
3519 segment->format, &stop);
3520 stop_type = GST_SEEK_TYPE_SET;
3523 /* And finally, configure our output segment in the desired format */
3524 gst_segment_do_seek (segment, rate, segment->format, flags, cur_type, cur,
3525 stop_type, stop, &update);
3534 GST_DEBUG_OBJECT (sink, "undefined format given, seek aborted.");
3539 /* perform a seek, only executed in pull mode */
3541 gst_base_sink_perform_seek (GstBaseSink * sink, GstPad * pad, GstEvent * event)
3545 GstFormat seek_format, dest_format;
3547 GstSeekType cur_type, stop_type;
3548 gboolean seekseg_configured = FALSE;
3550 gboolean update, res = TRUE;
3551 GstSegment seeksegment;
3553 dest_format = sink->segment.format;
3556 GST_DEBUG_OBJECT (sink, "performing seek with event %p", event);
3557 gst_event_parse_seek (event, &rate, &seek_format, &flags,
3558 &cur_type, &cur, &stop_type, &stop);
3560 flush = flags & GST_SEEK_FLAG_FLUSH;
3562 GST_DEBUG_OBJECT (sink, "performing seek without event");
3567 GST_DEBUG_OBJECT (sink, "flushing upstream");
3568 gst_pad_push_event (pad, gst_event_new_flush_start ());
3569 gst_base_sink_flush_start (sink, pad);
3571 GST_DEBUG_OBJECT (sink, "pausing pulling thread");
3574 GST_PAD_STREAM_LOCK (pad);
3576 /* If we configured the seeksegment above, don't overwrite it now. Otherwise
3577 * copy the current segment info into the temp segment that we can actually
3578 * attempt the seek with. We only update the real segment if the seek succeeds. */
3579 if (!seekseg_configured) {
3580 memcpy (&seeksegment, &sink->segment, sizeof (GstSegment));
3582 /* now configure the final seek segment */
3584 if (sink->segment.format != seek_format) {
3585 /* OK, here's where we give the subclass a chance to convert the relative
3586 * seek into an absolute one in the processing format. We set up any
3587 * absolute seek above, before taking the stream lock. */
3588 if (!gst_base_sink_default_prepare_seek_segment (sink, event,
3590 GST_DEBUG_OBJECT (sink,
3591 "Preparing the seek failed after flushing. " "Aborting seek");
3595 /* The seek format matches our processing format, no need to ask the
3596 * the subclass to configure the segment. */
3597 gst_segment_do_seek (&seeksegment, rate, seek_format, flags,
3598 cur_type, cur, stop_type, stop, &update);
3601 /* Else, no seek event passed, so we're just (re)starting the
3606 GST_DEBUG_OBJECT (sink, "segment configured from %" G_GINT64_FORMAT
3607 " to %" G_GINT64_FORMAT ", position %" G_GINT64_FORMAT,
3608 seeksegment.start, seeksegment.stop, seeksegment.position);
3610 /* do the seek, segment.position contains the new position. */
3611 res = gst_base_sink_default_do_seek (sink, &seeksegment);
3616 GST_DEBUG_OBJECT (sink, "stop flushing upstream");
3617 gst_pad_push_event (pad, gst_event_new_flush_stop (TRUE));
3618 gst_base_sink_flush_stop (sink, pad, TRUE);
3619 } else if (res && sink->running) {
3620 /* we are running the current segment and doing a non-flushing seek,
3621 * close the segment first based on the position. */
3622 GST_DEBUG_OBJECT (sink, "closing running segment %" G_GINT64_FORMAT
3623 " to %" G_GINT64_FORMAT, sink->segment.start, sink->segment.position);
3626 /* The subclass must have converted the segment to the processing format
3628 if (res && seeksegment.format != dest_format) {
3629 GST_DEBUG_OBJECT (sink, "Subclass failed to prepare a seek segment "
3630 "in the correct format. Aborting seek.");
3634 /* if successful seek, we update our real segment and push
3635 * out the new segment. */
3637 gst_segment_copy_into (&seeksegment, &sink->segment);
3639 if (sink->segment.flags & GST_SEEK_FLAG_SEGMENT) {
3640 gst_element_post_message (GST_ELEMENT (sink),
3641 gst_message_new_segment_start (GST_OBJECT (sink),
3642 sink->segment.format, sink->segment.position));
3646 sink->priv->discont = TRUE;
3647 sink->running = TRUE;
3649 GST_PAD_STREAM_UNLOCK (pad);
3655 set_step_info (GstBaseSink * sink, GstStepInfo * current, GstStepInfo * pending,
3656 guint seqnum, GstFormat format, guint64 amount, gdouble rate,
3657 gboolean flush, gboolean intermediate)
3659 GST_OBJECT_LOCK (sink);
3660 pending->seqnum = seqnum;
3661 pending->format = format;
3662 pending->amount = amount;
3663 pending->position = 0;
3664 pending->rate = rate;
3665 pending->flush = flush;
3666 pending->intermediate = intermediate;
3667 pending->valid = TRUE;
3668 /* flush invalidates the current stepping segment */
3670 current->valid = FALSE;
3671 GST_OBJECT_UNLOCK (sink);
3675 gst_base_sink_perform_step (GstBaseSink * sink, GstPad * pad, GstEvent * event)
3677 GstBaseSinkPrivate *priv;
3678 GstBaseSinkClass *bclass;
3679 gboolean flush, intermediate;
3684 GstStepInfo *pending, *current;
3685 GstMessage *message;
3687 bclass = GST_BASE_SINK_GET_CLASS (sink);
3690 GST_DEBUG_OBJECT (sink, "performing step with event %p", event);
3692 gst_event_parse_step (event, &format, &amount, &rate, &flush, &intermediate);
3693 seqnum = gst_event_get_seqnum (event);
3695 pending = &priv->pending_step;
3696 current = &priv->current_step;
3698 /* post message first */
3699 message = gst_message_new_step_start (GST_OBJECT (sink), FALSE, format,
3700 amount, rate, flush, intermediate);
3701 gst_message_set_seqnum (message, seqnum);
3702 gst_element_post_message (GST_ELEMENT (sink), message);
3705 /* we need to call ::unlock before locking PREROLL_LOCK
3706 * since we lock it before going into ::render */
3708 bclass->unlock (sink);
3710 GST_BASE_SINK_PREROLL_LOCK (sink);
3711 /* now that we have the PREROLL lock, clear our unlock request */
3712 if (bclass->unlock_stop)
3713 bclass->unlock_stop (sink);
3715 /* update the stepinfo and make it valid */
3716 set_step_info (sink, current, pending, seqnum, format, amount, rate, flush,
3719 if (sink->priv->async_enabled) {
3720 /* and we need to commit our state again on the next
3721 * prerolled buffer */
3722 sink->playing_async = TRUE;
3723 priv->pending_step.need_preroll = TRUE;
3724 sink->need_preroll = FALSE;
3725 gst_element_lost_state (GST_ELEMENT_CAST (sink));
3727 sink->priv->have_latency = TRUE;
3728 sink->need_preroll = FALSE;
3730 priv->current_sstart = GST_CLOCK_TIME_NONE;
3731 priv->current_sstop = GST_CLOCK_TIME_NONE;
3732 priv->eos_rtime = GST_CLOCK_TIME_NONE;
3733 priv->call_preroll = TRUE;
3734 gst_base_sink_set_last_buffer (sink, NULL);
3735 gst_base_sink_reset_qos (sink);
3737 if (sink->clock_id) {
3738 gst_clock_id_unschedule (sink->clock_id);
3741 if (sink->have_preroll) {
3742 GST_DEBUG_OBJECT (sink, "signal waiter");
3743 priv->step_unlock = TRUE;
3744 GST_BASE_SINK_PREROLL_SIGNAL (sink);
3746 GST_BASE_SINK_PREROLL_UNLOCK (sink);
3748 /* update the stepinfo and make it valid */
3749 set_step_info (sink, current, pending, seqnum, format, amount, rate, flush,
3759 gst_base_sink_loop (GstPad * pad)
3762 GstBaseSink *basesink;
3763 GstBuffer *buf = NULL;
3764 GstFlowReturn result;
3768 parent = GST_OBJECT_PARENT (pad);
3769 basesink = GST_BASE_SINK (parent);
3771 g_assert (basesink->pad_mode == GST_PAD_MODE_PULL);
3773 if ((blocksize = basesink->priv->blocksize) == 0)
3776 offset = basesink->segment.position;
3778 GST_DEBUG_OBJECT (basesink, "pulling %" G_GUINT64_FORMAT ", %u",
3781 result = gst_pad_pull_range (pad, offset, blocksize, &buf);
3782 if (G_UNLIKELY (result != GST_FLOW_OK))
3785 if (G_UNLIKELY (buf == NULL))
3788 offset += gst_buffer_get_size (buf);
3790 basesink->segment.position = offset;
3792 GST_BASE_SINK_PREROLL_LOCK (basesink);
3793 result = gst_base_sink_chain_unlocked (basesink, pad, _PR_IS_BUFFER, buf);
3794 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
3795 if (G_UNLIKELY (result != GST_FLOW_OK))
3803 GST_LOG_OBJECT (basesink, "pausing task, reason %s",
3804 gst_flow_get_name (result));
3805 gst_pad_pause_task (pad);
3806 if (result == GST_FLOW_EOS) {
3807 /* perform EOS logic */
3808 if (basesink->segment.flags & GST_SEEK_FLAG_SEGMENT) {
3809 gst_element_post_message (GST_ELEMENT_CAST (basesink),
3810 gst_message_new_segment_done (GST_OBJECT_CAST (basesink),
3811 basesink->segment.format, basesink->segment.position));
3813 gst_base_sink_event (pad, parent, gst_event_new_eos ());
3815 } else if (result == GST_FLOW_NOT_LINKED || result <= GST_FLOW_EOS) {
3816 /* for fatal errors we post an error message, post the error
3817 * first so the app knows about the error first.
3818 * wrong-state is not a fatal error because it happens due to
3819 * flushing and posting an error message in that case is the
3820 * wrong thing to do, e.g. when basesrc is doing a flushing
3822 GST_ELEMENT_ERROR (basesink, STREAM, FAILED,
3823 (_("Internal data stream error.")),
3824 ("stream stopped, reason %s", gst_flow_get_name (result)));
3825 gst_base_sink_event (pad, parent, gst_event_new_eos ());
3831 GST_LOG_OBJECT (basesink, "no buffer, pausing");
3832 GST_ELEMENT_ERROR (basesink, STREAM, FAILED,
3833 (_("Internal data flow error.")), ("element returned NULL buffer"));
3834 result = GST_FLOW_ERROR;
3840 gst_base_sink_set_flushing (GstBaseSink * basesink, GstPad * pad,
3843 GstBaseSinkClass *bclass;
3845 bclass = GST_BASE_SINK_GET_CLASS (basesink);
3848 /* unlock any subclasses, we need to do this before grabbing the
3849 * PREROLL_LOCK since we hold this lock before going into ::render. */
3851 bclass->unlock (basesink);
3854 GST_BASE_SINK_PREROLL_LOCK (basesink);
3855 basesink->flushing = flushing;
3857 /* step 1, now that we have the PREROLL lock, clear our unlock request */
3858 if (bclass->unlock_stop)
3859 bclass->unlock_stop (basesink);
3861 /* set need_preroll before we unblock the clock. If the clock is unblocked
3862 * before timing out, we can reuse the buffer for preroll. */
3863 basesink->need_preroll = TRUE;
3865 /* step 2, unblock clock sync (if any) or any other blocking thing */
3866 if (basesink->clock_id) {
3867 gst_clock_id_unschedule (basesink->clock_id);
3870 /* flush out the data thread if it's locked in finish_preroll, this will
3871 * also flush out the EOS state */
3872 GST_DEBUG_OBJECT (basesink,
3873 "flushing out data thread, need preroll to TRUE");
3875 /* we can't have EOS anymore now */
3876 basesink->eos = FALSE;
3877 basesink->priv->received_eos = FALSE;
3878 basesink->have_preroll = FALSE;
3879 basesink->priv->step_unlock = FALSE;
3880 /* can't report latency anymore until we preroll again */
3881 if (basesink->priv->async_enabled) {
3882 GST_OBJECT_LOCK (basesink);
3883 basesink->priv->have_latency = FALSE;
3884 GST_OBJECT_UNLOCK (basesink);
3886 /* and signal any waiters now */
3887 GST_BASE_SINK_PREROLL_SIGNAL (basesink);
3889 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
3895 gst_base_sink_default_activate_pull (GstBaseSink * basesink, gboolean active)
3901 result = gst_pad_start_task (basesink->sinkpad,
3902 (GstTaskFunction) gst_base_sink_loop, basesink->sinkpad);
3904 /* step 2, make sure streaming finishes */
3905 result = gst_pad_stop_task (basesink->sinkpad);
3912 gst_base_sink_pad_activate (GstPad * pad, GstObject * parent)
3914 gboolean result = FALSE;
3915 GstBaseSink *basesink;
3919 basesink = GST_BASE_SINK (parent);
3921 GST_DEBUG_OBJECT (basesink, "Trying pull mode first");
3923 gst_base_sink_set_flushing (basesink, pad, FALSE);
3925 /* we need to have the pull mode enabled */
3926 if (!basesink->can_activate_pull) {
3927 GST_DEBUG_OBJECT (basesink, "pull mode disabled");
3931 /* check if downstreams supports pull mode at all */
3932 query = gst_query_new_scheduling ();
3934 if (!gst_pad_peer_query (pad, query)) {
3935 gst_query_unref (query);
3936 GST_DEBUG_OBJECT (basesink, "peer query faild, no pull mode");
3940 /* parse result of the query */
3941 pull_mode = gst_query_has_scheduling_mode (query, GST_PAD_MODE_PULL);
3942 gst_query_unref (query);
3945 GST_DEBUG_OBJECT (basesink, "pull mode not supported");
3949 /* set the pad mode before starting the task so that it's in the
3950 * correct state for the new thread. also the sink set_caps and get_caps
3951 * function checks this */
3952 basesink->pad_mode = GST_PAD_MODE_PULL;
3954 /* we first try to negotiate a format so that when we try to activate
3955 * downstream, it knows about our format */
3956 if (!gst_base_sink_negotiate_pull (basesink)) {
3957 GST_DEBUG_OBJECT (basesink, "failed to negotiate in pull mode");
3961 /* ok activate now */
3962 if (!gst_pad_activate_mode (pad, GST_PAD_MODE_PULL, TRUE)) {
3963 /* clear any pending caps */
3964 GST_OBJECT_LOCK (basesink);
3965 gst_caps_replace (&basesink->priv->caps, NULL);
3966 GST_OBJECT_UNLOCK (basesink);
3967 GST_DEBUG_OBJECT (basesink, "failed to activate in pull mode");
3971 GST_DEBUG_OBJECT (basesink, "Success activating pull mode");
3975 /* push mode fallback */
3977 GST_DEBUG_OBJECT (basesink, "Falling back to push mode");
3978 if ((result = gst_pad_activate_mode (pad, GST_PAD_MODE_PUSH, TRUE))) {
3979 GST_DEBUG_OBJECT (basesink, "Success activating push mode");
3984 GST_WARNING_OBJECT (basesink, "Could not activate pad in either mode");
3985 gst_base_sink_set_flushing (basesink, pad, TRUE);
3992 gst_base_sink_pad_activate_push (GstPad * pad, GstObject * parent,
3996 GstBaseSink *basesink;
3998 basesink = GST_BASE_SINK (parent);
4001 if (!basesink->can_activate_push) {
4003 basesink->pad_mode = GST_PAD_MODE_NONE;
4006 basesink->pad_mode = GST_PAD_MODE_PUSH;
4009 if (G_UNLIKELY (basesink->pad_mode != GST_PAD_MODE_PUSH)) {
4010 g_warning ("Internal GStreamer activation error!!!");
4013 gst_base_sink_set_flushing (basesink, pad, TRUE);
4015 basesink->pad_mode = GST_PAD_MODE_NONE;
4023 gst_base_sink_negotiate_pull (GstBaseSink * basesink)
4030 /* this returns the intersection between our caps and the peer caps. If there
4031 * is no peer, it returns NULL and we can't operate in pull mode so we can
4032 * fail the negotiation. */
4033 caps = gst_pad_get_allowed_caps (GST_BASE_SINK_PAD (basesink));
4034 if (caps == NULL || gst_caps_is_empty (caps))
4035 goto no_caps_possible;
4037 GST_DEBUG_OBJECT (basesink, "allowed caps: %" GST_PTR_FORMAT, caps);
4039 if (gst_caps_is_any (caps)) {
4040 GST_DEBUG_OBJECT (basesink, "caps were ANY after fixating, "
4042 /* neither side has template caps in this case, so they are prepared for
4043 pull() without setcaps() */
4046 caps = gst_caps_make_writable (caps);
4048 gst_base_sink_fixate (basesink, caps);
4049 GST_DEBUG_OBJECT (basesink, "fixated to: %" GST_PTR_FORMAT, caps);
4051 if (gst_caps_is_fixed (caps)) {
4052 if (!gst_pad_send_event (GST_BASE_SINK_PAD (basesink),
4053 gst_event_new_caps (caps)))
4054 goto could_not_set_caps;
4060 gst_caps_unref (caps);
4066 GST_INFO_OBJECT (basesink, "Pipeline could not agree on caps");
4067 GST_DEBUG_OBJECT (basesink, "get_allowed_caps() returned EMPTY");
4069 gst_caps_unref (caps);
4074 GST_INFO_OBJECT (basesink, "Could not set caps: %" GST_PTR_FORMAT, caps);
4075 gst_caps_unref (caps);
4080 /* this won't get called until we implement an activate function */
4082 gst_base_sink_pad_activate_pull (GstPad * pad, GstObject * parent,
4085 gboolean result = FALSE;
4086 GstBaseSink *basesink;
4087 GstBaseSinkClass *bclass;
4089 basesink = GST_BASE_SINK (parent);
4090 bclass = GST_BASE_SINK_GET_CLASS (basesink);
4095 /* we mark we have a newsegment here because pull based
4096 * mode works just fine without having a newsegment before the
4098 gst_segment_init (&basesink->segment, GST_FORMAT_BYTES);
4099 GST_OBJECT_LOCK (basesink);
4100 basesink->have_newsegment = TRUE;
4101 GST_OBJECT_UNLOCK (basesink);
4103 /* get the peer duration in bytes */
4104 result = gst_pad_peer_query_duration (pad, GST_FORMAT_BYTES, &duration);
4106 GST_DEBUG_OBJECT (basesink,
4107 "setting duration in bytes to %" G_GINT64_FORMAT, duration);
4108 basesink->segment.duration = duration;
4110 GST_DEBUG_OBJECT (basesink, "unknown duration");
4113 if (bclass->activate_pull)
4114 result = bclass->activate_pull (basesink, TRUE);
4119 goto activate_failed;
4122 if (G_UNLIKELY (basesink->pad_mode != GST_PAD_MODE_PULL)) {
4123 g_warning ("Internal GStreamer activation error!!!");
4126 result = gst_base_sink_set_flushing (basesink, pad, TRUE);
4127 if (bclass->activate_pull)
4128 result &= bclass->activate_pull (basesink, FALSE);
4129 basesink->pad_mode = GST_PAD_MODE_NONE;
4138 /* reset, as starting the thread failed */
4139 basesink->pad_mode = GST_PAD_MODE_NONE;
4141 GST_ERROR_OBJECT (basesink, "subclass failed to activate in pull mode");
4147 gst_base_sink_pad_activate_mode (GstPad * pad, GstObject * parent,
4148 GstPadMode mode, gboolean active)
4153 case GST_PAD_MODE_PULL:
4154 res = gst_base_sink_pad_activate_pull (pad, parent, active);
4156 case GST_PAD_MODE_PUSH:
4157 res = gst_base_sink_pad_activate_push (pad, parent, active);
4160 GST_LOG_OBJECT (pad, "unknown activation mode %d", mode);
4167 /* send an event to our sinkpad peer. */
4169 gst_base_sink_send_event (GstElement * element, GstEvent * event)
4172 GstBaseSink *basesink = GST_BASE_SINK (element);
4173 gboolean forward, result = TRUE;
4176 GST_OBJECT_LOCK (element);
4177 /* get the pad and the scheduling mode */
4178 pad = gst_object_ref (basesink->sinkpad);
4179 mode = basesink->pad_mode;
4180 GST_OBJECT_UNLOCK (element);
4182 /* only push UPSTREAM events upstream */
4183 forward = GST_EVENT_IS_UPSTREAM (event);
4185 GST_DEBUG_OBJECT (basesink, "handling event %p %" GST_PTR_FORMAT, event,
4188 switch (GST_EVENT_TYPE (event)) {
4189 case GST_EVENT_LATENCY:
4191 GstClockTime latency;
4193 gst_event_parse_latency (event, &latency);
4195 /* store the latency. We use this to adjust the running_time before syncing
4196 * it to the clock. */
4197 GST_OBJECT_LOCK (element);
4198 basesink->priv->latency = latency;
4199 if (!basesink->priv->have_latency)
4201 GST_OBJECT_UNLOCK (element);
4202 GST_DEBUG_OBJECT (basesink, "latency set to %" GST_TIME_FORMAT,
4203 GST_TIME_ARGS (latency));
4205 /* We forward this event so that all elements know about the global pipeline
4206 * latency. This is interesting for an element when it wants to figure out
4207 * when a particular piece of data will be rendered. */
4210 case GST_EVENT_SEEK:
4211 /* in pull mode we will execute the seek */
4212 if (mode == GST_PAD_MODE_PULL)
4213 result = gst_base_sink_perform_seek (basesink, pad, event);
4215 case GST_EVENT_STEP:
4216 result = gst_base_sink_perform_step (basesink, pad, event);
4224 result = gst_pad_push_event (pad, event);
4226 /* not forwarded, unref the event */
4227 gst_event_unref (event);
4230 gst_object_unref (pad);
4232 GST_DEBUG_OBJECT (basesink, "handled event %p %" GST_PTR_FORMAT ": %d", event,
4239 gst_base_sink_get_position (GstBaseSink * basesink, GstFormat format,
4240 gint64 * cur, gboolean * upstream)
4242 GstClock *clock = NULL;
4243 gboolean res = FALSE;
4245 GstSegment *segment;
4246 GstClockTime now, latency;
4247 GstClockTimeDiff base_time;
4248 gint64 time, base, duration;
4251 gboolean last_seen, with_clock, in_paused;
4253 GST_OBJECT_LOCK (basesink);
4254 /* we can only get the segment when we are not NULL or READY */
4255 if (!basesink->have_newsegment)
4259 /* when not in PLAYING or when we're busy with a state change, we
4260 * cannot read from the clock so we report time based on the
4261 * last seen timestamp. */
4262 if (GST_STATE (basesink) != GST_STATE_PLAYING ||
4263 GST_STATE_PENDING (basesink) != GST_STATE_VOID_PENDING) {
4267 segment = &basesink->segment;
4269 /* get the format in the segment */
4270 oformat = segment->format;
4272 /* report with last seen position when EOS */
4273 last_seen = basesink->eos;
4275 /* assume we will use the clock for getting the current position */
4277 if (basesink->sync == FALSE)
4280 /* and we need a clock */
4281 if (G_UNLIKELY ((clock = GST_ELEMENT_CLOCK (basesink)) == NULL))
4284 gst_object_ref (clock);
4286 /* mainloop might be querying position when going to playing async,
4287 * while (audio) rendering might be quickly advancing stream position,
4288 * so use clock asap rather than last reported position */
4289 if (in_paused && with_clock && g_atomic_int_get (&basesink->priv->to_playing)) {
4290 GST_DEBUG_OBJECT (basesink, "going to PLAYING, so not PAUSED");
4294 /* collect all data we need holding the lock */
4295 if (GST_CLOCK_TIME_IS_VALID (segment->time))
4296 time = segment->time;
4300 if (GST_CLOCK_TIME_IS_VALID (segment->stop))
4301 duration = segment->stop - segment->start;
4305 base = segment->base;
4306 rate = segment->rate * segment->applied_rate;
4307 latency = basesink->priv->latency;
4309 if (oformat == GST_FORMAT_TIME) {
4312 start = basesink->priv->current_sstart;
4313 stop = basesink->priv->current_sstop;
4316 /* in paused we use the last position as a lower bound */
4317 if (stop == -1 || segment->rate > 0.0)
4322 /* in playing, use last stop time as upper bound */
4323 if (start == -1 || segment->rate > 0.0)
4329 /* convert last stop to stream time */
4330 last = gst_segment_to_stream_time (segment, oformat, segment->position);
4334 /* in paused, use start_time */
4335 base_time = GST_ELEMENT_START_TIME (basesink);
4336 GST_DEBUG_OBJECT (basesink, "in paused, using start time %" GST_TIME_FORMAT,
4337 GST_TIME_ARGS (base_time));
4338 } else if (with_clock) {
4339 /* else use clock when needed */
4340 base_time = GST_ELEMENT_CAST (basesink)->base_time;
4341 GST_DEBUG_OBJECT (basesink, "using clock and base time %" GST_TIME_FORMAT,
4342 GST_TIME_ARGS (base_time));
4344 /* else, no sync or clock -> no base time */
4345 GST_DEBUG_OBJECT (basesink, "no sync or no clock");
4349 /* no base_time, we can't calculate running_time, use last seem timestamp to report
4351 if (base_time == -1)
4354 /* need to release the object lock before we can get the time,
4355 * a clock might take the LOCK of the provider, which could be
4356 * a basesink subclass. */
4357 GST_OBJECT_UNLOCK (basesink);
4360 /* in EOS or when no valid stream_time, report the value of last seen
4363 /* no timestamp, we need to ask upstream */
4364 GST_DEBUG_OBJECT (basesink, "no last seen timestamp, asking upstream");
4369 GST_DEBUG_OBJECT (basesink, "using last seen timestamp %" GST_TIME_FORMAT,
4370 GST_TIME_ARGS (last));
4373 if (oformat != GST_FORMAT_TIME) {
4374 /* convert base, time and duration to time */
4375 if (!gst_pad_query_convert (basesink->sinkpad, oformat, base,
4376 GST_FORMAT_TIME, &base))
4377 goto convert_failed;
4378 if (!gst_pad_query_convert (basesink->sinkpad, oformat, duration,
4379 GST_FORMAT_TIME, &duration))
4380 goto convert_failed;
4381 if (!gst_pad_query_convert (basesink->sinkpad, oformat, time,
4382 GST_FORMAT_TIME, &time))
4383 goto convert_failed;
4384 if (!gst_pad_query_convert (basesink->sinkpad, oformat, last,
4385 GST_FORMAT_TIME, &last))
4386 goto convert_failed;
4388 /* assume time format from now on */
4389 oformat = GST_FORMAT_TIME;
4392 if (!in_paused && with_clock) {
4393 now = gst_clock_get_time (clock);
4399 /* subtract base time and base time from the clock time.
4400 * Make sure we don't go negative. This is the current time in
4401 * the segment which we need to scale with the combined
4402 * rate and applied rate. */
4404 base_time += latency;
4405 if (GST_CLOCK_DIFF (base_time, now) < 0)
4408 /* for negative rates we need to count back from the segment
4413 *cur = time + gst_guint64_to_gdouble (now - base_time) * rate;
4416 /* never report less than segment values in paused */
4418 *cur = MAX (last, *cur);
4420 /* never report more than last seen position in playing */
4422 *cur = MIN (last, *cur);
4425 GST_DEBUG_OBJECT (basesink,
4426 "now %" GST_TIME_FORMAT " - base_time %" GST_TIME_FORMAT " - base %"
4427 GST_TIME_FORMAT " + time %" GST_TIME_FORMAT " last %" GST_TIME_FORMAT,
4428 GST_TIME_ARGS (now), GST_TIME_ARGS (base_time), GST_TIME_ARGS (base),
4429 GST_TIME_ARGS (time), GST_TIME_ARGS (last));
4432 if (oformat != format) {
4433 /* convert to final format */
4434 if (!gst_pad_query_convert (basesink->sinkpad, oformat, *cur, format, cur))
4435 goto convert_failed;
4441 GST_DEBUG_OBJECT (basesink, "res: %d, POSITION: %" GST_TIME_FORMAT,
4442 res, GST_TIME_ARGS (*cur));
4445 gst_object_unref (clock);
4452 /* in NULL or READY we always return FALSE and -1 */
4453 GST_DEBUG_OBJECT (basesink, "position in wrong state, return -1");
4456 GST_OBJECT_UNLOCK (basesink);
4461 GST_DEBUG_OBJECT (basesink, "convert failed, try upstream");
4469 gst_base_sink_get_duration (GstBaseSink * basesink, GstFormat format,
4470 gint64 * dur, gboolean * upstream)
4472 gboolean res = FALSE;
4474 if (basesink->pad_mode == GST_PAD_MODE_PULL) {
4477 /* get the duration in bytes, in pull mode that's all we are sure to
4478 * know. We have to explicitly get this value from upstream instead of
4479 * using our cached value because it might change. Duration caching
4480 * should be done at a higher level. */
4482 gst_pad_peer_query_duration (basesink->sinkpad, GST_FORMAT_BYTES,
4485 basesink->segment.duration = uduration;
4486 if (format != GST_FORMAT_BYTES) {
4487 /* convert to the requested format */
4489 gst_pad_query_convert (basesink->sinkpad, GST_FORMAT_BYTES,
4490 uduration, format, dur);
4504 default_element_query (GstElement * element, GstQuery * query)
4506 gboolean res = FALSE;
4508 GstBaseSink *basesink = GST_BASE_SINK (element);
4510 switch (GST_QUERY_TYPE (query)) {
4511 case GST_QUERY_POSITION:
4515 gboolean upstream = FALSE;
4517 gst_query_parse_position (query, &format, NULL);
4519 GST_DEBUG_OBJECT (basesink, "position query in format %s",
4520 gst_format_get_name (format));
4522 /* first try to get the position based on the clock */
4524 gst_base_sink_get_position (basesink, format, &cur, &upstream))) {
4525 gst_query_set_position (query, format, cur);
4526 } else if (upstream) {
4527 /* fallback to peer query */
4528 res = gst_pad_peer_query (basesink->sinkpad, query);
4531 /* we can handle a few things if upstream failed */
4532 if (format == GST_FORMAT_PERCENT) {
4535 res = gst_base_sink_get_position (basesink, GST_FORMAT_TIME, &cur,
4537 if (!res && upstream) {
4539 gst_pad_peer_query_position (basesink->sinkpad, GST_FORMAT_TIME,
4543 res = gst_base_sink_get_duration (basesink, GST_FORMAT_TIME, &dur,
4545 if (!res && upstream) {
4547 gst_pad_peer_query_duration (basesink->sinkpad,
4548 GST_FORMAT_TIME, &dur);
4554 pos = gst_util_uint64_scale (100 * GST_FORMAT_PERCENT_SCALE, cur,
4556 gst_query_set_position (query, GST_FORMAT_PERCENT, pos);
4562 case GST_QUERY_DURATION:
4566 gboolean upstream = FALSE;
4568 gst_query_parse_duration (query, &format, NULL);
4570 GST_DEBUG_OBJECT (basesink, "duration query in format %s",
4571 gst_format_get_name (format));
4574 gst_base_sink_get_duration (basesink, format, &dur, &upstream))) {
4575 gst_query_set_duration (query, format, dur);
4576 } else if (upstream) {
4577 /* fallback to peer query */
4578 res = gst_pad_peer_query (basesink->sinkpad, query);
4581 /* we can handle a few things if upstream failed */
4582 if (format == GST_FORMAT_PERCENT) {
4583 gst_query_set_duration (query, GST_FORMAT_PERCENT,
4584 GST_FORMAT_PERCENT_MAX);
4590 case GST_QUERY_LATENCY:
4592 gboolean live, us_live;
4593 GstClockTime min, max;
4595 if ((res = gst_base_sink_query_latency (basesink, &live, &us_live, &min,
4597 gst_query_set_latency (query, live, min, max);
4601 case GST_QUERY_JITTER:
4603 case GST_QUERY_RATE:
4604 /* gst_query_set_rate (query, basesink->segment_rate); */
4607 case GST_QUERY_SEGMENT:
4609 if (basesink->pad_mode == GST_PAD_MODE_PULL) {
4610 gst_query_set_segment (query, basesink->segment.rate,
4611 GST_FORMAT_TIME, basesink->segment.start, basesink->segment.stop);
4614 res = gst_pad_peer_query (basesink->sinkpad, query);
4618 case GST_QUERY_SEEKING:
4619 case GST_QUERY_CONVERT:
4620 case GST_QUERY_FORMATS:
4622 res = gst_pad_peer_query (basesink->sinkpad, query);
4625 GST_DEBUG_OBJECT (basesink, "query %s returns %d",
4626 GST_QUERY_TYPE_NAME (query), res);
4632 default_sink_query (GstBaseSink * basesink, GstQuery * query)
4635 GstBaseSinkClass *bclass;
4637 bclass = GST_BASE_SINK_GET_CLASS (basesink);
4639 switch (GST_QUERY_TYPE (query)) {
4640 case GST_QUERY_ALLOCATION:
4642 if (bclass->propose_allocation)
4643 res = bclass->propose_allocation (basesink, query);
4648 case GST_QUERY_CAPS:
4650 GstCaps *caps, *filter;
4652 gst_query_parse_caps (query, &filter);
4653 caps = gst_base_sink_query_caps (basesink, basesink->sinkpad, filter);
4654 gst_query_set_caps_result (query, caps);
4655 gst_caps_unref (caps);
4661 gst_pad_query_default (basesink->sinkpad, GST_OBJECT_CAST (basesink),
4669 gst_base_sink_sink_query (GstPad * pad, GstObject * parent, GstQuery * query)
4671 GstBaseSink *basesink;
4672 GstBaseSinkClass *bclass;
4675 basesink = GST_BASE_SINK_CAST (parent);
4676 bclass = GST_BASE_SINK_GET_CLASS (basesink);
4679 res = bclass->query (basesink, query);
4686 static GstStateChangeReturn
4687 gst_base_sink_change_state (GstElement * element, GstStateChange transition)
4689 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
4690 GstBaseSink *basesink = GST_BASE_SINK (element);
4691 GstBaseSinkClass *bclass;
4692 GstBaseSinkPrivate *priv;
4694 priv = basesink->priv;
4696 bclass = GST_BASE_SINK_GET_CLASS (basesink);
4698 switch (transition) {
4699 case GST_STATE_CHANGE_NULL_TO_READY:
4701 if (!bclass->start (basesink))
4704 case GST_STATE_CHANGE_READY_TO_PAUSED:
4705 /* need to complete preroll before this state change completes, there
4706 * is no data flow in READY so we can safely assume we need to preroll. */
4707 GST_BASE_SINK_PREROLL_LOCK (basesink);
4708 GST_DEBUG_OBJECT (basesink, "READY to PAUSED");
4709 basesink->have_newsegment = FALSE;
4710 gst_segment_init (&basesink->segment, GST_FORMAT_UNDEFINED);
4711 basesink->offset = 0;
4712 basesink->have_preroll = FALSE;
4713 priv->step_unlock = FALSE;
4714 basesink->need_preroll = TRUE;
4715 basesink->playing_async = TRUE;
4716 basesink->priv->reset_time = FALSE;
4717 priv->current_sstart = GST_CLOCK_TIME_NONE;
4718 priv->current_sstop = GST_CLOCK_TIME_NONE;
4719 priv->eos_rtime = GST_CLOCK_TIME_NONE;
4721 basesink->eos = FALSE;
4722 priv->received_eos = FALSE;
4723 gst_base_sink_reset_qos (basesink);
4724 priv->commited = FALSE;
4725 priv->call_preroll = TRUE;
4726 priv->current_step.valid = FALSE;
4727 priv->pending_step.valid = FALSE;
4728 if (priv->async_enabled) {
4729 GST_DEBUG_OBJECT (basesink, "doing async state change");
4730 /* when async enabled, post async-start message and return ASYNC from
4731 * the state change function */
4732 ret = GST_STATE_CHANGE_ASYNC;
4733 gst_element_post_message (GST_ELEMENT_CAST (basesink),
4734 gst_message_new_async_start (GST_OBJECT_CAST (basesink)));
4736 priv->have_latency = TRUE;
4738 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
4740 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
4741 GST_BASE_SINK_PREROLL_LOCK (basesink);
4742 g_atomic_int_set (&basesink->priv->to_playing, TRUE);
4743 if (!gst_base_sink_needs_preroll (basesink)) {
4744 GST_DEBUG_OBJECT (basesink, "PAUSED to PLAYING, don't need preroll");
4745 /* no preroll needed anymore now. */
4746 basesink->playing_async = FALSE;
4747 basesink->need_preroll = FALSE;
4748 if (basesink->eos) {
4749 GstMessage *message;
4751 /* need to post EOS message here */
4752 GST_DEBUG_OBJECT (basesink, "Now posting EOS");
4753 message = gst_message_new_eos (GST_OBJECT_CAST (basesink));
4754 gst_message_set_seqnum (message, basesink->priv->seqnum);
4755 gst_element_post_message (GST_ELEMENT_CAST (basesink), message);
4757 GST_DEBUG_OBJECT (basesink, "signal preroll");
4758 GST_BASE_SINK_PREROLL_SIGNAL (basesink);
4761 GST_DEBUG_OBJECT (basesink, "PAUSED to PLAYING, we are not prerolled");
4762 basesink->need_preroll = TRUE;
4763 basesink->playing_async = TRUE;
4764 priv->call_preroll = TRUE;
4765 priv->commited = FALSE;
4766 if (priv->async_enabled) {
4767 GST_DEBUG_OBJECT (basesink, "doing async state change");
4768 ret = GST_STATE_CHANGE_ASYNC;
4769 gst_element_post_message (GST_ELEMENT_CAST (basesink),
4770 gst_message_new_async_start (GST_OBJECT_CAST (basesink)));
4773 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
4780 GstStateChangeReturn bret;
4782 bret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
4783 if (G_UNLIKELY (bret == GST_STATE_CHANGE_FAILURE))
4784 goto activate_failed;
4787 switch (transition) {
4788 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
4789 /* completed transition, so need not be marked any longer
4790 * And it should be unmarked, since e.g. losing our position upon flush
4791 * does not really change state to PAUSED ... */
4792 g_atomic_int_set (&basesink->priv->to_playing, FALSE);
4794 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
4795 g_atomic_int_set (&basesink->priv->to_playing, FALSE);
4796 GST_DEBUG_OBJECT (basesink, "PLAYING to PAUSED");
4797 /* FIXME, make sure we cannot enter _render first */
4799 /* we need to call ::unlock before locking PREROLL_LOCK
4800 * since we lock it before going into ::render */
4802 bclass->unlock (basesink);
4804 GST_BASE_SINK_PREROLL_LOCK (basesink);
4805 GST_DEBUG_OBJECT (basesink, "got preroll lock");
4806 /* now that we have the PREROLL lock, clear our unlock request */
4807 if (bclass->unlock_stop)
4808 bclass->unlock_stop (basesink);
4810 /* we need preroll again and we set the flag before unlocking the clockid
4811 * because if the clockid is unlocked before a current buffer expired, we
4812 * can use that buffer to preroll with */
4813 basesink->need_preroll = TRUE;
4815 if (basesink->clock_id) {
4816 GST_DEBUG_OBJECT (basesink, "unschedule clock");
4817 gst_clock_id_unschedule (basesink->clock_id);
4820 /* if we don't have a preroll buffer we need to wait for a preroll and
4822 if (!gst_base_sink_needs_preroll (basesink)) {
4823 GST_DEBUG_OBJECT (basesink, "PLAYING to PAUSED, we are prerolled");
4824 basesink->playing_async = FALSE;
4826 if (GST_STATE_TARGET (GST_ELEMENT (basesink)) <= GST_STATE_READY) {
4827 GST_DEBUG_OBJECT (basesink, "element is <= READY");
4828 ret = GST_STATE_CHANGE_SUCCESS;
4830 GST_DEBUG_OBJECT (basesink,
4831 "PLAYING to PAUSED, we are not prerolled");
4832 basesink->playing_async = TRUE;
4833 priv->commited = FALSE;
4834 priv->call_preroll = TRUE;
4835 if (priv->async_enabled) {
4836 GST_DEBUG_OBJECT (basesink, "doing async state change");
4837 ret = GST_STATE_CHANGE_ASYNC;
4838 gst_element_post_message (GST_ELEMENT_CAST (basesink),
4839 gst_message_new_async_start (GST_OBJECT_CAST (basesink)));
4843 GST_DEBUG_OBJECT (basesink, "rendered: %" G_GUINT64_FORMAT
4844 ", dropped: %" G_GUINT64_FORMAT, priv->rendered, priv->dropped);
4846 gst_base_sink_reset_qos (basesink);
4847 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
4849 case GST_STATE_CHANGE_PAUSED_TO_READY:
4850 GST_BASE_SINK_PREROLL_LOCK (basesink);
4851 /* start by resetting our position state with the object lock so that the
4852 * position query gets the right idea. We do this before we post the
4853 * messages so that the message handlers pick this up. */
4854 GST_OBJECT_LOCK (basesink);
4855 basesink->have_newsegment = FALSE;
4856 priv->current_sstart = GST_CLOCK_TIME_NONE;
4857 priv->current_sstop = GST_CLOCK_TIME_NONE;
4858 priv->have_latency = FALSE;
4859 if (priv->cached_clock_id) {
4860 gst_clock_id_unref (priv->cached_clock_id);
4861 priv->cached_clock_id = NULL;
4863 gst_caps_replace (&basesink->priv->caps, NULL);
4864 GST_OBJECT_UNLOCK (basesink);
4866 gst_base_sink_set_last_buffer (basesink, NULL);
4867 priv->call_preroll = FALSE;
4869 if (!priv->commited) {
4870 if (priv->async_enabled) {
4871 GST_DEBUG_OBJECT (basesink, "PAUSED to READY, posting async-done");
4873 gst_element_post_message (GST_ELEMENT_CAST (basesink),
4874 gst_message_new_state_changed (GST_OBJECT_CAST (basesink),
4875 GST_STATE_PLAYING, GST_STATE_PAUSED, GST_STATE_READY));
4877 gst_element_post_message (GST_ELEMENT_CAST (basesink),
4878 gst_message_new_async_done (GST_OBJECT_CAST (basesink), FALSE));
4880 priv->commited = TRUE;
4882 GST_DEBUG_OBJECT (basesink, "PAUSED to READY, don't need_preroll");
4884 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
4886 case GST_STATE_CHANGE_READY_TO_NULL:
4888 if (!bclass->stop (basesink)) {
4889 GST_WARNING_OBJECT (basesink, "failed to stop");
4892 gst_base_sink_set_last_buffer (basesink, NULL);
4893 priv->call_preroll = FALSE;
4904 GST_DEBUG_OBJECT (basesink, "failed to start");
4905 return GST_STATE_CHANGE_FAILURE;
4909 GST_DEBUG_OBJECT (basesink,
4910 "element failed to change states -- activation problem?");
4911 return GST_STATE_CHANGE_FAILURE;