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., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
25 * @short_description: Base class for sink elements
26 * @see_also: #GstBaseTransform, #GstBaseSrc
28 * #GstBaseSink is the base class for sink elements in GStreamer, such as
29 * xvimagesink or filesink. It is a layer on top of #GstElement that provides a
30 * simplified interface to plugin writers. #GstBaseSink handles many details
31 * for you, for example: preroll, clock synchronization, state changes,
32 * activation in push or pull mode, and queries.
34 * In most cases, when writing sink elements, there is no need to implement
35 * class methods from #GstElement or to set functions on pads, because the
36 * #GstBaseSink infrastructure should be sufficient.
38 * #GstBaseSink provides support for exactly one sink pad, which should be
39 * named "sink". A sink implementation (subclass of #GstBaseSink) should
40 * install a pad template in its class_init function, like so:
41 * |[<!-- language="C" -->
43 * my_element_class_init (GstMyElementClass *klass)
45 * GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
47 * // sinktemplate should be a #GstStaticPadTemplate with direction
48 * // %GST_PAD_SINK and name "sink"
49 * gst_element_class_add_static_pad_template (gstelement_class, &sinktemplate);
51 * gst_element_class_set_static_metadata (gstelement_class,
55 * "The author <my.sink@my.email>");
59 * #GstBaseSink will handle the prerolling correctly. This means that it will
60 * return %GST_STATE_CHANGE_ASYNC from a state change to PAUSED until the first
61 * buffer arrives in this element. The base class will call the
62 * #GstBaseSinkClass.preroll() vmethod with this preroll buffer and will then
63 * commit the state change to the next asynchronously pending state.
65 * When the element is set to PLAYING, #GstBaseSink will synchronise on the
66 * clock using the times returned from #GstBaseSinkClass.get_times(). If this
67 * function returns %GST_CLOCK_TIME_NONE for the start time, no synchronisation
68 * will be done. Synchronisation can be disabled entirely by setting the object
69 * #GstBaseSink:sync property to %FALSE.
71 * After synchronisation the virtual method #GstBaseSinkClass.render() will be
72 * called. Subclasses should minimally implement this method.
74 * Subclasses that synchronise on the clock in the #GstBaseSinkClass.render()
75 * method are supported as well. These classes typically receive a buffer in
76 * the render method and can then potentially block on the clock while
77 * rendering. A typical example is an audiosink.
78 * These subclasses can use gst_base_sink_wait_preroll() to perform the
81 * Upon receiving the EOS event in the PLAYING state, #GstBaseSink will wait
82 * for the clock to reach the time indicated by the stop time of the last
83 * #GstBaseSinkClass.get_times() call before posting an EOS message. When the
84 * element receives EOS in PAUSED, preroll completes, the event is queued and an
85 * EOS message is posted when going to PLAYING.
87 * #GstBaseSink will internally use the %GST_EVENT_SEGMENT events to schedule
88 * synchronisation and clipping of buffers. Buffers that fall completely outside
89 * of the current segment are dropped. Buffers that fall partially in the
90 * segment are rendered (and prerolled). Subclasses should do any subbuffer
91 * clipping themselves when needed.
93 * #GstBaseSink will by default report the current playback position in
94 * %GST_FORMAT_TIME based on the current clock time and segment information.
95 * If no clock has been set on the element, the query will be forwarded
98 * The #GstBaseSinkClass.set_caps() function will be called when the subclass
99 * should configure itself to process a specific media type.
101 * The #GstBaseSinkClass.start() and #GstBaseSinkClass.stop() virtual methods
102 * will be called when resources should be allocated. Any
103 * #GstBaseSinkClass.preroll(), #GstBaseSinkClass.render() and
104 * #GstBaseSinkClass.set_caps() function will be called between the
105 * #GstBaseSinkClass.start() and #GstBaseSinkClass.stop() calls.
107 * The #GstBaseSinkClass.event() virtual method will be called when an event is
108 * received by #GstBaseSink. Normally this method should only be overridden by
109 * very specific elements (such as file sinks) which need to handle the
110 * newsegment event specially.
112 * The #GstBaseSinkClass.unlock() method is called when the elements should
113 * unblock any blocking operations they perform in the
114 * #GstBaseSinkClass.render() method. This is mostly useful when the
115 * #GstBaseSinkClass.render() method performs a blocking write on a file
116 * descriptor, for example.
118 * The #GstBaseSink:max-lateness property affects how the sink deals with
119 * buffers that arrive too late in the sink. A buffer arrives too late in the
120 * sink when the presentation time (as a combination of the last segment, buffer
121 * timestamp and element base_time) plus the duration is before the current
123 * If the frame is later than max-lateness, the sink will drop the buffer
124 * without calling the render method.
125 * This feature is disabled if sync is disabled, the
126 * #GstBaseSinkClass.get_times() method does not return a valid start time or
127 * max-lateness is set to -1 (the default).
128 * Subclasses can use gst_base_sink_set_max_lateness() to configure the
129 * max-lateness value.
131 * The #GstBaseSink:qos property will enable the quality-of-service features of
132 * the basesink which gather statistics about the real-time performance of the
133 * clock synchronisation. For each buffer received in the sink, statistics are
134 * gathered and a QOS event is sent upstream with these numbers. This
135 * information can then be used by upstream elements to reduce their processing
138 * The #GstBaseSink:async property can be used to instruct the sink to never
139 * perform an ASYNC state change. This feature is mostly usable when dealing
140 * with non-synchronized streams or sparse streams.
147 #include <gst/gst_private.h>
149 #include "gstbasesink.h"
150 #include <gst/gst-i18n-lib.h>
152 GST_DEBUG_CATEGORY_STATIC (gst_base_sink_debug);
153 #define GST_CAT_DEFAULT gst_base_sink_debug
155 #define GST_FLOW_STEP GST_FLOW_CUSTOM_ERROR
159 gboolean valid; /* if this info is valid */
160 guint32 seqnum; /* the seqnum of the STEP event */
161 GstFormat format; /* the format of the amount */
162 guint64 amount; /* the total amount of data to skip */
163 guint64 position; /* the position in the stepped data */
164 guint64 duration; /* the duration in time of the skipped data */
165 guint64 start; /* running_time of the start */
166 gdouble rate; /* rate of skipping */
167 gdouble start_rate; /* rate before skipping */
168 guint64 start_start; /* start position skipping */
169 guint64 start_stop; /* stop position skipping */
170 gboolean flush; /* if this was a flushing step */
171 gboolean intermediate; /* if this is an intermediate step */
172 gboolean need_preroll; /* if we need preroll after this step */
175 struct _GstBaseSinkPrivate
177 gint qos_enabled; /* ATOMIC */
178 gboolean async_enabled;
179 GstClockTimeDiff ts_offset;
180 GstClockTime render_delay;
181 GstClockTime processing_deadline;
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 */
203 GstClockTime avg_pt, avg_in_diff;
204 gdouble avg_rate; /* average with infinite window */
206 /* number of rendered and dropped frames */
211 GstClockTime latency;
213 /* if we already committed the state */
215 /* state change to playing ongoing */
218 /* when we received EOS */
219 gboolean received_eos;
221 /* when we are prerolled and able to report latency */
222 gboolean have_latency;
224 /* the last buffer we prerolled or rendered. Useful for making snapshots */
225 gint enable_last_sample; /* atomic */
226 GstBuffer *last_buffer;
228 GstBufferList *last_buffer_list;
230 /* negotiated caps */
233 /* blocksize for pulling */
238 /* seqnum of the stream */
241 gboolean call_preroll;
242 gboolean step_unlock;
244 /* we have a pending and a current step operation */
245 GstStepInfo current_step;
246 GstStepInfo pending_step;
248 /* Cached GstClockID */
249 GstClockID cached_clock_id;
251 /* for throttling and QoS */
252 GstClockTime earliest_in_time;
253 GstClockTime throttle_time;
255 /* for rate control */
257 GstClockTime rc_time;
258 GstClockTime rc_next;
259 gsize rc_accumulated;
261 gboolean drop_out_of_segment;
264 #define DO_RUNNING_AVG(avg,val,size) (((val) + ((size)-1) * (avg)) / (size))
266 /* generic running average, this has a neutral window size */
267 #define UPDATE_RUNNING_AVG(avg,val) DO_RUNNING_AVG(avg,val,8)
269 /* the windows for these running averages are experimentally obtained.
270 * positive values get averaged more while negative values use a small
271 * window so we can react faster to badness. */
272 #define UPDATE_RUNNING_AVG_P(avg,val) DO_RUNNING_AVG(avg,val,16)
273 #define UPDATE_RUNNING_AVG_N(avg,val) DO_RUNNING_AVG(avg,val,4)
275 /* BaseSink properties */
277 #define DEFAULT_CAN_ACTIVATE_PULL FALSE /* fixme: enable me */
278 #define DEFAULT_CAN_ACTIVATE_PUSH TRUE
280 #define DEFAULT_SYNC TRUE
281 #define DEFAULT_MAX_LATENESS -1
282 #define DEFAULT_QOS FALSE
283 #define DEFAULT_ASYNC TRUE
284 #define DEFAULT_TS_OFFSET 0
285 #define DEFAULT_BLOCKSIZE 4096
286 #define DEFAULT_RENDER_DELAY 0
287 #define DEFAULT_ENABLE_LAST_SAMPLE TRUE
288 #define DEFAULT_THROTTLE_TIME 0
289 #define DEFAULT_MAX_BITRATE 0
290 #define DEFAULT_DROP_OUT_OF_SEGMENT TRUE
291 #define DEFAULT_PROCESSING_DEADLINE (20 * GST_MSECOND)
301 PROP_ENABLE_LAST_SAMPLE,
307 PROP_PROCESSING_DEADLINE,
311 static GstElementClass *parent_class = NULL;
312 static gint private_offset = 0;
314 static void gst_base_sink_class_init (GstBaseSinkClass * klass);
315 static void gst_base_sink_init (GstBaseSink * trans, gpointer g_class);
316 static void gst_base_sink_finalize (GObject * object);
319 gst_base_sink_get_type (void)
321 static volatile gsize base_sink_type = 0;
323 if (g_once_init_enter (&base_sink_type)) {
325 static const GTypeInfo base_sink_info = {
326 sizeof (GstBaseSinkClass),
329 (GClassInitFunc) gst_base_sink_class_init,
332 sizeof (GstBaseSink),
334 (GInstanceInitFunc) gst_base_sink_init,
337 _type = g_type_register_static (GST_TYPE_ELEMENT,
338 "GstBaseSink", &base_sink_info, G_TYPE_FLAG_ABSTRACT);
341 g_type_add_instance_private (_type, sizeof (GstBaseSinkPrivate));
343 g_once_init_leave (&base_sink_type, _type);
345 return base_sink_type;
348 static inline GstBaseSinkPrivate *
349 gst_base_sink_get_instance_private (GstBaseSink * self)
351 return (G_STRUCT_MEMBER_P (self, private_offset));
354 static void gst_base_sink_set_property (GObject * object, guint prop_id,
355 const GValue * value, GParamSpec * pspec);
356 static void gst_base_sink_get_property (GObject * object, guint prop_id,
357 GValue * value, GParamSpec * pspec);
359 static gboolean gst_base_sink_send_event (GstElement * element,
361 static gboolean default_element_query (GstElement * element, GstQuery * query);
363 static GstCaps *gst_base_sink_default_get_caps (GstBaseSink * sink,
365 static gboolean gst_base_sink_default_set_caps (GstBaseSink * sink,
367 static void gst_base_sink_default_get_times (GstBaseSink * basesink,
368 GstBuffer * buffer, GstClockTime * start, GstClockTime * end);
369 static gboolean gst_base_sink_set_flushing (GstBaseSink * basesink,
370 GstPad * pad, gboolean flushing);
371 static gboolean gst_base_sink_default_activate_pull (GstBaseSink * basesink,
373 static gboolean gst_base_sink_default_do_seek (GstBaseSink * sink,
374 GstSegment * segment);
375 static gboolean gst_base_sink_default_prepare_seek_segment (GstBaseSink * sink,
376 GstEvent * event, GstSegment * segment);
378 static GstStateChangeReturn gst_base_sink_change_state (GstElement * element,
379 GstStateChange transition);
381 static gboolean gst_base_sink_sink_query (GstPad * pad, GstObject * parent,
383 static GstFlowReturn gst_base_sink_chain (GstPad * pad, GstObject * parent,
385 static GstFlowReturn gst_base_sink_chain_list (GstPad * pad, GstObject * parent,
386 GstBufferList * list);
388 static void gst_base_sink_loop (GstPad * pad);
389 static gboolean gst_base_sink_pad_activate (GstPad * pad, GstObject * parent);
390 static gboolean gst_base_sink_pad_activate_mode (GstPad * pad,
391 GstObject * parent, GstPadMode mode, gboolean active);
392 static gboolean gst_base_sink_default_event (GstBaseSink * basesink,
394 static GstFlowReturn gst_base_sink_default_wait_event (GstBaseSink * basesink,
396 static gboolean gst_base_sink_event (GstPad * pad, GstObject * parent,
399 static gboolean gst_base_sink_default_query (GstBaseSink * sink,
402 static gboolean gst_base_sink_negotiate_pull (GstBaseSink * basesink);
403 static GstCaps *gst_base_sink_default_fixate (GstBaseSink * bsink,
405 static GstCaps *gst_base_sink_fixate (GstBaseSink * bsink, GstCaps * caps);
407 /* check if an object was too late */
408 static gboolean gst_base_sink_is_too_late (GstBaseSink * basesink,
409 GstMiniObject * obj, GstClockTime rstart, GstClockTime rstop,
410 GstClockReturn status, GstClockTimeDiff jitter, gboolean render);
413 gst_base_sink_class_init (GstBaseSinkClass * klass)
415 GObjectClass *gobject_class;
416 GstElementClass *gstelement_class;
418 gobject_class = G_OBJECT_CLASS (klass);
419 gstelement_class = GST_ELEMENT_CLASS (klass);
421 if (private_offset != 0)
422 g_type_class_adjust_private_offset (klass, &private_offset);
424 GST_DEBUG_CATEGORY_INIT (gst_base_sink_debug, "basesink", 0,
427 parent_class = g_type_class_peek_parent (klass);
429 gobject_class->finalize = gst_base_sink_finalize;
430 gobject_class->set_property = gst_base_sink_set_property;
431 gobject_class->get_property = gst_base_sink_get_property;
433 g_object_class_install_property (gobject_class, PROP_SYNC,
434 g_param_spec_boolean ("sync", "Sync", "Sync on the clock", DEFAULT_SYNC,
435 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
437 g_object_class_install_property (gobject_class, PROP_MAX_LATENESS,
438 g_param_spec_int64 ("max-lateness", "Max Lateness",
439 "Maximum number of nanoseconds that a buffer can be late before it "
440 "is dropped (-1 unlimited)", -1, G_MAXINT64, DEFAULT_MAX_LATENESS,
441 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
443 g_object_class_install_property (gobject_class, PROP_QOS,
444 g_param_spec_boolean ("qos", "Qos",
445 "Generate Quality-of-Service events upstream", DEFAULT_QOS,
446 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
450 * If set to %TRUE, the basesink will perform asynchronous state changes.
451 * When set to %FALSE, the sink will not signal the parent when it prerolls.
452 * Use this option when dealing with sparse streams or when synchronisation is
455 g_object_class_install_property (gobject_class, PROP_ASYNC,
456 g_param_spec_boolean ("async", "Async",
457 "Go asynchronously to PAUSED", DEFAULT_ASYNC,
458 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
460 * GstBaseSink:ts-offset:
462 * Controls the final synchronisation, a negative value will render the buffer
463 * earlier while a positive value delays playback. This property can be
464 * used to fix synchronisation in bad files.
466 g_object_class_install_property (gobject_class, PROP_TS_OFFSET,
467 g_param_spec_int64 ("ts-offset", "TS Offset",
468 "Timestamp offset in nanoseconds", G_MININT64, G_MAXINT64,
469 DEFAULT_TS_OFFSET, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
472 * GstBaseSink:enable-last-sample:
474 * Enable the last-sample property. If %FALSE, basesink doesn't keep a
475 * reference to the last buffer arrived and the last-sample property is always
476 * set to %NULL. This can be useful if you need buffers to be released as soon
477 * as possible, eg. if you're using a buffer pool.
479 g_object_class_install_property (gobject_class, PROP_ENABLE_LAST_SAMPLE,
480 g_param_spec_boolean ("enable-last-sample", "Enable Last Buffer",
481 "Enable the last-sample property", DEFAULT_ENABLE_LAST_SAMPLE,
482 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
485 * GstBaseSink:last-sample:
487 * The last buffer that arrived in the sink and was used for preroll or for
488 * rendering. This property can be used to generate thumbnails. This property
489 * can be %NULL when the sink has not yet received a buffer.
491 g_object_class_install_property (gobject_class, PROP_LAST_SAMPLE,
492 g_param_spec_boxed ("last-sample", "Last Sample",
493 "The last sample received in the sink", GST_TYPE_SAMPLE,
494 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
496 * GstBaseSink:blocksize:
498 * The amount of bytes to pull when operating in pull mode.
500 /* FIXME 2.0: blocksize property should be int, otherwise min>max.. */
501 g_object_class_install_property (gobject_class, PROP_BLOCKSIZE,
502 g_param_spec_uint ("blocksize", "Block size",
503 "Size in bytes to pull per buffer (0 = default)", 0, G_MAXUINT,
504 DEFAULT_BLOCKSIZE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
506 * GstBaseSink:render-delay:
508 * The additional delay between synchronisation and actual rendering of the
509 * media. This property will add additional latency to the device in order to
510 * make other sinks compensate for the delay.
512 g_object_class_install_property (gobject_class, PROP_RENDER_DELAY,
513 g_param_spec_uint64 ("render-delay", "Render Delay",
514 "Additional render delay of the sink in nanoseconds", 0, G_MAXUINT64,
515 DEFAULT_RENDER_DELAY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
517 * GstBaseSink:throttle-time:
519 * The time to insert between buffers. This property can be used to control
520 * the maximum amount of buffers per second to render. Setting this property
521 * to a value bigger than 0 will make the sink create THROTTLE QoS events.
523 g_object_class_install_property (gobject_class, PROP_THROTTLE_TIME,
524 g_param_spec_uint64 ("throttle-time", "Throttle time",
525 "The time to keep between rendered buffers (0 = disabled)", 0,
526 G_MAXUINT64, DEFAULT_THROTTLE_TIME,
527 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
529 * GstBaseSink:max-bitrate:
531 * Control the maximum amount of bits that will be rendered per second.
532 * Setting this property to a value bigger than 0 will make the sink delay
533 * rendering of the buffers when it would exceed to max-bitrate.
537 g_object_class_install_property (gobject_class, PROP_MAX_BITRATE,
538 g_param_spec_uint64 ("max-bitrate", "Max Bitrate",
539 "The maximum bits per second to render (0 = disabled)", 0,
540 G_MAXUINT64, DEFAULT_MAX_BITRATE,
541 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
543 * GstBaseSink:processing-deadline:
545 * Maximum amount of time (in nanoseconds) that the pipeline can take
546 * for processing the buffer. This is added to the latency of live
551 g_object_class_install_property (gobject_class, PROP_PROCESSING_DEADLINE,
552 g_param_spec_uint64 ("processing-deadline", "Processing deadline",
553 "Maximum processing deadline in nanoseconds", 0, G_MAXUINT64,
554 DEFAULT_PROCESSING_DEADLINE,
555 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
557 gstelement_class->change_state =
558 GST_DEBUG_FUNCPTR (gst_base_sink_change_state);
559 gstelement_class->send_event = GST_DEBUG_FUNCPTR (gst_base_sink_send_event);
560 gstelement_class->query = GST_DEBUG_FUNCPTR (default_element_query);
562 klass->get_caps = GST_DEBUG_FUNCPTR (gst_base_sink_default_get_caps);
563 klass->set_caps = GST_DEBUG_FUNCPTR (gst_base_sink_default_set_caps);
564 klass->fixate = GST_DEBUG_FUNCPTR (gst_base_sink_default_fixate);
565 klass->activate_pull =
566 GST_DEBUG_FUNCPTR (gst_base_sink_default_activate_pull);
567 klass->get_times = GST_DEBUG_FUNCPTR (gst_base_sink_default_get_times);
568 klass->query = GST_DEBUG_FUNCPTR (gst_base_sink_default_query);
569 klass->event = GST_DEBUG_FUNCPTR (gst_base_sink_default_event);
570 klass->wait_event = GST_DEBUG_FUNCPTR (gst_base_sink_default_wait_event);
572 /* Registering debug symbols for function pointers */
573 GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_fixate);
574 GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_pad_activate);
575 GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_pad_activate_mode);
576 GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_event);
577 GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_chain);
578 GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_chain_list);
579 GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_sink_query);
583 gst_base_sink_query_caps (GstBaseSink * bsink, GstPad * pad, GstCaps * filter)
585 GstBaseSinkClass *bclass;
586 GstCaps *caps = NULL;
589 bclass = GST_BASE_SINK_GET_CLASS (bsink);
590 fixed = GST_PAD_IS_FIXED_CAPS (pad);
592 if (fixed || bsink->pad_mode == GST_PAD_MODE_PULL) {
593 /* if we are operating in pull mode or fixed caps, we only accept the
594 * currently negotiated caps */
595 caps = gst_pad_get_current_caps (pad);
598 if (bclass->get_caps)
599 caps = bclass->get_caps (bsink, filter);
602 GstPadTemplate *pad_template;
605 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (bclass),
607 if (pad_template != NULL) {
608 caps = gst_pad_template_get_caps (pad_template);
611 GstCaps *intersection;
614 gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
615 gst_caps_unref (caps);
626 gst_base_sink_default_fixate (GstBaseSink * bsink, GstCaps * caps)
628 GST_DEBUG_OBJECT (bsink, "using default caps fixate function");
629 return gst_caps_fixate (caps);
633 gst_base_sink_fixate (GstBaseSink * bsink, GstCaps * caps)
635 GstBaseSinkClass *bclass;
637 bclass = GST_BASE_SINK_GET_CLASS (bsink);
640 caps = bclass->fixate (bsink, caps);
646 gst_base_sink_init (GstBaseSink * basesink, gpointer g_class)
648 GstPadTemplate *pad_template;
649 GstBaseSinkPrivate *priv;
651 basesink->priv = priv = gst_base_sink_get_instance_private (basesink);
654 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (g_class), "sink");
655 g_return_if_fail (pad_template != NULL);
657 basesink->sinkpad = gst_pad_new_from_template (pad_template, "sink");
659 gst_pad_set_activate_function (basesink->sinkpad, gst_base_sink_pad_activate);
660 gst_pad_set_activatemode_function (basesink->sinkpad,
661 gst_base_sink_pad_activate_mode);
662 gst_pad_set_query_function (basesink->sinkpad, gst_base_sink_sink_query);
663 gst_pad_set_event_function (basesink->sinkpad, gst_base_sink_event);
664 gst_pad_set_chain_function (basesink->sinkpad, gst_base_sink_chain);
665 gst_pad_set_chain_list_function (basesink->sinkpad, gst_base_sink_chain_list);
666 gst_element_add_pad (GST_ELEMENT_CAST (basesink), basesink->sinkpad);
668 basesink->pad_mode = GST_PAD_MODE_NONE;
669 g_mutex_init (&basesink->preroll_lock);
670 g_cond_init (&basesink->preroll_cond);
671 priv->have_latency = FALSE;
673 basesink->can_activate_push = DEFAULT_CAN_ACTIVATE_PUSH;
674 basesink->can_activate_pull = DEFAULT_CAN_ACTIVATE_PULL;
676 basesink->sync = DEFAULT_SYNC;
677 basesink->max_lateness = DEFAULT_MAX_LATENESS;
678 g_atomic_int_set (&priv->qos_enabled, DEFAULT_QOS);
679 priv->async_enabled = DEFAULT_ASYNC;
680 priv->ts_offset = DEFAULT_TS_OFFSET;
681 priv->render_delay = DEFAULT_RENDER_DELAY;
682 priv->processing_deadline = DEFAULT_PROCESSING_DEADLINE;
683 priv->blocksize = DEFAULT_BLOCKSIZE;
684 priv->cached_clock_id = NULL;
685 g_atomic_int_set (&priv->enable_last_sample, DEFAULT_ENABLE_LAST_SAMPLE);
686 priv->throttle_time = DEFAULT_THROTTLE_TIME;
687 priv->max_bitrate = DEFAULT_MAX_BITRATE;
689 priv->drop_out_of_segment = DEFAULT_DROP_OUT_OF_SEGMENT;
691 GST_OBJECT_FLAG_SET (basesink, GST_ELEMENT_FLAG_SINK);
695 gst_base_sink_finalize (GObject * object)
697 GstBaseSink *basesink;
699 basesink = GST_BASE_SINK (object);
701 g_mutex_clear (&basesink->preroll_lock);
702 g_cond_clear (&basesink->preroll_cond);
704 G_OBJECT_CLASS (parent_class)->finalize (object);
708 * gst_base_sink_set_sync:
710 * @sync: the new sync value.
712 * Configures @sink to synchronize on the clock or not. When
713 * @sync is %FALSE, incoming samples will be played as fast as
714 * possible. If @sync is %TRUE, the timestamps of the incoming
715 * buffers will be used to schedule the exact render time of its
719 gst_base_sink_set_sync (GstBaseSink * sink, gboolean sync)
721 g_return_if_fail (GST_IS_BASE_SINK (sink));
723 GST_OBJECT_LOCK (sink);
725 GST_OBJECT_UNLOCK (sink);
729 * gst_base_sink_get_sync:
732 * Checks if @sink is currently configured to synchronize against the
735 * Returns: %TRUE if the sink is configured to synchronize against the clock.
738 gst_base_sink_get_sync (GstBaseSink * sink)
742 g_return_val_if_fail (GST_IS_BASE_SINK (sink), FALSE);
744 GST_OBJECT_LOCK (sink);
746 GST_OBJECT_UNLOCK (sink);
752 * gst_base_sink_set_drop_out_of_segment:
754 * @drop_out_of_segment: drop buffers outside the segment
756 * Configure @sink to drop buffers which are outside the current segment
761 gst_base_sink_set_drop_out_of_segment (GstBaseSink * sink,
762 gboolean drop_out_of_segment)
764 g_return_if_fail (GST_IS_BASE_SINK (sink));
766 GST_OBJECT_LOCK (sink);
767 sink->priv->drop_out_of_segment = drop_out_of_segment;
768 GST_OBJECT_UNLOCK (sink);
773 * gst_base_sink_get_drop_out_of_segment:
776 * Checks if @sink is currently configured to drop buffers which are outside
777 * the current segment
779 * Returns: %TRUE if the sink is configured to drop buffers outside the
785 gst_base_sink_get_drop_out_of_segment (GstBaseSink * sink)
789 g_return_val_if_fail (GST_IS_BASE_SINK (sink), FALSE);
791 GST_OBJECT_LOCK (sink);
792 res = sink->priv->drop_out_of_segment;
793 GST_OBJECT_UNLOCK (sink);
799 * gst_base_sink_set_max_lateness:
801 * @max_lateness: the new max lateness value.
803 * Sets the new max lateness value to @max_lateness. This value is
804 * used to decide if a buffer should be dropped or not based on the
805 * buffer timestamp and the current clock time. A value of -1 means
809 gst_base_sink_set_max_lateness (GstBaseSink * sink, gint64 max_lateness)
811 g_return_if_fail (GST_IS_BASE_SINK (sink));
813 GST_OBJECT_LOCK (sink);
814 sink->max_lateness = max_lateness;
815 GST_OBJECT_UNLOCK (sink);
819 * gst_base_sink_get_max_lateness:
822 * Gets the max lateness value. See gst_base_sink_set_max_lateness() for
825 * Returns: The maximum time in nanoseconds that a buffer can be late
826 * before it is dropped and not rendered. A value of -1 means an
830 gst_base_sink_get_max_lateness (GstBaseSink * sink)
834 g_return_val_if_fail (GST_IS_BASE_SINK (sink), -1);
836 GST_OBJECT_LOCK (sink);
837 res = sink->max_lateness;
838 GST_OBJECT_UNLOCK (sink);
844 * gst_base_sink_set_qos_enabled:
846 * @enabled: the new qos value.
848 * Configures @sink to send Quality-of-Service events upstream.
851 gst_base_sink_set_qos_enabled (GstBaseSink * sink, gboolean enabled)
853 g_return_if_fail (GST_IS_BASE_SINK (sink));
855 g_atomic_int_set (&sink->priv->qos_enabled, enabled);
859 * gst_base_sink_is_qos_enabled:
862 * Checks if @sink is currently configured to send Quality-of-Service events
865 * Returns: %TRUE if the sink is configured to perform Quality-of-Service.
868 gst_base_sink_is_qos_enabled (GstBaseSink * sink)
872 g_return_val_if_fail (GST_IS_BASE_SINK (sink), FALSE);
874 res = g_atomic_int_get (&sink->priv->qos_enabled);
880 * gst_base_sink_set_async_enabled:
882 * @enabled: the new async value.
884 * Configures @sink to perform all state changes asynchronously. When async is
885 * disabled, the sink will immediately go to PAUSED instead of waiting for a
886 * preroll buffer. This feature is useful if the sink does not synchronize
887 * against the clock or when it is dealing with sparse streams.
890 gst_base_sink_set_async_enabled (GstBaseSink * sink, gboolean enabled)
892 g_return_if_fail (GST_IS_BASE_SINK (sink));
894 GST_BASE_SINK_PREROLL_LOCK (sink);
895 g_atomic_int_set (&sink->priv->async_enabled, enabled);
896 GST_LOG_OBJECT (sink, "set async enabled to %d", enabled);
897 GST_BASE_SINK_PREROLL_UNLOCK (sink);
901 * gst_base_sink_is_async_enabled:
904 * Checks if @sink is currently configured to perform asynchronous state
907 * Returns: %TRUE if the sink is configured to perform asynchronous state
911 gst_base_sink_is_async_enabled (GstBaseSink * sink)
915 g_return_val_if_fail (GST_IS_BASE_SINK (sink), FALSE);
917 res = g_atomic_int_get (&sink->priv->async_enabled);
923 * gst_base_sink_set_ts_offset:
925 * @offset: the new offset
927 * Adjust the synchronisation of @sink with @offset. A negative value will
928 * render buffers earlier than their timestamp. A positive value will delay
929 * rendering. This function can be used to fix playback of badly timestamped
933 gst_base_sink_set_ts_offset (GstBaseSink * sink, GstClockTimeDiff offset)
935 g_return_if_fail (GST_IS_BASE_SINK (sink));
937 GST_OBJECT_LOCK (sink);
938 sink->priv->ts_offset = offset;
939 GST_LOG_OBJECT (sink, "set time offset to %" G_GINT64_FORMAT, offset);
940 GST_OBJECT_UNLOCK (sink);
944 * gst_base_sink_get_ts_offset:
947 * Get the synchronisation offset of @sink.
949 * Returns: The synchronisation offset.
952 gst_base_sink_get_ts_offset (GstBaseSink * sink)
954 GstClockTimeDiff res;
956 g_return_val_if_fail (GST_IS_BASE_SINK (sink), 0);
958 GST_OBJECT_LOCK (sink);
959 res = sink->priv->ts_offset;
960 GST_OBJECT_UNLOCK (sink);
966 * gst_base_sink_get_last_sample:
969 * Get the last sample that arrived in the sink and was used for preroll or for
970 * rendering. This property can be used to generate thumbnails.
972 * The #GstCaps on the sample can be used to determine the type of the buffer.
974 * Free-function: gst_sample_unref
976 * Returns: (transfer full) (nullable): a #GstSample. gst_sample_unref() after
977 * usage. This function returns %NULL when no buffer has arrived in the
978 * sink yet or when the sink is not in PAUSED or PLAYING.
981 gst_base_sink_get_last_sample (GstBaseSink * sink)
983 GstSample *res = NULL;
985 g_return_val_if_fail (GST_IS_BASE_SINK (sink), NULL);
987 GST_OBJECT_LOCK (sink);
988 if (sink->priv->last_buffer_list) {
989 GstBuffer *first_buffer = NULL;
991 /* Set the first buffer in the list to last sample's buffer */
992 first_buffer = gst_buffer_list_get (sink->priv->last_buffer_list, 0);
994 gst_sample_new (first_buffer, sink->priv->last_caps, &sink->segment,
996 gst_sample_set_buffer_list (res, sink->priv->last_buffer_list);
997 } else if (sink->priv->last_buffer) {
998 res = gst_sample_new (sink->priv->last_buffer,
999 sink->priv->last_caps, &sink->segment, NULL);
1001 GST_OBJECT_UNLOCK (sink);
1006 /* with OBJECT_LOCK */
1008 gst_base_sink_set_last_buffer_unlocked (GstBaseSink * sink, GstBuffer * buffer)
1012 old = sink->priv->last_buffer;
1013 if (G_LIKELY (old != buffer)) {
1014 GST_DEBUG_OBJECT (sink, "setting last buffer to %p", buffer);
1015 if (G_LIKELY (buffer))
1016 gst_buffer_ref (buffer);
1017 sink->priv->last_buffer = buffer;
1019 /* copy over the caps */
1020 gst_caps_replace (&sink->priv->last_caps, sink->priv->caps);
1022 gst_caps_replace (&sink->priv->last_caps, NULL);
1026 /* avoid unreffing with the lock because cleanup code might want to take the
1028 if (G_LIKELY (old)) {
1029 GST_OBJECT_UNLOCK (sink);
1030 gst_buffer_unref (old);
1031 GST_OBJECT_LOCK (sink);
1035 /* with OBJECT_LOCK */
1037 gst_base_sink_set_last_buffer_list_unlocked (GstBaseSink * sink,
1038 GstBufferList * buffer_list)
1042 old = sink->priv->last_buffer_list;
1043 if (G_LIKELY (old != buffer_list)) {
1044 GST_DEBUG_OBJECT (sink, "setting last buffer list to %p", buffer_list);
1045 if (G_LIKELY (buffer_list))
1046 gst_mini_object_ref (GST_MINI_OBJECT_CAST (buffer_list));
1047 sink->priv->last_buffer_list = buffer_list;
1052 /* avoid unreffing with the lock because cleanup code might want to take the
1054 if (G_LIKELY (old)) {
1055 GST_OBJECT_UNLOCK (sink);
1056 gst_mini_object_unref (GST_MINI_OBJECT_CAST (old));
1057 GST_OBJECT_LOCK (sink);
1062 gst_base_sink_set_last_buffer (GstBaseSink * sink, GstBuffer * buffer)
1064 if (!g_atomic_int_get (&sink->priv->enable_last_sample))
1067 GST_OBJECT_LOCK (sink);
1068 gst_base_sink_set_last_buffer_unlocked (sink, buffer);
1069 GST_OBJECT_UNLOCK (sink);
1073 gst_base_sink_set_last_buffer_list (GstBaseSink * sink,
1074 GstBufferList * buffer_list)
1076 if (!g_atomic_int_get (&sink->priv->enable_last_sample))
1079 GST_OBJECT_LOCK (sink);
1080 gst_base_sink_set_last_buffer_list_unlocked (sink, buffer_list);
1081 GST_OBJECT_UNLOCK (sink);
1085 * gst_base_sink_set_last_sample_enabled:
1087 * @enabled: the new enable-last-sample value.
1089 * Configures @sink to store the last received sample in the last-sample
1093 gst_base_sink_set_last_sample_enabled (GstBaseSink * sink, gboolean enabled)
1095 g_return_if_fail (GST_IS_BASE_SINK (sink));
1097 /* Only take lock if we change the value */
1098 if (g_atomic_int_compare_and_exchange (&sink->priv->enable_last_sample,
1099 !enabled, enabled) && !enabled) {
1100 GST_OBJECT_LOCK (sink);
1101 gst_base_sink_set_last_buffer_unlocked (sink, NULL);
1102 gst_base_sink_set_last_buffer_list_unlocked (sink, NULL);
1103 GST_OBJECT_UNLOCK (sink);
1108 * gst_base_sink_is_last_sample_enabled:
1111 * Checks if @sink is currently configured to store the last received sample in
1112 * the last-sample property.
1114 * Returns: %TRUE if the sink is configured to store the last received sample.
1117 gst_base_sink_is_last_sample_enabled (GstBaseSink * sink)
1119 g_return_val_if_fail (GST_IS_BASE_SINK (sink), FALSE);
1121 return g_atomic_int_get (&sink->priv->enable_last_sample);
1125 * gst_base_sink_get_latency:
1128 * Get the currently configured latency.
1130 * Returns: The configured latency.
1133 gst_base_sink_get_latency (GstBaseSink * sink)
1137 GST_OBJECT_LOCK (sink);
1138 res = sink->priv->latency;
1139 GST_OBJECT_UNLOCK (sink);
1145 * gst_base_sink_query_latency:
1147 * @live: (out) (allow-none): if the sink is live
1148 * @upstream_live: (out) (allow-none): if an upstream element is live
1149 * @min_latency: (out) (allow-none): the min latency of the upstream elements
1150 * @max_latency: (out) (allow-none): the max latency of the upstream elements
1152 * Query the sink for the latency parameters. The latency will be queried from
1153 * the upstream elements. @live will be %TRUE if @sink is configured to
1154 * synchronize against the clock. @upstream_live will be %TRUE if an upstream
1157 * If both @live and @upstream_live are %TRUE, the sink will want to compensate
1158 * for the latency introduced by the upstream elements by setting the
1159 * @min_latency to a strictly positive value.
1161 * This function is mostly used by subclasses.
1163 * Returns: %TRUE if the query succeeded.
1166 gst_base_sink_query_latency (GstBaseSink * sink, gboolean * live,
1167 gboolean * upstream_live, GstClockTime * min_latency,
1168 GstClockTime * max_latency)
1170 gboolean l, us_live, res, have_latency;
1171 GstClockTime min, max, render_delay, processing_deadline;
1173 GstClockTime us_min, us_max;
1175 /* we are live when we sync to the clock */
1176 GST_OBJECT_LOCK (sink);
1178 have_latency = sink->priv->have_latency;
1179 render_delay = sink->priv->render_delay;
1180 processing_deadline = sink->priv->processing_deadline;
1181 GST_OBJECT_UNLOCK (sink);
1183 /* assume no latency */
1189 GST_DEBUG_OBJECT (sink, "we are ready for LATENCY query");
1190 /* we are ready for a latency query this is when we preroll or when we are
1192 query = gst_query_new_latency ();
1194 /* ask the peer for the latency */
1195 if ((res = gst_pad_peer_query (sink->sinkpad, query))) {
1196 /* get upstream min and max latency */
1197 gst_query_parse_latency (query, &us_live, &us_min, &us_max);
1200 /* upstream live, use its latency, subclasses should use these
1201 * values to create the complete latency. */
1206 /* we need to add the render delay if we are live */
1207 min += render_delay;
1209 max += render_delay;
1211 if (max == -1 || min + processing_deadline <= max)
1212 min += processing_deadline;
1214 GST_ELEMENT_WARNING (sink, CORE, CLOCK,
1215 (_("Pipeline construction is invalid, please add queues.")),
1216 ("Not enough buffering available for "
1217 " the processing deadline of %" GST_TIME_FORMAT
1218 ", add enough queues to buffer %" GST_TIME_FORMAT
1219 " additional data. Shortening processing latency to %"
1220 GST_TIME_FORMAT ".",
1221 GST_TIME_ARGS (processing_deadline),
1222 GST_TIME_ARGS (min + processing_deadline - max),
1223 GST_TIME_ARGS (max - min)));
1228 gst_query_unref (query);
1230 GST_DEBUG_OBJECT (sink, "we are not yet ready for LATENCY query");
1234 /* not live, we tried to do the query, if it failed we return TRUE anyway */
1238 GST_DEBUG_OBJECT (sink, "latency query failed but we are not live");
1240 GST_DEBUG_OBJECT (sink, "latency query failed and we are live");
1245 GST_DEBUG_OBJECT (sink, "latency query: live: %d, have_latency %d,"
1246 " upstream: %d, min %" GST_TIME_FORMAT ", max %" GST_TIME_FORMAT, l,
1247 have_latency, us_live, GST_TIME_ARGS (min), GST_TIME_ARGS (max));
1252 *upstream_live = us_live;
1262 * gst_base_sink_set_render_delay:
1263 * @sink: a #GstBaseSink
1264 * @delay: the new delay
1266 * Set the render delay in @sink to @delay. The render delay is the time
1267 * between actual rendering of a buffer and its synchronisation time. Some
1268 * devices might delay media rendering which can be compensated for with this
1271 * After calling this function, this sink will report additional latency and
1272 * other sinks will adjust their latency to delay the rendering of their media.
1274 * This function is usually called by subclasses.
1277 gst_base_sink_set_render_delay (GstBaseSink * sink, GstClockTime delay)
1279 GstClockTime old_render_delay;
1281 g_return_if_fail (GST_IS_BASE_SINK (sink));
1283 GST_OBJECT_LOCK (sink);
1284 old_render_delay = sink->priv->render_delay;
1285 sink->priv->render_delay = delay;
1286 GST_LOG_OBJECT (sink, "set render delay to %" GST_TIME_FORMAT,
1287 GST_TIME_ARGS (delay));
1288 GST_OBJECT_UNLOCK (sink);
1290 if (delay != old_render_delay) {
1291 GST_DEBUG_OBJECT (sink, "posting latency changed");
1292 gst_element_post_message (GST_ELEMENT_CAST (sink),
1293 gst_message_new_latency (GST_OBJECT_CAST (sink)));
1298 * gst_base_sink_get_render_delay:
1299 * @sink: a #GstBaseSink
1301 * Get the render delay of @sink. see gst_base_sink_set_render_delay() for more
1302 * information about the render delay.
1304 * Returns: the render delay of @sink.
1307 gst_base_sink_get_render_delay (GstBaseSink * sink)
1309 GstClockTimeDiff res;
1311 g_return_val_if_fail (GST_IS_BASE_SINK (sink), 0);
1313 GST_OBJECT_LOCK (sink);
1314 res = sink->priv->render_delay;
1315 GST_OBJECT_UNLOCK (sink);
1321 * gst_base_sink_set_blocksize:
1322 * @sink: a #GstBaseSink
1323 * @blocksize: the blocksize in bytes
1325 * Set the number of bytes that the sink will pull when it is operating in pull
1328 /* FIXME 2.0: blocksize property should be int, otherwise min>max.. */
1330 gst_base_sink_set_blocksize (GstBaseSink * sink, guint blocksize)
1332 g_return_if_fail (GST_IS_BASE_SINK (sink));
1334 GST_OBJECT_LOCK (sink);
1335 sink->priv->blocksize = blocksize;
1336 GST_LOG_OBJECT (sink, "set blocksize to %u", blocksize);
1337 GST_OBJECT_UNLOCK (sink);
1341 * gst_base_sink_get_blocksize:
1342 * @sink: a #GstBaseSink
1344 * Get the number of bytes that the sink will pull when it is operating in pull
1347 * Returns: the number of bytes @sink will pull in pull mode.
1349 /* FIXME 2.0: blocksize property should be int, otherwise min>max.. */
1351 gst_base_sink_get_blocksize (GstBaseSink * sink)
1355 g_return_val_if_fail (GST_IS_BASE_SINK (sink), 0);
1357 GST_OBJECT_LOCK (sink);
1358 res = sink->priv->blocksize;
1359 GST_OBJECT_UNLOCK (sink);
1365 * gst_base_sink_set_throttle_time:
1366 * @sink: a #GstBaseSink
1367 * @throttle: the throttle time in nanoseconds
1369 * Set the time that will be inserted between rendered buffers. This
1370 * can be used to control the maximum buffers per second that the sink
1374 gst_base_sink_set_throttle_time (GstBaseSink * sink, guint64 throttle)
1376 g_return_if_fail (GST_IS_BASE_SINK (sink));
1378 GST_OBJECT_LOCK (sink);
1379 sink->priv->throttle_time = throttle;
1380 GST_LOG_OBJECT (sink, "set throttle_time to %" G_GUINT64_FORMAT, throttle);
1381 GST_OBJECT_UNLOCK (sink);
1385 * gst_base_sink_get_throttle_time:
1386 * @sink: a #GstBaseSink
1388 * Get the time that will be inserted between frames to control the
1389 * maximum buffers per second.
1391 * Returns: the number of nanoseconds @sink will put between frames.
1394 gst_base_sink_get_throttle_time (GstBaseSink * sink)
1398 g_return_val_if_fail (GST_IS_BASE_SINK (sink), 0);
1400 GST_OBJECT_LOCK (sink);
1401 res = sink->priv->throttle_time;
1402 GST_OBJECT_UNLOCK (sink);
1408 * gst_base_sink_set_max_bitrate:
1409 * @sink: a #GstBaseSink
1410 * @max_bitrate: the max_bitrate in bits per second
1412 * Set the maximum amount of bits per second that the sink will render.
1417 gst_base_sink_set_max_bitrate (GstBaseSink * sink, guint64 max_bitrate)
1419 g_return_if_fail (GST_IS_BASE_SINK (sink));
1421 GST_OBJECT_LOCK (sink);
1422 sink->priv->max_bitrate = max_bitrate;
1423 GST_LOG_OBJECT (sink, "set max_bitrate to %" G_GUINT64_FORMAT, max_bitrate);
1424 GST_OBJECT_UNLOCK (sink);
1428 * gst_base_sink_get_max_bitrate:
1429 * @sink: a #GstBaseSink
1431 * Get the maximum amount of bits per second that the sink will render.
1433 * Returns: the maximum number of bits per second @sink will render.
1438 gst_base_sink_get_max_bitrate (GstBaseSink * sink)
1442 g_return_val_if_fail (GST_IS_BASE_SINK (sink), 0);
1444 GST_OBJECT_LOCK (sink);
1445 res = sink->priv->max_bitrate;
1446 GST_OBJECT_UNLOCK (sink);
1452 * gst_base_sink_set_processing_deadline:
1453 * @sink: a #GstBaseSink
1454 * @delay: the new processing deadline in nanoseconds.
1456 * Maximum amount of time (in nanoseconds) that the pipeline can take
1457 * for processing the buffer. This is added to the latency of live
1460 * This function is usually called by subclasses.
1465 gst_base_sink_set_processing_deadline (GstBaseSink * sink, GstClockTime delay)
1467 GstClockTime old_processing_deadline;
1469 g_return_if_fail (GST_IS_BASE_SINK (sink));
1471 GST_OBJECT_LOCK (sink);
1472 old_processing_deadline = sink->priv->processing_deadline;
1473 sink->priv->processing_deadline = delay;
1474 GST_LOG_OBJECT (sink, "set render delay to %" GST_TIME_FORMAT,
1475 GST_TIME_ARGS (delay));
1476 GST_OBJECT_UNLOCK (sink);
1478 if (delay != old_processing_deadline) {
1479 GST_DEBUG_OBJECT (sink, "posting latency changed");
1480 gst_element_post_message (GST_ELEMENT_CAST (sink),
1481 gst_message_new_latency (GST_OBJECT_CAST (sink)));
1486 * gst_base_sink_get_processing_deadline:
1487 * @sink: a #GstBaseSink
1489 * Get the processing deadline of @sink. see
1490 * gst_base_sink_set_processing_deadline() for more information about
1491 * the processing deadline.
1493 * Returns: the processing deadline
1498 gst_base_sink_get_processing_deadline (GstBaseSink * sink)
1500 GstClockTimeDiff res;
1502 g_return_val_if_fail (GST_IS_BASE_SINK (sink), 0);
1504 GST_OBJECT_LOCK (sink);
1505 res = sink->priv->processing_deadline;
1506 GST_OBJECT_UNLOCK (sink);
1512 gst_base_sink_set_property (GObject * object, guint prop_id,
1513 const GValue * value, GParamSpec * pspec)
1515 GstBaseSink *sink = GST_BASE_SINK (object);
1519 gst_base_sink_set_sync (sink, g_value_get_boolean (value));
1521 case PROP_MAX_LATENESS:
1522 gst_base_sink_set_max_lateness (sink, g_value_get_int64 (value));
1525 gst_base_sink_set_qos_enabled (sink, g_value_get_boolean (value));
1528 gst_base_sink_set_async_enabled (sink, g_value_get_boolean (value));
1530 case PROP_TS_OFFSET:
1531 gst_base_sink_set_ts_offset (sink, g_value_get_int64 (value));
1533 case PROP_BLOCKSIZE:
1534 gst_base_sink_set_blocksize (sink, g_value_get_uint (value));
1536 case PROP_RENDER_DELAY:
1537 gst_base_sink_set_render_delay (sink, g_value_get_uint64 (value));
1539 case PROP_ENABLE_LAST_SAMPLE:
1540 gst_base_sink_set_last_sample_enabled (sink, g_value_get_boolean (value));
1542 case PROP_THROTTLE_TIME:
1543 gst_base_sink_set_throttle_time (sink, g_value_get_uint64 (value));
1545 case PROP_MAX_BITRATE:
1546 gst_base_sink_set_max_bitrate (sink, g_value_get_uint64 (value));
1548 case PROP_PROCESSING_DEADLINE:
1549 gst_base_sink_set_processing_deadline (sink, g_value_get_uint64 (value));
1552 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1558 gst_base_sink_get_property (GObject * object, guint prop_id, GValue * value,
1561 GstBaseSink *sink = GST_BASE_SINK (object);
1565 g_value_set_boolean (value, gst_base_sink_get_sync (sink));
1567 case PROP_MAX_LATENESS:
1568 g_value_set_int64 (value, gst_base_sink_get_max_lateness (sink));
1571 g_value_set_boolean (value, gst_base_sink_is_qos_enabled (sink));
1574 g_value_set_boolean (value, gst_base_sink_is_async_enabled (sink));
1576 case PROP_TS_OFFSET:
1577 g_value_set_int64 (value, gst_base_sink_get_ts_offset (sink));
1579 case PROP_LAST_SAMPLE:
1580 gst_value_take_sample (value, gst_base_sink_get_last_sample (sink));
1582 case PROP_ENABLE_LAST_SAMPLE:
1583 g_value_set_boolean (value, gst_base_sink_is_last_sample_enabled (sink));
1585 case PROP_BLOCKSIZE:
1586 g_value_set_uint (value, gst_base_sink_get_blocksize (sink));
1588 case PROP_RENDER_DELAY:
1589 g_value_set_uint64 (value, gst_base_sink_get_render_delay (sink));
1591 case PROP_THROTTLE_TIME:
1592 g_value_set_uint64 (value, gst_base_sink_get_throttle_time (sink));
1594 case PROP_MAX_BITRATE:
1595 g_value_set_uint64 (value, gst_base_sink_get_max_bitrate (sink));
1597 case PROP_PROCESSING_DEADLINE:
1598 g_value_set_uint64 (value, gst_base_sink_get_processing_deadline (sink));
1601 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1608 gst_base_sink_default_get_caps (GstBaseSink * sink, GstCaps * filter)
1614 gst_base_sink_default_set_caps (GstBaseSink * sink, GstCaps * caps)
1619 /* with PREROLL_LOCK, STREAM_LOCK */
1621 gst_base_sink_commit_state (GstBaseSink * basesink)
1623 /* commit state and proceed to next pending state */
1624 GstState current, next, pending, post_pending;
1625 gboolean post_paused = FALSE;
1626 gboolean post_async_done = FALSE;
1627 gboolean post_playing = FALSE;
1629 /* we are certainly not playing async anymore now */
1630 basesink->playing_async = FALSE;
1632 GST_OBJECT_LOCK (basesink);
1633 current = GST_STATE (basesink);
1634 next = GST_STATE_NEXT (basesink);
1635 pending = GST_STATE_PENDING (basesink);
1636 post_pending = pending;
1639 case GST_STATE_PLAYING:
1641 GST_DEBUG_OBJECT (basesink, "committing state to PLAYING");
1643 basesink->need_preroll = FALSE;
1644 post_async_done = TRUE;
1645 basesink->priv->committed = TRUE;
1646 post_playing = TRUE;
1647 /* post PAUSED too when we were READY */
1648 if (current == GST_STATE_READY) {
1653 case GST_STATE_PAUSED:
1654 GST_DEBUG_OBJECT (basesink, "committing state to PAUSED");
1656 post_async_done = TRUE;
1657 basesink->priv->committed = TRUE;
1658 post_pending = GST_STATE_VOID_PENDING;
1660 case GST_STATE_READY:
1661 case GST_STATE_NULL:
1663 case GST_STATE_VOID_PENDING:
1664 goto nothing_pending;
1669 /* we can report latency queries now */
1670 basesink->priv->have_latency = TRUE;
1672 GST_STATE (basesink) = pending;
1673 GST_STATE_NEXT (basesink) = GST_STATE_VOID_PENDING;
1674 GST_STATE_PENDING (basesink) = GST_STATE_VOID_PENDING;
1675 GST_STATE_RETURN (basesink) = GST_STATE_CHANGE_SUCCESS;
1676 GST_OBJECT_UNLOCK (basesink);
1679 GST_DEBUG_OBJECT (basesink, "posting PAUSED state change message");
1680 gst_element_post_message (GST_ELEMENT_CAST (basesink),
1681 gst_message_new_state_changed (GST_OBJECT_CAST (basesink),
1682 current, next, post_pending));
1684 if (post_async_done) {
1685 GST_DEBUG_OBJECT (basesink, "posting async-done message");
1686 gst_element_post_message (GST_ELEMENT_CAST (basesink),
1687 gst_message_new_async_done (GST_OBJECT_CAST (basesink),
1688 GST_CLOCK_TIME_NONE));
1692 GstElementClass *klass;
1694 klass = GST_ELEMENT_GET_CLASS (basesink);
1695 basesink->have_preroll = TRUE;
1696 /* after releasing this lock, the state change function
1697 * can execute concurrently with this thread. There is nothing we do to
1698 * prevent this for now. subclasses should be prepared to handle it. */
1699 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
1701 if (klass->change_state)
1702 klass->change_state (GST_ELEMENT_CAST (basesink),
1703 GST_STATE_CHANGE_PAUSED_TO_PLAYING);
1705 GST_BASE_SINK_PREROLL_LOCK (basesink);
1706 /* state change function could have been executed and we could be
1708 if (G_UNLIKELY (basesink->flushing))
1709 goto stopping_unlocked;
1711 GST_DEBUG_OBJECT (basesink, "posting PLAYING state change message");
1712 /* FIXME, we released the PREROLL lock above, it's possible that this
1713 * message is not correct anymore when the element went back to PAUSED */
1714 gst_element_post_message (GST_ELEMENT_CAST (basesink),
1715 gst_message_new_state_changed (GST_OBJECT_CAST (basesink),
1716 next, pending, GST_STATE_VOID_PENDING));
1719 GST_STATE_BROADCAST (basesink);
1725 /* Depending on the state, set our vars. We get in this situation when the
1726 * state change function got a change to update the state vars before the
1727 * streaming thread did. This is fine but we need to make sure that we
1728 * update the need_preroll var since it was %TRUE when we got here and might
1729 * become %FALSE if we got to PLAYING. */
1730 GST_DEBUG_OBJECT (basesink, "nothing to commit, now in %s",
1731 gst_element_state_get_name (current));
1733 case GST_STATE_PLAYING:
1734 basesink->need_preroll = FALSE;
1736 case GST_STATE_PAUSED:
1737 basesink->need_preroll = TRUE;
1740 basesink->need_preroll = FALSE;
1741 basesink->flushing = TRUE;
1744 /* we can report latency queries now */
1745 basesink->priv->have_latency = TRUE;
1746 GST_OBJECT_UNLOCK (basesink);
1751 GST_OBJECT_LOCK (basesink);
1756 /* app is going to READY */
1757 GST_DEBUG_OBJECT (basesink, "stopping");
1758 basesink->need_preroll = FALSE;
1759 basesink->flushing = TRUE;
1760 GST_OBJECT_UNLOCK (basesink);
1766 start_stepping (GstBaseSink * sink, GstSegment * segment,
1767 GstStepInfo * pending, GstStepInfo * current)
1770 GstMessage *message;
1772 GST_DEBUG_OBJECT (sink, "update pending step");
1774 GST_OBJECT_LOCK (sink);
1775 memcpy (current, pending, sizeof (GstStepInfo));
1776 pending->valid = FALSE;
1777 GST_OBJECT_UNLOCK (sink);
1779 /* post message first */
1781 gst_message_new_step_start (GST_OBJECT (sink), TRUE, current->format,
1782 current->amount, current->rate, current->flush, current->intermediate);
1783 gst_message_set_seqnum (message, current->seqnum);
1784 gst_element_post_message (GST_ELEMENT (sink), message);
1786 /* get the running time of where we paused and remember it */
1787 current->start = gst_element_get_start_time (GST_ELEMENT_CAST (sink));
1788 gst_segment_set_running_time (segment, GST_FORMAT_TIME, current->start);
1790 /* set the new rate for the remainder of the segment */
1791 current->start_rate = segment->rate;
1792 segment->rate *= current->rate;
1795 if (segment->rate > 0.0)
1796 current->start_stop = segment->stop;
1798 current->start_start = segment->start;
1800 if (current->format == GST_FORMAT_TIME) {
1801 /* calculate the running-time when the step operation should stop */
1802 if (current->amount != -1)
1803 end = current->start + current->amount;
1807 if (!current->flush) {
1810 /* update the segment clipping regions for non-flushing seeks */
1811 if (segment->rate > 0.0) {
1814 gst_segment_position_from_running_time (segment, GST_FORMAT_TIME,
1817 position = segment->stop;
1819 segment->stop = position;
1820 segment->position = position;
1824 gst_segment_position_from_running_time (segment, GST_FORMAT_TIME,
1827 position = segment->start;
1829 segment->time = position;
1830 segment->start = position;
1831 segment->position = position;
1836 GST_DEBUG_OBJECT (sink, "segment now %" GST_SEGMENT_FORMAT, segment);
1837 GST_DEBUG_OBJECT (sink, "step started at running_time %" GST_TIME_FORMAT,
1838 GST_TIME_ARGS (current->start));
1840 GST_DEBUG_OBJECT (sink, "step amount: %" G_GUINT64_FORMAT ", format: %s, "
1841 "rate: %f", current->amount, gst_format_get_name (current->format),
1846 stop_stepping (GstBaseSink * sink, GstSegment * segment,
1847 GstStepInfo * current, gint64 rstart, gint64 rstop, gboolean eos)
1849 gint64 stop, position;
1850 GstMessage *message;
1852 GST_DEBUG_OBJECT (sink, "step complete");
1854 if (segment->rate > 0.0)
1859 GST_DEBUG_OBJECT (sink,
1860 "step stop at running_time %" GST_TIME_FORMAT, GST_TIME_ARGS (stop));
1863 current->duration = current->position;
1865 current->duration = stop - current->start;
1867 GST_DEBUG_OBJECT (sink, "step elapsed running_time %" GST_TIME_FORMAT,
1868 GST_TIME_ARGS (current->duration));
1870 position = current->start + current->duration;
1872 /* now move the segment to the new running time */
1873 gst_segment_set_running_time (segment, GST_FORMAT_TIME, position);
1875 if (current->flush) {
1876 /* and remove the time we flushed, start time did not change */
1877 segment->base = current->start;
1879 /* start time is now the stepped position */
1880 gst_element_set_start_time (GST_ELEMENT_CAST (sink), position);
1883 /* restore the previous rate */
1884 segment->rate = current->start_rate;
1886 if (segment->rate > 0.0)
1887 segment->stop = current->start_stop;
1889 segment->start = current->start_start;
1891 /* post the step done when we know the stepped duration in TIME */
1893 gst_message_new_step_done (GST_OBJECT_CAST (sink), current->format,
1894 current->amount, current->rate, current->flush, current->intermediate,
1895 current->duration, eos);
1896 gst_message_set_seqnum (message, current->seqnum);
1897 gst_element_post_message (GST_ELEMENT_CAST (sink), message);
1899 if (!current->intermediate)
1900 sink->need_preroll = current->need_preroll;
1902 /* and the current step info finished and becomes invalid */
1903 current->valid = FALSE;
1907 handle_stepping (GstBaseSink * sink, GstSegment * segment,
1908 GstStepInfo * current, guint64 * cstart, guint64 * cstop, guint64 * rstart,
1911 gboolean step_end = FALSE;
1913 /* stepping never stops */
1914 if (current->amount == -1)
1917 /* see if we need to skip this buffer because of stepping */
1918 switch (current->format) {
1919 case GST_FORMAT_TIME:
1922 guint64 first, last;
1925 if (segment->rate > 0.0) {
1926 if (segment->stop == *cstop)
1927 *rstop = *rstart + current->amount;
1932 if (segment->start == *cstart)
1933 *rstart = *rstop + current->amount;
1939 end = current->start + current->amount;
1940 current->position = first - current->start;
1942 abs_rate = ABS (segment->rate);
1943 if (G_UNLIKELY (abs_rate != 1.0))
1944 current->position /= abs_rate;
1946 GST_DEBUG_OBJECT (sink,
1947 "buffer: %" GST_TIME_FORMAT "-%" GST_TIME_FORMAT,
1948 GST_TIME_ARGS (first), GST_TIME_ARGS (last));
1949 GST_DEBUG_OBJECT (sink,
1950 "got time step %" GST_TIME_FORMAT "-%" GST_TIME_FORMAT "/%"
1951 GST_TIME_FORMAT, GST_TIME_ARGS (current->position),
1952 GST_TIME_ARGS (last - current->start),
1953 GST_TIME_ARGS (current->amount));
1955 if ((current->flush && current->position >= current->amount)
1957 GST_DEBUG_OBJECT (sink, "step ended, we need clipping");
1959 if (segment->rate > 0.0) {
1962 gst_segment_position_from_running_time (segment, GST_FORMAT_TIME,
1967 gst_segment_position_from_running_time (segment, GST_FORMAT_TIME,
1971 GST_DEBUG_OBJECT (sink,
1972 "cstart %" GST_TIME_FORMAT ", rstart %" GST_TIME_FORMAT,
1973 GST_TIME_ARGS (*cstart), GST_TIME_ARGS (*rstart));
1974 GST_DEBUG_OBJECT (sink,
1975 "cstop %" GST_TIME_FORMAT ", rstop %" GST_TIME_FORMAT,
1976 GST_TIME_ARGS (*cstop), GST_TIME_ARGS (*rstop));
1979 case GST_FORMAT_BUFFERS:
1980 GST_DEBUG_OBJECT (sink,
1981 "got default step %" G_GUINT64_FORMAT "/%" G_GUINT64_FORMAT,
1982 current->position, current->amount);
1984 if (current->position < current->amount) {
1985 current->position++;
1990 case GST_FORMAT_DEFAULT:
1992 GST_DEBUG_OBJECT (sink,
1993 "got unknown step %" G_GUINT64_FORMAT "/%" G_GUINT64_FORMAT,
1994 current->position, current->amount);
2000 /* with STREAM_LOCK, PREROLL_LOCK
2002 * Returns %TRUE if the object needs synchronisation and takes therefore
2003 * part in prerolling.
2005 * rsstart/rsstop contain the start/stop in stream time.
2006 * rrstart/rrstop contain the start/stop in running time.
2009 gst_base_sink_get_sync_times (GstBaseSink * basesink, GstMiniObject * obj,
2010 GstClockTime * rsstart, GstClockTime * rsstop,
2011 GstClockTime * rrstart, GstClockTime * rrstop, GstClockTime * rrnext,
2012 gboolean * do_sync, gboolean * stepped, GstStepInfo * step,
2013 gboolean * step_end)
2015 GstBaseSinkClass *bclass;
2016 GstClockTime start, stop; /* raw start/stop timestamps */
2017 guint64 cstart, cstop; /* clipped raw timestamps */
2018 guint64 rstart, rstop, rnext; /* clipped timestamps converted to running time */
2019 GstClockTime sstart, sstop; /* clipped timestamps converted to stream time */
2021 GstBaseSinkPrivate *priv;
2022 GstSegment *segment;
2025 priv = basesink->priv;
2026 segment = &basesink->segment;
2028 bclass = GST_BASE_SINK_GET_CLASS (basesink);
2031 /* start with nothing */
2032 start = stop = GST_CLOCK_TIME_NONE;
2035 if (G_UNLIKELY (GST_IS_EVENT (obj))) {
2036 GstEvent *event = GST_EVENT_CAST (obj);
2038 switch (GST_EVENT_TYPE (event)) {
2039 /* EOS event needs syncing */
2042 if (segment->rate >= 0.0) {
2043 sstart = sstop = priv->current_sstop;
2044 if (!GST_CLOCK_TIME_IS_VALID (sstart)) {
2045 /* we have not seen a buffer yet, use the segment values */
2046 sstart = sstop = gst_segment_to_stream_time (segment,
2047 segment->format, segment->stop);
2050 sstart = sstop = priv->current_sstart;
2051 if (!GST_CLOCK_TIME_IS_VALID (sstart)) {
2052 /* we have not seen a buffer yet, use the segment values */
2053 sstart = sstop = gst_segment_to_stream_time (segment,
2054 segment->format, segment->start);
2058 rstart = rstop = rnext = priv->eos_rtime;
2059 *do_sync = GST_CLOCK_TIME_IS_VALID (rstart);
2060 GST_DEBUG_OBJECT (basesink, "sync times for EOS %" GST_TIME_FORMAT,
2061 GST_TIME_ARGS (rstart));
2062 /* if we are stepping, we end now */
2063 *step_end = step->valid;
2069 GstClockTime timestamp, duration;
2070 gst_event_parse_gap (event, ×tamp, &duration);
2072 GST_DEBUG_OBJECT (basesink, "Got Gap time %" GST_TIME_FORMAT
2073 " duration %" GST_TIME_FORMAT,
2074 GST_TIME_ARGS (timestamp), GST_TIME_ARGS (duration));
2076 if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
2078 if (GST_CLOCK_TIME_IS_VALID (duration))
2079 stop = start + duration;
2085 /* other events do not need syncing */
2089 /* else do buffer sync code */
2090 GstBuffer *buffer = GST_BUFFER_CAST (obj);
2092 /* just get the times to see if we need syncing, if the retuned start is -1
2094 if (bclass->get_times)
2095 bclass->get_times (basesink, buffer, &start, &stop);
2097 if (!GST_CLOCK_TIME_IS_VALID (start)) {
2098 /* we don't need to sync but we still want to get the timestamps for
2099 * tracking the position */
2100 gst_base_sink_default_get_times (basesink, buffer, &start, &stop);
2107 GST_DEBUG_OBJECT (basesink, "got times start: %" GST_TIME_FORMAT
2108 ", stop: %" GST_TIME_FORMAT ", do_sync %d", GST_TIME_ARGS (start),
2109 GST_TIME_ARGS (stop), *do_sync);
2111 /* collect segment and format for code clarity */
2112 format = segment->format;
2115 if (G_UNLIKELY (!gst_segment_clip (segment, format,
2116 start, stop, &cstart, &cstop))) {
2118 GST_DEBUG_OBJECT (basesink, "step out of segment");
2119 /* when we are stepping, pretend we're at the end of the segment */
2120 if (segment->rate > 0.0) {
2121 cstart = segment->stop;
2122 cstop = segment->stop;
2124 cstart = segment->start;
2125 cstop = segment->start;
2129 goto out_of_segment;
2132 if (G_UNLIKELY (start != cstart || stop != cstop)) {
2133 GST_DEBUG_OBJECT (basesink, "clipped to: start %" GST_TIME_FORMAT
2134 ", stop: %" GST_TIME_FORMAT, GST_TIME_ARGS (cstart),
2135 GST_TIME_ARGS (cstop));
2138 /* set last stop position */
2139 if (G_LIKELY (stop != GST_CLOCK_TIME_NONE && cstop != GST_CLOCK_TIME_NONE))
2140 segment->position = cstop;
2142 segment->position = cstart;
2145 rstart = gst_segment_to_running_time (segment, format, cstart);
2146 rstop = gst_segment_to_running_time (segment, format, cstop);
2148 if (GST_CLOCK_TIME_IS_VALID (stop))
2153 if (G_UNLIKELY (step->valid)) {
2154 if (!(*step_end = handle_stepping (basesink, segment, step, &cstart, &cstop,
2155 &rstart, &rstop))) {
2156 /* step is still busy, we discard data when we are flushing */
2157 *stepped = step->flush;
2158 GST_DEBUG_OBJECT (basesink, "stepping busy");
2161 /* this can produce wrong values if we accumulated non-TIME segments. If this happens,
2162 * upstream is behaving very badly */
2163 sstart = gst_segment_to_stream_time (segment, format, cstart);
2164 sstop = gst_segment_to_stream_time (segment, format, cstop);
2167 /* eos_done label only called when doing EOS, we also stop stepping then */
2168 if (*step_end && step->flush) {
2169 GST_DEBUG_OBJECT (basesink, "flushing step ended");
2170 stop_stepping (basesink, segment, step, rstart, rstop, eos);
2172 /* re-determine running start times for adjusted segment
2173 * (which has a flushed amount of running/accumulated time removed) */
2174 if (!GST_IS_EVENT (obj)) {
2175 GST_DEBUG_OBJECT (basesink, "refresh sync times");
2187 /* buffers and EOS always need syncing and preroll */
2193 /* we usually clip in the chain function already but stepping could cause
2194 * the segment to be updated later. we return %FALSE so that we don't try
2196 GST_LOG_OBJECT (basesink, "buffer skipped, not in segment");
2201 /* with STREAM_LOCK, PREROLL_LOCK, LOCK
2202 * adjust a timestamp with the latency and timestamp offset. This function does
2203 * not adjust for the render delay. */
2205 gst_base_sink_adjust_time (GstBaseSink * basesink, GstClockTime time)
2207 GstClockTimeDiff ts_offset;
2209 /* don't do anything funny with invalid timestamps */
2210 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (time)))
2213 time += basesink->priv->latency;
2215 /* apply offset, be careful for underflows */
2216 ts_offset = basesink->priv->ts_offset;
2217 if (ts_offset < 0) {
2218 ts_offset = -ts_offset;
2219 if (ts_offset < time)
2226 /* subtract the render delay again, which was included in the latency */
2227 if (time > basesink->priv->render_delay)
2228 time -= basesink->priv->render_delay;
2236 * gst_base_sink_wait_clock:
2238 * @time: the running_time to be reached
2239 * @jitter: (out) (allow-none): the jitter to be filled with time diff, or %NULL
2241 * This function will block until @time is reached. It is usually called by
2242 * subclasses that use their own internal synchronisation.
2244 * If @time is not valid, no synchronisation is done and %GST_CLOCK_BADTIME is
2245 * returned. Likewise, if synchronisation is disabled in the element or there
2246 * is no clock, no synchronisation is done and %GST_CLOCK_BADTIME is returned.
2248 * This function should only be called with the PREROLL_LOCK held, like when
2249 * receiving an EOS event in the #GstBaseSinkClass.event() vmethod or when
2250 * receiving a buffer in
2251 * the #GstBaseSinkClass.render() vmethod.
2253 * The @time argument should be the running_time of when this method should
2254 * return and is not adjusted with any latency or offset configured in the
2257 * Returns: #GstClockReturn
2260 gst_base_sink_wait_clock (GstBaseSink * sink, GstClockTime time,
2261 GstClockTimeDiff * jitter)
2265 GstClockTime base_time;
2267 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (time)))
2270 GST_OBJECT_LOCK (sink);
2271 if (G_UNLIKELY (!sink->sync))
2274 if (G_UNLIKELY ((clock = GST_ELEMENT_CLOCK (sink)) == NULL))
2277 base_time = GST_ELEMENT_CAST (sink)->base_time;
2278 GST_LOG_OBJECT (sink,
2279 "time %" GST_TIME_FORMAT ", base_time %" GST_TIME_FORMAT,
2280 GST_TIME_ARGS (time), GST_TIME_ARGS (base_time));
2282 /* add base_time to running_time to get the time against the clock */
2285 /* Re-use existing clockid if available */
2286 /* FIXME: Casting to GstClockEntry only works because the types
2288 if (G_LIKELY (sink->priv->cached_clock_id != NULL
2289 && GST_CLOCK_ENTRY_CLOCK ((GstClockEntry *) sink->
2290 priv->cached_clock_id) == clock)) {
2291 if (!gst_clock_single_shot_id_reinit (clock, sink->priv->cached_clock_id,
2293 gst_clock_id_unref (sink->priv->cached_clock_id);
2294 sink->priv->cached_clock_id = gst_clock_new_single_shot_id (clock, time);
2297 if (sink->priv->cached_clock_id != NULL)
2298 gst_clock_id_unref (sink->priv->cached_clock_id);
2299 sink->priv->cached_clock_id = gst_clock_new_single_shot_id (clock, time);
2301 GST_OBJECT_UNLOCK (sink);
2303 /* A blocking wait is performed on the clock. We save the ClockID
2304 * so we can unlock the entry at any time. While we are blocking, we
2305 * release the PREROLL_LOCK so that other threads can interrupt the
2307 sink->clock_id = sink->priv->cached_clock_id;
2308 /* release the preroll lock while waiting */
2309 GST_BASE_SINK_PREROLL_UNLOCK (sink);
2311 ret = gst_clock_id_wait (sink->priv->cached_clock_id, jitter);
2313 GST_BASE_SINK_PREROLL_LOCK (sink);
2314 sink->clock_id = NULL;
2318 /* no syncing needed */
2321 GST_DEBUG_OBJECT (sink, "time not valid, no sync needed");
2322 return GST_CLOCK_BADTIME;
2326 GST_DEBUG_OBJECT (sink, "sync disabled");
2327 GST_OBJECT_UNLOCK (sink);
2328 return GST_CLOCK_BADTIME;
2332 GST_DEBUG_OBJECT (sink, "no clock, can't sync");
2333 GST_OBJECT_UNLOCK (sink);
2334 return GST_CLOCK_BADTIME;
2339 * gst_base_sink_wait_preroll:
2342 * If the #GstBaseSinkClass.render() method performs its own synchronisation
2343 * against the clock it must unblock when going from PLAYING to the PAUSED state
2344 * and call this method before continuing to render the remaining data.
2346 * If the #GstBaseSinkClass.render() method can block on something else than
2347 * the clock, it must also be ready to unblock immediately on
2348 * the #GstBaseSinkClass.unlock() method and cause the
2349 * #GstBaseSinkClass.render() method to immediately call this function.
2350 * In this case, the subclass must be prepared to continue rendering where it
2351 * left off if this function returns %GST_FLOW_OK.
2353 * This function will block until a state change to PLAYING happens (in which
2354 * case this function returns %GST_FLOW_OK) or the processing must be stopped due
2355 * to a state change to READY or a FLUSH event (in which case this function
2356 * returns %GST_FLOW_FLUSHING).
2358 * This function should only be called with the PREROLL_LOCK held, like in the
2361 * Returns: %GST_FLOW_OK if the preroll completed and processing can
2362 * continue. Any other return value should be returned from the render vmethod.
2365 gst_base_sink_wait_preroll (GstBaseSink * sink)
2367 sink->have_preroll = TRUE;
2368 GST_DEBUG_OBJECT (sink, "waiting in preroll for flush or PLAYING");
2369 /* block until the state changes, or we get a flush, or something */
2370 GST_BASE_SINK_PREROLL_WAIT (sink);
2371 sink->have_preroll = FALSE;
2372 if (G_UNLIKELY (sink->flushing))
2374 if (G_UNLIKELY (sink->priv->step_unlock))
2376 GST_DEBUG_OBJECT (sink, "continue after preroll");
2383 GST_DEBUG_OBJECT (sink, "preroll interrupted because of flush");
2384 return GST_FLOW_FLUSHING;
2388 sink->priv->step_unlock = FALSE;
2389 GST_DEBUG_OBJECT (sink, "preroll interrupted because of step");
2390 return GST_FLOW_STEP;
2395 * gst_base_sink_do_preroll:
2397 * @obj: (transfer none): the mini object that caused the preroll
2399 * If the @sink spawns its own thread for pulling buffers from upstream it
2400 * should call this method after it has pulled a buffer. If the element needed
2401 * to preroll, this function will perform the preroll and will then block
2402 * until the element state is changed.
2404 * This function should be called with the PREROLL_LOCK held.
2406 * Returns: %GST_FLOW_OK if the preroll completed and processing can
2407 * continue. Any other return value should be returned from the render vmethod.
2410 gst_base_sink_do_preroll (GstBaseSink * sink, GstMiniObject * obj)
2414 while (G_UNLIKELY (sink->need_preroll)) {
2415 GST_DEBUG_OBJECT (sink, "prerolling object %p", obj);
2417 /* if it's a buffer, we need to call the preroll method */
2418 if (sink->priv->call_preroll) {
2419 GstBaseSinkClass *bclass;
2422 if (GST_IS_BUFFER_LIST (obj)) {
2423 buf = gst_buffer_list_get (GST_BUFFER_LIST_CAST (obj), 0);
2424 gst_base_sink_set_last_buffer (sink, buf);
2425 gst_base_sink_set_last_buffer_list (sink, GST_BUFFER_LIST_CAST (obj));
2426 g_assert (NULL != buf);
2427 } else if (GST_IS_BUFFER (obj)) {
2428 buf = GST_BUFFER_CAST (obj);
2429 /* For buffer lists do not set last buffer for now */
2430 gst_base_sink_set_last_buffer (sink, buf);
2431 gst_base_sink_set_last_buffer_list (sink, NULL);
2437 GST_DEBUG_OBJECT (sink, "preroll buffer %" GST_TIME_FORMAT,
2438 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)));
2440 bclass = GST_BASE_SINK_GET_CLASS (sink);
2442 if (bclass->prepare)
2443 if ((ret = bclass->prepare (sink, buf)) != GST_FLOW_OK)
2444 goto prepare_canceled;
2446 if (bclass->preroll)
2447 if ((ret = bclass->preroll (sink, buf)) != GST_FLOW_OK)
2448 goto preroll_canceled;
2450 sink->priv->call_preroll = FALSE;
2455 if (G_LIKELY (sink->playing_async)) {
2456 if (G_UNLIKELY (!gst_base_sink_commit_state (sink)))
2460 /* need to recheck here because the commit state could have
2461 * made us not need the preroll anymore */
2462 if (G_LIKELY (sink->need_preroll)) {
2463 /* block until the state changes, or we get a flush, or something */
2464 ret = gst_base_sink_wait_preroll (sink);
2465 if ((ret != GST_FLOW_OK) && (ret != GST_FLOW_STEP))
2466 goto preroll_failed;
2474 GST_DEBUG_OBJECT (sink, "prepare failed, abort state");
2475 gst_element_abort_state (GST_ELEMENT_CAST (sink));
2480 GST_DEBUG_OBJECT (sink, "preroll failed, abort state");
2481 gst_element_abort_state (GST_ELEMENT_CAST (sink));
2486 GST_DEBUG_OBJECT (sink, "stopping while committing state");
2487 return GST_FLOW_FLUSHING;
2491 GST_DEBUG_OBJECT (sink, "preroll failed: %s", gst_flow_get_name (ret));
2497 * gst_base_sink_wait:
2499 * @time: the running_time to be reached
2500 * @jitter: (out) (allow-none): the jitter to be filled with time diff, or %NULL
2502 * This function will wait for preroll to complete and will then block until @time
2503 * is reached. It is usually called by subclasses that use their own internal
2504 * synchronisation but want to let some synchronization (like EOS) be handled
2505 * by the base class.
2507 * This function should only be called with the PREROLL_LOCK held (like when
2508 * receiving an EOS event in the ::event vmethod or when handling buffers in
2511 * The @time argument should be the running_time of when the timeout should happen
2512 * and will be adjusted with any latency and offset configured in the sink.
2514 * Returns: #GstFlowReturn
2517 gst_base_sink_wait (GstBaseSink * sink, GstClockTime time,
2518 GstClockTimeDiff * jitter)
2520 GstClockReturn status;
2526 GST_DEBUG_OBJECT (sink, "checking preroll");
2528 /* first wait for the playing state before we can continue */
2529 while (G_UNLIKELY (sink->need_preroll)) {
2530 ret = gst_base_sink_wait_preroll (sink);
2531 if ((ret != GST_FLOW_OK) && (ret != GST_FLOW_STEP))
2535 /* preroll done, we can sync since we are in PLAYING now. */
2536 GST_DEBUG_OBJECT (sink, "possibly waiting for clock to reach %"
2537 GST_TIME_FORMAT, GST_TIME_ARGS (time));
2539 /* compensate for latency, ts_offset and render delay */
2540 stime = gst_base_sink_adjust_time (sink, time);
2542 /* wait for the clock, this can be interrupted because we got shut down or
2544 status = gst_base_sink_wait_clock (sink, stime, jitter);
2546 GST_DEBUG_OBJECT (sink, "clock returned %d", status);
2548 /* invalid time, no clock or sync disabled, just continue then */
2549 if (status == GST_CLOCK_BADTIME)
2552 /* waiting could have been interrupted and we can be flushing now */
2553 if (G_UNLIKELY (sink->flushing))
2556 /* retry if we got unscheduled, which means we did not reach the timeout
2557 * yet. if some other error occurs, we continue. */
2558 } while (status == GST_CLOCK_UNSCHEDULED);
2560 GST_DEBUG_OBJECT (sink, "end of stream");
2567 GST_DEBUG_OBJECT (sink, "we are flushing");
2568 return GST_FLOW_FLUSHING;
2572 /* with STREAM_LOCK, PREROLL_LOCK
2574 * Make sure we are in PLAYING and synchronize an object to the clock.
2576 * If we need preroll, we are not in PLAYING. We try to commit the state
2577 * if needed and then block if we still are not PLAYING.
2579 * We start waiting on the clock in PLAYING. If we got interrupted, we
2580 * immediately try to re-preroll.
2582 * Some objects do not need synchronisation (most events) and so this function
2583 * immediately returns GST_FLOW_OK.
2585 * for objects that arrive later than max-lateness to be synchronized to the
2586 * clock have the @late boolean set to %TRUE.
2588 * This function keeps a running average of the jitter (the diff between the
2589 * clock time and the requested sync time). The jitter is negative for
2590 * objects that arrive in time and positive for late buffers.
2592 * does not take ownership of obj.
2594 static GstFlowReturn
2595 gst_base_sink_do_sync (GstBaseSink * basesink,
2596 GstMiniObject * obj, gboolean * late, gboolean * step_end)
2598 GstClockTimeDiff jitter = 0;
2600 GstClockReturn status = GST_CLOCK_OK;
2601 GstClockTime rstart, rstop, rnext, sstart, sstop, stime;
2603 GstBaseSinkPrivate *priv;
2605 GstStepInfo *current, *pending;
2608 priv = basesink->priv;
2611 sstart = sstop = rstart = rstop = rnext = GST_CLOCK_TIME_NONE;
2615 priv->current_rstart = GST_CLOCK_TIME_NONE;
2617 /* get stepping info */
2618 current = &priv->current_step;
2619 pending = &priv->pending_step;
2621 /* get timing information for this object against the render segment */
2622 syncable = gst_base_sink_get_sync_times (basesink, obj,
2623 &sstart, &sstop, &rstart, &rstop, &rnext, &do_sync, &stepped, current,
2626 if (G_UNLIKELY (stepped))
2629 /* a syncable object needs to participate in preroll and
2630 * clocking. All buffers and EOS are syncable. */
2631 if (G_UNLIKELY (!syncable))
2634 /* store timing info for current object */
2635 priv->current_rstart = rstart;
2636 priv->current_rstop = (GST_CLOCK_TIME_IS_VALID (rstop) ? rstop : rstart);
2638 /* save sync time for eos when the previous object needed sync */
2639 priv->eos_rtime = (do_sync ? rnext : GST_CLOCK_TIME_NONE);
2641 /* calculate inter frame spacing */
2642 if (G_UNLIKELY (GST_CLOCK_TIME_IS_VALID (priv->prev_rstart) &&
2643 priv->prev_rstart < rstart)) {
2644 GstClockTime in_diff;
2646 in_diff = rstart - priv->prev_rstart;
2648 if (priv->avg_in_diff == -1)
2649 priv->avg_in_diff = in_diff;
2651 priv->avg_in_diff = UPDATE_RUNNING_AVG (priv->avg_in_diff, in_diff);
2653 GST_LOG_OBJECT (basesink, "avg frame diff %" GST_TIME_FORMAT,
2654 GST_TIME_ARGS (priv->avg_in_diff));
2657 priv->prev_rstart = rstart;
2659 if (G_UNLIKELY (GST_CLOCK_TIME_IS_VALID (priv->earliest_in_time) &&
2660 rstart < priv->earliest_in_time))
2664 /* first do preroll, this makes sure we commit our state
2665 * to PAUSED and can continue to PLAYING. We cannot perform
2666 * any clock sync in PAUSED because there is no clock. */
2667 ret = gst_base_sink_do_preroll (basesink, obj);
2668 if (G_UNLIKELY (ret != GST_FLOW_OK))
2669 goto preroll_failed;
2671 /* update the segment with a pending step if the current one is invalid and we
2672 * have a new pending one. We only accept new step updates after a preroll */
2673 if (G_UNLIKELY (pending->valid && !current->valid)) {
2674 start_stepping (basesink, &basesink->segment, pending, current);
2678 /* After rendering we store the position of the last buffer so that we can use
2679 * it to report the position. We need to take the lock here. */
2680 GST_OBJECT_LOCK (basesink);
2681 priv->current_sstart = sstart;
2682 priv->current_sstop = (GST_CLOCK_TIME_IS_VALID (sstop) ? sstop : sstart);
2683 GST_OBJECT_UNLOCK (basesink);
2688 /* adjust for latency */
2689 stime = gst_base_sink_adjust_time (basesink, rstart);
2691 /* adjust for rate control */
2692 if (priv->rc_next == -1 || (stime != -1 && stime >= priv->rc_next)) {
2693 GST_DEBUG_OBJECT (basesink, "reset rc_time to time %" GST_TIME_FORMAT,
2694 GST_TIME_ARGS (stime));
2695 priv->rc_time = stime;
2696 priv->rc_accumulated = 0;
2698 GST_DEBUG_OBJECT (basesink, "rate control next %" GST_TIME_FORMAT,
2699 GST_TIME_ARGS (priv->rc_next));
2700 stime = priv->rc_next;
2703 /* preroll done, we can sync since we are in PLAYING now. */
2704 GST_DEBUG_OBJECT (basesink, "possibly waiting for clock to reach %"
2705 GST_TIME_FORMAT ", adjusted %" GST_TIME_FORMAT,
2706 GST_TIME_ARGS (rstart), GST_TIME_ARGS (stime));
2708 /* This function will return immediately if start == -1, no clock
2709 * or sync is disabled with GST_CLOCK_BADTIME. */
2710 status = gst_base_sink_wait_clock (basesink, stime, &jitter);
2712 GST_DEBUG_OBJECT (basesink, "clock returned %d, jitter %c%" GST_TIME_FORMAT,
2713 status, (jitter < 0 ? '-' : ' '), GST_TIME_ARGS (ABS (jitter)));
2715 /* invalid time, no clock or sync disabled, just render */
2716 if (status == GST_CLOCK_BADTIME)
2719 /* waiting could have been interrupted and we can be flushing now */
2720 if (G_UNLIKELY (basesink->flushing))
2723 /* check for unlocked by a state change, we are not flushing so
2724 * we can try to preroll on the current buffer. */
2725 if (G_UNLIKELY (status == GST_CLOCK_UNSCHEDULED)) {
2726 GST_DEBUG_OBJECT (basesink, "unscheduled, waiting some more");
2727 priv->call_preroll = TRUE;
2731 /* successful syncing done, record observation */
2732 priv->current_jitter = jitter;
2734 /* check if the object should be dropped */
2735 *late = gst_base_sink_is_too_late (basesink, obj, rstart, rstop,
2736 status, jitter, TRUE);
2744 GST_DEBUG_OBJECT (basesink, "skipped stepped object %p", obj);
2750 GST_DEBUG_OBJECT (basesink, "non syncable object %p", obj);
2755 GST_DEBUG_OBJECT (basesink, "dropped because of QoS %p", obj);
2761 GST_DEBUG_OBJECT (basesink, "we are flushing");
2762 return GST_FLOW_FLUSHING;
2766 GST_DEBUG_OBJECT (basesink, "preroll failed");
2773 gst_base_sink_send_qos (GstBaseSink * basesink, GstQOSType type,
2774 gdouble proportion, GstClockTime time, GstClockTimeDiff diff)
2779 /* generate Quality-of-Service event */
2780 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, basesink,
2781 "qos: type %d, proportion: %lf, diff %" G_GINT64_FORMAT ", timestamp %"
2782 GST_TIME_FORMAT, type, proportion, diff, GST_TIME_ARGS (time));
2784 event = gst_event_new_qos (type, proportion, diff, time);
2787 res = gst_pad_push_event (basesink->sinkpad, event);
2793 gst_base_sink_perform_qos (GstBaseSink * sink, gboolean dropped)
2795 GstBaseSinkPrivate *priv;
2796 GstClockTime start, stop;
2797 GstClockTimeDiff jitter;
2798 GstClockTime pt, entered, left;
2799 GstClockTime duration;
2804 start = priv->current_rstart;
2806 if (priv->current_step.valid)
2809 /* if Quality-of-Service disabled, do nothing */
2810 if (!g_atomic_int_get (&priv->qos_enabled) ||
2811 !GST_CLOCK_TIME_IS_VALID (start))
2814 stop = priv->current_rstop;
2815 jitter = priv->current_jitter;
2818 /* this is the time the buffer entered the sink */
2819 if (start < -jitter)
2822 entered = start + jitter;
2825 /* this is the time the buffer entered the sink */
2826 entered = start + jitter;
2827 /* this is the time the buffer left the sink */
2828 left = start + jitter;
2831 /* calculate duration of the buffer, only use buffer durations if not in
2832 * trick mode or key-unit mode. Otherwise the buffer durations will be
2833 * meaningless as frames are being dropped in-between without updating the
2835 duration = priv->avg_in_diff;
2837 /* if we have the time when the last buffer left us, calculate
2838 * processing time */
2839 if (GST_CLOCK_TIME_IS_VALID (priv->last_left)) {
2840 if (entered > priv->last_left) {
2841 pt = entered - priv->last_left;
2849 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, sink, "start: %" GST_TIME_FORMAT
2850 ", stop %" GST_TIME_FORMAT ", entered %" GST_TIME_FORMAT ", left %"
2851 GST_TIME_FORMAT ", pt: %" GST_TIME_FORMAT ", duration %" GST_TIME_FORMAT
2852 ",jitter %" G_GINT64_FORMAT, GST_TIME_ARGS (start), GST_TIME_ARGS (stop),
2853 GST_TIME_ARGS (entered), GST_TIME_ARGS (left), GST_TIME_ARGS (pt),
2854 GST_TIME_ARGS (duration), jitter);
2856 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, sink,
2857 "avg_pt: %" GST_TIME_FORMAT ", avg_rate: %g",
2858 GST_TIME_ARGS (priv->avg_pt), priv->avg_rate);
2860 /* collect running averages. for first observations, we copy the
2862 if (!GST_CLOCK_TIME_IS_VALID (priv->avg_pt))
2865 priv->avg_pt = UPDATE_RUNNING_AVG (priv->avg_pt, pt);
2867 if (duration != -1 && duration != 0) {
2869 gst_guint64_to_gdouble (priv->avg_pt) /
2870 gst_guint64_to_gdouble (duration);
2875 if (GST_CLOCK_TIME_IS_VALID (priv->last_left)) {
2876 if (dropped || priv->avg_rate < 0.0) {
2877 priv->avg_rate = rate;
2880 priv->avg_rate = UPDATE_RUNNING_AVG_N (priv->avg_rate, rate);
2882 priv->avg_rate = UPDATE_RUNNING_AVG_P (priv->avg_rate, rate);
2886 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, sink,
2887 "updated: avg_pt: %" GST_TIME_FORMAT
2888 ", avg_rate: %g", GST_TIME_ARGS (priv->avg_pt), priv->avg_rate);
2891 if (priv->avg_rate >= 0.0) {
2893 GstClockTimeDiff diff;
2895 /* if we have a valid rate, start sending QoS messages */
2896 if (priv->current_jitter < 0) {
2897 /* make sure we never go below 0 when adding the jitter to the
2899 if (priv->current_rstart < -priv->current_jitter)
2900 priv->current_jitter = -priv->current_rstart;
2903 if (priv->throttle_time > 0) {
2904 diff = priv->throttle_time;
2905 type = GST_QOS_TYPE_THROTTLE;
2907 diff = priv->current_jitter;
2909 type = GST_QOS_TYPE_OVERFLOW;
2911 type = GST_QOS_TYPE_UNDERFLOW;
2914 gst_base_sink_send_qos (sink, type, priv->avg_rate, priv->current_rstart,
2918 /* record when this buffer will leave us */
2919 priv->last_left = left;
2922 /* reset all qos measuring */
2924 gst_base_sink_reset_qos (GstBaseSink * sink)
2926 GstBaseSinkPrivate *priv;
2930 priv->last_render_time = GST_CLOCK_TIME_NONE;
2931 priv->prev_rstart = GST_CLOCK_TIME_NONE;
2932 priv->earliest_in_time = GST_CLOCK_TIME_NONE;
2933 priv->last_left = GST_CLOCK_TIME_NONE;
2934 priv->avg_pt = GST_CLOCK_TIME_NONE;
2935 priv->avg_rate = -1.0;
2936 priv->avg_in_diff = GST_CLOCK_TIME_NONE;
2942 /* Checks if the object was scheduled too late.
2944 * rstart/rstop contain the running_time start and stop values
2947 * status and jitter contain the return values from the clock wait.
2949 * returns %TRUE if the buffer was too late.
2952 gst_base_sink_is_too_late (GstBaseSink * basesink, GstMiniObject * obj,
2953 GstClockTime rstart, GstClockTime rstop,
2954 GstClockReturn status, GstClockTimeDiff jitter, gboolean render)
2957 guint64 max_lateness;
2958 GstBaseSinkPrivate *priv;
2960 priv = basesink->priv;
2964 /* only for objects that were too late */
2965 if (G_LIKELY (status != GST_CLOCK_EARLY))
2968 max_lateness = basesink->max_lateness;
2970 /* check if frame dropping is enabled */
2971 if (max_lateness == -1)
2974 /* only check for buffers */
2975 if (G_UNLIKELY (!GST_IS_BUFFER (obj)))
2978 /* can't do check if we don't have a timestamp */
2979 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (rstart)))
2982 /* we can add a valid stop time */
2983 if (GST_CLOCK_TIME_IS_VALID (rstop))
2984 max_lateness += rstop;
2986 max_lateness += rstart;
2987 /* no stop time, use avg frame diff */
2988 if (priv->avg_in_diff != -1)
2989 max_lateness += priv->avg_in_diff;
2992 /* if the jitter bigger than duration and lateness we are too late */
2993 if ((late = rstart + jitter > max_lateness)) {
2994 GST_CAT_DEBUG_OBJECT (GST_CAT_PERFORMANCE, basesink,
2995 "buffer is too late %" GST_TIME_FORMAT
2996 " > %" GST_TIME_FORMAT, GST_TIME_ARGS (rstart + jitter),
2997 GST_TIME_ARGS (max_lateness));
2998 /* !!emergency!!, if we did not receive anything valid for more than a
2999 * second, render it anyway so the user sees something */
3000 if (GST_CLOCK_TIME_IS_VALID (priv->last_render_time) &&
3001 rstart - priv->last_render_time > GST_SECOND) {
3003 GST_ELEMENT_WARNING (basesink, CORE, CLOCK,
3004 (_("A lot of buffers are being dropped.")),
3005 ("There may be a timestamping problem, or this computer is too slow."));
3006 GST_CAT_DEBUG_OBJECT (GST_CAT_PERFORMANCE, basesink,
3007 "**emergency** last buffer at %" GST_TIME_FORMAT " > GST_SECOND",
3008 GST_TIME_ARGS (priv->last_render_time));
3013 if (render && (!late || !GST_CLOCK_TIME_IS_VALID (priv->last_render_time))) {
3014 priv->last_render_time = rstart;
3015 /* the next allowed input timestamp */
3016 if (priv->throttle_time > 0)
3017 priv->earliest_in_time = rstart + priv->throttle_time;
3024 GST_DEBUG_OBJECT (basesink, "object was scheduled in time");
3029 GST_DEBUG_OBJECT (basesink, "frame dropping disabled");
3034 GST_DEBUG_OBJECT (basesink, "object is not a buffer");
3039 GST_DEBUG_OBJECT (basesink, "buffer has no timestamp");
3045 gst_base_sink_update_start_time (GstBaseSink * basesink)
3049 GST_OBJECT_LOCK (basesink);
3050 if (GST_STATE (basesink) == GST_STATE_PLAYING
3051 && (clock = GST_ELEMENT_CLOCK (basesink))) {
3054 gst_object_ref (clock);
3055 GST_OBJECT_UNLOCK (basesink);
3057 /* calculate the time when we stopped */
3058 now = gst_clock_get_time (clock);
3059 gst_object_unref (clock);
3061 GST_OBJECT_LOCK (basesink);
3062 /* store the current running time */
3063 if (GST_ELEMENT_START_TIME (basesink) != GST_CLOCK_TIME_NONE) {
3064 if (now != GST_CLOCK_TIME_NONE)
3065 GST_ELEMENT_START_TIME (basesink) =
3066 now - GST_ELEMENT_CAST (basesink)->base_time;
3068 GST_WARNING_OBJECT (basesink,
3069 "Clock %s returned invalid time, can't calculate "
3070 "running_time when going to the PAUSED state",
3071 GST_OBJECT_NAME (clock));
3073 GST_DEBUG_OBJECT (basesink,
3074 "start_time=%" GST_TIME_FORMAT ", now=%" GST_TIME_FORMAT
3075 ", base_time %" GST_TIME_FORMAT,
3076 GST_TIME_ARGS (GST_ELEMENT_START_TIME (basesink)),
3077 GST_TIME_ARGS (now),
3078 GST_TIME_ARGS (GST_ELEMENT_CAST (basesink)->base_time));
3080 GST_OBJECT_UNLOCK (basesink);
3084 gst_base_sink_flush_start (GstBaseSink * basesink, GstPad * pad)
3086 /* make sure we are not blocked on the clock also clear any pending
3088 gst_base_sink_set_flushing (basesink, pad, TRUE);
3090 /* we grab the stream lock but that is not needed since setting the
3091 * sink to flushing would make sure no state commit is being done
3093 GST_PAD_STREAM_LOCK (pad);
3094 gst_base_sink_reset_qos (basesink);
3095 /* and we need to commit our state again on the next
3096 * prerolled buffer */
3097 basesink->playing_async = TRUE;
3098 if (basesink->priv->async_enabled) {
3099 gst_base_sink_update_start_time (basesink);
3100 gst_element_lost_state (GST_ELEMENT_CAST (basesink));
3102 /* start time reset in above case as well;
3103 * arranges for a.o. proper position reporting when flushing in PAUSED */
3104 gst_element_set_start_time (GST_ELEMENT_CAST (basesink), 0);
3105 basesink->priv->have_latency = TRUE;
3107 gst_base_sink_set_last_buffer (basesink, NULL);
3108 gst_base_sink_set_last_buffer_list (basesink, NULL);
3109 GST_PAD_STREAM_UNLOCK (pad);
3113 gst_base_sink_flush_stop (GstBaseSink * basesink, GstPad * pad,
3114 gboolean reset_time)
3116 /* unset flushing so we can accept new data, this also flushes out any EOS
3118 gst_base_sink_set_flushing (basesink, pad, FALSE);
3120 /* for position reporting */
3121 GST_OBJECT_LOCK (basesink);
3122 basesink->priv->current_sstart = GST_CLOCK_TIME_NONE;
3123 basesink->priv->current_sstop = GST_CLOCK_TIME_NONE;
3124 basesink->priv->eos_rtime = GST_CLOCK_TIME_NONE;
3125 basesink->priv->call_preroll = TRUE;
3126 basesink->priv->current_step.valid = FALSE;
3127 basesink->priv->pending_step.valid = FALSE;
3128 if (basesink->pad_mode == GST_PAD_MODE_PUSH) {
3129 /* we need new segment info after the flush. */
3130 basesink->have_newsegment = FALSE;
3132 gst_segment_init (&basesink->segment, GST_FORMAT_UNDEFINED);
3133 GST_ELEMENT_START_TIME (basesink) = 0;
3136 GST_OBJECT_UNLOCK (basesink);
3139 GST_DEBUG_OBJECT (basesink, "posting reset-time message");
3140 gst_element_post_message (GST_ELEMENT_CAST (basesink),
3141 gst_message_new_reset_time (GST_OBJECT_CAST (basesink), 0));
3145 static GstFlowReturn
3146 gst_base_sink_default_wait_event (GstBaseSink * basesink, GstEvent * event)
3149 gboolean late, step_end = FALSE;
3151 ret = gst_base_sink_do_sync (basesink, GST_MINI_OBJECT_CAST (event),
3157 static GstFlowReturn
3158 gst_base_sink_wait_event (GstBaseSink * basesink, GstEvent * event)
3161 GstBaseSinkClass *bclass;
3163 bclass = GST_BASE_SINK_GET_CLASS (basesink);
3165 if (G_LIKELY (bclass->wait_event))
3166 ret = bclass->wait_event (basesink, event);
3168 ret = GST_FLOW_NOT_SUPPORTED;
3174 gst_base_sink_default_event (GstBaseSink * basesink, GstEvent * event)
3176 gboolean result = TRUE;
3177 GstBaseSinkClass *bclass;
3179 bclass = GST_BASE_SINK_GET_CLASS (basesink);
3181 switch (GST_EVENT_TYPE (event)) {
3182 case GST_EVENT_FLUSH_START:
3184 GST_DEBUG_OBJECT (basesink, "flush-start %p", event);
3185 gst_base_sink_flush_start (basesink, basesink->sinkpad);
3188 case GST_EVENT_FLUSH_STOP:
3190 gboolean reset_time;
3192 gst_event_parse_flush_stop (event, &reset_time);
3193 GST_DEBUG_OBJECT (basesink, "flush-stop %p, reset_time: %d", event,
3195 gst_base_sink_flush_stop (basesink, basesink->sinkpad, reset_time);
3200 GstMessage *message;
3203 /* we set the received EOS flag here so that we can use it when testing if
3204 * we are prerolled and to refuse more buffers. */
3205 basesink->priv->received_eos = TRUE;
3208 if (G_UNLIKELY (gst_base_sink_wait_event (basesink,
3209 event) != GST_FLOW_OK)) {
3214 /* the EOS event is completely handled so we mark
3215 * ourselves as being in the EOS state. eos is also
3216 * protected by the object lock so we can read it when
3217 * answering the POSITION query. */
3218 GST_OBJECT_LOCK (basesink);
3219 basesink->eos = TRUE;
3220 GST_OBJECT_UNLOCK (basesink);
3222 /* ok, now we can post the message */
3223 GST_DEBUG_OBJECT (basesink, "Now posting EOS");
3225 seqnum = basesink->priv->seqnum = gst_event_get_seqnum (event);
3226 GST_DEBUG_OBJECT (basesink, "Got seqnum #%" G_GUINT32_FORMAT, seqnum);
3228 message = gst_message_new_eos (GST_OBJECT_CAST (basesink));
3229 gst_message_set_seqnum (message, seqnum);
3230 gst_element_post_message (GST_ELEMENT_CAST (basesink), message);
3233 case GST_EVENT_STREAM_START:
3235 GstMessage *message;
3239 seqnum = gst_event_get_seqnum (event);
3240 GST_DEBUG_OBJECT (basesink, "Now posting STREAM_START (seqnum:%d)",
3242 message = gst_message_new_stream_start (GST_OBJECT_CAST (basesink));
3243 if (gst_event_parse_group_id (event, &group_id)) {
3244 gst_message_set_group_id (message, group_id);
3246 GST_FIXME_OBJECT (basesink, "stream-start event without group-id. "
3247 "Consider implementing group-id handling in the upstream "
3250 gst_message_set_seqnum (message, seqnum);
3251 gst_element_post_message (GST_ELEMENT_CAST (basesink), message);
3254 case GST_EVENT_CAPS:
3256 GstCaps *caps, *current_caps;
3258 GST_DEBUG_OBJECT (basesink, "caps %p", event);
3260 gst_event_parse_caps (event, &caps);
3261 current_caps = gst_pad_get_current_caps (GST_BASE_SINK_PAD (basesink));
3263 if (current_caps && gst_caps_is_equal (current_caps, caps)) {
3264 GST_DEBUG_OBJECT (basesink,
3265 "New caps equal to old ones: %" GST_PTR_FORMAT, caps);
3267 if (bclass->set_caps)
3268 result = bclass->set_caps (basesink, caps);
3271 GST_OBJECT_LOCK (basesink);
3272 gst_caps_replace (&basesink->priv->caps, caps);
3273 GST_OBJECT_UNLOCK (basesink);
3277 gst_caps_unref (current_caps);
3280 case GST_EVENT_SEGMENT:
3281 /* configure the segment */
3282 /* The segment is protected with both the STREAM_LOCK and the OBJECT_LOCK.
3283 * We protect with the OBJECT_LOCK so that we can use the values to
3284 * safely answer a POSITION query. */
3285 GST_OBJECT_LOCK (basesink);
3286 /* the newsegment event is needed to bring the buffer timestamps to the
3287 * stream time and to drop samples outside of the playback segment. */
3288 gst_event_copy_segment (event, &basesink->segment);
3289 GST_DEBUG_OBJECT (basesink, "configured segment %" GST_SEGMENT_FORMAT,
3290 &basesink->segment);
3291 basesink->have_newsegment = TRUE;
3292 gst_base_sink_reset_qos (basesink);
3293 GST_OBJECT_UNLOCK (basesink);
3297 if (G_UNLIKELY (gst_base_sink_wait_event (basesink,
3298 event) != GST_FLOW_OK))
3304 GstTagList *taglist;
3306 gst_event_parse_tag (event, &taglist);
3308 gst_element_post_message (GST_ELEMENT_CAST (basesink),
3309 gst_message_new_tag (GST_OBJECT_CAST (basesink),
3310 gst_tag_list_copy (taglist)));
3318 gst_event_parse_toc (event, &toc, &updated);
3320 gst_element_post_message (GST_ELEMENT_CAST (basesink),
3321 gst_message_new_toc (GST_OBJECT_CAST (basesink), toc, updated));
3323 gst_toc_unref (toc);
3326 case GST_EVENT_SINK_MESSAGE:
3328 GstMessage *msg = NULL;
3330 gst_event_parse_sink_message (event, &msg);
3332 gst_element_post_message (GST_ELEMENT_CAST (basesink), msg);
3339 gst_event_unref (event);
3345 gst_base_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
3347 GstBaseSink *basesink;
3348 gboolean result = TRUE;
3349 GstBaseSinkClass *bclass;
3351 basesink = GST_BASE_SINK_CAST (parent);
3352 bclass = GST_BASE_SINK_GET_CLASS (basesink);
3354 GST_DEBUG_OBJECT (basesink, "received event %p %" GST_PTR_FORMAT, event,
3357 switch (GST_EVENT_TYPE (event)) {
3358 case GST_EVENT_FLUSH_STOP:
3359 /* special case for this serialized event because we don't want to grab
3360 * the PREROLL lock or check if we were flushing */
3362 result = bclass->event (basesink, event);
3365 if (GST_EVENT_IS_SERIALIZED (event)) {
3366 GST_BASE_SINK_PREROLL_LOCK (basesink);
3367 if (G_UNLIKELY (basesink->flushing))
3370 if (G_UNLIKELY (basesink->priv->received_eos))
3374 result = bclass->event (basesink, event);
3376 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
3379 result = bclass->event (basesink, event);
3389 GST_DEBUG_OBJECT (basesink, "we are flushing");
3390 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
3391 gst_event_unref (event);
3398 GST_DEBUG_OBJECT (basesink, "Event received after EOS, dropping");
3399 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
3400 gst_event_unref (event);
3406 /* default implementation to calculate the start and end
3407 * timestamps on a buffer, subclasses can override
3410 gst_base_sink_default_get_times (GstBaseSink * basesink, GstBuffer * buffer,
3411 GstClockTime * start, GstClockTime * end)
3413 GstClockTime timestamp, duration;
3415 /* first sync on DTS, else use PTS */
3416 timestamp = GST_BUFFER_DTS (buffer);
3417 if (!GST_CLOCK_TIME_IS_VALID (timestamp))
3418 timestamp = GST_BUFFER_PTS (buffer);
3420 if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
3421 /* get duration to calculate end time */
3422 duration = GST_BUFFER_DURATION (buffer);
3423 if (GST_CLOCK_TIME_IS_VALID (duration)) {
3424 *end = timestamp + duration;
3430 /* must be called with PREROLL_LOCK */
3432 gst_base_sink_needs_preroll (GstBaseSink * basesink)
3434 gboolean is_prerolled, res;
3436 /* we have 2 cases where the PREROLL_LOCK is released:
3437 * 1) we are blocking in the PREROLL_LOCK and thus are prerolled.
3438 * 2) we are syncing on the clock
3440 is_prerolled = basesink->have_preroll || basesink->priv->received_eos;
3441 res = !is_prerolled;
3443 GST_DEBUG_OBJECT (basesink, "have_preroll: %d, EOS: %d => needs preroll: %d",
3444 basesink->have_preroll, basesink->priv->received_eos, res);
3449 /* with STREAM_LOCK, PREROLL_LOCK
3451 * Takes a buffer and compare the timestamps with the last segment.
3452 * If the buffer falls outside of the segment boundaries, drop it.
3453 * Else send the buffer for preroll and rendering.
3455 * This function takes ownership of the buffer.
3457 static GstFlowReturn
3458 gst_base_sink_chain_unlocked (GstBaseSink * basesink, GstPad * pad,
3459 gpointer obj, gboolean is_list)
3461 GstBaseSinkClass *bclass;
3462 GstBaseSinkPrivate *priv = basesink->priv;
3463 GstFlowReturn ret = GST_FLOW_OK;
3464 GstClockTime start = GST_CLOCK_TIME_NONE, end = GST_CLOCK_TIME_NONE;
3465 GstSegment *segment;
3466 GstBuffer *sync_buf;
3467 gboolean late, step_end, prepared = FALSE;
3469 if (G_UNLIKELY (basesink->flushing))
3472 if (G_UNLIKELY (priv->received_eos))
3476 GstBufferList *buffer_list = GST_BUFFER_LIST_CAST (obj);
3478 if (gst_buffer_list_length (buffer_list) == 0)
3481 sync_buf = gst_buffer_list_get (buffer_list, 0);
3482 g_assert (NULL != sync_buf);
3484 sync_buf = GST_BUFFER_CAST (obj);
3487 /* for code clarity */
3488 segment = &basesink->segment;
3490 if (G_UNLIKELY (!basesink->have_newsegment)) {
3493 sync = gst_base_sink_get_sync (basesink);
3495 GST_ELEMENT_WARNING (basesink, STREAM, FAILED,
3496 (_("Internal data flow problem.")),
3497 ("Received buffer without a new-segment. Assuming timestamps start from 0."));
3500 /* this means this sink will assume timestamps start from 0 */
3501 GST_OBJECT_LOCK (basesink);
3504 basesink->segment.start = 0;
3505 basesink->segment.stop = -1;
3506 basesink->have_newsegment = TRUE;
3507 GST_OBJECT_UNLOCK (basesink);
3510 bclass = GST_BASE_SINK_GET_CLASS (basesink);
3512 /* check if the buffer needs to be dropped, we first ask the subclass for the
3514 if (bclass->get_times)
3515 bclass->get_times (basesink, sync_buf, &start, &end);
3517 if (!GST_CLOCK_TIME_IS_VALID (start)) {
3518 /* if the subclass does not want sync, we use our own values so that we at
3519 * least clip the buffer to the segment */
3520 gst_base_sink_default_get_times (basesink, sync_buf, &start, &end);
3523 GST_DEBUG_OBJECT (basesink, "got times start: %" GST_TIME_FORMAT
3524 ", end: %" GST_TIME_FORMAT, GST_TIME_ARGS (start), GST_TIME_ARGS (end));
3526 /* a dropped buffer does not participate in anything. Buffer can only be
3527 * dropped if their PTS falls completely outside the segment, while we sync
3528 * preferably on DTS */
3529 if (GST_CLOCK_TIME_IS_VALID (start) && (segment->format == GST_FORMAT_TIME)) {
3530 GstClockTime pts = GST_BUFFER_PTS (sync_buf);
3531 GstClockTime pts_end = GST_CLOCK_TIME_NONE;
3533 if (!GST_CLOCK_TIME_IS_VALID (pts))
3536 if (GST_CLOCK_TIME_IS_VALID (end))
3537 pts_end = pts + (end - start);
3539 if (G_UNLIKELY (!gst_segment_clip (segment,
3540 GST_FORMAT_TIME, pts, pts_end, NULL, NULL)
3541 && priv->drop_out_of_segment))
3542 goto out_of_segment;
3545 if (bclass->prepare || bclass->prepare_list) {
3546 gboolean do_sync = TRUE, stepped = FALSE, syncable = TRUE;
3547 GstClockTime sstart, sstop, rstart, rstop, rnext;
3548 GstStepInfo *current;
3553 current = &priv->current_step;
3555 gst_base_sink_get_sync_times (basesink, obj, &sstart, &sstop, &rstart,
3556 &rstop, &rnext, &do_sync, &stepped, current, &step_end);
3558 if (G_UNLIKELY (stepped))
3561 if (syncable && do_sync && gst_base_sink_get_sync (basesink)) {
3564 GST_OBJECT_LOCK (basesink);
3565 clock = GST_ELEMENT_CLOCK (basesink);
3566 if (clock && GST_STATE (basesink) == GST_STATE_PLAYING) {
3567 GstClockTime base_time;
3571 base_time = GST_ELEMENT_CAST (basesink)->base_time;
3572 stime = base_time + gst_base_sink_adjust_time (basesink, rstart);
3573 now = gst_clock_get_time (clock);
3574 GST_OBJECT_UNLOCK (basesink);
3577 gst_base_sink_is_too_late (basesink, obj, rstart, rstop,
3578 GST_CLOCK_EARLY, GST_CLOCK_DIFF (stime, now), FALSE);
3580 GST_OBJECT_UNLOCK (basesink);
3584 /* We are about to prepare the first frame, make sure we have prerolled
3585 * already. This prevent nesting prepare/render calls. */
3586 ret = gst_base_sink_do_preroll (basesink, obj);
3587 if (G_UNLIKELY (ret != GST_FLOW_OK))
3588 goto preroll_failed;
3590 if (G_UNLIKELY (late))
3594 if (bclass->prepare) {
3595 ret = bclass->prepare (basesink, GST_BUFFER_CAST (obj));
3596 if (G_UNLIKELY (ret != GST_FLOW_OK))
3597 goto prepare_failed;
3600 if (bclass->prepare_list) {
3601 ret = bclass->prepare_list (basesink, GST_BUFFER_LIST_CAST (obj));
3602 if (G_UNLIKELY (ret != GST_FLOW_OK))
3603 goto prepare_failed;
3614 /* synchronize this object, non syncable objects return OK
3616 ret = gst_base_sink_do_sync (basesink, GST_MINI_OBJECT_CAST (sync_buf),
3618 if (G_UNLIKELY (ret != GST_FLOW_OK))
3621 /* Don't skip if prepare() was called on time */
3622 late = late && !prepared;
3624 /* drop late buffers unconditionally, let's hope it's unlikely */
3625 if (G_UNLIKELY (late))
3628 if (priv->max_bitrate) {
3632 size = gst_buffer_list_calculate_size (GST_BUFFER_LIST_CAST (obj));
3634 size = gst_buffer_get_size (GST_BUFFER_CAST (obj));
3636 priv->rc_accumulated += size;
3637 priv->rc_next = priv->rc_time + gst_util_uint64_scale (priv->rc_accumulated,
3638 8 * GST_SECOND, priv->max_bitrate);
3641 GST_DEBUG_OBJECT (basesink, "rendering object %p", obj);
3644 /* For buffer lists do not set last buffer for now. */
3645 gst_base_sink_set_last_buffer (basesink, GST_BUFFER_CAST (obj));
3646 gst_base_sink_set_last_buffer_list (basesink, NULL);
3649 ret = bclass->render (basesink, GST_BUFFER_CAST (obj));
3651 GstBufferList *buffer_list = GST_BUFFER_LIST_CAST (obj);
3653 if (bclass->render_list)
3654 ret = bclass->render_list (basesink, buffer_list);
3656 /* Set the first buffer and buffer list to be included in last sample */
3657 gst_base_sink_set_last_buffer (basesink, sync_buf);
3658 gst_base_sink_set_last_buffer_list (basesink, buffer_list);
3661 if (ret == GST_FLOW_STEP)
3664 if (G_UNLIKELY (basesink->flushing))
3671 /* the step ended, check if we need to activate a new step */
3672 GST_DEBUG_OBJECT (basesink, "step ended");
3673 stop_stepping (basesink, &basesink->segment, &priv->current_step,
3674 priv->current_rstart, priv->current_rstop, basesink->eos);
3678 gst_base_sink_perform_qos (basesink, late);
3680 GST_DEBUG_OBJECT (basesink, "object unref after render %p", obj);
3681 gst_mini_object_unref (GST_MINI_OBJECT_CAST (obj));
3688 GST_DEBUG_OBJECT (basesink, "sink is flushing");
3689 gst_mini_object_unref (GST_MINI_OBJECT_CAST (obj));
3690 return GST_FLOW_FLUSHING;
3694 GST_DEBUG_OBJECT (basesink, "we are EOS, dropping object, return EOS");
3695 gst_mini_object_unref (GST_MINI_OBJECT_CAST (obj));
3696 return GST_FLOW_EOS;
3700 GST_DEBUG_OBJECT (basesink, "buffer list with no buffers");
3701 gst_mini_object_unref (GST_MINI_OBJECT_CAST (obj));
3706 GST_DEBUG_OBJECT (basesink, "dropping buffer, out of clipping segment");
3707 gst_mini_object_unref (GST_MINI_OBJECT_CAST (obj));
3712 GST_DEBUG_OBJECT (basesink, "prepare buffer failed %s",
3713 gst_flow_get_name (ret));
3714 gst_mini_object_unref (GST_MINI_OBJECT_CAST (obj));
3719 GST_DEBUG_OBJECT (basesink, "do_sync returned %s", gst_flow_get_name (ret));
3725 GST_DEBUG_OBJECT (basesink, "buffer late, dropping");
3727 if (g_atomic_int_get (&priv->qos_enabled)) {
3728 GstMessage *qos_msg;
3729 GstClockTime timestamp, duration;
3731 timestamp = GST_BUFFER_TIMESTAMP (GST_BUFFER_CAST (sync_buf));
3732 duration = GST_BUFFER_DURATION (GST_BUFFER_CAST (sync_buf));
3734 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, basesink,
3735 "qos: dropped buffer rt %" GST_TIME_FORMAT ", st %" GST_TIME_FORMAT
3736 ", ts %" GST_TIME_FORMAT ", dur %" GST_TIME_FORMAT,
3737 GST_TIME_ARGS (priv->current_rstart),
3738 GST_TIME_ARGS (priv->current_sstart), GST_TIME_ARGS (timestamp),
3739 GST_TIME_ARGS (duration));
3740 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, basesink,
3741 "qos: rendered %" G_GUINT64_FORMAT ", dropped %" G_GUINT64_FORMAT,
3742 priv->rendered, priv->dropped);
3745 gst_message_new_qos (GST_OBJECT_CAST (basesink), basesink->sync,
3746 priv->current_rstart, priv->current_sstart, timestamp, duration);
3747 gst_message_set_qos_values (qos_msg, priv->current_jitter, priv->avg_rate,
3749 gst_message_set_qos_stats (qos_msg, GST_FORMAT_BUFFERS, priv->rendered,
3751 gst_element_post_message (GST_ELEMENT_CAST (basesink), qos_msg);
3757 GST_DEBUG_OBJECT (basesink, "preroll failed: %s", gst_flow_get_name (ret));
3758 gst_mini_object_unref (GST_MINI_OBJECT_CAST (obj));
3765 static GstFlowReturn
3766 gst_base_sink_chain_main (GstBaseSink * basesink, GstPad * pad, gpointer obj,
3769 GstFlowReturn result;
3771 if (G_UNLIKELY (basesink->pad_mode != GST_PAD_MODE_PUSH))
3774 GST_BASE_SINK_PREROLL_LOCK (basesink);
3775 result = gst_base_sink_chain_unlocked (basesink, pad, obj, is_list);
3776 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
3784 GST_OBJECT_LOCK (pad);
3785 GST_WARNING_OBJECT (basesink,
3786 "Push on pad %s:%s, but it was not activated in push mode",
3787 GST_DEBUG_PAD_NAME (pad));
3788 GST_OBJECT_UNLOCK (pad);
3789 gst_mini_object_unref (GST_MINI_OBJECT_CAST (obj));
3790 /* we don't post an error message this will signal to the peer
3791 * pushing that EOS is reached. */
3792 result = GST_FLOW_EOS;
3797 static GstFlowReturn
3798 gst_base_sink_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
3800 GstBaseSink *basesink;
3802 basesink = GST_BASE_SINK (parent);
3804 return gst_base_sink_chain_main (basesink, pad, buf, FALSE);
3807 static GstFlowReturn
3808 gst_base_sink_chain_list (GstPad * pad, GstObject * parent,
3809 GstBufferList * list)
3811 GstBaseSink *basesink;
3812 GstBaseSinkClass *bclass;
3813 GstFlowReturn result;
3815 basesink = GST_BASE_SINK (parent);
3816 bclass = GST_BASE_SINK_GET_CLASS (basesink);
3818 if (G_LIKELY (bclass->render_list)) {
3819 result = gst_base_sink_chain_main (basesink, pad, list, TRUE);
3824 GST_LOG_OBJECT (pad, "chaining each buffer in list");
3826 len = gst_buffer_list_length (list);
3828 result = GST_FLOW_OK;
3829 for (i = 0; i < len; i++) {
3830 buffer = gst_buffer_list_get (list, i);
3831 result = gst_base_sink_chain_main (basesink, pad,
3832 gst_buffer_ref (buffer), FALSE);
3833 if (result != GST_FLOW_OK)
3836 gst_buffer_list_unref (list);
3843 gst_base_sink_default_do_seek (GstBaseSink * sink, GstSegment * segment)
3845 gboolean res = TRUE;
3847 /* update our offset if the start/stop position was updated */
3848 if (segment->format == GST_FORMAT_BYTES) {
3849 segment->time = segment->start;
3850 } else if (segment->start == 0) {
3851 /* seek to start, we can implement a default for this. */
3855 GST_INFO_OBJECT (sink, "Can't do a default seek");
3861 #define SEEK_TYPE_IS_RELATIVE(t) (((t) != GST_SEEK_TYPE_NONE) && ((t) != GST_SEEK_TYPE_SET))
3864 gst_base_sink_default_prepare_seek_segment (GstBaseSink * sink,
3865 GstEvent * event, GstSegment * segment)
3867 /* By default, we try one of 2 things:
3868 * - For absolute seek positions, convert the requested position to our
3869 * configured processing format and place it in the output segment \
3870 * - For relative seek positions, convert our current (input) values to the
3871 * seek format, adjust by the relative seek offset and then convert back to
3872 * the processing format
3874 GstSeekType start_type, stop_type;
3877 GstFormat seek_format;
3880 gboolean res = TRUE;
3882 gst_event_parse_seek (event, &rate, &seek_format, &flags,
3883 &start_type, &start, &stop_type, &stop);
3885 if (seek_format == segment->format) {
3886 gst_segment_do_seek (segment, rate, seek_format, flags,
3887 start_type, start, stop_type, stop, &update);
3891 if (start_type != GST_SEEK_TYPE_NONE) {
3892 /* FIXME: Handle seek_end by converting the input segment vals */
3894 gst_pad_query_convert (sink->sinkpad, seek_format, start,
3895 segment->format, &start);
3896 start_type = GST_SEEK_TYPE_SET;
3899 if (res && stop_type != GST_SEEK_TYPE_NONE) {
3900 /* FIXME: Handle seek_end by converting the input segment vals */
3902 gst_pad_query_convert (sink->sinkpad, seek_format, stop,
3903 segment->format, &stop);
3904 stop_type = GST_SEEK_TYPE_SET;
3907 /* And finally, configure our output segment in the desired format */
3908 gst_segment_do_seek (segment, rate, segment->format, flags, start_type, start,
3909 stop_type, stop, &update);
3918 GST_DEBUG_OBJECT (sink, "undefined format given, seek aborted.");
3923 /* perform a seek, only executed in pull mode */
3925 gst_base_sink_perform_seek (GstBaseSink * sink, GstPad * pad, GstEvent * event)
3929 GstFormat seek_format, dest_format;
3931 GstSeekType start_type, stop_type;
3932 gboolean seekseg_configured = FALSE;
3934 gboolean update, res = TRUE;
3935 GstSegment seeksegment;
3937 dest_format = sink->segment.format;
3940 GST_DEBUG_OBJECT (sink, "performing seek with event %p", event);
3941 gst_event_parse_seek (event, &rate, &seek_format, &flags,
3942 &start_type, &start, &stop_type, &stop);
3944 flush = flags & GST_SEEK_FLAG_FLUSH;
3946 GST_DEBUG_OBJECT (sink, "performing seek without event");
3951 GST_DEBUG_OBJECT (sink, "flushing upstream");
3952 gst_pad_push_event (pad, gst_event_new_flush_start ());
3953 gst_base_sink_flush_start (sink, pad);
3955 GST_DEBUG_OBJECT (sink, "pausing pulling thread");
3958 GST_PAD_STREAM_LOCK (pad);
3960 /* If we configured the seeksegment above, don't overwrite it now. Otherwise
3961 * copy the current segment info into the temp segment that we can actually
3962 * attempt the seek with. We only update the real segment if the seek succeeds. */
3963 if (!seekseg_configured) {
3964 memcpy (&seeksegment, &sink->segment, sizeof (GstSegment));
3966 /* now configure the final seek segment */
3968 if (sink->segment.format != seek_format) {
3969 /* OK, here's where we give the subclass a chance to convert the relative
3970 * seek into an absolute one in the processing format. We set up any
3971 * absolute seek above, before taking the stream lock. */
3972 if (!gst_base_sink_default_prepare_seek_segment (sink, event,
3974 GST_DEBUG_OBJECT (sink,
3975 "Preparing the seek failed after flushing. " "Aborting seek");
3979 /* The seek format matches our processing format, no need to ask the
3980 * the subclass to configure the segment. */
3981 gst_segment_do_seek (&seeksegment, rate, seek_format, flags,
3982 start_type, start, stop_type, stop, &update);
3985 /* Else, no seek event passed, so we're just (re)starting the
3990 GST_DEBUG_OBJECT (sink, "segment configured from %" G_GINT64_FORMAT
3991 " to %" G_GINT64_FORMAT ", position %" G_GINT64_FORMAT,
3992 seeksegment.start, seeksegment.stop, seeksegment.position);
3994 /* do the seek, segment.position contains the new position. */
3995 res = gst_base_sink_default_do_seek (sink, &seeksegment);
3999 GST_DEBUG_OBJECT (sink, "stop flushing upstream");
4000 gst_pad_push_event (pad, gst_event_new_flush_stop (TRUE));
4001 gst_base_sink_flush_stop (sink, pad, TRUE);
4002 } else if (res && sink->running) {
4003 /* we are running the current segment and doing a non-flushing seek,
4004 * close the segment first based on the position. */
4005 GST_DEBUG_OBJECT (sink, "closing running segment %" G_GINT64_FORMAT
4006 " to %" G_GINT64_FORMAT, sink->segment.start, sink->segment.position);
4009 /* The subclass must have converted the segment to the processing format
4011 if (res && seeksegment.format != dest_format) {
4012 GST_DEBUG_OBJECT (sink, "Subclass failed to prepare a seek segment "
4013 "in the correct format. Aborting seek.");
4017 GST_INFO_OBJECT (sink, "seeking done %d: %" GST_SEGMENT_FORMAT, res,
4020 /* if successful seek, we update our real segment and push
4021 * out the new segment. */
4023 gst_segment_copy_into (&seeksegment, &sink->segment);
4025 if (sink->segment.flags & GST_SEGMENT_FLAG_SEGMENT) {
4026 gst_element_post_message (GST_ELEMENT (sink),
4027 gst_message_new_segment_start (GST_OBJECT (sink),
4028 sink->segment.format, sink->segment.position));
4032 sink->priv->discont = TRUE;
4033 sink->running = TRUE;
4035 GST_PAD_STREAM_UNLOCK (pad);
4041 set_step_info (GstBaseSink * sink, GstStepInfo * current, GstStepInfo * pending,
4042 guint seqnum, GstFormat format, guint64 amount, gdouble rate,
4043 gboolean flush, gboolean intermediate)
4045 GST_OBJECT_LOCK (sink);
4046 pending->seqnum = seqnum;
4047 pending->format = format;
4048 pending->amount = amount;
4049 pending->position = 0;
4050 pending->rate = rate;
4051 pending->flush = flush;
4052 pending->intermediate = intermediate;
4053 pending->valid = TRUE;
4054 /* flush invalidates the current stepping segment */
4056 current->valid = FALSE;
4057 GST_OBJECT_UNLOCK (sink);
4061 gst_base_sink_perform_step (GstBaseSink * sink, GstPad * pad, GstEvent * event)
4063 GstBaseSinkPrivate *priv;
4064 GstBaseSinkClass *bclass;
4065 gboolean flush, intermediate;
4070 GstStepInfo *pending, *current;
4071 GstMessage *message;
4073 bclass = GST_BASE_SINK_GET_CLASS (sink);
4076 GST_DEBUG_OBJECT (sink, "performing step with event %p", event);
4078 gst_event_parse_step (event, &format, &amount, &rate, &flush, &intermediate);
4079 seqnum = gst_event_get_seqnum (event);
4081 pending = &priv->pending_step;
4082 current = &priv->current_step;
4084 /* post message first */
4085 message = gst_message_new_step_start (GST_OBJECT (sink), FALSE, format,
4086 amount, rate, flush, intermediate);
4087 gst_message_set_seqnum (message, seqnum);
4088 gst_element_post_message (GST_ELEMENT (sink), message);
4091 /* we need to call ::unlock before locking PREROLL_LOCK
4092 * since we lock it before going into ::render */
4094 bclass->unlock (sink);
4096 GST_BASE_SINK_PREROLL_LOCK (sink);
4097 /* now that we have the PREROLL lock, clear our unlock request */
4098 if (bclass->unlock_stop)
4099 bclass->unlock_stop (sink);
4101 /* update the stepinfo and make it valid */
4102 set_step_info (sink, current, pending, seqnum, format, amount, rate, flush,
4105 if (sink->priv->async_enabled) {
4106 /* and we need to commit our state again on the next
4107 * prerolled buffer */
4108 sink->playing_async = TRUE;
4109 priv->pending_step.need_preroll = TRUE;
4110 sink->need_preroll = FALSE;
4111 gst_base_sink_update_start_time (sink);
4112 gst_element_lost_state (GST_ELEMENT_CAST (sink));
4114 sink->priv->have_latency = TRUE;
4115 sink->need_preroll = FALSE;
4117 priv->current_sstart = GST_CLOCK_TIME_NONE;
4118 priv->current_sstop = GST_CLOCK_TIME_NONE;
4119 priv->eos_rtime = GST_CLOCK_TIME_NONE;
4120 priv->call_preroll = TRUE;
4121 gst_base_sink_set_last_buffer (sink, NULL);
4122 gst_base_sink_set_last_buffer_list (sink, NULL);
4123 gst_base_sink_reset_qos (sink);
4125 if (sink->clock_id) {
4126 gst_clock_id_unschedule (sink->clock_id);
4129 if (sink->have_preroll) {
4130 GST_DEBUG_OBJECT (sink, "signal waiter");
4131 priv->step_unlock = TRUE;
4132 GST_BASE_SINK_PREROLL_SIGNAL (sink);
4134 GST_BASE_SINK_PREROLL_UNLOCK (sink);
4136 /* update the stepinfo and make it valid */
4137 set_step_info (sink, current, pending, seqnum, format, amount, rate, flush,
4147 gst_base_sink_loop (GstPad * pad)
4150 GstBaseSink *basesink;
4151 GstBuffer *buf = NULL;
4152 GstFlowReturn result;
4156 parent = GST_OBJECT_PARENT (pad);
4157 basesink = GST_BASE_SINK (parent);
4159 g_assert (basesink->pad_mode == GST_PAD_MODE_PULL);
4161 if ((blocksize = basesink->priv->blocksize) == 0)
4164 offset = basesink->segment.position;
4166 GST_DEBUG_OBJECT (basesink, "pulling %" G_GUINT64_FORMAT ", %u",
4169 result = gst_pad_pull_range (pad, offset, blocksize, &buf);
4170 if (G_UNLIKELY (result != GST_FLOW_OK))
4173 if (G_UNLIKELY (buf == NULL))
4176 offset += gst_buffer_get_size (buf);
4178 basesink->segment.position = offset;
4180 GST_BASE_SINK_PREROLL_LOCK (basesink);
4181 result = gst_base_sink_chain_unlocked (basesink, pad, buf, FALSE);
4182 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
4183 if (G_UNLIKELY (result != GST_FLOW_OK))
4191 GST_LOG_OBJECT (basesink, "pausing task, reason %s",
4192 gst_flow_get_name (result));
4193 gst_pad_pause_task (pad);
4194 if (result == GST_FLOW_EOS) {
4195 /* perform EOS logic */
4196 if (basesink->segment.flags & GST_SEGMENT_FLAG_SEGMENT) {
4197 gst_element_post_message (GST_ELEMENT_CAST (basesink),
4198 gst_message_new_segment_done (GST_OBJECT_CAST (basesink),
4199 basesink->segment.format, basesink->segment.position));
4200 gst_base_sink_event (pad, parent,
4201 gst_event_new_segment_done (basesink->segment.format,
4202 basesink->segment.position));
4204 gst_base_sink_event (pad, parent, gst_event_new_eos ());
4206 } else if (result == GST_FLOW_NOT_LINKED || result <= GST_FLOW_EOS) {
4207 /* for fatal errors we post an error message, post the error
4208 * first so the app knows about the error first.
4209 * wrong-state is not a fatal error because it happens due to
4210 * flushing and posting an error message in that case is the
4211 * wrong thing to do, e.g. when basesrc is doing a flushing
4213 GST_ELEMENT_FLOW_ERROR (basesink, result);
4214 gst_base_sink_event (pad, parent, gst_event_new_eos ());
4220 GST_LOG_OBJECT (basesink, "no buffer, pausing");
4221 GST_ELEMENT_ERROR (basesink, STREAM, FAILED,
4222 (_("Internal data flow error.")), ("element returned NULL buffer"));
4223 result = GST_FLOW_ERROR;
4229 gst_base_sink_set_flushing (GstBaseSink * basesink, GstPad * pad,
4232 GstBaseSinkClass *bclass;
4234 bclass = GST_BASE_SINK_GET_CLASS (basesink);
4237 /* unlock any subclasses, we need to do this before grabbing the
4238 * PREROLL_LOCK since we hold this lock before going into ::render. */
4240 bclass->unlock (basesink);
4243 GST_BASE_SINK_PREROLL_LOCK (basesink);
4244 basesink->flushing = flushing;
4246 /* step 1, now that we have the PREROLL lock, clear our unlock request */
4247 if (bclass->unlock_stop)
4248 bclass->unlock_stop (basesink);
4250 /* set need_preroll before we unblock the clock. If the clock is unblocked
4251 * before timing out, we can reuse the buffer for preroll. */
4252 basesink->need_preroll = TRUE;
4254 /* step 2, unblock clock sync (if any) or any other blocking thing */
4255 if (basesink->clock_id) {
4256 gst_clock_id_unschedule (basesink->clock_id);
4259 /* flush out the data thread if it's locked in finish_preroll, this will
4260 * also flush out the EOS state */
4261 GST_DEBUG_OBJECT (basesink,
4262 "flushing out data thread, need preroll to TRUE");
4264 /* we can't have EOS anymore now */
4265 basesink->eos = FALSE;
4266 basesink->priv->received_eos = FALSE;
4267 basesink->have_preroll = FALSE;
4268 basesink->priv->step_unlock = FALSE;
4269 /* can't report latency anymore until we preroll again */
4270 if (basesink->priv->async_enabled) {
4271 GST_OBJECT_LOCK (basesink);
4272 basesink->priv->have_latency = FALSE;
4273 GST_OBJECT_UNLOCK (basesink);
4275 /* and signal any waiters now */
4276 GST_BASE_SINK_PREROLL_SIGNAL (basesink);
4278 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
4284 gst_base_sink_default_activate_pull (GstBaseSink * basesink, gboolean active)
4290 result = gst_pad_start_task (basesink->sinkpad,
4291 (GstTaskFunction) gst_base_sink_loop, basesink->sinkpad, NULL);
4293 /* step 2, make sure streaming finishes */
4294 result = gst_pad_stop_task (basesink->sinkpad);
4301 gst_base_sink_pad_activate (GstPad * pad, GstObject * parent)
4303 gboolean result = FALSE;
4304 GstBaseSink *basesink;
4308 basesink = GST_BASE_SINK (parent);
4310 GST_DEBUG_OBJECT (basesink, "Trying pull mode first");
4312 gst_base_sink_set_flushing (basesink, pad, FALSE);
4314 /* we need to have the pull mode enabled */
4315 if (!basesink->can_activate_pull) {
4316 GST_DEBUG_OBJECT (basesink, "pull mode disabled");
4320 /* check if downstreams supports pull mode at all */
4321 query = gst_query_new_scheduling ();
4323 if (!gst_pad_peer_query (pad, query)) {
4324 gst_query_unref (query);
4325 GST_DEBUG_OBJECT (basesink, "peer query failed, no pull mode");
4329 /* parse result of the query */
4330 pull_mode = gst_query_has_scheduling_mode (query, GST_PAD_MODE_PULL);
4331 gst_query_unref (query);
4334 GST_DEBUG_OBJECT (basesink, "pull mode not supported");
4338 /* set the pad mode before starting the task so that it's in the
4339 * correct state for the new thread. also the sink set_caps and get_caps
4340 * function checks this */
4341 basesink->pad_mode = GST_PAD_MODE_PULL;
4343 /* we first try to negotiate a format so that when we try to activate
4344 * downstream, it knows about our format */
4345 if (!gst_base_sink_negotiate_pull (basesink)) {
4346 GST_DEBUG_OBJECT (basesink, "failed to negotiate in pull mode");
4350 /* ok activate now */
4351 if (!gst_pad_activate_mode (pad, GST_PAD_MODE_PULL, TRUE)) {
4352 /* clear any pending caps */
4353 GST_OBJECT_LOCK (basesink);
4354 gst_caps_replace (&basesink->priv->caps, NULL);
4355 GST_OBJECT_UNLOCK (basesink);
4356 GST_DEBUG_OBJECT (basesink, "failed to activate in pull mode");
4360 GST_DEBUG_OBJECT (basesink, "Success activating pull mode");
4364 /* push mode fallback */
4366 GST_DEBUG_OBJECT (basesink, "Falling back to push mode");
4367 if ((result = gst_pad_activate_mode (pad, GST_PAD_MODE_PUSH, TRUE))) {
4368 GST_DEBUG_OBJECT (basesink, "Success activating push mode");
4373 GST_WARNING_OBJECT (basesink, "Could not activate pad in either mode");
4374 gst_base_sink_set_flushing (basesink, pad, TRUE);
4381 gst_base_sink_pad_activate_push (GstPad * pad, GstObject * parent,
4385 GstBaseSink *basesink;
4387 basesink = GST_BASE_SINK (parent);
4390 if (!basesink->can_activate_push) {
4392 basesink->pad_mode = GST_PAD_MODE_NONE;
4395 basesink->pad_mode = GST_PAD_MODE_PUSH;
4398 if (G_UNLIKELY (basesink->pad_mode != GST_PAD_MODE_PUSH)) {
4399 g_warning ("Internal GStreamer activation error!!!");
4402 gst_base_sink_set_flushing (basesink, pad, TRUE);
4404 basesink->pad_mode = GST_PAD_MODE_NONE;
4412 gst_base_sink_negotiate_pull (GstBaseSink * basesink)
4419 /* this returns the intersection between our caps and the peer caps. If there
4420 * is no peer, it returns %NULL and we can't operate in pull mode so we can
4421 * fail the negotiation. */
4422 caps = gst_pad_get_allowed_caps (GST_BASE_SINK_PAD (basesink));
4423 if (caps == NULL || gst_caps_is_empty (caps))
4424 goto no_caps_possible;
4426 GST_DEBUG_OBJECT (basesink, "allowed caps: %" GST_PTR_FORMAT, caps);
4428 if (gst_caps_is_any (caps)) {
4429 GST_DEBUG_OBJECT (basesink, "caps were ANY after fixating, "
4431 /* neither side has template caps in this case, so they are prepared for
4432 pull() without setcaps() */
4436 caps = gst_base_sink_fixate (basesink, caps);
4437 GST_DEBUG_OBJECT (basesink, "fixated to: %" GST_PTR_FORMAT, caps);
4439 if (gst_caps_is_fixed (caps)) {
4440 if (!gst_pad_set_caps (GST_BASE_SINK_PAD (basesink), caps))
4441 goto could_not_set_caps;
4447 gst_caps_unref (caps);
4453 GST_INFO_OBJECT (basesink, "Pipeline could not agree on caps");
4454 GST_DEBUG_OBJECT (basesink, "get_allowed_caps() returned EMPTY");
4456 gst_caps_unref (caps);
4461 GST_INFO_OBJECT (basesink, "Could not set caps: %" GST_PTR_FORMAT, caps);
4462 gst_caps_unref (caps);
4467 /* this won't get called until we implement an activate function */
4469 gst_base_sink_pad_activate_pull (GstPad * pad, GstObject * parent,
4472 gboolean result = FALSE;
4473 GstBaseSink *basesink;
4474 GstBaseSinkClass *bclass;
4476 basesink = GST_BASE_SINK (parent);
4477 bclass = GST_BASE_SINK_GET_CLASS (basesink);
4482 /* we mark we have a newsegment here because pull based
4483 * mode works just fine without having a newsegment before the
4485 gst_segment_init (&basesink->segment, GST_FORMAT_BYTES);
4486 GST_OBJECT_LOCK (basesink);
4487 basesink->have_newsegment = TRUE;
4488 GST_OBJECT_UNLOCK (basesink);
4490 /* get the peer duration in bytes */
4491 result = gst_pad_peer_query_duration (pad, GST_FORMAT_BYTES, &duration);
4493 GST_DEBUG_OBJECT (basesink,
4494 "setting duration in bytes to %" G_GINT64_FORMAT, duration);
4495 basesink->segment.duration = duration;
4497 GST_DEBUG_OBJECT (basesink, "unknown duration");
4500 if (bclass->activate_pull)
4501 result = bclass->activate_pull (basesink, TRUE);
4506 goto activate_failed;
4509 if (G_UNLIKELY (basesink->pad_mode != GST_PAD_MODE_PULL)) {
4510 g_warning ("Internal GStreamer activation error!!!");
4513 result = gst_base_sink_set_flushing (basesink, pad, TRUE);
4514 if (bclass->activate_pull)
4515 result &= bclass->activate_pull (basesink, FALSE);
4516 basesink->pad_mode = GST_PAD_MODE_NONE;
4525 /* reset, as starting the thread failed */
4526 basesink->pad_mode = GST_PAD_MODE_NONE;
4528 GST_ERROR_OBJECT (basesink, "subclass failed to activate in pull mode");
4534 gst_base_sink_pad_activate_mode (GstPad * pad, GstObject * parent,
4535 GstPadMode mode, gboolean active)
4540 case GST_PAD_MODE_PULL:
4541 res = gst_base_sink_pad_activate_pull (pad, parent, active);
4543 case GST_PAD_MODE_PUSH:
4544 res = gst_base_sink_pad_activate_push (pad, parent, active);
4547 GST_LOG_OBJECT (pad, "unknown activation mode %d", mode);
4554 /* send an event to our sinkpad peer. */
4556 gst_base_sink_send_event (GstElement * element, GstEvent * event)
4559 GstBaseSink *basesink = GST_BASE_SINK (element);
4560 gboolean forward, result = TRUE;
4563 GST_OBJECT_LOCK (element);
4564 /* get the pad and the scheduling mode */
4565 pad = gst_object_ref (basesink->sinkpad);
4566 mode = basesink->pad_mode;
4567 GST_OBJECT_UNLOCK (element);
4569 /* only push UPSTREAM events upstream */
4570 forward = GST_EVENT_IS_UPSTREAM (event);
4572 GST_DEBUG_OBJECT (basesink, "handling event %p %" GST_PTR_FORMAT, event,
4575 switch (GST_EVENT_TYPE (event)) {
4576 case GST_EVENT_LATENCY:
4578 GstClockTime latency;
4580 gst_event_parse_latency (event, &latency);
4582 /* store the latency. We use this to adjust the running_time before syncing
4583 * it to the clock. */
4584 GST_OBJECT_LOCK (element);
4585 basesink->priv->latency = latency;
4586 if (!basesink->priv->have_latency)
4588 GST_OBJECT_UNLOCK (element);
4589 GST_DEBUG_OBJECT (basesink, "latency set to %" GST_TIME_FORMAT,
4590 GST_TIME_ARGS (latency));
4592 /* We forward this event so that all elements know about the global pipeline
4593 * latency. This is interesting for an element when it wants to figure out
4594 * when a particular piece of data will be rendered. */
4597 case GST_EVENT_SEEK:
4598 /* in pull mode we will execute the seek */
4599 if (mode == GST_PAD_MODE_PULL)
4600 result = gst_base_sink_perform_seek (basesink, pad, event);
4602 case GST_EVENT_STEP:
4603 result = gst_base_sink_perform_step (basesink, pad, event);
4611 GST_DEBUG_OBJECT (basesink, "sending event %p %" GST_PTR_FORMAT, event,
4613 result = gst_pad_push_event (pad, event);
4615 /* not forwarded, unref the event */
4616 gst_event_unref (event);
4619 gst_object_unref (pad);
4621 GST_DEBUG_OBJECT (basesink, "handled event: %d", result);
4627 gst_base_sink_get_position (GstBaseSink * basesink, GstFormat format,
4628 gint64 * cur, gboolean * upstream)
4630 GstClock *clock = NULL;
4631 gboolean res = FALSE;
4633 GstSegment *segment;
4634 GstClockTime now, latency;
4635 GstClockTimeDiff base_time;
4636 gint64 time, base, offset, duration;
4639 gboolean last_seen, with_clock, in_paused;
4641 GST_OBJECT_LOCK (basesink);
4642 /* we can only get the segment when we are not NULL or READY */
4643 if (!basesink->have_newsegment)
4647 /* when not in PLAYING or when we're busy with a state change, we
4648 * cannot read from the clock so we report time based on the
4649 * last seen timestamp. */
4650 if (GST_STATE (basesink) != GST_STATE_PLAYING ||
4651 GST_STATE_PENDING (basesink) != GST_STATE_VOID_PENDING) {
4655 segment = &basesink->segment;
4657 /* get the format in the segment */
4658 oformat = segment->format;
4660 /* report with last seen position when EOS */
4661 last_seen = basesink->eos;
4663 /* assume we will use the clock for getting the current position */
4665 if (!basesink->sync)
4668 /* and we need a clock */
4669 if (G_UNLIKELY ((clock = GST_ELEMENT_CLOCK (basesink)) == NULL))
4672 gst_object_ref (clock);
4674 /* mainloop might be querying position when going to playing async,
4675 * while (audio) rendering might be quickly advancing stream position,
4676 * so use clock asap rather than last reported position */
4677 if (in_paused && with_clock && g_atomic_int_get (&basesink->priv->to_playing)) {
4678 GST_DEBUG_OBJECT (basesink, "going to PLAYING, so not PAUSED");
4682 /* collect all data we need holding the lock */
4683 if (GST_CLOCK_TIME_IS_VALID (segment->time))
4684 time = segment->time;
4688 if (GST_CLOCK_TIME_IS_VALID (segment->offset))
4689 offset = segment->offset;
4693 if (GST_CLOCK_TIME_IS_VALID (segment->stop))
4694 duration = segment->stop - segment->start;
4698 base = segment->base;
4699 rate = segment->rate * segment->applied_rate;
4700 latency = basesink->priv->latency;
4703 /* in paused, use start_time */
4704 base_time = GST_ELEMENT_START_TIME (basesink);
4705 GST_DEBUG_OBJECT (basesink, "in paused, using start time %" GST_TIME_FORMAT,
4706 GST_TIME_ARGS (base_time));
4707 } else if (with_clock) {
4708 /* else use clock when needed */
4709 base_time = GST_ELEMENT_CAST (basesink)->base_time;
4710 GST_DEBUG_OBJECT (basesink, "using clock and base time %" GST_TIME_FORMAT,
4711 GST_TIME_ARGS (base_time));
4713 /* else, no sync or clock -> no base time */
4714 GST_DEBUG_OBJECT (basesink, "no sync or no clock");
4718 /* no base_time, we can't calculate running_time, use last seem timestamp to report
4720 if (base_time == -1)
4723 if (oformat == GST_FORMAT_TIME) {
4726 start = basesink->priv->current_sstart;
4727 stop = basesink->priv->current_sstop;
4730 /* when we don't use the clock, we use the last position as a lower bound */
4731 if (stop == -1 || segment->rate > 0.0)
4736 GST_DEBUG_OBJECT (basesink, "in PAUSED using last %" GST_TIME_FORMAT,
4737 GST_TIME_ARGS (last));
4739 /* in playing and paused, use last stop time as upper bound */
4740 if (start == -1 || segment->rate > 0.0)
4745 GST_DEBUG_OBJECT (basesink, "in PLAYING using last %" GST_TIME_FORMAT,
4746 GST_TIME_ARGS (last));
4749 /* convert position to stream time */
4750 last = gst_segment_to_stream_time (segment, oformat, segment->position);
4752 GST_DEBUG_OBJECT (basesink, "in using last %" G_GINT64_FORMAT, last);
4755 /* need to release the object lock before we can get the time,
4756 * a clock might take the LOCK of the provider, which could be
4757 * a basesink subclass. */
4758 GST_OBJECT_UNLOCK (basesink);
4761 /* in EOS or when no valid stream_time, report the value of last seen
4764 /* no timestamp, we need to ask upstream */
4765 GST_DEBUG_OBJECT (basesink, "no last seen timestamp, asking upstream");
4770 GST_DEBUG_OBJECT (basesink, "using last seen timestamp %" GST_TIME_FORMAT,
4771 GST_TIME_ARGS (last));
4774 if (oformat != GST_FORMAT_TIME) {
4775 /* convert base, time and duration to time */
4776 if (!gst_pad_query_convert (basesink->sinkpad, oformat, base,
4777 GST_FORMAT_TIME, &base))
4778 goto convert_failed;
4779 if (!gst_pad_query_convert (basesink->sinkpad, oformat, duration,
4780 GST_FORMAT_TIME, &duration))
4781 goto convert_failed;
4782 if (!gst_pad_query_convert (basesink->sinkpad, oformat, time,
4783 GST_FORMAT_TIME, &time))
4784 goto convert_failed;
4785 if (!gst_pad_query_convert (basesink->sinkpad, oformat, last,
4786 GST_FORMAT_TIME, &last))
4787 goto convert_failed;
4789 /* assume time format from now on */
4790 oformat = GST_FORMAT_TIME;
4793 if (!in_paused && with_clock) {
4794 now = gst_clock_get_time (clock);
4800 /* subtract base time and base time from the clock time.
4801 * Make sure we don't go negative. This is the current time in
4802 * the segment which we need to scale with the combined
4803 * rate and applied rate. */
4805 base_time += latency;
4806 if (GST_CLOCK_DIFF (base_time, now) < 0)
4809 /* for negative rates we need to count back from the segment
4814 *cur = time + offset + gst_guint64_to_gdouble (now - base_time) * rate;
4816 /* never report more than last seen position */
4819 *cur = MIN (last, *cur);
4821 *cur = MAX (last, *cur);
4824 GST_DEBUG_OBJECT (basesink,
4825 "now %" GST_TIME_FORMAT " - base_time %" GST_TIME_FORMAT " - base %"
4826 GST_TIME_FORMAT " + time %" GST_TIME_FORMAT " last %" GST_TIME_FORMAT,
4827 GST_TIME_ARGS (now), GST_TIME_ARGS (base_time), GST_TIME_ARGS (base),
4828 GST_TIME_ARGS (time), GST_TIME_ARGS (last));
4831 if (oformat != format) {
4832 /* convert to final format */
4833 if (!gst_pad_query_convert (basesink->sinkpad, oformat, *cur, format, cur))
4834 goto convert_failed;
4840 GST_DEBUG_OBJECT (basesink, "res: %d, POSITION: %" GST_TIME_FORMAT,
4841 res, GST_TIME_ARGS (*cur));
4844 gst_object_unref (clock);
4851 /* in NULL or READY we always return FALSE and -1 */
4852 GST_DEBUG_OBJECT (basesink, "position in wrong state, return -1");
4855 GST_OBJECT_UNLOCK (basesink);
4860 GST_DEBUG_OBJECT (basesink, "convert failed, try upstream");
4868 gst_base_sink_get_duration (GstBaseSink * basesink, GstFormat format,
4869 gint64 * dur, gboolean * upstream)
4871 gboolean res = FALSE;
4873 if (basesink->pad_mode == GST_PAD_MODE_PULL) {
4876 /* get the duration in bytes, in pull mode that's all we are sure to
4877 * know. We have to explicitly get this value from upstream instead of
4878 * using our cached value because it might change. Duration caching
4879 * should be done at a higher level. */
4881 gst_pad_peer_query_duration (basesink->sinkpad, GST_FORMAT_BYTES,
4884 basesink->segment.duration = uduration;
4885 if (format != GST_FORMAT_BYTES) {
4886 /* convert to the requested format */
4888 gst_pad_query_convert (basesink->sinkpad, GST_FORMAT_BYTES,
4889 uduration, format, dur);
4903 default_element_query (GstElement * element, GstQuery * query)
4905 gboolean res = FALSE;
4907 GstBaseSink *basesink = GST_BASE_SINK (element);
4909 switch (GST_QUERY_TYPE (query)) {
4910 case GST_QUERY_POSITION:
4914 gboolean upstream = FALSE;
4916 gst_query_parse_position (query, &format, NULL);
4918 GST_DEBUG_OBJECT (basesink, "position query in format %s",
4919 gst_format_get_name (format));
4921 /* first try to get the position based on the clock */
4923 gst_base_sink_get_position (basesink, format, &cur, &upstream))) {
4924 gst_query_set_position (query, format, cur);
4925 } else if (upstream) {
4926 /* fallback to peer query */
4927 res = gst_pad_peer_query (basesink->sinkpad, query);
4930 /* we can handle a few things if upstream failed */
4931 if (format == GST_FORMAT_PERCENT) {
4934 res = gst_base_sink_get_position (basesink, GST_FORMAT_TIME, &cur,
4936 if (!res && upstream) {
4938 gst_pad_peer_query_position (basesink->sinkpad, GST_FORMAT_TIME,
4942 res = gst_base_sink_get_duration (basesink, GST_FORMAT_TIME, &dur,
4944 if (!res && upstream) {
4946 gst_pad_peer_query_duration (basesink->sinkpad,
4947 GST_FORMAT_TIME, &dur);
4953 pos = gst_util_uint64_scale (100 * GST_FORMAT_PERCENT_SCALE, cur,
4955 gst_query_set_position (query, GST_FORMAT_PERCENT, pos);
4961 case GST_QUERY_DURATION:
4965 gboolean upstream = FALSE;
4967 gst_query_parse_duration (query, &format, NULL);
4969 GST_DEBUG_OBJECT (basesink, "duration query in format %s",
4970 gst_format_get_name (format));
4973 gst_base_sink_get_duration (basesink, format, &dur, &upstream))) {
4974 gst_query_set_duration (query, format, dur);
4975 } else if (upstream) {
4976 /* fallback to peer query */
4977 res = gst_pad_peer_query (basesink->sinkpad, query);
4980 /* we can handle a few things if upstream failed */
4981 if (format == GST_FORMAT_PERCENT) {
4982 gst_query_set_duration (query, GST_FORMAT_PERCENT,
4983 GST_FORMAT_PERCENT_MAX);
4989 case GST_QUERY_LATENCY:
4991 gboolean live, us_live;
4992 GstClockTime min, max;
4994 if ((res = gst_base_sink_query_latency (basesink, &live, &us_live, &min,
4996 gst_query_set_latency (query, live, min, max);
5000 case GST_QUERY_JITTER:
5002 case GST_QUERY_RATE:
5003 /* gst_query_set_rate (query, basesink->segment_rate); */
5006 case GST_QUERY_SEGMENT:
5008 if (basesink->pad_mode == GST_PAD_MODE_PULL) {
5012 format = basesink->segment.format;
5015 gst_segment_to_stream_time (&basesink->segment, format,
5016 basesink->segment.start);
5017 if ((stop = basesink->segment.stop) == -1)
5018 stop = basesink->segment.duration;
5020 stop = gst_segment_to_stream_time (&basesink->segment, format, stop);
5022 gst_query_set_segment (query, basesink->segment.rate, format, start,
5026 res = gst_pad_peer_query (basesink->sinkpad, query);
5030 case GST_QUERY_SEEKING:
5031 case GST_QUERY_CONVERT:
5032 case GST_QUERY_FORMATS:
5034 res = gst_pad_peer_query (basesink->sinkpad, query);
5037 GST_DEBUG_OBJECT (basesink, "query %s returns %d",
5038 GST_QUERY_TYPE_NAME (query), res);
5043 gst_base_sink_drain (GstBaseSink * basesink)
5046 GstBufferList *old_list;
5048 GST_OBJECT_LOCK (basesink);
5049 if ((old = basesink->priv->last_buffer))
5050 basesink->priv->last_buffer = gst_buffer_copy_deep (old);
5052 if ((old_list = basesink->priv->last_buffer_list))
5053 basesink->priv->last_buffer_list = gst_buffer_list_copy_deep (old_list);
5054 GST_OBJECT_UNLOCK (basesink);
5057 gst_buffer_unref (old);
5059 gst_mini_object_unref (GST_MINI_OBJECT_CAST (old_list));
5063 gst_base_sink_default_query (GstBaseSink * basesink, GstQuery * query)
5066 GstBaseSinkClass *bclass;
5068 bclass = GST_BASE_SINK_GET_CLASS (basesink);
5070 switch (GST_QUERY_TYPE (query)) {
5071 case GST_QUERY_ALLOCATION:
5073 gst_base_sink_drain (basesink);
5074 if (bclass->propose_allocation)
5075 res = bclass->propose_allocation (basesink, query);
5080 case GST_QUERY_CAPS:
5082 GstCaps *caps, *filter;
5084 gst_query_parse_caps (query, &filter);
5085 caps = gst_base_sink_query_caps (basesink, basesink->sinkpad, filter);
5086 gst_query_set_caps_result (query, caps);
5087 gst_caps_unref (caps);
5091 case GST_QUERY_ACCEPT_CAPS:
5093 GstCaps *caps, *allowed;
5096 /* slightly faster than the default implementation */
5097 gst_query_parse_accept_caps (query, &caps);
5098 allowed = gst_base_sink_query_caps (basesink, basesink->sinkpad, NULL);
5099 subset = gst_caps_is_subset (caps, allowed);
5100 GST_DEBUG_OBJECT (basesink, "Checking if requested caps %" GST_PTR_FORMAT
5101 " are a subset of pad caps %" GST_PTR_FORMAT " result %d", caps,
5103 gst_caps_unref (allowed);
5104 gst_query_set_accept_caps_result (query, subset);
5108 case GST_QUERY_DRAIN:
5110 gst_base_sink_drain (basesink);
5114 case GST_QUERY_POSITION:
5116 res = default_element_query (GST_ELEMENT (basesink), query);
5121 gst_pad_query_default (basesink->sinkpad, GST_OBJECT_CAST (basesink),
5129 gst_base_sink_sink_query (GstPad * pad, GstObject * parent, GstQuery * query)
5131 GstBaseSink *basesink;
5132 GstBaseSinkClass *bclass;
5135 basesink = GST_BASE_SINK_CAST (parent);
5136 bclass = GST_BASE_SINK_GET_CLASS (basesink);
5139 res = bclass->query (basesink, query);
5146 static GstStateChangeReturn
5147 gst_base_sink_change_state (GstElement * element, GstStateChange transition)
5149 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
5150 GstBaseSink *basesink = GST_BASE_SINK (element);
5151 GstBaseSinkClass *bclass;
5152 GstBaseSinkPrivate *priv;
5154 priv = basesink->priv;
5156 bclass = GST_BASE_SINK_GET_CLASS (basesink);
5158 switch (transition) {
5159 case GST_STATE_CHANGE_NULL_TO_READY:
5161 if (!bclass->start (basesink))
5164 case GST_STATE_CHANGE_READY_TO_PAUSED:
5165 /* need to complete preroll before this state change completes, there
5166 * is no data flow in READY so we can safely assume we need to preroll. */
5167 GST_BASE_SINK_PREROLL_LOCK (basesink);
5168 GST_DEBUG_OBJECT (basesink, "READY to PAUSED");
5169 basesink->have_newsegment = FALSE;
5170 gst_segment_init (&basesink->segment, GST_FORMAT_UNDEFINED);
5171 basesink->offset = 0;
5172 basesink->have_preroll = FALSE;
5173 priv->step_unlock = FALSE;
5174 basesink->need_preroll = TRUE;
5175 basesink->playing_async = TRUE;
5176 priv->current_sstart = GST_CLOCK_TIME_NONE;
5177 priv->current_sstop = GST_CLOCK_TIME_NONE;
5178 priv->eos_rtime = GST_CLOCK_TIME_NONE;
5180 basesink->eos = FALSE;
5181 priv->received_eos = FALSE;
5182 gst_base_sink_reset_qos (basesink);
5184 priv->committed = FALSE;
5185 priv->call_preroll = TRUE;
5186 priv->current_step.valid = FALSE;
5187 priv->pending_step.valid = FALSE;
5188 if (priv->async_enabled) {
5189 GST_DEBUG_OBJECT (basesink, "doing async state change");
5190 /* when async enabled, post async-start message and return ASYNC from
5191 * the state change function */
5192 ret = GST_STATE_CHANGE_ASYNC;
5193 gst_element_post_message (GST_ELEMENT_CAST (basesink),
5194 gst_message_new_async_start (GST_OBJECT_CAST (basesink)));
5196 priv->have_latency = TRUE;
5198 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
5200 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
5201 GST_BASE_SINK_PREROLL_LOCK (basesink);
5202 g_atomic_int_set (&basesink->priv->to_playing, TRUE);
5203 if (!gst_base_sink_needs_preroll (basesink)) {
5204 GST_DEBUG_OBJECT (basesink, "PAUSED to PLAYING, don't need preroll");
5205 /* no preroll needed anymore now. */
5206 basesink->playing_async = FALSE;
5207 basesink->need_preroll = FALSE;
5208 if (basesink->eos) {
5209 GstMessage *message;
5211 /* need to post EOS message here */
5212 GST_DEBUG_OBJECT (basesink, "Now posting EOS");
5213 message = gst_message_new_eos (GST_OBJECT_CAST (basesink));
5214 gst_message_set_seqnum (message, basesink->priv->seqnum);
5215 gst_element_post_message (GST_ELEMENT_CAST (basesink), message);
5217 GST_DEBUG_OBJECT (basesink, "signal preroll");
5218 GST_BASE_SINK_PREROLL_SIGNAL (basesink);
5221 GST_DEBUG_OBJECT (basesink, "PAUSED to PLAYING, we are not prerolled");
5222 basesink->need_preroll = TRUE;
5223 basesink->playing_async = TRUE;
5224 priv->call_preroll = TRUE;
5225 priv->committed = FALSE;
5226 if (priv->async_enabled) {
5227 GST_DEBUG_OBJECT (basesink, "doing async state change");
5228 ret = GST_STATE_CHANGE_ASYNC;
5229 gst_element_post_message (GST_ELEMENT_CAST (basesink),
5230 gst_message_new_async_start (GST_OBJECT_CAST (basesink)));
5233 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
5240 GstStateChangeReturn bret;
5242 bret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
5243 if (G_UNLIKELY (bret == GST_STATE_CHANGE_FAILURE))
5244 goto activate_failed;
5247 switch (transition) {
5248 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
5249 /* completed transition, so need not be marked any longer
5250 * And it should be unmarked, since e.g. losing our position upon flush
5251 * does not really change state to PAUSED ... */
5252 g_atomic_int_set (&basesink->priv->to_playing, FALSE);
5254 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
5255 g_atomic_int_set (&basesink->priv->to_playing, FALSE);
5256 GST_DEBUG_OBJECT (basesink, "PLAYING to PAUSED");
5257 /* FIXME, make sure we cannot enter _render first */
5259 /* we need to call ::unlock before locking PREROLL_LOCK
5260 * since we lock it before going into ::render */
5262 bclass->unlock (basesink);
5264 GST_BASE_SINK_PREROLL_LOCK (basesink);
5265 GST_DEBUG_OBJECT (basesink, "got preroll lock");
5266 /* now that we have the PREROLL lock, clear our unlock request */
5267 if (bclass->unlock_stop)
5268 bclass->unlock_stop (basesink);
5270 if (basesink->clock_id) {
5271 GST_DEBUG_OBJECT (basesink, "unschedule clock");
5272 gst_clock_id_unschedule (basesink->clock_id);
5275 /* if we don't have a preroll buffer we need to wait for a preroll and
5277 if (!gst_base_sink_needs_preroll (basesink)) {
5278 GST_DEBUG_OBJECT (basesink, "PLAYING to PAUSED, we are prerolled");
5279 basesink->playing_async = FALSE;
5280 basesink->need_preroll = FALSE;
5282 if (GST_STATE_TARGET (GST_ELEMENT (basesink)) <= GST_STATE_READY) {
5283 GST_DEBUG_OBJECT (basesink, "element is <= READY");
5284 ret = GST_STATE_CHANGE_SUCCESS;
5286 GST_DEBUG_OBJECT (basesink,
5287 "PLAYING to PAUSED, we are not prerolled");
5288 basesink->playing_async = TRUE;
5289 basesink->need_preroll = TRUE;
5290 priv->committed = FALSE;
5291 priv->call_preroll = TRUE;
5292 if (priv->async_enabled) {
5293 GST_DEBUG_OBJECT (basesink, "doing async state change");
5294 ret = GST_STATE_CHANGE_ASYNC;
5295 gst_element_post_message (GST_ELEMENT_CAST (basesink),
5296 gst_message_new_async_start (GST_OBJECT_CAST (basesink)));
5300 GST_DEBUG_OBJECT (basesink, "rendered: %" G_GUINT64_FORMAT
5301 ", dropped: %" G_GUINT64_FORMAT, priv->rendered, priv->dropped);
5303 gst_base_sink_reset_qos (basesink);
5304 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
5306 case GST_STATE_CHANGE_PAUSED_TO_READY:
5307 GST_BASE_SINK_PREROLL_LOCK (basesink);
5308 /* start by resetting our position state with the object lock so that the
5309 * position query gets the right idea. We do this before we post the
5310 * messages so that the message handlers pick this up. */
5311 GST_OBJECT_LOCK (basesink);
5312 basesink->have_newsegment = FALSE;
5313 priv->current_sstart = GST_CLOCK_TIME_NONE;
5314 priv->current_sstop = GST_CLOCK_TIME_NONE;
5315 priv->have_latency = FALSE;
5316 if (priv->cached_clock_id) {
5317 gst_clock_id_unref (priv->cached_clock_id);
5318 priv->cached_clock_id = NULL;
5320 gst_caps_replace (&basesink->priv->caps, NULL);
5321 GST_OBJECT_UNLOCK (basesink);
5323 gst_base_sink_set_last_buffer (basesink, NULL);
5324 gst_base_sink_set_last_buffer_list (basesink, NULL);
5325 priv->call_preroll = FALSE;
5327 if (!priv->committed) {
5328 if (priv->async_enabled) {
5329 GST_DEBUG_OBJECT (basesink, "PAUSED to READY, posting async-done");
5331 gst_element_post_message (GST_ELEMENT_CAST (basesink),
5332 gst_message_new_state_changed (GST_OBJECT_CAST (basesink),
5333 GST_STATE_PLAYING, GST_STATE_PAUSED, GST_STATE_READY));
5335 gst_element_post_message (GST_ELEMENT_CAST (basesink),
5336 gst_message_new_async_done (GST_OBJECT_CAST (basesink),
5337 GST_CLOCK_TIME_NONE));
5339 priv->committed = TRUE;
5341 GST_DEBUG_OBJECT (basesink, "PAUSED to READY, don't need_preroll");
5343 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
5345 case GST_STATE_CHANGE_READY_TO_NULL:
5347 if (!bclass->stop (basesink)) {
5348 GST_WARNING_OBJECT (basesink, "failed to stop");
5351 gst_base_sink_set_last_buffer (basesink, NULL);
5352 gst_base_sink_set_last_buffer_list (basesink, NULL);
5353 priv->call_preroll = FALSE;
5364 GST_DEBUG_OBJECT (basesink, "failed to start");
5365 /* subclass is supposed to post a message but we post one as a fallback
5367 GST_ELEMENT_ERROR (basesink, CORE, STATE_CHANGE, (NULL),
5368 ("Failed to start"));
5369 return GST_STATE_CHANGE_FAILURE;
5373 GST_DEBUG_OBJECT (basesink,
5374 "element failed to change states -- activation problem?");
5375 return GST_STATE_CHANGE_FAILURE;