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.
23 * SECTION:gstbasesink:
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 * Subclasses that synchronise on the clock in the #GstBaseSinkClass.render()
71 * method are supported as well. These classes typically receive a buffer in
72 * the render method and can then potentially block on the clock while
73 * rendering. A typical example is an audiosink.
74 * These subclasses can use gst_base_sink_wait_preroll() to perform the
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 * The #GstBaseSink:async property can be used to instruct the sink to never
135 * perform an ASYNC state change. This feature is mostly usable when dealing
136 * with non-synchronized streams or sparse streams.
138 * Last reviewed on 2007-08-29 (0.10.15)
145 #include <gst/gst_private.h>
147 #include "gstbasesink.h"
148 #include <gst/gst-i18n-lib.h>
150 GST_DEBUG_CATEGORY_STATIC (gst_base_sink_debug);
151 #define GST_CAT_DEFAULT gst_base_sink_debug
153 #define GST_BASE_SINK_GET_PRIVATE(obj) \
154 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_BASE_SINK, GstBaseSinkPrivate))
156 #define GST_FLOW_STEP GST_FLOW_CUSTOM_ERROR
160 gboolean valid; /* if this info is valid */
161 guint32 seqnum; /* the seqnum of the STEP event */
162 GstFormat format; /* the format of the amount */
163 guint64 amount; /* the total amount of data to skip */
164 guint64 position; /* the position in the stepped data */
165 guint64 duration; /* the duration in time of the skipped data */
166 guint64 start; /* running_time of the start */
167 gdouble rate; /* rate of skipping */
168 gdouble start_rate; /* rate before skipping */
169 guint64 start_start; /* start position skipping */
170 guint64 start_stop; /* stop position skipping */
171 gboolean flush; /* if this was a flushing step */
172 gboolean intermediate; /* if this is an intermediate step */
173 gboolean need_preroll; /* if we need preroll after this step */
176 struct _GstBaseSinkPrivate
178 gint qos_enabled; /* ATOMIC */
179 gboolean async_enabled;
180 GstClockTimeDiff ts_offset;
181 GstClockTime render_delay;
183 /* start, stop of current buffer, stream time, used to report position */
184 GstClockTime current_sstart;
185 GstClockTime current_sstop;
187 /* start, stop and jitter of current buffer, running time */
188 GstClockTime current_rstart;
189 GstClockTime current_rstop;
190 GstClockTimeDiff current_jitter;
191 /* the running time of the previous buffer */
192 GstClockTime prev_rstart;
194 /* EOS sync time in running time */
195 GstClockTime eos_rtime;
197 /* last buffer that arrived in time, running time */
198 GstClockTime last_render_time;
199 /* when the last buffer left the sink, running time */
200 GstClockTime last_left;
202 /* running averages go here these are done on running time */
204 GstClockTime avg_duration;
206 GstClockTime avg_in_diff;
208 /* these are done on system time. avg_jitter and avg_render are
209 * compared to eachother to see if the rendering time takes a
210 * huge amount of the processing, If so we are flooded with
212 GstClockTime last_left_systime;
213 GstClockTime avg_jitter;
214 GstClockTime start, stop;
215 GstClockTime avg_render;
217 /* number of rendered and dropped frames */
222 GstClockTime latency;
224 /* if we already commited the state */
226 /* state change to playing ongoing */
229 /* when we received EOS */
230 gboolean received_eos;
232 /* when we are prerolled and able to report latency */
233 gboolean have_latency;
235 /* the last buffer we prerolled or rendered. Useful for making snapshots */
236 gint enable_last_sample; /* atomic */
237 GstBuffer *last_buffer;
240 /* negotiated caps */
243 /* blocksize for pulling */
248 /* seqnum of the stream */
251 gboolean call_preroll;
252 gboolean step_unlock;
254 /* we have a pending and a current step operation */
255 GstStepInfo current_step;
256 GstStepInfo pending_step;
258 /* Cached GstClockID */
259 GstClockID cached_clock_id;
261 /* for throttling and QoS */
262 GstClockTime earliest_in_time;
263 GstClockTime throttle_time;
266 #define DO_RUNNING_AVG(avg,val,size) (((val) + ((size)-1) * (avg)) / (size))
268 /* generic running average, this has a neutral window size */
269 #define UPDATE_RUNNING_AVG(avg,val) DO_RUNNING_AVG(avg,val,8)
271 /* the windows for these running averages are experimentally obtained.
272 * possitive values get averaged more while negative values use a small
273 * window so we can react faster to badness. */
274 #define UPDATE_RUNNING_AVG_P(avg,val) DO_RUNNING_AVG(avg,val,16)
275 #define UPDATE_RUNNING_AVG_N(avg,val) DO_RUNNING_AVG(avg,val,4)
277 /* BaseSink properties */
279 #define DEFAULT_CAN_ACTIVATE_PULL FALSE /* fixme: enable me */
280 #define DEFAULT_CAN_ACTIVATE_PUSH TRUE
282 #define DEFAULT_SYNC TRUE
283 #define DEFAULT_MAX_LATENESS -1
284 #define DEFAULT_QOS FALSE
285 #define DEFAULT_ASYNC TRUE
286 #define DEFAULT_TS_OFFSET 0
287 #define DEFAULT_BLOCKSIZE 4096
288 #define DEFAULT_RENDER_DELAY 0
289 #define DEFAULT_ENABLE_LAST_SAMPLE TRUE
290 #define DEFAULT_THROTTLE_TIME 0
300 PROP_ENABLE_LAST_SAMPLE,
308 static GstElementClass *parent_class = NULL;
310 static void gst_base_sink_class_init (GstBaseSinkClass * klass);
311 static void gst_base_sink_init (GstBaseSink * trans, gpointer g_class);
312 static void gst_base_sink_finalize (GObject * object);
315 gst_base_sink_get_type (void)
317 static volatile gsize base_sink_type = 0;
319 if (g_once_init_enter (&base_sink_type)) {
321 static const GTypeInfo base_sink_info = {
322 sizeof (GstBaseSinkClass),
325 (GClassInitFunc) gst_base_sink_class_init,
328 sizeof (GstBaseSink),
330 (GInstanceInitFunc) gst_base_sink_init,
333 _type = g_type_register_static (GST_TYPE_ELEMENT,
334 "GstBaseSink", &base_sink_info, G_TYPE_FLAG_ABSTRACT);
335 g_once_init_leave (&base_sink_type, _type);
337 return base_sink_type;
340 static void gst_base_sink_set_property (GObject * object, guint prop_id,
341 const GValue * value, GParamSpec * pspec);
342 static void gst_base_sink_get_property (GObject * object, guint prop_id,
343 GValue * value, GParamSpec * pspec);
345 static gboolean gst_base_sink_send_event (GstElement * element,
347 static gboolean default_element_query (GstElement * element, GstQuery * query);
349 static GstCaps *gst_base_sink_default_get_caps (GstBaseSink * sink,
351 static gboolean gst_base_sink_default_set_caps (GstBaseSink * sink,
353 static void gst_base_sink_default_get_times (GstBaseSink * basesink,
354 GstBuffer * buffer, GstClockTime * start, GstClockTime * end);
355 static gboolean gst_base_sink_set_flushing (GstBaseSink * basesink,
356 GstPad * pad, gboolean flushing);
357 static gboolean gst_base_sink_default_activate_pull (GstBaseSink * basesink,
359 static gboolean gst_base_sink_default_do_seek (GstBaseSink * sink,
360 GstSegment * segment);
361 static gboolean gst_base_sink_default_prepare_seek_segment (GstBaseSink * sink,
362 GstEvent * event, GstSegment * segment);
364 static GstStateChangeReturn gst_base_sink_change_state (GstElement * element,
365 GstStateChange transition);
367 static gboolean gst_base_sink_sink_query (GstPad * pad, GstObject * parent,
369 static GstFlowReturn gst_base_sink_chain (GstPad * pad, GstObject * parent,
371 static GstFlowReturn gst_base_sink_chain_list (GstPad * pad, GstObject * parent,
372 GstBufferList * list);
374 static void gst_base_sink_loop (GstPad * pad);
375 static gboolean gst_base_sink_pad_activate (GstPad * pad, GstObject * parent);
376 static gboolean gst_base_sink_pad_activate_mode (GstPad * pad,
377 GstObject * parent, GstPadMode mode, gboolean active);
378 static gboolean gst_base_sink_default_event (GstBaseSink * basesink,
380 static GstFlowReturn gst_base_sink_default_wait_eos (GstBaseSink * basesink,
382 static gboolean gst_base_sink_event (GstPad * pad, GstObject * parent,
385 static gboolean gst_base_sink_default_query (GstBaseSink * sink,
388 static gboolean gst_base_sink_negotiate_pull (GstBaseSink * basesink);
389 static GstCaps *gst_base_sink_default_fixate (GstBaseSink * bsink,
391 static GstCaps *gst_base_sink_fixate (GstBaseSink * bsink, GstCaps * caps);
393 /* check if an object was too late */
394 static gboolean gst_base_sink_is_too_late (GstBaseSink * basesink,
395 GstMiniObject * obj, GstClockTime rstart, GstClockTime rstop,
396 GstClockReturn status, GstClockTimeDiff jitter);
399 gst_base_sink_class_init (GstBaseSinkClass * klass)
401 GObjectClass *gobject_class;
402 GstElementClass *gstelement_class;
404 gobject_class = G_OBJECT_CLASS (klass);
405 gstelement_class = GST_ELEMENT_CLASS (klass);
407 GST_DEBUG_CATEGORY_INIT (gst_base_sink_debug, "basesink", 0,
410 g_type_class_add_private (klass, sizeof (GstBaseSinkPrivate));
412 parent_class = g_type_class_peek_parent (klass);
414 gobject_class->finalize = gst_base_sink_finalize;
415 gobject_class->set_property = gst_base_sink_set_property;
416 gobject_class->get_property = gst_base_sink_get_property;
418 g_object_class_install_property (gobject_class, PROP_SYNC,
419 g_param_spec_boolean ("sync", "Sync", "Sync on the clock", DEFAULT_SYNC,
420 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
422 g_object_class_install_property (gobject_class, PROP_MAX_LATENESS,
423 g_param_spec_int64 ("max-lateness", "Max Lateness",
424 "Maximum number of nanoseconds that a buffer can be late before it "
425 "is dropped (-1 unlimited)", -1, G_MAXINT64, DEFAULT_MAX_LATENESS,
426 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
428 g_object_class_install_property (gobject_class, PROP_QOS,
429 g_param_spec_boolean ("qos", "Qos",
430 "Generate Quality-of-Service events upstream", DEFAULT_QOS,
431 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
435 * If set to #TRUE, the basesink will perform asynchronous state changes.
436 * When set to #FALSE, the sink will not signal the parent when it prerolls.
437 * Use this option when dealing with sparse streams or when synchronisation is
440 g_object_class_install_property (gobject_class, PROP_ASYNC,
441 g_param_spec_boolean ("async", "Async",
442 "Go asynchronously to PAUSED", DEFAULT_ASYNC,
443 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
445 * GstBaseSink:ts-offset:
447 * Controls the final synchronisation, a negative value will render the buffer
448 * earlier while a positive value delays playback. This property can be
449 * used to fix synchronisation in bad files.
451 g_object_class_install_property (gobject_class, PROP_TS_OFFSET,
452 g_param_spec_int64 ("ts-offset", "TS Offset",
453 "Timestamp offset in nanoseconds", G_MININT64, G_MAXINT64,
454 DEFAULT_TS_OFFSET, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
457 * GstBaseSink:enable-last-sample:
459 * Enable the last-sample property. If FALSE, basesink doesn't keep a
460 * reference to the last buffer arrived and the last-sample property is always
461 * set to NULL. This can be useful if you need buffers to be released as soon
462 * as possible, eg. if you're using a buffer pool.
464 g_object_class_install_property (gobject_class, PROP_ENABLE_LAST_SAMPLE,
465 g_param_spec_boolean ("enable-last-sample", "Enable Last Buffer",
466 "Enable the last-sample property", DEFAULT_ENABLE_LAST_SAMPLE,
467 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
470 * GstBaseSink:last-sample:
472 * The last buffer that arrived in the sink and was used for preroll or for
473 * rendering. This property can be used to generate thumbnails. This property
474 * can be NULL when the sink has not yet received a bufer.
476 g_object_class_install_property (gobject_class, PROP_LAST_SAMPLE,
477 g_param_spec_boxed ("last-sample", "Last Sample",
478 "The last sample received in the sink", GST_TYPE_SAMPLE,
479 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
481 * GstBaseSink:blocksize:
483 * The amount of bytes to pull when operating in pull mode.
485 /* FIXME 0.11: blocksize property should be int, otherwise min>max.. */
486 g_object_class_install_property (gobject_class, PROP_BLOCKSIZE,
487 g_param_spec_uint ("blocksize", "Block size",
488 "Size in bytes to pull per buffer (0 = default)", 0, G_MAXUINT,
489 DEFAULT_BLOCKSIZE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
491 * GstBaseSink:render-delay:
493 * The additional delay between synchronisation and actual rendering of the
494 * media. This property will add additional latency to the device in order to
495 * make other sinks compensate for the delay.
497 g_object_class_install_property (gobject_class, PROP_RENDER_DELAY,
498 g_param_spec_uint64 ("render-delay", "Render Delay",
499 "Additional render delay of the sink in nanoseconds", 0, G_MAXUINT64,
500 DEFAULT_RENDER_DELAY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
502 * GstBaseSink:throttle-time:
504 * The time to insert between buffers. This property can be used to control
505 * the maximum amount of buffers per second to render. Setting this property
506 * to a value bigger than 0 will make the sink create THROTTLE QoS events.
508 g_object_class_install_property (gobject_class, PROP_THROTTLE_TIME,
509 g_param_spec_uint64 ("throttle-time", "Throttle time",
510 "The time to keep between rendered buffers", 0, G_MAXUINT64,
511 DEFAULT_THROTTLE_TIME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
513 gstelement_class->change_state =
514 GST_DEBUG_FUNCPTR (gst_base_sink_change_state);
515 gstelement_class->send_event = GST_DEBUG_FUNCPTR (gst_base_sink_send_event);
516 gstelement_class->query = GST_DEBUG_FUNCPTR (default_element_query);
518 klass->get_caps = GST_DEBUG_FUNCPTR (gst_base_sink_default_get_caps);
519 klass->set_caps = GST_DEBUG_FUNCPTR (gst_base_sink_default_set_caps);
520 klass->fixate = GST_DEBUG_FUNCPTR (gst_base_sink_default_fixate);
521 klass->activate_pull =
522 GST_DEBUG_FUNCPTR (gst_base_sink_default_activate_pull);
523 klass->get_times = GST_DEBUG_FUNCPTR (gst_base_sink_default_get_times);
524 klass->query = GST_DEBUG_FUNCPTR (gst_base_sink_default_query);
525 klass->event = GST_DEBUG_FUNCPTR (gst_base_sink_default_event);
526 klass->wait_eos = GST_DEBUG_FUNCPTR (gst_base_sink_default_wait_eos);
528 /* Registering debug symbols for function pointers */
529 GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_fixate);
530 GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_pad_activate);
531 GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_pad_activate_mode);
532 GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_event);
533 GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_chain);
534 GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_chain_list);
535 GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_sink_query);
539 gst_base_sink_query_caps (GstBaseSink * bsink, GstPad * pad, GstCaps * filter)
541 GstBaseSinkClass *bclass;
542 GstCaps *caps = NULL;
545 bclass = GST_BASE_SINK_GET_CLASS (bsink);
546 fixed = GST_PAD_IS_FIXED_CAPS (pad);
548 if (fixed || bsink->pad_mode == GST_PAD_MODE_PULL) {
549 /* if we are operating in pull mode or fixed caps, we only accept the
550 * currently negotiated caps */
551 caps = gst_pad_get_current_caps (pad);
554 if (bclass->get_caps)
555 caps = bclass->get_caps (bsink, filter);
558 GstPadTemplate *pad_template;
561 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (bclass),
563 if (pad_template != NULL) {
564 caps = gst_pad_template_get_caps (pad_template);
567 GstCaps *intersection;
570 gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
571 gst_caps_unref (caps);
582 gst_base_sink_default_fixate (GstBaseSink * bsink, GstCaps * caps)
584 GST_DEBUG_OBJECT (bsink, "using default caps fixate function");
585 return gst_caps_fixate (caps);
589 gst_base_sink_fixate (GstBaseSink * bsink, GstCaps * caps)
591 GstBaseSinkClass *bclass;
593 bclass = GST_BASE_SINK_GET_CLASS (bsink);
596 caps = bclass->fixate (bsink, caps);
602 gst_base_sink_init (GstBaseSink * basesink, gpointer g_class)
604 GstPadTemplate *pad_template;
605 GstBaseSinkPrivate *priv;
607 basesink->priv = priv = GST_BASE_SINK_GET_PRIVATE (basesink);
610 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (g_class), "sink");
611 g_return_if_fail (pad_template != NULL);
613 basesink->sinkpad = gst_pad_new_from_template (pad_template, "sink");
615 gst_pad_set_activate_function (basesink->sinkpad, gst_base_sink_pad_activate);
616 gst_pad_set_activatemode_function (basesink->sinkpad,
617 gst_base_sink_pad_activate_mode);
618 gst_pad_set_query_function (basesink->sinkpad, gst_base_sink_sink_query);
619 gst_pad_set_event_function (basesink->sinkpad, gst_base_sink_event);
620 gst_pad_set_chain_function (basesink->sinkpad, gst_base_sink_chain);
621 gst_pad_set_chain_list_function (basesink->sinkpad, gst_base_sink_chain_list);
622 gst_element_add_pad (GST_ELEMENT_CAST (basesink), basesink->sinkpad);
624 basesink->pad_mode = GST_PAD_MODE_NONE;
625 g_mutex_init (&basesink->preroll_lock);
626 g_cond_init (&basesink->preroll_cond);
627 priv->have_latency = FALSE;
629 basesink->can_activate_push = DEFAULT_CAN_ACTIVATE_PUSH;
630 basesink->can_activate_pull = DEFAULT_CAN_ACTIVATE_PULL;
632 basesink->sync = DEFAULT_SYNC;
633 basesink->max_lateness = DEFAULT_MAX_LATENESS;
634 g_atomic_int_set (&priv->qos_enabled, DEFAULT_QOS);
635 priv->async_enabled = DEFAULT_ASYNC;
636 priv->ts_offset = DEFAULT_TS_OFFSET;
637 priv->render_delay = DEFAULT_RENDER_DELAY;
638 priv->blocksize = DEFAULT_BLOCKSIZE;
639 priv->cached_clock_id = NULL;
640 g_atomic_int_set (&priv->enable_last_sample, DEFAULT_ENABLE_LAST_SAMPLE);
641 priv->throttle_time = DEFAULT_THROTTLE_TIME;
643 GST_OBJECT_FLAG_SET (basesink, GST_ELEMENT_FLAG_SINK);
647 gst_base_sink_finalize (GObject * object)
649 GstBaseSink *basesink;
651 basesink = GST_BASE_SINK (object);
653 g_mutex_clear (&basesink->preroll_lock);
654 g_cond_clear (&basesink->preroll_cond);
656 G_OBJECT_CLASS (parent_class)->finalize (object);
660 * gst_base_sink_set_sync:
662 * @sync: the new sync value.
664 * Configures @sink to synchronize on the clock or not. When
665 * @sync is FALSE, incoming samples will be played as fast as
666 * possible. If @sync is TRUE, the timestamps of the incomming
667 * buffers will be used to schedule the exact render time of its
671 gst_base_sink_set_sync (GstBaseSink * sink, gboolean sync)
673 g_return_if_fail (GST_IS_BASE_SINK (sink));
675 GST_OBJECT_LOCK (sink);
677 GST_OBJECT_UNLOCK (sink);
681 * gst_base_sink_get_sync:
684 * Checks if @sink is currently configured to synchronize against the
687 * Returns: TRUE if the sink is configured to synchronize against the clock.
690 gst_base_sink_get_sync (GstBaseSink * sink)
694 g_return_val_if_fail (GST_IS_BASE_SINK (sink), FALSE);
696 GST_OBJECT_LOCK (sink);
698 GST_OBJECT_UNLOCK (sink);
704 * gst_base_sink_set_max_lateness:
706 * @max_lateness: the new max lateness value.
708 * Sets the new max lateness value to @max_lateness. This value is
709 * used to decide if a buffer should be dropped or not based on the
710 * buffer timestamp and the current clock time. A value of -1 means
714 gst_base_sink_set_max_lateness (GstBaseSink * sink, gint64 max_lateness)
716 g_return_if_fail (GST_IS_BASE_SINK (sink));
718 GST_OBJECT_LOCK (sink);
719 sink->max_lateness = max_lateness;
720 GST_OBJECT_UNLOCK (sink);
724 * gst_base_sink_get_max_lateness:
727 * Gets the max lateness value. See gst_base_sink_set_max_lateness for
730 * Returns: The maximum time in nanoseconds that a buffer can be late
731 * before it is dropped and not rendered. A value of -1 means an
735 gst_base_sink_get_max_lateness (GstBaseSink * sink)
739 g_return_val_if_fail (GST_IS_BASE_SINK (sink), -1);
741 GST_OBJECT_LOCK (sink);
742 res = sink->max_lateness;
743 GST_OBJECT_UNLOCK (sink);
749 * gst_base_sink_set_qos_enabled:
751 * @enabled: the new qos value.
753 * Configures @sink to send Quality-of-Service events upstream.
756 gst_base_sink_set_qos_enabled (GstBaseSink * sink, gboolean enabled)
758 g_return_if_fail (GST_IS_BASE_SINK (sink));
760 g_atomic_int_set (&sink->priv->qos_enabled, enabled);
764 * gst_base_sink_is_qos_enabled:
767 * Checks if @sink is currently configured to send Quality-of-Service events
770 * Returns: TRUE if the sink is configured to perform Quality-of-Service.
773 gst_base_sink_is_qos_enabled (GstBaseSink * sink)
777 g_return_val_if_fail (GST_IS_BASE_SINK (sink), FALSE);
779 res = g_atomic_int_get (&sink->priv->qos_enabled);
785 * gst_base_sink_set_async_enabled:
787 * @enabled: the new async value.
789 * Configures @sink to perform all state changes asynchronusly. When async is
790 * disabled, the sink will immediately go to PAUSED instead of waiting for a
791 * preroll buffer. This feature is useful if the sink does not synchronize
792 * against the clock or when it is dealing with sparse streams.
795 gst_base_sink_set_async_enabled (GstBaseSink * sink, gboolean enabled)
797 g_return_if_fail (GST_IS_BASE_SINK (sink));
799 GST_BASE_SINK_PREROLL_LOCK (sink);
800 g_atomic_int_set (&sink->priv->async_enabled, enabled);
801 GST_LOG_OBJECT (sink, "set async enabled to %d", enabled);
802 GST_BASE_SINK_PREROLL_UNLOCK (sink);
806 * gst_base_sink_is_async_enabled:
809 * Checks if @sink is currently configured to perform asynchronous state
812 * Returns: TRUE if the sink is configured to perform asynchronous state
816 gst_base_sink_is_async_enabled (GstBaseSink * sink)
820 g_return_val_if_fail (GST_IS_BASE_SINK (sink), FALSE);
822 res = g_atomic_int_get (&sink->priv->async_enabled);
828 * gst_base_sink_set_ts_offset:
830 * @offset: the new offset
832 * Adjust the synchronisation of @sink with @offset. A negative value will
833 * render buffers earlier than their timestamp. A positive value will delay
834 * rendering. This function can be used to fix playback of badly timestamped
838 gst_base_sink_set_ts_offset (GstBaseSink * sink, GstClockTimeDiff offset)
840 g_return_if_fail (GST_IS_BASE_SINK (sink));
842 GST_OBJECT_LOCK (sink);
843 sink->priv->ts_offset = offset;
844 GST_LOG_OBJECT (sink, "set time offset to %" G_GINT64_FORMAT, offset);
845 GST_OBJECT_UNLOCK (sink);
849 * gst_base_sink_get_ts_offset:
852 * Get the synchronisation offset of @sink.
854 * Returns: The synchronisation offset.
857 gst_base_sink_get_ts_offset (GstBaseSink * sink)
859 GstClockTimeDiff res;
861 g_return_val_if_fail (GST_IS_BASE_SINK (sink), 0);
863 GST_OBJECT_LOCK (sink);
864 res = sink->priv->ts_offset;
865 GST_OBJECT_UNLOCK (sink);
871 * gst_base_sink_get_last_sample:
874 * Get the last sample that arrived in the sink and was used for preroll or for
875 * rendering. This property can be used to generate thumbnails.
877 * The #GstCaps on the sample can be used to determine the type of the buffer.
879 * Free-function: gst_sample_unref
881 * Returns: (transfer full): a #GstSample. gst_sample_unref() after usage.
882 * This function returns NULL when no buffer has arrived in the sink yet
883 * or when the sink is not in PAUSED or PLAYING.
886 gst_base_sink_get_last_sample (GstBaseSink * sink)
888 GstSample *res = NULL;
890 g_return_val_if_fail (GST_IS_BASE_SINK (sink), NULL);
892 GST_OBJECT_LOCK (sink);
893 if (sink->priv->last_buffer) {
894 res = gst_sample_new (sink->priv->last_buffer,
895 sink->priv->last_caps, &sink->segment, NULL);
897 GST_OBJECT_UNLOCK (sink);
902 /* with OBJECT_LOCK */
904 gst_base_sink_set_last_buffer_unlocked (GstBaseSink * sink, GstBuffer * buffer)
908 old = sink->priv->last_buffer;
909 if (G_LIKELY (old != buffer)) {
910 GST_DEBUG_OBJECT (sink, "setting last buffer to %p", buffer);
911 if (G_LIKELY (buffer))
912 gst_buffer_ref (buffer);
913 sink->priv->last_buffer = buffer;
915 /* copy over the caps */
916 gst_caps_replace (&sink->priv->last_caps, sink->priv->caps);
918 gst_caps_replace (&sink->priv->last_caps, NULL);
922 /* avoid unreffing with the lock because cleanup code might want to take the
924 if (G_LIKELY (old)) {
925 GST_OBJECT_UNLOCK (sink);
926 gst_buffer_unref (old);
927 GST_OBJECT_LOCK (sink);
932 gst_base_sink_set_last_buffer (GstBaseSink * sink, GstBuffer * buffer)
934 if (!g_atomic_int_get (&sink->priv->enable_last_sample))
937 GST_OBJECT_LOCK (sink);
938 gst_base_sink_set_last_buffer_unlocked (sink, buffer);
939 GST_OBJECT_UNLOCK (sink);
943 * gst_base_sink_set_last_sample_enabled:
945 * @enabled: the new enable-last-sample value.
947 * Configures @sink to store the last received sample in the last-sample
951 gst_base_sink_set_last_sample_enabled (GstBaseSink * sink, gboolean enabled)
953 g_return_if_fail (GST_IS_BASE_SINK (sink));
955 /* Only take lock if we change the value */
956 if (g_atomic_int_compare_and_exchange (&sink->priv->enable_last_sample,
957 !enabled, enabled) && !enabled) {
958 GST_OBJECT_LOCK (sink);
959 gst_base_sink_set_last_buffer_unlocked (sink, NULL);
960 GST_OBJECT_UNLOCK (sink);
965 * gst_base_sink_is_last_sample_enabled:
968 * Checks if @sink is currently configured to store the last received sample in
969 * the last-sample property.
971 * Returns: TRUE if the sink is configured to store the last received sample.
974 gst_base_sink_is_last_sample_enabled (GstBaseSink * sink)
976 g_return_val_if_fail (GST_IS_BASE_SINK (sink), FALSE);
978 return g_atomic_int_get (&sink->priv->enable_last_sample);
982 * gst_base_sink_get_latency:
985 * Get the currently configured latency.
987 * Returns: The configured latency.
990 gst_base_sink_get_latency (GstBaseSink * sink)
994 GST_OBJECT_LOCK (sink);
995 res = sink->priv->latency;
996 GST_OBJECT_UNLOCK (sink);
1002 * gst_base_sink_query_latency:
1004 * @live: (out) (allow-none): if the sink is live
1005 * @upstream_live: (out) (allow-none): if an upstream element is live
1006 * @min_latency: (out) (allow-none): the min latency of the upstream elements
1007 * @max_latency: (out) (allow-none): the max latency of the upstream elements
1009 * Query the sink for the latency parameters. The latency will be queried from
1010 * the upstream elements. @live will be TRUE if @sink is configured to
1011 * synchronize against the clock. @upstream_live will be TRUE if an upstream
1014 * If both @live and @upstream_live are TRUE, the sink will want to compensate
1015 * for the latency introduced by the upstream elements by setting the
1016 * @min_latency to a strictly possitive value.
1018 * This function is mostly used by subclasses.
1020 * Returns: TRUE if the query succeeded.
1023 gst_base_sink_query_latency (GstBaseSink * sink, gboolean * live,
1024 gboolean * upstream_live, GstClockTime * min_latency,
1025 GstClockTime * max_latency)
1027 gboolean l, us_live, res, have_latency;
1028 GstClockTime min, max, render_delay;
1030 GstClockTime us_min, us_max;
1032 /* we are live when we sync to the clock */
1033 GST_OBJECT_LOCK (sink);
1035 have_latency = sink->priv->have_latency;
1036 render_delay = sink->priv->render_delay;
1037 GST_OBJECT_UNLOCK (sink);
1039 /* assume no latency */
1045 GST_DEBUG_OBJECT (sink, "we are ready for LATENCY query");
1046 /* we are ready for a latency query this is when we preroll or when we are
1048 query = gst_query_new_latency ();
1050 /* ask the peer for the latency */
1051 if ((res = gst_pad_peer_query (sink->sinkpad, query))) {
1052 /* get upstream min and max latency */
1053 gst_query_parse_latency (query, &us_live, &us_min, &us_max);
1056 /* upstream live, use its latency, subclasses should use these
1057 * values to create the complete latency. */
1062 /* we need to add the render delay if we are live */
1064 min += render_delay;
1066 max += render_delay;
1069 gst_query_unref (query);
1071 GST_DEBUG_OBJECT (sink, "we are not yet ready for LATENCY query");
1075 /* not live, we tried to do the query, if it failed we return TRUE anyway */
1079 GST_DEBUG_OBJECT (sink, "latency query failed but we are not live");
1081 GST_DEBUG_OBJECT (sink, "latency query failed and we are live");
1086 GST_DEBUG_OBJECT (sink, "latency query: live: %d, have_latency %d,"
1087 " upstream: %d, min %" GST_TIME_FORMAT ", max %" GST_TIME_FORMAT, l,
1088 have_latency, us_live, GST_TIME_ARGS (min), GST_TIME_ARGS (max));
1093 *upstream_live = us_live;
1103 * gst_base_sink_set_render_delay:
1104 * @sink: a #GstBaseSink
1105 * @delay: the new delay
1107 * Set the render delay in @sink to @delay. The render delay is the time
1108 * between actual rendering of a buffer and its synchronisation time. Some
1109 * devices might delay media rendering which can be compensated for with this
1112 * After calling this function, this sink will report additional latency and
1113 * other sinks will adjust their latency to delay the rendering of their media.
1115 * This function is usually called by subclasses.
1118 gst_base_sink_set_render_delay (GstBaseSink * sink, GstClockTime delay)
1120 GstClockTime old_render_delay;
1122 g_return_if_fail (GST_IS_BASE_SINK (sink));
1124 GST_OBJECT_LOCK (sink);
1125 old_render_delay = sink->priv->render_delay;
1126 sink->priv->render_delay = delay;
1127 GST_LOG_OBJECT (sink, "set render delay to %" GST_TIME_FORMAT,
1128 GST_TIME_ARGS (delay));
1129 GST_OBJECT_UNLOCK (sink);
1131 if (delay != old_render_delay) {
1132 GST_DEBUG_OBJECT (sink, "posting latency changed");
1133 gst_element_post_message (GST_ELEMENT_CAST (sink),
1134 gst_message_new_latency (GST_OBJECT_CAST (sink)));
1139 * gst_base_sink_get_render_delay:
1140 * @sink: a #GstBaseSink
1142 * Get the render delay of @sink. see gst_base_sink_set_render_delay() for more
1143 * information about the render delay.
1145 * Returns: the render delay of @sink.
1148 gst_base_sink_get_render_delay (GstBaseSink * sink)
1150 GstClockTimeDiff res;
1152 g_return_val_if_fail (GST_IS_BASE_SINK (sink), 0);
1154 GST_OBJECT_LOCK (sink);
1155 res = sink->priv->render_delay;
1156 GST_OBJECT_UNLOCK (sink);
1162 * gst_base_sink_set_blocksize:
1163 * @sink: a #GstBaseSink
1164 * @blocksize: the blocksize in bytes
1166 * Set the number of bytes that the sink will pull when it is operating in pull
1169 /* FIXME 0.11: blocksize property should be int, otherwise min>max.. */
1171 gst_base_sink_set_blocksize (GstBaseSink * sink, guint blocksize)
1173 g_return_if_fail (GST_IS_BASE_SINK (sink));
1175 GST_OBJECT_LOCK (sink);
1176 sink->priv->blocksize = blocksize;
1177 GST_LOG_OBJECT (sink, "set blocksize to %u", blocksize);
1178 GST_OBJECT_UNLOCK (sink);
1182 * gst_base_sink_get_blocksize:
1183 * @sink: a #GstBaseSink
1185 * Get the number of bytes that the sink will pull when it is operating in pull
1188 * Returns: the number of bytes @sink will pull in pull mode.
1190 /* FIXME 0.11: blocksize property should be int, otherwise min>max.. */
1192 gst_base_sink_get_blocksize (GstBaseSink * sink)
1196 g_return_val_if_fail (GST_IS_BASE_SINK (sink), 0);
1198 GST_OBJECT_LOCK (sink);
1199 res = sink->priv->blocksize;
1200 GST_OBJECT_UNLOCK (sink);
1206 * gst_base_sink_set_throttle_time:
1207 * @sink: a #GstBaseSink
1208 * @throttle: the throttle time in nanoseconds
1210 * Set the time that will be inserted between rendered buffers. This
1211 * can be used to control the maximum buffers per second that the sink
1215 gst_base_sink_set_throttle_time (GstBaseSink * sink, guint64 throttle)
1217 g_return_if_fail (GST_IS_BASE_SINK (sink));
1219 GST_OBJECT_LOCK (sink);
1220 sink->priv->throttle_time = throttle;
1221 GST_LOG_OBJECT (sink, "set throttle_time to %" G_GUINT64_FORMAT, throttle);
1222 GST_OBJECT_UNLOCK (sink);
1226 * gst_base_sink_get_throttle_time:
1227 * @sink: a #GstBaseSink
1229 * Get the time that will be inserted between frames to control the
1230 * maximum buffers per second.
1232 * Returns: the number of nanoseconds @sink will put between frames.
1235 gst_base_sink_get_throttle_time (GstBaseSink * sink)
1239 g_return_val_if_fail (GST_IS_BASE_SINK (sink), 0);
1241 GST_OBJECT_LOCK (sink);
1242 res = sink->priv->throttle_time;
1243 GST_OBJECT_UNLOCK (sink);
1249 gst_base_sink_set_property (GObject * object, guint prop_id,
1250 const GValue * value, GParamSpec * pspec)
1252 GstBaseSink *sink = GST_BASE_SINK (object);
1256 gst_base_sink_set_sync (sink, g_value_get_boolean (value));
1258 case PROP_MAX_LATENESS:
1259 gst_base_sink_set_max_lateness (sink, g_value_get_int64 (value));
1262 gst_base_sink_set_qos_enabled (sink, g_value_get_boolean (value));
1265 gst_base_sink_set_async_enabled (sink, g_value_get_boolean (value));
1267 case PROP_TS_OFFSET:
1268 gst_base_sink_set_ts_offset (sink, g_value_get_int64 (value));
1270 case PROP_BLOCKSIZE:
1271 gst_base_sink_set_blocksize (sink, g_value_get_uint (value));
1273 case PROP_RENDER_DELAY:
1274 gst_base_sink_set_render_delay (sink, g_value_get_uint64 (value));
1276 case PROP_ENABLE_LAST_SAMPLE:
1277 gst_base_sink_set_last_sample_enabled (sink, g_value_get_boolean (value));
1279 case PROP_THROTTLE_TIME:
1280 gst_base_sink_set_throttle_time (sink, g_value_get_uint64 (value));
1283 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1289 gst_base_sink_get_property (GObject * object, guint prop_id, GValue * value,
1292 GstBaseSink *sink = GST_BASE_SINK (object);
1296 g_value_set_boolean (value, gst_base_sink_get_sync (sink));
1298 case PROP_MAX_LATENESS:
1299 g_value_set_int64 (value, gst_base_sink_get_max_lateness (sink));
1302 g_value_set_boolean (value, gst_base_sink_is_qos_enabled (sink));
1305 g_value_set_boolean (value, gst_base_sink_is_async_enabled (sink));
1307 case PROP_TS_OFFSET:
1308 g_value_set_int64 (value, gst_base_sink_get_ts_offset (sink));
1310 case PROP_LAST_SAMPLE:
1311 gst_value_take_buffer (value, gst_base_sink_get_last_sample (sink));
1313 case PROP_ENABLE_LAST_SAMPLE:
1314 g_value_set_boolean (value, gst_base_sink_is_last_sample_enabled (sink));
1316 case PROP_BLOCKSIZE:
1317 g_value_set_uint (value, gst_base_sink_get_blocksize (sink));
1319 case PROP_RENDER_DELAY:
1320 g_value_set_uint64 (value, gst_base_sink_get_render_delay (sink));
1322 case PROP_THROTTLE_TIME:
1323 g_value_set_uint64 (value, gst_base_sink_get_throttle_time (sink));
1326 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1333 gst_base_sink_default_get_caps (GstBaseSink * sink, GstCaps * filter)
1339 gst_base_sink_default_set_caps (GstBaseSink * sink, GstCaps * caps)
1344 /* with PREROLL_LOCK, STREAM_LOCK */
1346 gst_base_sink_commit_state (GstBaseSink * basesink)
1348 /* commit state and proceed to next pending state */
1349 GstState current, next, pending, post_pending;
1350 gboolean post_paused = FALSE;
1351 gboolean post_async_done = FALSE;
1352 gboolean post_playing = FALSE;
1354 /* we are certainly not playing async anymore now */
1355 basesink->playing_async = FALSE;
1357 GST_OBJECT_LOCK (basesink);
1358 current = GST_STATE (basesink);
1359 next = GST_STATE_NEXT (basesink);
1360 pending = GST_STATE_PENDING (basesink);
1361 post_pending = pending;
1364 case GST_STATE_PLAYING:
1366 GST_DEBUG_OBJECT (basesink, "commiting state to PLAYING");
1368 basesink->need_preroll = FALSE;
1369 post_async_done = TRUE;
1370 basesink->priv->commited = TRUE;
1371 post_playing = TRUE;
1372 /* post PAUSED too when we were READY */
1373 if (current == GST_STATE_READY) {
1378 case GST_STATE_PAUSED:
1379 GST_DEBUG_OBJECT (basesink, "commiting state to PAUSED");
1381 post_async_done = TRUE;
1382 basesink->priv->commited = TRUE;
1383 post_pending = GST_STATE_VOID_PENDING;
1385 case GST_STATE_READY:
1386 case GST_STATE_NULL:
1388 case GST_STATE_VOID_PENDING:
1389 goto nothing_pending;
1394 /* we can report latency queries now */
1395 basesink->priv->have_latency = TRUE;
1397 GST_STATE (basesink) = pending;
1398 GST_STATE_NEXT (basesink) = GST_STATE_VOID_PENDING;
1399 GST_STATE_PENDING (basesink) = GST_STATE_VOID_PENDING;
1400 GST_STATE_RETURN (basesink) = GST_STATE_CHANGE_SUCCESS;
1401 GST_OBJECT_UNLOCK (basesink);
1404 GST_DEBUG_OBJECT (basesink, "posting PAUSED state change message");
1405 gst_element_post_message (GST_ELEMENT_CAST (basesink),
1406 gst_message_new_state_changed (GST_OBJECT_CAST (basesink),
1407 current, next, post_pending));
1409 if (post_async_done) {
1410 GST_DEBUG_OBJECT (basesink, "posting async-done message");
1411 gst_element_post_message (GST_ELEMENT_CAST (basesink),
1412 gst_message_new_async_done (GST_OBJECT_CAST (basesink),
1413 GST_CLOCK_TIME_NONE));
1416 GST_DEBUG_OBJECT (basesink, "posting PLAYING state change message");
1417 gst_element_post_message (GST_ELEMENT_CAST (basesink),
1418 gst_message_new_state_changed (GST_OBJECT_CAST (basesink),
1419 next, pending, GST_STATE_VOID_PENDING));
1422 GST_STATE_BROADCAST (basesink);
1428 /* Depending on the state, set our vars. We get in this situation when the
1429 * state change function got a change to update the state vars before the
1430 * streaming thread did. This is fine but we need to make sure that we
1431 * update the need_preroll var since it was TRUE when we got here and might
1432 * become FALSE if we got to PLAYING. */
1433 GST_DEBUG_OBJECT (basesink, "nothing to commit, now in %s",
1434 gst_element_state_get_name (current));
1436 case GST_STATE_PLAYING:
1437 basesink->need_preroll = FALSE;
1439 case GST_STATE_PAUSED:
1440 basesink->need_preroll = TRUE;
1443 basesink->need_preroll = FALSE;
1444 basesink->flushing = TRUE;
1447 /* we can report latency queries now */
1448 basesink->priv->have_latency = TRUE;
1449 GST_OBJECT_UNLOCK (basesink);
1454 /* app is going to READY */
1455 GST_DEBUG_OBJECT (basesink, "stopping");
1456 basesink->need_preroll = FALSE;
1457 basesink->flushing = TRUE;
1458 GST_OBJECT_UNLOCK (basesink);
1464 start_stepping (GstBaseSink * sink, GstSegment * segment,
1465 GstStepInfo * pending, GstStepInfo * current)
1468 GstMessage *message;
1470 GST_DEBUG_OBJECT (sink, "update pending step");
1472 GST_OBJECT_LOCK (sink);
1473 memcpy (current, pending, sizeof (GstStepInfo));
1474 pending->valid = FALSE;
1475 GST_OBJECT_UNLOCK (sink);
1477 /* post message first */
1479 gst_message_new_step_start (GST_OBJECT (sink), TRUE, current->format,
1480 current->amount, current->rate, current->flush, current->intermediate);
1481 gst_message_set_seqnum (message, current->seqnum);
1482 gst_element_post_message (GST_ELEMENT (sink), message);
1484 /* get the running time of where we paused and remember it */
1485 current->start = gst_element_get_start_time (GST_ELEMENT_CAST (sink));
1486 gst_segment_set_running_time (segment, GST_FORMAT_TIME, current->start);
1488 /* set the new rate for the remainder of the segment */
1489 current->start_rate = segment->rate;
1490 segment->rate *= current->rate;
1493 if (segment->rate > 0.0)
1494 current->start_stop = segment->stop;
1496 current->start_start = segment->start;
1498 if (current->format == GST_FORMAT_TIME) {
1499 end = current->start + current->amount;
1500 if (!current->flush) {
1501 /* update the segment clipping regions for non-flushing seeks */
1502 if (segment->rate > 0.0) {
1503 segment->stop = gst_segment_to_position (segment, GST_FORMAT_TIME, end);
1504 segment->position = segment->stop;
1508 position = gst_segment_to_position (segment, GST_FORMAT_TIME, end);
1509 segment->time = position;
1510 segment->start = position;
1511 segment->position = position;
1516 GST_DEBUG_OBJECT (sink, "segment now %" GST_SEGMENT_FORMAT, segment);
1517 GST_DEBUG_OBJECT (sink, "step started at running_time %" GST_TIME_FORMAT,
1518 GST_TIME_ARGS (current->start));
1520 GST_DEBUG_OBJECT (sink, "step amount: %" G_GUINT64_FORMAT ", format: %s, "
1521 "rate: %f", current->amount, gst_format_get_name (current->format),
1526 stop_stepping (GstBaseSink * sink, GstSegment * segment,
1527 GstStepInfo * current, gint64 rstart, gint64 rstop, gboolean eos)
1529 gint64 stop, position;
1530 GstMessage *message;
1532 GST_DEBUG_OBJECT (sink, "step complete");
1534 if (segment->rate > 0.0)
1539 GST_DEBUG_OBJECT (sink,
1540 "step stop at running_time %" GST_TIME_FORMAT, GST_TIME_ARGS (stop));
1543 current->duration = current->position;
1545 current->duration = stop - current->start;
1547 GST_DEBUG_OBJECT (sink, "step elapsed running_time %" GST_TIME_FORMAT,
1548 GST_TIME_ARGS (current->duration));
1550 position = current->start + current->duration;
1552 /* now move the segment to the new running time */
1553 gst_segment_set_running_time (segment, GST_FORMAT_TIME, position);
1555 if (current->flush) {
1556 /* and remove the time we flushed, start time did not change */
1557 segment->base = current->start;
1559 /* start time is now the stepped position */
1560 gst_element_set_start_time (GST_ELEMENT_CAST (sink), position);
1563 /* restore the previous rate */
1564 segment->rate = current->start_rate;
1566 if (segment->rate > 0.0)
1567 segment->stop = current->start_stop;
1569 segment->start = current->start_start;
1571 /* post the step done when we know the stepped duration in TIME */
1573 gst_message_new_step_done (GST_OBJECT_CAST (sink), current->format,
1574 current->amount, current->rate, current->flush, current->intermediate,
1575 current->duration, eos);
1576 gst_message_set_seqnum (message, current->seqnum);
1577 gst_element_post_message (GST_ELEMENT_CAST (sink), message);
1579 if (!current->intermediate)
1580 sink->need_preroll = current->need_preroll;
1582 /* and the current step info finished and becomes invalid */
1583 current->valid = FALSE;
1587 handle_stepping (GstBaseSink * sink, GstSegment * segment,
1588 GstStepInfo * current, guint64 * cstart, guint64 * cstop, guint64 * rstart,
1591 gboolean step_end = FALSE;
1593 if (current->amount == -1)
1596 /* see if we need to skip this buffer because of stepping */
1597 switch (current->format) {
1598 case GST_FORMAT_TIME:
1601 guint64 first, last;
1604 if (segment->rate > 0.0) {
1605 if (segment->stop == *cstop)
1606 *rstop = *rstart + current->amount;
1611 if (segment->start == *cstart)
1612 *rstart = *rstop + current->amount;
1618 end = current->start + current->amount;
1619 current->position = first - current->start;
1621 abs_rate = ABS (segment->rate);
1622 if (G_UNLIKELY (abs_rate != 1.0))
1623 current->position /= abs_rate;
1625 GST_DEBUG_OBJECT (sink,
1626 "buffer: %" GST_TIME_FORMAT "-%" GST_TIME_FORMAT,
1627 GST_TIME_ARGS (first), GST_TIME_ARGS (last));
1628 GST_DEBUG_OBJECT (sink,
1629 "got time step %" GST_TIME_FORMAT "-%" GST_TIME_FORMAT "/%"
1630 GST_TIME_FORMAT, GST_TIME_ARGS (current->position),
1631 GST_TIME_ARGS (last - current->start),
1632 GST_TIME_ARGS (current->amount));
1634 if ((current->flush && current->position >= current->amount)
1636 GST_DEBUG_OBJECT (sink, "step ended, we need clipping");
1638 if (segment->rate > 0.0) {
1640 *cstart = gst_segment_to_position (segment, GST_FORMAT_TIME, end);
1643 *cstop = gst_segment_to_position (segment, GST_FORMAT_TIME, end);
1646 GST_DEBUG_OBJECT (sink,
1647 "cstart %" GST_TIME_FORMAT ", rstart %" GST_TIME_FORMAT,
1648 GST_TIME_ARGS (*cstart), GST_TIME_ARGS (*rstart));
1649 GST_DEBUG_OBJECT (sink,
1650 "cstop %" GST_TIME_FORMAT ", rstop %" GST_TIME_FORMAT,
1651 GST_TIME_ARGS (*cstop), GST_TIME_ARGS (*rstop));
1654 case GST_FORMAT_BUFFERS:
1655 GST_DEBUG_OBJECT (sink,
1656 "got default step %" G_GUINT64_FORMAT "/%" G_GUINT64_FORMAT,
1657 current->position, current->amount);
1659 if (current->position < current->amount) {
1660 current->position++;
1665 case GST_FORMAT_DEFAULT:
1667 GST_DEBUG_OBJECT (sink,
1668 "got unknown step %" G_GUINT64_FORMAT "/%" G_GUINT64_FORMAT,
1669 current->position, current->amount);
1675 /* with STREAM_LOCK, PREROLL_LOCK
1677 * Returns TRUE if the object needs synchronisation and takes therefore
1678 * part in prerolling.
1680 * rsstart/rsstop contain the start/stop in stream time.
1681 * rrstart/rrstop contain the start/stop in running time.
1684 gst_base_sink_get_sync_times (GstBaseSink * basesink, GstMiniObject * obj,
1685 GstClockTime * rsstart, GstClockTime * rsstop,
1686 GstClockTime * rrstart, GstClockTime * rrstop, gboolean * do_sync,
1687 gboolean * stepped, GstStepInfo * step, gboolean * step_end)
1689 GstBaseSinkClass *bclass;
1690 GstClockTime start, stop; /* raw start/stop timestamps */
1691 guint64 cstart, cstop; /* clipped raw timestamps */
1692 guint64 rstart, rstop; /* clipped timestamps converted to running time */
1693 GstClockTime sstart, sstop; /* clipped timestamps converted to stream time */
1695 GstBaseSinkPrivate *priv;
1696 GstSegment *segment;
1699 priv = basesink->priv;
1700 segment = &basesink->segment;
1702 bclass = GST_BASE_SINK_GET_CLASS (basesink);
1705 /* start with nothing */
1706 start = stop = GST_CLOCK_TIME_NONE;
1709 if (G_UNLIKELY (GST_IS_EVENT (obj))) {
1710 GstEvent *event = GST_EVENT_CAST (obj);
1712 switch (GST_EVENT_TYPE (event)) {
1713 /* EOS event needs syncing */
1716 if (segment->rate >= 0.0) {
1717 sstart = sstop = priv->current_sstop;
1718 if (!GST_CLOCK_TIME_IS_VALID (sstart)) {
1719 /* we have not seen a buffer yet, use the segment values */
1720 sstart = sstop = gst_segment_to_stream_time (segment,
1721 segment->format, segment->stop);
1724 sstart = sstop = priv->current_sstart;
1725 if (!GST_CLOCK_TIME_IS_VALID (sstart)) {
1726 /* we have not seen a buffer yet, use the segment values */
1727 sstart = sstop = gst_segment_to_stream_time (segment,
1728 segment->format, segment->start);
1732 rstart = rstop = priv->eos_rtime;
1733 *do_sync = rstart != -1;
1734 GST_DEBUG_OBJECT (basesink, "sync times for EOS %" GST_TIME_FORMAT,
1735 GST_TIME_ARGS (rstart));
1736 /* if we are stepping, we end now */
1737 *step_end = step->valid;
1743 GstClockTime timestamp, duration;
1744 gst_event_parse_gap (event, ×tamp, &duration);
1746 if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
1748 if (GST_CLOCK_TIME_IS_VALID (duration))
1749 stop = start + duration;
1755 /* other events do not need syncing */
1759 /* else do buffer sync code */
1760 GstBuffer *buffer = GST_BUFFER_CAST (obj);
1762 /* just get the times to see if we need syncing, if the start returns -1 we
1764 if (bclass->get_times)
1765 bclass->get_times (basesink, buffer, &start, &stop);
1767 if (!GST_CLOCK_TIME_IS_VALID (start)) {
1768 /* we don't need to sync but we still want to get the timestamps for
1769 * tracking the position */
1770 gst_base_sink_default_get_times (basesink, buffer, &start, &stop);
1777 GST_DEBUG_OBJECT (basesink, "got times start: %" GST_TIME_FORMAT
1778 ", stop: %" GST_TIME_FORMAT ", do_sync %d", GST_TIME_ARGS (start),
1779 GST_TIME_ARGS (stop), *do_sync);
1781 /* collect segment and format for code clarity */
1782 format = segment->format;
1785 if (G_UNLIKELY (!gst_segment_clip (segment, format,
1786 start, stop, &cstart, &cstop))) {
1788 GST_DEBUG_OBJECT (basesink, "step out of segment");
1789 /* when we are stepping, pretend we're at the end of the segment */
1790 if (segment->rate > 0.0) {
1791 cstart = segment->stop;
1792 cstop = segment->stop;
1794 cstart = segment->start;
1795 cstop = segment->start;
1799 goto out_of_segment;
1802 if (G_UNLIKELY (start != cstart || stop != cstop)) {
1803 GST_DEBUG_OBJECT (basesink, "clipped to: start %" GST_TIME_FORMAT
1804 ", stop: %" GST_TIME_FORMAT, GST_TIME_ARGS (cstart),
1805 GST_TIME_ARGS (cstop));
1808 /* set last stop position */
1809 if (G_LIKELY (stop != GST_CLOCK_TIME_NONE && cstop != GST_CLOCK_TIME_NONE))
1810 segment->position = cstop;
1812 segment->position = cstart;
1815 rstart = gst_segment_to_running_time (segment, format, cstart);
1816 rstop = gst_segment_to_running_time (segment, format, cstop);
1818 if (G_UNLIKELY (step->valid)) {
1819 if (!(*step_end = handle_stepping (basesink, segment, step, &cstart, &cstop,
1820 &rstart, &rstop))) {
1821 /* step is still busy, we discard data when we are flushing */
1822 *stepped = step->flush;
1823 GST_DEBUG_OBJECT (basesink, "stepping busy");
1826 /* this can produce wrong values if we accumulated non-TIME segments. If this happens,
1827 * upstream is behaving very badly */
1828 sstart = gst_segment_to_stream_time (segment, format, cstart);
1829 sstop = gst_segment_to_stream_time (segment, format, cstop);
1832 /* eos_done label only called when doing EOS, we also stop stepping then */
1833 if (*step_end && step->flush) {
1834 GST_DEBUG_OBJECT (basesink, "flushing step ended");
1835 stop_stepping (basesink, segment, step, rstart, rstop, eos);
1837 /* re-determine running start times for adjusted segment
1838 * (which has a flushed amount of running/accumulated time removed) */
1839 if (!GST_IS_EVENT (obj)) {
1840 GST_DEBUG_OBJECT (basesink, "refresh sync times");
1851 /* buffers and EOS always need syncing and preroll */
1857 /* we usually clip in the chain function already but stepping could cause
1858 * the segment to be updated later. we return FALSE so that we don't try
1860 GST_LOG_OBJECT (basesink, "buffer skipped, not in segment");
1865 /* with STREAM_LOCK, PREROLL_LOCK, LOCK
1866 * adjust a timestamp with the latency and timestamp offset. This function does
1867 * not adjust for the render delay. */
1869 gst_base_sink_adjust_time (GstBaseSink * basesink, GstClockTime time)
1871 GstClockTimeDiff ts_offset;
1873 /* don't do anything funny with invalid timestamps */
1874 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (time)))
1877 time += basesink->priv->latency;
1879 /* apply offset, be carefull for underflows */
1880 ts_offset = basesink->priv->ts_offset;
1881 if (ts_offset < 0) {
1882 ts_offset = -ts_offset;
1883 if (ts_offset < time)
1890 /* subtract the render delay again, which was included in the latency */
1891 if (time > basesink->priv->render_delay)
1892 time -= basesink->priv->render_delay;
1900 * gst_base_sink_wait_clock:
1902 * @time: the running_time to be reached
1903 * @jitter: (out) (allow-none): the jitter to be filled with time diff, or NULL
1905 * This function will block until @time is reached. It is usually called by
1906 * subclasses that use their own internal synchronisation.
1908 * If @time is not valid, no sycnhronisation is done and #GST_CLOCK_BADTIME is
1909 * returned. Likewise, if synchronisation is disabled in the element or there
1910 * is no clock, no synchronisation is done and #GST_CLOCK_BADTIME is returned.
1912 * This function should only be called with the PREROLL_LOCK held, like when
1913 * receiving an EOS event in the #GstBaseSinkClass.event() vmethod or when
1914 * receiving a buffer in
1915 * the #GstBaseSinkClass.render() vmethod.
1917 * The @time argument should be the running_time of when this method should
1918 * return and is not adjusted with any latency or offset configured in the
1921 * Returns: #GstClockReturn
1924 gst_base_sink_wait_clock (GstBaseSink * sink, GstClockTime time,
1925 GstClockTimeDiff * jitter)
1929 GstClockTime base_time;
1931 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (time)))
1934 GST_OBJECT_LOCK (sink);
1935 if (G_UNLIKELY (!sink->sync))
1938 if (G_UNLIKELY ((clock = GST_ELEMENT_CLOCK (sink)) == NULL))
1941 base_time = GST_ELEMENT_CAST (sink)->base_time;
1942 GST_LOG_OBJECT (sink,
1943 "time %" GST_TIME_FORMAT ", base_time %" GST_TIME_FORMAT,
1944 GST_TIME_ARGS (time), GST_TIME_ARGS (base_time));
1946 /* add base_time to running_time to get the time against the clock */
1949 /* Re-use existing clockid if available */
1950 /* FIXME: Casting to GstClockEntry only works because the types
1952 if (G_LIKELY (sink->priv->cached_clock_id != NULL
1953 && GST_CLOCK_ENTRY_CLOCK ((GstClockEntry *) sink->
1954 priv->cached_clock_id) == clock)) {
1955 if (!gst_clock_single_shot_id_reinit (clock, sink->priv->cached_clock_id,
1957 gst_clock_id_unref (sink->priv->cached_clock_id);
1958 sink->priv->cached_clock_id = gst_clock_new_single_shot_id (clock, time);
1961 if (sink->priv->cached_clock_id != NULL)
1962 gst_clock_id_unref (sink->priv->cached_clock_id);
1963 sink->priv->cached_clock_id = gst_clock_new_single_shot_id (clock, time);
1965 GST_OBJECT_UNLOCK (sink);
1967 /* A blocking wait is performed on the clock. We save the ClockID
1968 * so we can unlock the entry at any time. While we are blocking, we
1969 * release the PREROLL_LOCK so that other threads can interrupt the
1971 sink->clock_id = sink->priv->cached_clock_id;
1972 /* release the preroll lock while waiting */
1973 GST_BASE_SINK_PREROLL_UNLOCK (sink);
1975 ret = gst_clock_id_wait (sink->priv->cached_clock_id, jitter);
1977 GST_BASE_SINK_PREROLL_LOCK (sink);
1978 sink->clock_id = NULL;
1982 /* no syncing needed */
1985 GST_DEBUG_OBJECT (sink, "time not valid, no sync needed");
1986 return GST_CLOCK_BADTIME;
1990 GST_DEBUG_OBJECT (sink, "sync disabled");
1991 GST_OBJECT_UNLOCK (sink);
1992 return GST_CLOCK_BADTIME;
1996 GST_DEBUG_OBJECT (sink, "no clock, can't sync");
1997 GST_OBJECT_UNLOCK (sink);
1998 return GST_CLOCK_BADTIME;
2003 * gst_base_sink_wait_preroll:
2006 * If the #GstBaseSinkClass.render() method performs its own synchronisation
2007 * against the clock it must unblock when going from PLAYING to the PAUSED state
2008 * and call this method before continuing to render the remaining data.
2010 * This function will block until a state change to PLAYING happens (in which
2011 * case this function returns #GST_FLOW_OK) or the processing must be stopped due
2012 * to a state change to READY or a FLUSH event (in which case this function
2013 * returns #GST_FLOW_FLUSHING).
2015 * This function should only be called with the PREROLL_LOCK held, like in the
2018 * Returns: #GST_FLOW_OK if the preroll completed and processing can
2019 * continue. Any other return value should be returned from the render vmethod.
2022 gst_base_sink_wait_preroll (GstBaseSink * sink)
2024 sink->have_preroll = TRUE;
2025 GST_DEBUG_OBJECT (sink, "waiting in preroll for flush or PLAYING");
2026 /* block until the state changes, or we get a flush, or something */
2027 GST_BASE_SINK_PREROLL_WAIT (sink);
2028 sink->have_preroll = FALSE;
2029 if (G_UNLIKELY (sink->flushing))
2031 if (G_UNLIKELY (sink->priv->step_unlock))
2033 GST_DEBUG_OBJECT (sink, "continue after preroll");
2040 GST_DEBUG_OBJECT (sink, "preroll interrupted because of flush");
2041 return GST_FLOW_FLUSHING;
2045 sink->priv->step_unlock = FALSE;
2046 GST_DEBUG_OBJECT (sink, "preroll interrupted because of step");
2047 return GST_FLOW_STEP;
2052 * gst_base_sink_do_preroll:
2054 * @obj: (transfer none): the mini object that caused the preroll
2056 * If the @sink spawns its own thread for pulling buffers from upstream it
2057 * should call this method after it has pulled a buffer. If the element needed
2058 * to preroll, this function will perform the preroll and will then block
2059 * until the element state is changed.
2061 * This function should be called with the PREROLL_LOCK held.
2063 * Returns: #GST_FLOW_OK if the preroll completed and processing can
2064 * continue. Any other return value should be returned from the render vmethod.
2067 gst_base_sink_do_preroll (GstBaseSink * sink, GstMiniObject * obj)
2071 while (G_UNLIKELY (sink->need_preroll)) {
2072 GST_DEBUG_OBJECT (sink, "prerolling object %p", obj);
2074 /* if it's a buffer, we need to call the preroll method */
2075 if (sink->priv->call_preroll) {
2076 GstBaseSinkClass *bclass;
2079 if (GST_IS_BUFFER_LIST (obj)) {
2080 buf = gst_buffer_list_get (GST_BUFFER_LIST_CAST (obj), 0);
2081 g_assert (NULL != buf);
2082 } else if (GST_IS_BUFFER (obj)) {
2083 buf = GST_BUFFER_CAST (obj);
2084 /* For buffer lists do not set last buffer for now */
2085 gst_base_sink_set_last_buffer (sink, buf);
2090 GST_DEBUG_OBJECT (sink, "preroll buffer %" GST_TIME_FORMAT,
2091 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)));
2093 bclass = GST_BASE_SINK_GET_CLASS (sink);
2095 if (bclass->prepare)
2096 if ((ret = bclass->prepare (sink, buf)) != GST_FLOW_OK)
2097 goto prepare_canceled;
2099 if (bclass->preroll)
2100 if ((ret = bclass->preroll (sink, buf)) != GST_FLOW_OK)
2101 goto preroll_canceled;
2103 sink->priv->call_preroll = FALSE;
2108 if (G_LIKELY (sink->playing_async)) {
2109 if (G_UNLIKELY (!gst_base_sink_commit_state (sink)))
2113 /* need to recheck here because the commit state could have
2114 * made us not need the preroll anymore */
2115 if (G_LIKELY (sink->need_preroll)) {
2116 /* block until the state changes, or we get a flush, or something */
2117 ret = gst_base_sink_wait_preroll (sink);
2118 if ((ret != GST_FLOW_OK) && (ret != GST_FLOW_STEP))
2119 goto preroll_failed;
2127 GST_DEBUG_OBJECT (sink, "prepare failed, abort state");
2128 gst_element_abort_state (GST_ELEMENT_CAST (sink));
2133 GST_DEBUG_OBJECT (sink, "preroll failed, abort state");
2134 gst_element_abort_state (GST_ELEMENT_CAST (sink));
2139 GST_DEBUG_OBJECT (sink, "stopping while commiting state");
2140 return GST_FLOW_FLUSHING;
2144 GST_DEBUG_OBJECT (sink, "preroll failed: %s", gst_flow_get_name (ret));
2150 * gst_base_sink_wait:
2152 * @time: the running_time to be reached
2153 * @jitter: (out) (allow-none): the jitter to be filled with time diff, or NULL
2155 * This function will wait for preroll to complete and will then block until @time
2156 * is reached. It is usually called by subclasses that use their own internal
2157 * synchronisation but want to let some synchronization (like EOS) be handled
2158 * by the base class.
2160 * This function should only be called with the PREROLL_LOCK held (like when
2161 * receiving an EOS event in the ::event vmethod or when handling buffers in
2164 * The @time argument should be the running_time of when the timeout should happen
2165 * and will be adjusted with any latency and offset configured in the sink.
2167 * Returns: #GstFlowReturn
2170 gst_base_sink_wait (GstBaseSink * sink, GstClockTime time,
2171 GstClockTimeDiff * jitter)
2173 GstClockReturn status;
2179 GST_DEBUG_OBJECT (sink, "checking preroll");
2181 /* first wait for the playing state before we can continue */
2182 while (G_UNLIKELY (sink->need_preroll)) {
2183 ret = gst_base_sink_wait_preroll (sink);
2184 if ((ret != GST_FLOW_OK) && (ret != GST_FLOW_STEP))
2188 /* preroll done, we can sync since we are in PLAYING now. */
2189 GST_DEBUG_OBJECT (sink, "possibly waiting for clock to reach %"
2190 GST_TIME_FORMAT, GST_TIME_ARGS (time));
2192 /* compensate for latency, ts_offset and render delay */
2193 stime = gst_base_sink_adjust_time (sink, time);
2195 /* wait for the clock, this can be interrupted because we got shut down or
2197 status = gst_base_sink_wait_clock (sink, stime, jitter);
2199 GST_DEBUG_OBJECT (sink, "clock returned %d", status);
2201 /* invalid time, no clock or sync disabled, just continue then */
2202 if (status == GST_CLOCK_BADTIME)
2205 /* waiting could have been interrupted and we can be flushing now */
2206 if (G_UNLIKELY (sink->flushing))
2209 /* retry if we got unscheduled, which means we did not reach the timeout
2210 * yet. if some other error occures, we continue. */
2211 } while (status == GST_CLOCK_UNSCHEDULED);
2213 GST_DEBUG_OBJECT (sink, "end of stream");
2220 GST_DEBUG_OBJECT (sink, "we are flushing");
2221 return GST_FLOW_FLUSHING;
2225 /* with STREAM_LOCK, PREROLL_LOCK
2227 * Make sure we are in PLAYING and synchronize an object to the clock.
2229 * If we need preroll, we are not in PLAYING. We try to commit the state
2230 * if needed and then block if we still are not PLAYING.
2232 * We start waiting on the clock in PLAYING. If we got interrupted, we
2233 * immediately try to re-preroll.
2235 * Some objects do not need synchronisation (most events) and so this function
2236 * immediately returns GST_FLOW_OK.
2238 * for objects that arrive later than max-lateness to be synchronized to the
2239 * clock have the @late boolean set to TRUE.
2241 * This function keeps a running average of the jitter (the diff between the
2242 * clock time and the requested sync time). The jitter is negative for
2243 * objects that arrive in time and positive for late buffers.
2245 * does not take ownership of obj.
2247 static GstFlowReturn
2248 gst_base_sink_do_sync (GstBaseSink * basesink,
2249 GstMiniObject * obj, gboolean * late, gboolean * step_end)
2251 GstClockTimeDiff jitter = 0;
2253 GstClockReturn status = GST_CLOCK_OK;
2254 GstClockTime rstart, rstop, sstart, sstop, stime;
2256 GstBaseSinkPrivate *priv;
2258 GstStepInfo *current, *pending;
2261 priv = basesink->priv;
2264 sstart = sstop = rstart = rstop = GST_CLOCK_TIME_NONE;
2268 priv->current_rstart = GST_CLOCK_TIME_NONE;
2270 /* get stepping info */
2271 current = &priv->current_step;
2272 pending = &priv->pending_step;
2274 /* get timing information for this object against the render segment */
2275 syncable = gst_base_sink_get_sync_times (basesink, obj,
2276 &sstart, &sstop, &rstart, &rstop, &do_sync, &stepped, current, step_end);
2278 if (G_UNLIKELY (stepped))
2281 /* a syncable object needs to participate in preroll and
2282 * clocking. All buffers and EOS are syncable. */
2283 if (G_UNLIKELY (!syncable))
2286 /* store timing info for current object */
2287 priv->current_rstart = rstart;
2288 priv->current_rstop = (GST_CLOCK_TIME_IS_VALID (rstop) ? rstop : rstart);
2290 /* save sync time for eos when the previous object needed sync */
2291 priv->eos_rtime = (do_sync ? priv->current_rstop : GST_CLOCK_TIME_NONE);
2293 /* calculate inter frame spacing */
2294 if (G_UNLIKELY (priv->prev_rstart != -1 && priv->prev_rstart < rstart)) {
2295 GstClockTime in_diff;
2297 in_diff = rstart - priv->prev_rstart;
2299 if (priv->avg_in_diff == -1)
2300 priv->avg_in_diff = in_diff;
2302 priv->avg_in_diff = UPDATE_RUNNING_AVG (priv->avg_in_diff, in_diff);
2304 GST_LOG_OBJECT (basesink, "avg frame diff %" GST_TIME_FORMAT,
2305 GST_TIME_ARGS (priv->avg_in_diff));
2308 priv->prev_rstart = rstart;
2310 if (G_UNLIKELY (priv->earliest_in_time != -1
2311 && rstart < priv->earliest_in_time))
2315 /* first do preroll, this makes sure we commit our state
2316 * to PAUSED and can continue to PLAYING. We cannot perform
2317 * any clock sync in PAUSED because there is no clock. */
2318 ret = gst_base_sink_do_preroll (basesink, obj);
2319 if (G_UNLIKELY (ret != GST_FLOW_OK))
2320 goto preroll_failed;
2322 /* update the segment with a pending step if the current one is invalid and we
2323 * have a new pending one. We only accept new step updates after a preroll */
2324 if (G_UNLIKELY (pending->valid && !current->valid)) {
2325 start_stepping (basesink, &basesink->segment, pending, current);
2329 /* After rendering we store the position of the last buffer so that we can use
2330 * it to report the position. We need to take the lock here. */
2331 GST_OBJECT_LOCK (basesink);
2332 priv->current_sstart = sstart;
2333 priv->current_sstop = (GST_CLOCK_TIME_IS_VALID (sstop) ? sstop : sstart);
2334 GST_OBJECT_UNLOCK (basesink);
2339 /* adjust for latency */
2340 stime = gst_base_sink_adjust_time (basesink, rstart);
2342 /* preroll done, we can sync since we are in PLAYING now. */
2343 GST_DEBUG_OBJECT (basesink, "possibly waiting for clock to reach %"
2344 GST_TIME_FORMAT ", adjusted %" GST_TIME_FORMAT,
2345 GST_TIME_ARGS (rstart), GST_TIME_ARGS (stime));
2347 /* This function will return immediately if start == -1, no clock
2348 * or sync is disabled with GST_CLOCK_BADTIME. */
2349 status = gst_base_sink_wait_clock (basesink, stime, &jitter);
2351 GST_DEBUG_OBJECT (basesink, "clock returned %d, jitter %c%" GST_TIME_FORMAT,
2352 status, (jitter < 0 ? '-' : ' '), GST_TIME_ARGS (ABS (jitter)));
2354 /* invalid time, no clock or sync disabled, just render */
2355 if (status == GST_CLOCK_BADTIME)
2358 /* waiting could have been interrupted and we can be flushing now */
2359 if (G_UNLIKELY (basesink->flushing))
2362 /* check for unlocked by a state change, we are not flushing so
2363 * we can try to preroll on the current buffer. */
2364 if (G_UNLIKELY (status == GST_CLOCK_UNSCHEDULED)) {
2365 GST_DEBUG_OBJECT (basesink, "unscheduled, waiting some more");
2366 priv->call_preroll = TRUE;
2370 /* successful syncing done, record observation */
2371 priv->current_jitter = jitter;
2373 /* check if the object should be dropped */
2374 *late = gst_base_sink_is_too_late (basesink, obj, rstart, rstop,
2383 GST_DEBUG_OBJECT (basesink, "skipped stepped object %p", obj);
2389 GST_DEBUG_OBJECT (basesink, "non syncable object %p", obj);
2394 GST_DEBUG_OBJECT (basesink, "dropped because of QoS %p", obj);
2400 GST_DEBUG_OBJECT (basesink, "we are flushing");
2401 return GST_FLOW_FLUSHING;
2405 GST_DEBUG_OBJECT (basesink, "preroll failed");
2412 gst_base_sink_send_qos (GstBaseSink * basesink, GstQOSType type,
2413 gdouble proportion, GstClockTime time, GstClockTimeDiff diff)
2418 /* generate Quality-of-Service event */
2419 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, basesink,
2420 "qos: type %d, proportion: %lf, diff %" G_GINT64_FORMAT ", timestamp %"
2421 GST_TIME_FORMAT, type, proportion, diff, GST_TIME_ARGS (time));
2423 event = gst_event_new_qos (type, proportion, diff, time);
2426 res = gst_pad_push_event (basesink->sinkpad, event);
2432 gst_base_sink_perform_qos (GstBaseSink * sink, gboolean dropped)
2434 GstBaseSinkPrivate *priv;
2435 GstClockTime start, stop;
2436 GstClockTimeDiff jitter;
2437 GstClockTime pt, entered, left;
2438 GstClockTime duration;
2443 start = priv->current_rstart;
2445 if (priv->current_step.valid)
2448 /* if Quality-of-Service disabled, do nothing */
2449 if (!g_atomic_int_get (&priv->qos_enabled) ||
2450 !GST_CLOCK_TIME_IS_VALID (start))
2453 stop = priv->current_rstop;
2454 jitter = priv->current_jitter;
2457 /* this is the time the buffer entered the sink */
2458 if (start < -jitter)
2461 entered = start + jitter;
2464 /* this is the time the buffer entered the sink */
2465 entered = start + jitter;
2466 /* this is the time the buffer left the sink */
2467 left = start + jitter;
2470 /* calculate duration of the buffer */
2471 if (GST_CLOCK_TIME_IS_VALID (stop) && stop != start)
2472 duration = stop - start;
2474 duration = priv->avg_in_diff;
2476 /* if we have the time when the last buffer left us, calculate
2477 * processing time */
2478 if (GST_CLOCK_TIME_IS_VALID (priv->last_left)) {
2479 if (entered > priv->last_left) {
2480 pt = entered - priv->last_left;
2488 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, sink, "start: %" GST_TIME_FORMAT
2489 ", stop %" GST_TIME_FORMAT ", entered %" GST_TIME_FORMAT ", left %"
2490 GST_TIME_FORMAT ", pt: %" GST_TIME_FORMAT ", duration %" GST_TIME_FORMAT
2491 ",jitter %" G_GINT64_FORMAT, GST_TIME_ARGS (start), GST_TIME_ARGS (stop),
2492 GST_TIME_ARGS (entered), GST_TIME_ARGS (left), GST_TIME_ARGS (pt),
2493 GST_TIME_ARGS (duration), jitter);
2495 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, sink, "avg_duration: %" GST_TIME_FORMAT
2496 ", avg_pt: %" GST_TIME_FORMAT ", avg_rate: %g",
2497 GST_TIME_ARGS (priv->avg_duration), GST_TIME_ARGS (priv->avg_pt),
2500 /* collect running averages. for first observations, we copy the
2502 if (!GST_CLOCK_TIME_IS_VALID (priv->avg_duration))
2503 priv->avg_duration = duration;
2505 priv->avg_duration = UPDATE_RUNNING_AVG (priv->avg_duration, duration);
2507 if (!GST_CLOCK_TIME_IS_VALID (priv->avg_pt))
2510 priv->avg_pt = UPDATE_RUNNING_AVG (priv->avg_pt, pt);
2512 if (priv->avg_duration != 0)
2514 gst_guint64_to_gdouble (priv->avg_pt) /
2515 gst_guint64_to_gdouble (priv->avg_duration);
2519 if (GST_CLOCK_TIME_IS_VALID (priv->last_left)) {
2520 if (dropped || priv->avg_rate < 0.0) {
2521 priv->avg_rate = rate;
2524 priv->avg_rate = UPDATE_RUNNING_AVG_N (priv->avg_rate, rate);
2526 priv->avg_rate = UPDATE_RUNNING_AVG_P (priv->avg_rate, rate);
2530 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, sink,
2531 "updated: avg_duration: %" GST_TIME_FORMAT ", avg_pt: %" GST_TIME_FORMAT
2532 ", avg_rate: %g", GST_TIME_ARGS (priv->avg_duration),
2533 GST_TIME_ARGS (priv->avg_pt), priv->avg_rate);
2536 if (priv->avg_rate >= 0.0) {
2538 GstClockTimeDiff diff;
2540 /* if we have a valid rate, start sending QoS messages */
2541 if (priv->current_jitter < 0) {
2542 /* make sure we never go below 0 when adding the jitter to the
2544 if (priv->current_rstart < -priv->current_jitter)
2545 priv->current_jitter = -priv->current_rstart;
2548 if (priv->throttle_time > 0) {
2549 diff = priv->throttle_time;
2550 type = GST_QOS_TYPE_THROTTLE;
2552 diff = priv->current_jitter;
2554 type = GST_QOS_TYPE_OVERFLOW;
2556 type = GST_QOS_TYPE_UNDERFLOW;
2559 gst_base_sink_send_qos (sink, type, priv->avg_rate, priv->current_rstart,
2563 /* record when this buffer will leave us */
2564 priv->last_left = left;
2567 /* reset all qos measuring */
2569 gst_base_sink_reset_qos (GstBaseSink * sink)
2571 GstBaseSinkPrivate *priv;
2575 priv->last_render_time = GST_CLOCK_TIME_NONE;
2576 priv->prev_rstart = GST_CLOCK_TIME_NONE;
2577 priv->earliest_in_time = GST_CLOCK_TIME_NONE;
2578 priv->last_left = GST_CLOCK_TIME_NONE;
2579 priv->avg_duration = GST_CLOCK_TIME_NONE;
2580 priv->avg_pt = GST_CLOCK_TIME_NONE;
2581 priv->avg_rate = -1.0;
2582 priv->avg_render = GST_CLOCK_TIME_NONE;
2583 priv->avg_in_diff = GST_CLOCK_TIME_NONE;
2589 /* Checks if the object was scheduled too late.
2591 * rstart/rstop contain the running_time start and stop values
2594 * status and jitter contain the return values from the clock wait.
2596 * returns TRUE if the buffer was too late.
2599 gst_base_sink_is_too_late (GstBaseSink * basesink, GstMiniObject * obj,
2600 GstClockTime rstart, GstClockTime rstop,
2601 GstClockReturn status, GstClockTimeDiff jitter)
2604 guint64 max_lateness;
2605 GstBaseSinkPrivate *priv;
2607 priv = basesink->priv;
2611 /* only for objects that were too late */
2612 if (G_LIKELY (status != GST_CLOCK_EARLY))
2615 max_lateness = basesink->max_lateness;
2617 /* check if frame dropping is enabled */
2618 if (max_lateness == -1)
2621 /* only check for buffers */
2622 if (G_UNLIKELY (!GST_IS_BUFFER (obj)))
2625 /* can't do check if we don't have a timestamp */
2626 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (rstart)))
2629 /* we can add a valid stop time */
2630 if (GST_CLOCK_TIME_IS_VALID (rstop))
2631 max_lateness += rstop;
2633 max_lateness += rstart;
2634 /* no stop time, use avg frame diff */
2635 if (priv->avg_in_diff != -1)
2636 max_lateness += priv->avg_in_diff;
2639 /* if the jitter bigger than duration and lateness we are too late */
2640 if ((late = rstart + jitter > max_lateness)) {
2641 GST_CAT_DEBUG_OBJECT (GST_CAT_PERFORMANCE, basesink,
2642 "buffer is too late %" GST_TIME_FORMAT
2643 " > %" GST_TIME_FORMAT, GST_TIME_ARGS (rstart + jitter),
2644 GST_TIME_ARGS (max_lateness));
2645 /* !!emergency!!, if we did not receive anything valid for more than a
2646 * second, render it anyway so the user sees something */
2647 if (GST_CLOCK_TIME_IS_VALID (priv->last_render_time) &&
2648 rstart - priv->last_render_time > GST_SECOND) {
2650 GST_ELEMENT_WARNING (basesink, CORE, CLOCK,
2651 (_("A lot of buffers are being dropped.")),
2652 ("There may be a timestamping problem, or this computer is too slow."));
2653 GST_CAT_DEBUG_OBJECT (GST_CAT_PERFORMANCE, basesink,
2654 "**emergency** last buffer at %" GST_TIME_FORMAT " > GST_SECOND",
2655 GST_TIME_ARGS (priv->last_render_time));
2660 if (!late || !GST_CLOCK_TIME_IS_VALID (priv->last_render_time)) {
2661 priv->last_render_time = rstart;
2662 /* the next allowed input timestamp */
2663 if (priv->throttle_time > 0)
2664 priv->earliest_in_time = rstart + priv->throttle_time;
2671 GST_DEBUG_OBJECT (basesink, "object was scheduled in time");
2676 GST_DEBUG_OBJECT (basesink, "frame dropping disabled");
2681 GST_DEBUG_OBJECT (basesink, "object is not a buffer");
2686 GST_DEBUG_OBJECT (basesink, "buffer has no timestamp");
2691 /* called before and after calling the render vmethod. It keeps track of how
2692 * much time was spent in the render method and is used to check if we are
2695 gst_base_sink_do_render_stats (GstBaseSink * basesink, gboolean start)
2697 GstBaseSinkPrivate *priv;
2699 priv = basesink->priv;
2702 priv->start = gst_util_get_timestamp ();
2704 GstClockTime elapsed;
2706 priv->stop = gst_util_get_timestamp ();
2708 elapsed = GST_CLOCK_DIFF (priv->start, priv->stop);
2710 if (!GST_CLOCK_TIME_IS_VALID (priv->avg_render))
2711 priv->avg_render = elapsed;
2713 priv->avg_render = UPDATE_RUNNING_AVG (priv->avg_render, elapsed);
2715 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, basesink,
2716 "avg_render: %" GST_TIME_FORMAT, GST_TIME_ARGS (priv->avg_render));
2721 gst_base_sink_flush_start (GstBaseSink * basesink, GstPad * pad)
2723 /* make sure we are not blocked on the clock also clear any pending
2725 gst_base_sink_set_flushing (basesink, pad, TRUE);
2727 /* we grab the stream lock but that is not needed since setting the
2728 * sink to flushing would make sure no state commit is being done
2730 GST_PAD_STREAM_LOCK (pad);
2731 gst_base_sink_reset_qos (basesink);
2732 /* and we need to commit our state again on the next
2733 * prerolled buffer */
2734 basesink->playing_async = TRUE;
2735 if (basesink->priv->async_enabled) {
2736 gst_element_lost_state (GST_ELEMENT_CAST (basesink));
2738 /* start time reset in above case as well;
2739 * arranges for a.o. proper position reporting when flushing in PAUSED */
2740 gst_element_set_start_time (GST_ELEMENT_CAST (basesink), 0);
2741 basesink->priv->have_latency = TRUE;
2743 gst_base_sink_set_last_buffer (basesink, NULL);
2744 GST_PAD_STREAM_UNLOCK (pad);
2748 gst_base_sink_flush_stop (GstBaseSink * basesink, GstPad * pad,
2749 gboolean reset_time)
2751 /* unset flushing so we can accept new data, this also flushes out any EOS
2753 gst_base_sink_set_flushing (basesink, pad, FALSE);
2755 /* for position reporting */
2756 GST_OBJECT_LOCK (basesink);
2757 basesink->priv->current_sstart = GST_CLOCK_TIME_NONE;
2758 basesink->priv->current_sstop = GST_CLOCK_TIME_NONE;
2759 basesink->priv->eos_rtime = GST_CLOCK_TIME_NONE;
2760 basesink->priv->call_preroll = TRUE;
2761 basesink->priv->current_step.valid = FALSE;
2762 basesink->priv->pending_step.valid = FALSE;
2763 if (basesink->pad_mode == GST_PAD_MODE_PUSH) {
2764 /* we need new segment info after the flush. */
2765 basesink->have_newsegment = FALSE;
2767 gst_segment_init (&basesink->segment, GST_FORMAT_UNDEFINED);
2770 GST_OBJECT_UNLOCK (basesink);
2773 GST_DEBUG_OBJECT (basesink, "posting reset-time message");
2774 gst_element_post_message (GST_ELEMENT_CAST (basesink),
2775 gst_message_new_reset_time (GST_OBJECT_CAST (basesink), 0));
2779 static GstFlowReturn
2780 gst_base_sink_default_wait_eos (GstBaseSink * basesink, GstEvent * event)
2783 gboolean late, step_end;
2785 ret = gst_base_sink_do_sync (basesink, GST_MINI_OBJECT_CAST (event),
2792 gst_base_sink_default_event (GstBaseSink * basesink, GstEvent * event)
2794 gboolean result = TRUE;
2795 GstBaseSinkClass *bclass;
2797 bclass = GST_BASE_SINK_GET_CLASS (basesink);
2799 switch (GST_EVENT_TYPE (event)) {
2800 case GST_EVENT_FLUSH_START:
2802 GST_DEBUG_OBJECT (basesink, "flush-start %p", event);
2803 gst_base_sink_flush_start (basesink, basesink->sinkpad);
2806 case GST_EVENT_FLUSH_STOP:
2808 gboolean reset_time;
2810 gst_event_parse_flush_stop (event, &reset_time);
2811 GST_DEBUG_OBJECT (basesink, "flush-stop %p, reset_time: %d", event,
2813 gst_base_sink_flush_stop (basesink, basesink->sinkpad, reset_time);
2818 GstMessage *message;
2821 /* we set the received EOS flag here so that we can use it when testing if
2822 * we are prerolled and to refuse more buffers. */
2823 basesink->priv->received_eos = TRUE;
2826 if (G_LIKELY (bclass->wait_eos)) {
2829 ret = bclass->wait_eos (basesink, event);
2830 if (G_UNLIKELY (ret != GST_FLOW_OK)) {
2836 /* the EOS event is completely handled so we mark
2837 * ourselves as being in the EOS state. eos is also
2838 * protected by the object lock so we can read it when
2839 * answering the POSITION query. */
2840 GST_OBJECT_LOCK (basesink);
2841 basesink->eos = TRUE;
2842 GST_OBJECT_UNLOCK (basesink);
2844 /* ok, now we can post the message */
2845 GST_DEBUG_OBJECT (basesink, "Now posting EOS");
2847 seqnum = basesink->priv->seqnum = gst_event_get_seqnum (event);
2848 GST_DEBUG_OBJECT (basesink, "Got seqnum #%" G_GUINT32_FORMAT, seqnum);
2850 message = gst_message_new_eos (GST_OBJECT_CAST (basesink));
2851 gst_message_set_seqnum (message, seqnum);
2852 gst_element_post_message (GST_ELEMENT_CAST (basesink), message);
2855 case GST_EVENT_STREAM_START:
2857 GstMessage *message;
2860 seqnum = gst_event_get_seqnum (event);
2861 GST_DEBUG_OBJECT (basesink, "Now posting STREAM_START (seqnum:%d)",
2863 message = gst_message_new_stream_start (GST_OBJECT_CAST (basesink));
2864 gst_message_set_seqnum (message, seqnum);
2865 gst_element_post_message (GST_ELEMENT_CAST (basesink), message);
2868 case GST_EVENT_CAPS:
2872 GST_DEBUG_OBJECT (basesink, "caps %p", event);
2874 gst_event_parse_caps (event, &caps);
2875 if (bclass->set_caps)
2876 result = bclass->set_caps (basesink, caps);
2879 GST_OBJECT_LOCK (basesink);
2880 gst_caps_replace (&basesink->priv->caps, caps);
2881 GST_OBJECT_UNLOCK (basesink);
2885 case GST_EVENT_SEGMENT:
2886 /* configure the segment */
2887 /* The segment is protected with both the STREAM_LOCK and the OBJECT_LOCK.
2888 * We protect with the OBJECT_LOCK so that we can use the values to
2889 * safely answer a POSITION query. */
2890 GST_OBJECT_LOCK (basesink);
2891 /* the newsegment event is needed to bring the buffer timestamps to the
2892 * stream time and to drop samples outside of the playback segment. */
2893 gst_event_copy_segment (event, &basesink->segment);
2894 GST_DEBUG_OBJECT (basesink, "configured SEGMENT %" GST_SEGMENT_FORMAT,
2895 &basesink->segment);
2896 basesink->have_newsegment = TRUE;
2897 GST_OBJECT_UNLOCK (basesink);
2901 GstTagList *taglist;
2903 gst_event_parse_tag (event, &taglist);
2905 gst_element_post_message (GST_ELEMENT_CAST (basesink),
2906 gst_message_new_tag (GST_OBJECT_CAST (basesink),
2907 gst_tag_list_copy (taglist)));
2915 gst_event_parse_toc (event, &toc, &updated);
2917 gst_element_post_message (GST_ELEMENT_CAST (basesink),
2918 gst_message_new_toc (GST_OBJECT_CAST (basesink), toc, updated));
2920 gst_toc_unref (toc);
2923 case GST_EVENT_SINK_MESSAGE:
2925 GstMessage *msg = NULL;
2927 gst_event_parse_sink_message (event, &msg);
2929 gst_element_post_message (GST_ELEMENT_CAST (basesink), msg);
2936 gst_event_unref (event);
2942 gst_base_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
2944 GstBaseSink *basesink;
2945 gboolean result = TRUE;
2946 GstBaseSinkClass *bclass;
2948 basesink = GST_BASE_SINK_CAST (parent);
2949 bclass = GST_BASE_SINK_GET_CLASS (basesink);
2951 GST_DEBUG_OBJECT (basesink, "received event %p %" GST_PTR_FORMAT, event,
2954 switch (GST_EVENT_TYPE (event)) {
2955 case GST_EVENT_FLUSH_STOP:
2956 /* special case for this serialized event because we don't want to grab
2957 * the PREROLL lock or check if we were flushing */
2959 result = bclass->event (basesink, event);
2962 if (GST_EVENT_IS_SERIALIZED (event)) {
2963 GST_BASE_SINK_PREROLL_LOCK (basesink);
2964 if (G_UNLIKELY (basesink->flushing))
2967 if (G_UNLIKELY (basesink->priv->received_eos))
2971 result = bclass->event (basesink, event);
2973 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
2976 result = bclass->event (basesink, event);
2986 GST_DEBUG_OBJECT (basesink, "we are flushing");
2987 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
2988 gst_event_unref (event);
2995 GST_DEBUG_OBJECT (basesink, "Event received after EOS, dropping");
2996 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
2997 gst_event_unref (event);
3003 /* default implementation to calculate the start and end
3004 * timestamps on a buffer, subclasses can override
3007 gst_base_sink_default_get_times (GstBaseSink * basesink, GstBuffer * buffer,
3008 GstClockTime * start, GstClockTime * end)
3010 GstClockTime timestamp, duration;
3012 /* first sync on DTS, else use PTS */
3013 timestamp = GST_BUFFER_DTS (buffer);
3014 if (!GST_CLOCK_TIME_IS_VALID (timestamp))
3015 timestamp = GST_BUFFER_PTS (buffer);
3017 if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
3018 /* get duration to calculate end time */
3019 duration = GST_BUFFER_DURATION (buffer);
3020 if (GST_CLOCK_TIME_IS_VALID (duration)) {
3021 *end = timestamp + duration;
3027 /* must be called with PREROLL_LOCK */
3029 gst_base_sink_needs_preroll (GstBaseSink * basesink)
3031 gboolean is_prerolled, res;
3033 /* we have 2 cases where the PREROLL_LOCK is released:
3034 * 1) we are blocking in the PREROLL_LOCK and thus are prerolled.
3035 * 2) we are syncing on the clock
3037 is_prerolled = basesink->have_preroll || basesink->priv->received_eos;
3038 res = !is_prerolled;
3040 GST_DEBUG_OBJECT (basesink, "have_preroll: %d, EOS: %d => needs preroll: %d",
3041 basesink->have_preroll, basesink->priv->received_eos, res);
3046 /* with STREAM_LOCK, PREROLL_LOCK
3048 * Takes a buffer and compare the timestamps with the last segment.
3049 * If the buffer falls outside of the segment boundaries, drop it.
3050 * Else send the buffer for preroll and rendering.
3052 * This function takes ownership of the buffer.
3054 static GstFlowReturn
3055 gst_base_sink_chain_unlocked (GstBaseSink * basesink, GstPad * pad,
3056 gpointer obj, gboolean is_list)
3058 GstBaseSinkClass *bclass;
3059 GstBaseSinkPrivate *priv = basesink->priv;
3061 GstClockTime start = GST_CLOCK_TIME_NONE, end = GST_CLOCK_TIME_NONE;
3062 GstSegment *segment;
3063 GstBuffer *sync_buf;
3065 gboolean late, step_end;
3067 if (G_UNLIKELY (basesink->flushing))
3070 if (G_UNLIKELY (priv->received_eos))
3074 sync_buf = gst_buffer_list_get (GST_BUFFER_LIST_CAST (obj), 0);
3075 g_assert (NULL != sync_buf);
3077 sync_buf = GST_BUFFER_CAST (obj);
3080 /* for code clarity */
3081 segment = &basesink->segment;
3083 if (G_UNLIKELY (!basesink->have_newsegment)) {
3086 sync = gst_base_sink_get_sync (basesink);
3088 GST_ELEMENT_WARNING (basesink, STREAM, FAILED,
3089 (_("Internal data flow problem.")),
3090 ("Received buffer without a new-segment. Assuming timestamps start from 0."));
3093 /* this means this sink will assume timestamps start from 0 */
3094 GST_OBJECT_LOCK (basesink);
3097 basesink->segment.start = 0;
3098 basesink->segment.stop = -1;
3099 basesink->have_newsegment = TRUE;
3100 GST_OBJECT_UNLOCK (basesink);
3103 bclass = GST_BASE_SINK_GET_CLASS (basesink);
3105 /* check if the buffer needs to be dropped, we first ask the subclass for the
3107 if (bclass->get_times)
3108 bclass->get_times (basesink, sync_buf, &start, &end);
3110 if (!GST_CLOCK_TIME_IS_VALID (start)) {
3111 /* if the subclass does not want sync, we use our own values so that we at
3112 * least clip the buffer to the segment */
3113 gst_base_sink_default_get_times (basesink, sync_buf, &start, &end);
3116 GST_DEBUG_OBJECT (basesink, "got times start: %" GST_TIME_FORMAT
3117 ", end: %" GST_TIME_FORMAT, GST_TIME_ARGS (start), GST_TIME_ARGS (end));
3119 /* a dropped buffer does not participate in anything */
3120 if (GST_CLOCK_TIME_IS_VALID (start) && (segment->format == GST_FORMAT_TIME)) {
3121 if (G_UNLIKELY (!gst_segment_clip (segment,
3122 GST_FORMAT_TIME, start, end, NULL, NULL)))
3123 goto out_of_segment;
3127 if (bclass->prepare) {
3128 ret = bclass->prepare (basesink, GST_BUFFER_CAST (obj));
3129 if (G_UNLIKELY (ret != GST_FLOW_OK))
3130 goto prepare_failed;
3133 if (bclass->prepare_list) {
3134 ret = bclass->prepare_list (basesink, GST_BUFFER_LIST_CAST (obj));
3135 if (G_UNLIKELY (ret != GST_FLOW_OK))
3136 goto prepare_failed;
3144 /* synchronize this object, non syncable objects return OK
3146 ret = gst_base_sink_do_sync (basesink, GST_MINI_OBJECT_CAST (sync_buf),
3148 if (G_UNLIKELY (ret != GST_FLOW_OK))
3151 /* drop late buffers unconditionally, let's hope it's unlikely */
3152 if (G_UNLIKELY (late))
3155 /* read once, to get same value before and after */
3156 do_qos = g_atomic_int_get (&priv->qos_enabled);
3158 GST_DEBUG_OBJECT (basesink, "rendering object %p", obj);
3160 /* record rendering time for QoS and stats */
3162 gst_base_sink_do_render_stats (basesink, TRUE);
3165 /* For buffer lists do not set last buffer for now. */
3166 gst_base_sink_set_last_buffer (basesink, GST_BUFFER_CAST (obj));
3169 ret = bclass->render (basesink, GST_BUFFER_CAST (obj));
3171 if (bclass->render_list)
3172 ret = bclass->render_list (basesink, GST_BUFFER_LIST_CAST (obj));
3176 gst_base_sink_do_render_stats (basesink, FALSE);
3178 if (ret == GST_FLOW_STEP)
3181 if (G_UNLIKELY (basesink->flushing))
3188 /* the step ended, check if we need to activate a new step */
3189 GST_DEBUG_OBJECT (basesink, "step ended");
3190 stop_stepping (basesink, &basesink->segment, &priv->current_step,
3191 priv->current_rstart, priv->current_rstop, basesink->eos);
3195 gst_base_sink_perform_qos (basesink, late);
3197 GST_DEBUG_OBJECT (basesink, "object unref after render %p", obj);
3198 gst_mini_object_unref (GST_MINI_OBJECT_CAST (obj));
3205 GST_DEBUG_OBJECT (basesink, "sink is flushing");
3206 gst_mini_object_unref (GST_MINI_OBJECT_CAST (obj));
3207 return GST_FLOW_FLUSHING;
3211 GST_DEBUG_OBJECT (basesink, "we are EOS, dropping object, return EOS");
3212 gst_mini_object_unref (GST_MINI_OBJECT_CAST (obj));
3213 return GST_FLOW_EOS;
3217 GST_DEBUG_OBJECT (basesink, "dropping buffer, out of clipping segment");
3218 gst_mini_object_unref (GST_MINI_OBJECT_CAST (obj));
3223 GST_DEBUG_OBJECT (basesink, "prepare buffer failed %s",
3224 gst_flow_get_name (ret));
3225 gst_mini_object_unref (GST_MINI_OBJECT_CAST (obj));
3230 GST_DEBUG_OBJECT (basesink, "do_sync returned %s", gst_flow_get_name (ret));
3236 GST_DEBUG_OBJECT (basesink, "buffer late, dropping");
3238 if (g_atomic_int_get (&priv->qos_enabled)) {
3239 GstMessage *qos_msg;
3240 GstClockTime timestamp, duration;
3242 timestamp = GST_BUFFER_TIMESTAMP (GST_BUFFER_CAST (sync_buf));
3243 duration = GST_BUFFER_DURATION (GST_BUFFER_CAST (sync_buf));
3245 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, basesink,
3246 "qos: dropped buffer rt %" GST_TIME_FORMAT ", st %" GST_TIME_FORMAT
3247 ", ts %" GST_TIME_FORMAT ", dur %" GST_TIME_FORMAT,
3248 GST_TIME_ARGS (priv->current_rstart),
3249 GST_TIME_ARGS (priv->current_sstart), GST_TIME_ARGS (timestamp),
3250 GST_TIME_ARGS (duration));
3251 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, basesink,
3252 "qos: rendered %" G_GUINT64_FORMAT ", dropped %" G_GUINT64_FORMAT,
3253 priv->rendered, priv->dropped);
3256 gst_message_new_qos (GST_OBJECT_CAST (basesink), basesink->sync,
3257 priv->current_rstart, priv->current_sstart, timestamp, duration);
3258 gst_message_set_qos_values (qos_msg, priv->current_jitter, priv->avg_rate,
3260 gst_message_set_qos_stats (qos_msg, GST_FORMAT_BUFFERS, priv->rendered,
3262 gst_element_post_message (GST_ELEMENT_CAST (basesink), qos_msg);
3270 static GstFlowReturn
3271 gst_base_sink_chain_main (GstBaseSink * basesink, GstPad * pad, gpointer obj,
3274 GstFlowReturn result;
3276 if (G_UNLIKELY (basesink->pad_mode != GST_PAD_MODE_PUSH))
3279 GST_BASE_SINK_PREROLL_LOCK (basesink);
3280 result = gst_base_sink_chain_unlocked (basesink, pad, obj, is_list);
3281 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
3289 GST_OBJECT_LOCK (pad);
3290 GST_WARNING_OBJECT (basesink,
3291 "Push on pad %s:%s, but it was not activated in push mode",
3292 GST_DEBUG_PAD_NAME (pad));
3293 GST_OBJECT_UNLOCK (pad);
3294 gst_mini_object_unref (GST_MINI_OBJECT_CAST (obj));
3295 /* we don't post an error message this will signal to the peer
3296 * pushing that EOS is reached. */
3297 result = GST_FLOW_EOS;
3302 static GstFlowReturn
3303 gst_base_sink_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
3305 GstBaseSink *basesink;
3307 basesink = GST_BASE_SINK (parent);
3309 return gst_base_sink_chain_main (basesink, pad, buf, FALSE);
3312 static GstFlowReturn
3313 gst_base_sink_chain_list (GstPad * pad, GstObject * parent,
3314 GstBufferList * list)
3316 GstBaseSink *basesink;
3317 GstBaseSinkClass *bclass;
3318 GstFlowReturn result;
3320 basesink = GST_BASE_SINK (parent);
3321 bclass = GST_BASE_SINK_GET_CLASS (basesink);
3323 if (G_LIKELY (bclass->render_list)) {
3324 result = gst_base_sink_chain_main (basesink, pad, list, TRUE);
3329 GST_INFO_OBJECT (pad, "chaining each group in list as a merged buffer");
3331 len = gst_buffer_list_length (list);
3333 result = GST_FLOW_OK;
3334 for (i = 0; i < len; i++) {
3335 buffer = gst_buffer_list_get (list, i);
3336 result = gst_base_sink_chain_main (basesink, pad,
3337 gst_buffer_ref (buffer), FALSE);
3338 if (result != GST_FLOW_OK)
3341 gst_buffer_list_unref (list);
3348 gst_base_sink_default_do_seek (GstBaseSink * sink, GstSegment * segment)
3350 gboolean res = TRUE;
3352 /* update our offset if the start/stop position was updated */
3353 if (segment->format == GST_FORMAT_BYTES) {
3354 segment->time = segment->start;
3355 } else if (segment->start == 0) {
3356 /* seek to start, we can implement a default for this. */
3360 GST_INFO_OBJECT (sink, "Can't do a default seek");
3366 #define SEEK_TYPE_IS_RELATIVE(t) (((t) != GST_SEEK_TYPE_NONE) && ((t) != GST_SEEK_TYPE_SET))
3369 gst_base_sink_default_prepare_seek_segment (GstBaseSink * sink,
3370 GstEvent * event, GstSegment * segment)
3372 /* By default, we try one of 2 things:
3373 * - For absolute seek positions, convert the requested position to our
3374 * configured processing format and place it in the output segment \
3375 * - For relative seek positions, convert our current (input) values to the
3376 * seek format, adjust by the relative seek offset and then convert back to
3377 * the processing format
3379 GstSeekType cur_type, stop_type;
3382 GstFormat seek_format;
3385 gboolean res = TRUE;
3387 gst_event_parse_seek (event, &rate, &seek_format, &flags,
3388 &cur_type, &cur, &stop_type, &stop);
3390 if (seek_format == segment->format) {
3391 gst_segment_do_seek (segment, rate, seek_format, flags,
3392 cur_type, cur, stop_type, stop, &update);
3396 if (cur_type != GST_SEEK_TYPE_NONE) {
3397 /* FIXME: Handle seek_cur & seek_end by converting the input segment vals */
3399 gst_pad_query_convert (sink->sinkpad, seek_format, cur, segment->format,
3401 cur_type = GST_SEEK_TYPE_SET;
3404 if (res && stop_type != GST_SEEK_TYPE_NONE) {
3405 /* FIXME: Handle seek_cur & seek_end by converting the input segment vals */
3407 gst_pad_query_convert (sink->sinkpad, seek_format, stop,
3408 segment->format, &stop);
3409 stop_type = GST_SEEK_TYPE_SET;
3412 /* And finally, configure our output segment in the desired format */
3413 gst_segment_do_seek (segment, rate, segment->format, flags, cur_type, cur,
3414 stop_type, stop, &update);
3423 GST_DEBUG_OBJECT (sink, "undefined format given, seek aborted.");
3428 /* perform a seek, only executed in pull mode */
3430 gst_base_sink_perform_seek (GstBaseSink * sink, GstPad * pad, GstEvent * event)
3434 GstFormat seek_format, dest_format;
3436 GstSeekType cur_type, stop_type;
3437 gboolean seekseg_configured = FALSE;
3439 gboolean update, res = TRUE;
3440 GstSegment seeksegment;
3442 dest_format = sink->segment.format;
3445 GST_DEBUG_OBJECT (sink, "performing seek with event %p", event);
3446 gst_event_parse_seek (event, &rate, &seek_format, &flags,
3447 &cur_type, &cur, &stop_type, &stop);
3449 flush = flags & GST_SEEK_FLAG_FLUSH;
3451 GST_DEBUG_OBJECT (sink, "performing seek without event");
3456 GST_DEBUG_OBJECT (sink, "flushing upstream");
3457 gst_pad_push_event (pad, gst_event_new_flush_start ());
3458 gst_base_sink_flush_start (sink, pad);
3460 GST_DEBUG_OBJECT (sink, "pausing pulling thread");
3463 GST_PAD_STREAM_LOCK (pad);
3465 /* If we configured the seeksegment above, don't overwrite it now. Otherwise
3466 * copy the current segment info into the temp segment that we can actually
3467 * attempt the seek with. We only update the real segment if the seek succeeds. */
3468 if (!seekseg_configured) {
3469 memcpy (&seeksegment, &sink->segment, sizeof (GstSegment));
3471 /* now configure the final seek segment */
3473 if (sink->segment.format != seek_format) {
3474 /* OK, here's where we give the subclass a chance to convert the relative
3475 * seek into an absolute one in the processing format. We set up any
3476 * absolute seek above, before taking the stream lock. */
3477 if (!gst_base_sink_default_prepare_seek_segment (sink, event,
3479 GST_DEBUG_OBJECT (sink,
3480 "Preparing the seek failed after flushing. " "Aborting seek");
3484 /* The seek format matches our processing format, no need to ask the
3485 * the subclass to configure the segment. */
3486 gst_segment_do_seek (&seeksegment, rate, seek_format, flags,
3487 cur_type, cur, stop_type, stop, &update);
3490 /* Else, no seek event passed, so we're just (re)starting the
3495 GST_DEBUG_OBJECT (sink, "segment configured from %" G_GINT64_FORMAT
3496 " to %" G_GINT64_FORMAT ", position %" G_GINT64_FORMAT,
3497 seeksegment.start, seeksegment.stop, seeksegment.position);
3499 /* do the seek, segment.position contains the new position. */
3500 res = gst_base_sink_default_do_seek (sink, &seeksegment);
3505 GST_DEBUG_OBJECT (sink, "stop flushing upstream");
3506 gst_pad_push_event (pad, gst_event_new_flush_stop (TRUE));
3507 gst_base_sink_flush_stop (sink, pad, TRUE);
3508 } else if (res && sink->running) {
3509 /* we are running the current segment and doing a non-flushing seek,
3510 * close the segment first based on the position. */
3511 GST_DEBUG_OBJECT (sink, "closing running segment %" G_GINT64_FORMAT
3512 " to %" G_GINT64_FORMAT, sink->segment.start, sink->segment.position);
3515 /* The subclass must have converted the segment to the processing format
3517 if (res && seeksegment.format != dest_format) {
3518 GST_DEBUG_OBJECT (sink, "Subclass failed to prepare a seek segment "
3519 "in the correct format. Aborting seek.");
3523 /* if successful seek, we update our real segment and push
3524 * out the new segment. */
3526 gst_segment_copy_into (&seeksegment, &sink->segment);
3528 if (sink->segment.flags & GST_SEGMENT_FLAG_SEGMENT) {
3529 gst_element_post_message (GST_ELEMENT (sink),
3530 gst_message_new_segment_start (GST_OBJECT (sink),
3531 sink->segment.format, sink->segment.position));
3535 sink->priv->discont = TRUE;
3536 sink->running = TRUE;
3538 GST_PAD_STREAM_UNLOCK (pad);
3544 set_step_info (GstBaseSink * sink, GstStepInfo * current, GstStepInfo * pending,
3545 guint seqnum, GstFormat format, guint64 amount, gdouble rate,
3546 gboolean flush, gboolean intermediate)
3548 GST_OBJECT_LOCK (sink);
3549 pending->seqnum = seqnum;
3550 pending->format = format;
3551 pending->amount = amount;
3552 pending->position = 0;
3553 pending->rate = rate;
3554 pending->flush = flush;
3555 pending->intermediate = intermediate;
3556 pending->valid = TRUE;
3557 /* flush invalidates the current stepping segment */
3559 current->valid = FALSE;
3560 GST_OBJECT_UNLOCK (sink);
3564 gst_base_sink_perform_step (GstBaseSink * sink, GstPad * pad, GstEvent * event)
3566 GstBaseSinkPrivate *priv;
3567 GstBaseSinkClass *bclass;
3568 gboolean flush, intermediate;
3573 GstStepInfo *pending, *current;
3574 GstMessage *message;
3576 bclass = GST_BASE_SINK_GET_CLASS (sink);
3579 GST_DEBUG_OBJECT (sink, "performing step with event %p", event);
3581 gst_event_parse_step (event, &format, &amount, &rate, &flush, &intermediate);
3582 seqnum = gst_event_get_seqnum (event);
3584 pending = &priv->pending_step;
3585 current = &priv->current_step;
3587 /* post message first */
3588 message = gst_message_new_step_start (GST_OBJECT (sink), FALSE, format,
3589 amount, rate, flush, intermediate);
3590 gst_message_set_seqnum (message, seqnum);
3591 gst_element_post_message (GST_ELEMENT (sink), message);
3594 /* we need to call ::unlock before locking PREROLL_LOCK
3595 * since we lock it before going into ::render */
3597 bclass->unlock (sink);
3599 GST_BASE_SINK_PREROLL_LOCK (sink);
3600 /* now that we have the PREROLL lock, clear our unlock request */
3601 if (bclass->unlock_stop)
3602 bclass->unlock_stop (sink);
3604 /* update the stepinfo and make it valid */
3605 set_step_info (sink, current, pending, seqnum, format, amount, rate, flush,
3608 if (sink->priv->async_enabled) {
3609 /* and we need to commit our state again on the next
3610 * prerolled buffer */
3611 sink->playing_async = TRUE;
3612 priv->pending_step.need_preroll = TRUE;
3613 sink->need_preroll = FALSE;
3614 gst_element_lost_state (GST_ELEMENT_CAST (sink));
3616 sink->priv->have_latency = TRUE;
3617 sink->need_preroll = FALSE;
3619 priv->current_sstart = GST_CLOCK_TIME_NONE;
3620 priv->current_sstop = GST_CLOCK_TIME_NONE;
3621 priv->eos_rtime = GST_CLOCK_TIME_NONE;
3622 priv->call_preroll = TRUE;
3623 gst_base_sink_set_last_buffer (sink, NULL);
3624 gst_base_sink_reset_qos (sink);
3626 if (sink->clock_id) {
3627 gst_clock_id_unschedule (sink->clock_id);
3630 if (sink->have_preroll) {
3631 GST_DEBUG_OBJECT (sink, "signal waiter");
3632 priv->step_unlock = TRUE;
3633 GST_BASE_SINK_PREROLL_SIGNAL (sink);
3635 GST_BASE_SINK_PREROLL_UNLOCK (sink);
3637 /* update the stepinfo and make it valid */
3638 set_step_info (sink, current, pending, seqnum, format, amount, rate, flush,
3648 gst_base_sink_loop (GstPad * pad)
3651 GstBaseSink *basesink;
3652 GstBuffer *buf = NULL;
3653 GstFlowReturn result;
3657 parent = GST_OBJECT_PARENT (pad);
3658 basesink = GST_BASE_SINK (parent);
3660 g_assert (basesink->pad_mode == GST_PAD_MODE_PULL);
3662 if ((blocksize = basesink->priv->blocksize) == 0)
3665 offset = basesink->segment.position;
3667 GST_DEBUG_OBJECT (basesink, "pulling %" G_GUINT64_FORMAT ", %u",
3670 result = gst_pad_pull_range (pad, offset, blocksize, &buf);
3671 if (G_UNLIKELY (result != GST_FLOW_OK))
3674 if (G_UNLIKELY (buf == NULL))
3677 offset += gst_buffer_get_size (buf);
3679 basesink->segment.position = offset;
3681 GST_BASE_SINK_PREROLL_LOCK (basesink);
3682 result = gst_base_sink_chain_unlocked (basesink, pad, buf, FALSE);
3683 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
3684 if (G_UNLIKELY (result != GST_FLOW_OK))
3692 GST_LOG_OBJECT (basesink, "pausing task, reason %s",
3693 gst_flow_get_name (result));
3694 gst_pad_pause_task (pad);
3695 if (result == GST_FLOW_EOS) {
3696 /* perform EOS logic */
3697 if (basesink->segment.flags & GST_SEGMENT_FLAG_SEGMENT) {
3698 gst_element_post_message (GST_ELEMENT_CAST (basesink),
3699 gst_message_new_segment_done (GST_OBJECT_CAST (basesink),
3700 basesink->segment.format, basesink->segment.position));
3701 gst_base_sink_event (pad, parent,
3702 gst_event_new_segment_done (basesink->segment.format,
3703 basesink->segment.position));
3705 gst_base_sink_event (pad, parent, gst_event_new_eos ());
3707 } else if (result == GST_FLOW_NOT_LINKED || result <= GST_FLOW_EOS) {
3708 /* for fatal errors we post an error message, post the error
3709 * first so the app knows about the error first.
3710 * wrong-state is not a fatal error because it happens due to
3711 * flushing and posting an error message in that case is the
3712 * wrong thing to do, e.g. when basesrc is doing a flushing
3714 GST_ELEMENT_ERROR (basesink, STREAM, FAILED,
3715 (_("Internal data stream error.")),
3716 ("stream stopped, reason %s", gst_flow_get_name (result)));
3717 gst_base_sink_event (pad, parent, gst_event_new_eos ());
3723 GST_LOG_OBJECT (basesink, "no buffer, pausing");
3724 GST_ELEMENT_ERROR (basesink, STREAM, FAILED,
3725 (_("Internal data flow error.")), ("element returned NULL buffer"));
3726 result = GST_FLOW_ERROR;
3732 gst_base_sink_set_flushing (GstBaseSink * basesink, GstPad * pad,
3735 GstBaseSinkClass *bclass;
3737 bclass = GST_BASE_SINK_GET_CLASS (basesink);
3740 /* unlock any subclasses, we need to do this before grabbing the
3741 * PREROLL_LOCK since we hold this lock before going into ::render. */
3743 bclass->unlock (basesink);
3746 GST_BASE_SINK_PREROLL_LOCK (basesink);
3747 basesink->flushing = flushing;
3749 /* step 1, now that we have the PREROLL lock, clear our unlock request */
3750 if (bclass->unlock_stop)
3751 bclass->unlock_stop (basesink);
3753 /* set need_preroll before we unblock the clock. If the clock is unblocked
3754 * before timing out, we can reuse the buffer for preroll. */
3755 basesink->need_preroll = TRUE;
3757 /* step 2, unblock clock sync (if any) or any other blocking thing */
3758 if (basesink->clock_id) {
3759 gst_clock_id_unschedule (basesink->clock_id);
3762 /* flush out the data thread if it's locked in finish_preroll, this will
3763 * also flush out the EOS state */
3764 GST_DEBUG_OBJECT (basesink,
3765 "flushing out data thread, need preroll to TRUE");
3767 /* we can't have EOS anymore now */
3768 basesink->eos = FALSE;
3769 basesink->priv->received_eos = FALSE;
3770 basesink->have_preroll = FALSE;
3771 basesink->priv->step_unlock = FALSE;
3772 /* can't report latency anymore until we preroll again */
3773 if (basesink->priv->async_enabled) {
3774 GST_OBJECT_LOCK (basesink);
3775 basesink->priv->have_latency = FALSE;
3776 GST_OBJECT_UNLOCK (basesink);
3778 /* and signal any waiters now */
3779 GST_BASE_SINK_PREROLL_SIGNAL (basesink);
3781 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
3787 gst_base_sink_default_activate_pull (GstBaseSink * basesink, gboolean active)
3793 result = gst_pad_start_task (basesink->sinkpad,
3794 (GstTaskFunction) gst_base_sink_loop, basesink->sinkpad, NULL);
3796 /* step 2, make sure streaming finishes */
3797 result = gst_pad_stop_task (basesink->sinkpad);
3804 gst_base_sink_pad_activate (GstPad * pad, GstObject * parent)
3806 gboolean result = FALSE;
3807 GstBaseSink *basesink;
3811 basesink = GST_BASE_SINK (parent);
3813 GST_DEBUG_OBJECT (basesink, "Trying pull mode first");
3815 gst_base_sink_set_flushing (basesink, pad, FALSE);
3817 /* we need to have the pull mode enabled */
3818 if (!basesink->can_activate_pull) {
3819 GST_DEBUG_OBJECT (basesink, "pull mode disabled");
3823 /* check if downstreams supports pull mode at all */
3824 query = gst_query_new_scheduling ();
3826 if (!gst_pad_peer_query (pad, query)) {
3827 gst_query_unref (query);
3828 GST_DEBUG_OBJECT (basesink, "peer query faild, no pull mode");
3832 /* parse result of the query */
3833 pull_mode = gst_query_has_scheduling_mode (query, GST_PAD_MODE_PULL);
3834 gst_query_unref (query);
3837 GST_DEBUG_OBJECT (basesink, "pull mode not supported");
3841 /* set the pad mode before starting the task so that it's in the
3842 * correct state for the new thread. also the sink set_caps and get_caps
3843 * function checks this */
3844 basesink->pad_mode = GST_PAD_MODE_PULL;
3846 /* we first try to negotiate a format so that when we try to activate
3847 * downstream, it knows about our format */
3848 if (!gst_base_sink_negotiate_pull (basesink)) {
3849 GST_DEBUG_OBJECT (basesink, "failed to negotiate in pull mode");
3853 /* ok activate now */
3854 if (!gst_pad_activate_mode (pad, GST_PAD_MODE_PULL, TRUE)) {
3855 /* clear any pending caps */
3856 GST_OBJECT_LOCK (basesink);
3857 gst_caps_replace (&basesink->priv->caps, NULL);
3858 GST_OBJECT_UNLOCK (basesink);
3859 GST_DEBUG_OBJECT (basesink, "failed to activate in pull mode");
3863 GST_DEBUG_OBJECT (basesink, "Success activating pull mode");
3867 /* push mode fallback */
3869 GST_DEBUG_OBJECT (basesink, "Falling back to push mode");
3870 if ((result = gst_pad_activate_mode (pad, GST_PAD_MODE_PUSH, TRUE))) {
3871 GST_DEBUG_OBJECT (basesink, "Success activating push mode");
3876 GST_WARNING_OBJECT (basesink, "Could not activate pad in either mode");
3877 gst_base_sink_set_flushing (basesink, pad, TRUE);
3884 gst_base_sink_pad_activate_push (GstPad * pad, GstObject * parent,
3888 GstBaseSink *basesink;
3890 basesink = GST_BASE_SINK (parent);
3893 if (!basesink->can_activate_push) {
3895 basesink->pad_mode = GST_PAD_MODE_NONE;
3898 basesink->pad_mode = GST_PAD_MODE_PUSH;
3901 if (G_UNLIKELY (basesink->pad_mode != GST_PAD_MODE_PUSH)) {
3902 g_warning ("Internal GStreamer activation error!!!");
3905 gst_base_sink_set_flushing (basesink, pad, TRUE);
3907 basesink->pad_mode = GST_PAD_MODE_NONE;
3915 gst_base_sink_negotiate_pull (GstBaseSink * basesink)
3922 /* this returns the intersection between our caps and the peer caps. If there
3923 * is no peer, it returns NULL and we can't operate in pull mode so we can
3924 * fail the negotiation. */
3925 caps = gst_pad_get_allowed_caps (GST_BASE_SINK_PAD (basesink));
3926 if (caps == NULL || gst_caps_is_empty (caps))
3927 goto no_caps_possible;
3929 GST_DEBUG_OBJECT (basesink, "allowed caps: %" GST_PTR_FORMAT, caps);
3931 if (gst_caps_is_any (caps)) {
3932 GST_DEBUG_OBJECT (basesink, "caps were ANY after fixating, "
3934 /* neither side has template caps in this case, so they are prepared for
3935 pull() without setcaps() */
3939 caps = gst_base_sink_fixate (basesink, caps);
3940 GST_DEBUG_OBJECT (basesink, "fixated to: %" GST_PTR_FORMAT, caps);
3942 if (gst_caps_is_fixed (caps)) {
3943 if (!gst_pad_set_caps (GST_BASE_SINK_PAD (basesink), caps))
3944 goto could_not_set_caps;
3950 gst_caps_unref (caps);
3956 GST_INFO_OBJECT (basesink, "Pipeline could not agree on caps");
3957 GST_DEBUG_OBJECT (basesink, "get_allowed_caps() returned EMPTY");
3959 gst_caps_unref (caps);
3964 GST_INFO_OBJECT (basesink, "Could not set caps: %" GST_PTR_FORMAT, caps);
3965 gst_caps_unref (caps);
3970 /* this won't get called until we implement an activate function */
3972 gst_base_sink_pad_activate_pull (GstPad * pad, GstObject * parent,
3975 gboolean result = FALSE;
3976 GstBaseSink *basesink;
3977 GstBaseSinkClass *bclass;
3979 basesink = GST_BASE_SINK (parent);
3980 bclass = GST_BASE_SINK_GET_CLASS (basesink);
3985 /* we mark we have a newsegment here because pull based
3986 * mode works just fine without having a newsegment before the
3988 gst_segment_init (&basesink->segment, GST_FORMAT_BYTES);
3989 GST_OBJECT_LOCK (basesink);
3990 basesink->have_newsegment = TRUE;
3991 GST_OBJECT_UNLOCK (basesink);
3993 /* get the peer duration in bytes */
3994 result = gst_pad_peer_query_duration (pad, GST_FORMAT_BYTES, &duration);
3996 GST_DEBUG_OBJECT (basesink,
3997 "setting duration in bytes to %" G_GINT64_FORMAT, duration);
3998 basesink->segment.duration = duration;
4000 GST_DEBUG_OBJECT (basesink, "unknown duration");
4003 if (bclass->activate_pull)
4004 result = bclass->activate_pull (basesink, TRUE);
4009 goto activate_failed;
4012 if (G_UNLIKELY (basesink->pad_mode != GST_PAD_MODE_PULL)) {
4013 g_warning ("Internal GStreamer activation error!!!");
4016 result = gst_base_sink_set_flushing (basesink, pad, TRUE);
4017 if (bclass->activate_pull)
4018 result &= bclass->activate_pull (basesink, FALSE);
4019 basesink->pad_mode = GST_PAD_MODE_NONE;
4028 /* reset, as starting the thread failed */
4029 basesink->pad_mode = GST_PAD_MODE_NONE;
4031 GST_ERROR_OBJECT (basesink, "subclass failed to activate in pull mode");
4037 gst_base_sink_pad_activate_mode (GstPad * pad, GstObject * parent,
4038 GstPadMode mode, gboolean active)
4043 case GST_PAD_MODE_PULL:
4044 res = gst_base_sink_pad_activate_pull (pad, parent, active);
4046 case GST_PAD_MODE_PUSH:
4047 res = gst_base_sink_pad_activate_push (pad, parent, active);
4050 GST_LOG_OBJECT (pad, "unknown activation mode %d", mode);
4057 /* send an event to our sinkpad peer. */
4059 gst_base_sink_send_event (GstElement * element, GstEvent * event)
4062 GstBaseSink *basesink = GST_BASE_SINK (element);
4063 gboolean forward, result = TRUE;
4066 GST_OBJECT_LOCK (element);
4067 /* get the pad and the scheduling mode */
4068 pad = gst_object_ref (basesink->sinkpad);
4069 mode = basesink->pad_mode;
4070 GST_OBJECT_UNLOCK (element);
4072 /* only push UPSTREAM events upstream */
4073 forward = GST_EVENT_IS_UPSTREAM (event);
4075 GST_DEBUG_OBJECT (basesink, "handling event %p %" GST_PTR_FORMAT, event,
4078 switch (GST_EVENT_TYPE (event)) {
4079 case GST_EVENT_LATENCY:
4081 GstClockTime latency;
4083 gst_event_parse_latency (event, &latency);
4085 /* store the latency. We use this to adjust the running_time before syncing
4086 * it to the clock. */
4087 GST_OBJECT_LOCK (element);
4088 basesink->priv->latency = latency;
4089 if (!basesink->priv->have_latency)
4091 GST_OBJECT_UNLOCK (element);
4092 GST_DEBUG_OBJECT (basesink, "latency set to %" GST_TIME_FORMAT,
4093 GST_TIME_ARGS (latency));
4095 /* We forward this event so that all elements know about the global pipeline
4096 * latency. This is interesting for an element when it wants to figure out
4097 * when a particular piece of data will be rendered. */
4100 case GST_EVENT_SEEK:
4101 /* in pull mode we will execute the seek */
4102 if (mode == GST_PAD_MODE_PULL)
4103 result = gst_base_sink_perform_seek (basesink, pad, event);
4105 case GST_EVENT_STEP:
4106 result = gst_base_sink_perform_step (basesink, pad, event);
4114 result = gst_pad_push_event (pad, event);
4116 /* not forwarded, unref the event */
4117 gst_event_unref (event);
4120 gst_object_unref (pad);
4122 GST_DEBUG_OBJECT (basesink, "handled event %p %" GST_PTR_FORMAT ": %d", event,
4129 gst_base_sink_get_position (GstBaseSink * basesink, GstFormat format,
4130 gint64 * cur, gboolean * upstream)
4132 GstClock *clock = NULL;
4133 gboolean res = FALSE;
4135 GstSegment *segment;
4136 GstClockTime now, latency;
4137 GstClockTimeDiff base_time;
4138 gint64 time, base, duration;
4141 gboolean last_seen, with_clock, in_paused;
4143 GST_OBJECT_LOCK (basesink);
4144 /* we can only get the segment when we are not NULL or READY */
4145 if (!basesink->have_newsegment)
4149 /* when not in PLAYING or when we're busy with a state change, we
4150 * cannot read from the clock so we report time based on the
4151 * last seen timestamp. */
4152 if (GST_STATE (basesink) != GST_STATE_PLAYING ||
4153 GST_STATE_PENDING (basesink) != GST_STATE_VOID_PENDING) {
4157 segment = &basesink->segment;
4159 /* get the format in the segment */
4160 oformat = segment->format;
4162 /* report with last seen position when EOS */
4163 last_seen = basesink->eos;
4165 /* assume we will use the clock for getting the current position */
4167 if (basesink->sync == FALSE)
4170 /* and we need a clock */
4171 if (G_UNLIKELY ((clock = GST_ELEMENT_CLOCK (basesink)) == NULL))
4174 gst_object_ref (clock);
4176 /* mainloop might be querying position when going to playing async,
4177 * while (audio) rendering might be quickly advancing stream position,
4178 * so use clock asap rather than last reported position */
4179 if (in_paused && with_clock && g_atomic_int_get (&basesink->priv->to_playing)) {
4180 GST_DEBUG_OBJECT (basesink, "going to PLAYING, so not PAUSED");
4184 /* collect all data we need holding the lock */
4185 if (GST_CLOCK_TIME_IS_VALID (segment->time))
4186 time = segment->time;
4190 if (GST_CLOCK_TIME_IS_VALID (segment->stop))
4191 duration = segment->stop - segment->start;
4195 base = segment->base;
4196 rate = segment->rate * segment->applied_rate;
4197 latency = basesink->priv->latency;
4199 if (oformat == GST_FORMAT_TIME) {
4202 start = basesink->priv->current_sstart;
4203 stop = basesink->priv->current_sstop;
4206 /* in paused we use the last position as a lower bound */
4207 if (stop == -1 || segment->rate > 0.0)
4212 /* in playing, use last stop time as upper bound */
4213 if (start == -1 || segment->rate > 0.0)
4219 /* convert last stop to stream time */
4220 last = gst_segment_to_stream_time (segment, oformat, segment->position);
4224 /* in paused, use start_time */
4225 base_time = GST_ELEMENT_START_TIME (basesink);
4226 GST_DEBUG_OBJECT (basesink, "in paused, using start time %" GST_TIME_FORMAT,
4227 GST_TIME_ARGS (base_time));
4228 } else if (with_clock) {
4229 /* else use clock when needed */
4230 base_time = GST_ELEMENT_CAST (basesink)->base_time;
4231 GST_DEBUG_OBJECT (basesink, "using clock and base time %" GST_TIME_FORMAT,
4232 GST_TIME_ARGS (base_time));
4234 /* else, no sync or clock -> no base time */
4235 GST_DEBUG_OBJECT (basesink, "no sync or no clock");
4239 /* no base_time, we can't calculate running_time, use last seem timestamp to report
4241 if (base_time == -1)
4244 /* need to release the object lock before we can get the time,
4245 * a clock might take the LOCK of the provider, which could be
4246 * a basesink subclass. */
4247 GST_OBJECT_UNLOCK (basesink);
4250 /* in EOS or when no valid stream_time, report the value of last seen
4253 /* no timestamp, we need to ask upstream */
4254 GST_DEBUG_OBJECT (basesink, "no last seen timestamp, asking upstream");
4259 GST_DEBUG_OBJECT (basesink, "using last seen timestamp %" GST_TIME_FORMAT,
4260 GST_TIME_ARGS (last));
4263 if (oformat != GST_FORMAT_TIME) {
4264 /* convert base, time and duration to time */
4265 if (!gst_pad_query_convert (basesink->sinkpad, oformat, base,
4266 GST_FORMAT_TIME, &base))
4267 goto convert_failed;
4268 if (!gst_pad_query_convert (basesink->sinkpad, oformat, duration,
4269 GST_FORMAT_TIME, &duration))
4270 goto convert_failed;
4271 if (!gst_pad_query_convert (basesink->sinkpad, oformat, time,
4272 GST_FORMAT_TIME, &time))
4273 goto convert_failed;
4274 if (!gst_pad_query_convert (basesink->sinkpad, oformat, last,
4275 GST_FORMAT_TIME, &last))
4276 goto convert_failed;
4278 /* assume time format from now on */
4279 oformat = GST_FORMAT_TIME;
4282 if (!in_paused && with_clock) {
4283 now = gst_clock_get_time (clock);
4289 /* subtract base time and base time from the clock time.
4290 * Make sure we don't go negative. This is the current time in
4291 * the segment which we need to scale with the combined
4292 * rate and applied rate. */
4294 base_time += latency;
4295 if (GST_CLOCK_DIFF (base_time, now) < 0)
4298 /* for negative rates we need to count back from the segment
4303 *cur = time + gst_guint64_to_gdouble (now - base_time) * rate;
4306 /* never report less than segment values in paused */
4308 *cur = MAX (last, *cur);
4310 /* never report more than last seen position in playing */
4312 *cur = MIN (last, *cur);
4315 GST_DEBUG_OBJECT (basesink,
4316 "now %" GST_TIME_FORMAT " - base_time %" GST_TIME_FORMAT " - base %"
4317 GST_TIME_FORMAT " + time %" GST_TIME_FORMAT " last %" GST_TIME_FORMAT,
4318 GST_TIME_ARGS (now), GST_TIME_ARGS (base_time), GST_TIME_ARGS (base),
4319 GST_TIME_ARGS (time), GST_TIME_ARGS (last));
4322 if (oformat != format) {
4323 /* convert to final format */
4324 if (!gst_pad_query_convert (basesink->sinkpad, oformat, *cur, format, cur))
4325 goto convert_failed;
4331 GST_DEBUG_OBJECT (basesink, "res: %d, POSITION: %" GST_TIME_FORMAT,
4332 res, GST_TIME_ARGS (*cur));
4335 gst_object_unref (clock);
4342 /* in NULL or READY we always return FALSE and -1 */
4343 GST_DEBUG_OBJECT (basesink, "position in wrong state, return -1");
4346 GST_OBJECT_UNLOCK (basesink);
4351 GST_DEBUG_OBJECT (basesink, "convert failed, try upstream");
4359 gst_base_sink_get_duration (GstBaseSink * basesink, GstFormat format,
4360 gint64 * dur, gboolean * upstream)
4362 gboolean res = FALSE;
4364 if (basesink->pad_mode == GST_PAD_MODE_PULL) {
4367 /* get the duration in bytes, in pull mode that's all we are sure to
4368 * know. We have to explicitly get this value from upstream instead of
4369 * using our cached value because it might change. Duration caching
4370 * should be done at a higher level. */
4372 gst_pad_peer_query_duration (basesink->sinkpad, GST_FORMAT_BYTES,
4375 basesink->segment.duration = uduration;
4376 if (format != GST_FORMAT_BYTES) {
4377 /* convert to the requested format */
4379 gst_pad_query_convert (basesink->sinkpad, GST_FORMAT_BYTES,
4380 uduration, format, dur);
4394 default_element_query (GstElement * element, GstQuery * query)
4396 gboolean res = FALSE;
4398 GstBaseSink *basesink = GST_BASE_SINK (element);
4400 switch (GST_QUERY_TYPE (query)) {
4401 case GST_QUERY_POSITION:
4405 gboolean upstream = FALSE;
4407 gst_query_parse_position (query, &format, NULL);
4409 GST_DEBUG_OBJECT (basesink, "position query in format %s",
4410 gst_format_get_name (format));
4412 /* first try to get the position based on the clock */
4414 gst_base_sink_get_position (basesink, format, &cur, &upstream))) {
4415 gst_query_set_position (query, format, cur);
4416 } else if (upstream) {
4417 /* fallback to peer query */
4418 res = gst_pad_peer_query (basesink->sinkpad, query);
4421 /* we can handle a few things if upstream failed */
4422 if (format == GST_FORMAT_PERCENT) {
4425 res = gst_base_sink_get_position (basesink, GST_FORMAT_TIME, &cur,
4427 if (!res && upstream) {
4429 gst_pad_peer_query_position (basesink->sinkpad, GST_FORMAT_TIME,
4433 res = gst_base_sink_get_duration (basesink, GST_FORMAT_TIME, &dur,
4435 if (!res && upstream) {
4437 gst_pad_peer_query_duration (basesink->sinkpad,
4438 GST_FORMAT_TIME, &dur);
4444 pos = gst_util_uint64_scale (100 * GST_FORMAT_PERCENT_SCALE, cur,
4446 gst_query_set_position (query, GST_FORMAT_PERCENT, pos);
4452 case GST_QUERY_DURATION:
4456 gboolean upstream = FALSE;
4458 gst_query_parse_duration (query, &format, NULL);
4460 GST_DEBUG_OBJECT (basesink, "duration query in format %s",
4461 gst_format_get_name (format));
4464 gst_base_sink_get_duration (basesink, format, &dur, &upstream))) {
4465 gst_query_set_duration (query, format, dur);
4466 } else if (upstream) {
4467 /* fallback to peer query */
4468 res = gst_pad_peer_query (basesink->sinkpad, query);
4471 /* we can handle a few things if upstream failed */
4472 if (format == GST_FORMAT_PERCENT) {
4473 gst_query_set_duration (query, GST_FORMAT_PERCENT,
4474 GST_FORMAT_PERCENT_MAX);
4480 case GST_QUERY_LATENCY:
4482 gboolean live, us_live;
4483 GstClockTime min, max;
4485 if ((res = gst_base_sink_query_latency (basesink, &live, &us_live, &min,
4487 gst_query_set_latency (query, live, min, max);
4491 case GST_QUERY_JITTER:
4493 case GST_QUERY_RATE:
4494 /* gst_query_set_rate (query, basesink->segment_rate); */
4497 case GST_QUERY_SEGMENT:
4499 if (basesink->pad_mode == GST_PAD_MODE_PULL) {
4500 gst_query_set_segment (query, basesink->segment.rate,
4501 GST_FORMAT_TIME, basesink->segment.start, basesink->segment.stop);
4504 res = gst_pad_peer_query (basesink->sinkpad, query);
4508 case GST_QUERY_SEEKING:
4509 case GST_QUERY_CONVERT:
4510 case GST_QUERY_FORMATS:
4512 res = gst_pad_peer_query (basesink->sinkpad, query);
4515 GST_DEBUG_OBJECT (basesink, "query %s returns %d",
4516 GST_QUERY_TYPE_NAME (query), res);
4522 gst_base_sink_default_query (GstBaseSink * basesink, GstQuery * query)
4525 GstBaseSinkClass *bclass;
4527 bclass = GST_BASE_SINK_GET_CLASS (basesink);
4529 switch (GST_QUERY_TYPE (query)) {
4530 case GST_QUERY_ALLOCATION:
4532 if (bclass->propose_allocation)
4533 res = bclass->propose_allocation (basesink, query);
4538 case GST_QUERY_CAPS:
4540 GstCaps *caps, *filter;
4542 gst_query_parse_caps (query, &filter);
4543 caps = gst_base_sink_query_caps (basesink, basesink->sinkpad, filter);
4544 gst_query_set_caps_result (query, caps);
4545 gst_caps_unref (caps);
4549 case GST_QUERY_ACCEPT_CAPS:
4551 GstCaps *caps, *allowed;
4554 /* slightly faster than the default implementation */
4555 gst_query_parse_accept_caps (query, &caps);
4556 allowed = gst_base_sink_query_caps (basesink, basesink->sinkpad, NULL);
4557 subset = gst_caps_is_subset (caps, allowed);
4558 gst_caps_unref (allowed);
4559 gst_query_set_accept_caps_result (query, subset);
4563 case GST_QUERY_DRAIN:
4568 gst_pad_query_default (basesink->sinkpad, GST_OBJECT_CAST (basesink),
4576 gst_base_sink_sink_query (GstPad * pad, GstObject * parent, GstQuery * query)
4578 GstBaseSink *basesink;
4579 GstBaseSinkClass *bclass;
4582 basesink = GST_BASE_SINK_CAST (parent);
4583 bclass = GST_BASE_SINK_GET_CLASS (basesink);
4586 res = bclass->query (basesink, query);
4593 static GstStateChangeReturn
4594 gst_base_sink_change_state (GstElement * element, GstStateChange transition)
4596 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
4597 GstBaseSink *basesink = GST_BASE_SINK (element);
4598 GstBaseSinkClass *bclass;
4599 GstBaseSinkPrivate *priv;
4601 priv = basesink->priv;
4603 bclass = GST_BASE_SINK_GET_CLASS (basesink);
4605 switch (transition) {
4606 case GST_STATE_CHANGE_NULL_TO_READY:
4608 if (!bclass->start (basesink))
4611 case GST_STATE_CHANGE_READY_TO_PAUSED:
4612 /* need to complete preroll before this state change completes, there
4613 * is no data flow in READY so we can safely assume we need to preroll. */
4614 GST_BASE_SINK_PREROLL_LOCK (basesink);
4615 GST_DEBUG_OBJECT (basesink, "READY to PAUSED");
4616 basesink->have_newsegment = FALSE;
4617 gst_segment_init (&basesink->segment, GST_FORMAT_UNDEFINED);
4618 basesink->offset = 0;
4619 basesink->have_preroll = FALSE;
4620 priv->step_unlock = FALSE;
4621 basesink->need_preroll = TRUE;
4622 basesink->playing_async = TRUE;
4623 priv->current_sstart = GST_CLOCK_TIME_NONE;
4624 priv->current_sstop = GST_CLOCK_TIME_NONE;
4625 priv->eos_rtime = GST_CLOCK_TIME_NONE;
4627 basesink->eos = FALSE;
4628 priv->received_eos = FALSE;
4629 gst_base_sink_reset_qos (basesink);
4630 priv->commited = FALSE;
4631 priv->call_preroll = TRUE;
4632 priv->current_step.valid = FALSE;
4633 priv->pending_step.valid = FALSE;
4634 if (priv->async_enabled) {
4635 GST_DEBUG_OBJECT (basesink, "doing async state change");
4636 /* when async enabled, post async-start message and return ASYNC from
4637 * the state change function */
4638 ret = GST_STATE_CHANGE_ASYNC;
4639 gst_element_post_message (GST_ELEMENT_CAST (basesink),
4640 gst_message_new_async_start (GST_OBJECT_CAST (basesink)));
4642 priv->have_latency = TRUE;
4644 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
4646 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
4647 GST_BASE_SINK_PREROLL_LOCK (basesink);
4648 g_atomic_int_set (&basesink->priv->to_playing, TRUE);
4649 if (!gst_base_sink_needs_preroll (basesink)) {
4650 GST_DEBUG_OBJECT (basesink, "PAUSED to PLAYING, don't need preroll");
4651 /* no preroll needed anymore now. */
4652 basesink->playing_async = FALSE;
4653 basesink->need_preroll = FALSE;
4654 if (basesink->eos) {
4655 GstMessage *message;
4657 /* need to post EOS message here */
4658 GST_DEBUG_OBJECT (basesink, "Now posting EOS");
4659 message = gst_message_new_eos (GST_OBJECT_CAST (basesink));
4660 gst_message_set_seqnum (message, basesink->priv->seqnum);
4661 gst_element_post_message (GST_ELEMENT_CAST (basesink), message);
4663 GST_DEBUG_OBJECT (basesink, "signal preroll");
4664 GST_BASE_SINK_PREROLL_SIGNAL (basesink);
4667 GST_DEBUG_OBJECT (basesink, "PAUSED to PLAYING, we are not prerolled");
4668 basesink->need_preroll = TRUE;
4669 basesink->playing_async = TRUE;
4670 priv->call_preroll = TRUE;
4671 priv->commited = FALSE;
4672 if (priv->async_enabled) {
4673 GST_DEBUG_OBJECT (basesink, "doing async state change");
4674 ret = GST_STATE_CHANGE_ASYNC;
4675 gst_element_post_message (GST_ELEMENT_CAST (basesink),
4676 gst_message_new_async_start (GST_OBJECT_CAST (basesink)));
4679 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
4686 GstStateChangeReturn bret;
4688 bret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
4689 if (G_UNLIKELY (bret == GST_STATE_CHANGE_FAILURE))
4690 goto activate_failed;
4693 switch (transition) {
4694 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
4695 /* completed transition, so need not be marked any longer
4696 * And it should be unmarked, since e.g. losing our position upon flush
4697 * does not really change state to PAUSED ... */
4698 g_atomic_int_set (&basesink->priv->to_playing, FALSE);
4700 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
4701 g_atomic_int_set (&basesink->priv->to_playing, FALSE);
4702 GST_DEBUG_OBJECT (basesink, "PLAYING to PAUSED");
4703 /* FIXME, make sure we cannot enter _render first */
4705 /* we need to call ::unlock before locking PREROLL_LOCK
4706 * since we lock it before going into ::render */
4708 bclass->unlock (basesink);
4710 GST_BASE_SINK_PREROLL_LOCK (basesink);
4711 GST_DEBUG_OBJECT (basesink, "got preroll lock");
4712 /* now that we have the PREROLL lock, clear our unlock request */
4713 if (bclass->unlock_stop)
4714 bclass->unlock_stop (basesink);
4716 /* we need preroll again and we set the flag before unlocking the clockid
4717 * because if the clockid is unlocked before a current buffer expired, we
4718 * can use that buffer to preroll with */
4719 basesink->need_preroll = TRUE;
4721 if (basesink->clock_id) {
4722 GST_DEBUG_OBJECT (basesink, "unschedule clock");
4723 gst_clock_id_unschedule (basesink->clock_id);
4726 /* if we don't have a preroll buffer we need to wait for a preroll and
4728 if (!gst_base_sink_needs_preroll (basesink)) {
4729 GST_DEBUG_OBJECT (basesink, "PLAYING to PAUSED, we are prerolled");
4730 basesink->playing_async = FALSE;
4732 if (GST_STATE_TARGET (GST_ELEMENT (basesink)) <= GST_STATE_READY) {
4733 GST_DEBUG_OBJECT (basesink, "element is <= READY");
4734 ret = GST_STATE_CHANGE_SUCCESS;
4736 GST_DEBUG_OBJECT (basesink,
4737 "PLAYING to PAUSED, we are not prerolled");
4738 basesink->playing_async = TRUE;
4739 priv->commited = FALSE;
4740 priv->call_preroll = TRUE;
4741 if (priv->async_enabled) {
4742 GST_DEBUG_OBJECT (basesink, "doing async state change");
4743 ret = GST_STATE_CHANGE_ASYNC;
4744 gst_element_post_message (GST_ELEMENT_CAST (basesink),
4745 gst_message_new_async_start (GST_OBJECT_CAST (basesink)));
4749 GST_DEBUG_OBJECT (basesink, "rendered: %" G_GUINT64_FORMAT
4750 ", dropped: %" G_GUINT64_FORMAT, priv->rendered, priv->dropped);
4752 gst_base_sink_reset_qos (basesink);
4753 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
4755 case GST_STATE_CHANGE_PAUSED_TO_READY:
4756 GST_BASE_SINK_PREROLL_LOCK (basesink);
4757 /* start by resetting our position state with the object lock so that the
4758 * position query gets the right idea. We do this before we post the
4759 * messages so that the message handlers pick this up. */
4760 GST_OBJECT_LOCK (basesink);
4761 basesink->have_newsegment = FALSE;
4762 priv->current_sstart = GST_CLOCK_TIME_NONE;
4763 priv->current_sstop = GST_CLOCK_TIME_NONE;
4764 priv->have_latency = FALSE;
4765 if (priv->cached_clock_id) {
4766 gst_clock_id_unref (priv->cached_clock_id);
4767 priv->cached_clock_id = NULL;
4769 gst_caps_replace (&basesink->priv->caps, NULL);
4770 GST_OBJECT_UNLOCK (basesink);
4772 gst_base_sink_set_last_buffer (basesink, NULL);
4773 priv->call_preroll = FALSE;
4775 if (!priv->commited) {
4776 if (priv->async_enabled) {
4777 GST_DEBUG_OBJECT (basesink, "PAUSED to READY, posting async-done");
4779 gst_element_post_message (GST_ELEMENT_CAST (basesink),
4780 gst_message_new_state_changed (GST_OBJECT_CAST (basesink),
4781 GST_STATE_PLAYING, GST_STATE_PAUSED, GST_STATE_READY));
4783 gst_element_post_message (GST_ELEMENT_CAST (basesink),
4784 gst_message_new_async_done (GST_OBJECT_CAST (basesink),
4785 GST_CLOCK_TIME_NONE));
4787 priv->commited = TRUE;
4789 GST_DEBUG_OBJECT (basesink, "PAUSED to READY, don't need_preroll");
4791 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
4793 case GST_STATE_CHANGE_READY_TO_NULL:
4795 if (!bclass->stop (basesink)) {
4796 GST_WARNING_OBJECT (basesink, "failed to stop");
4799 gst_base_sink_set_last_buffer (basesink, NULL);
4800 priv->call_preroll = FALSE;
4811 GST_DEBUG_OBJECT (basesink, "failed to start");
4812 return GST_STATE_CHANGE_FAILURE;
4816 GST_DEBUG_OBJECT (basesink,
4817 "element failed to change states -- activation problem?");
4818 return GST_STATE_CHANGE_FAILURE;