2 * Copyright (C) 2005-2007 Wim Taymans <wim.taymans@gmail.com>
4 * gstbasesink.c: Base class for sink elements
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
24 * @short_description: Base class for sink elements
25 * @see_also: #GstBaseTransform, #GstBaseSrc
27 * #GstBaseSink is the base class for sink elements in GStreamer, such as
28 * xvimagesink or filesink. It is a layer on top of #GstElement that provides a
29 * simplified interface to plugin writers. #GstBaseSink handles many details
30 * for you, for example: preroll, clock synchronization, state changes,
31 * activation in push or pull mode, and queries.
33 * In most cases, when writing sink elements, there is no need to implement
34 * class methods from #GstElement or to set functions on pads, because the
35 * #GstBaseSink infrastructure should be sufficient.
37 * #GstBaseSink provides support for exactly one sink pad, which should be
38 * named "sink". A sink implementation (subclass of #GstBaseSink) should
39 * install a pad template in its base_init function, like so:
42 * my_element_base_init (gpointer g_class)
44 * GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
46 * // sinktemplate should be a #GstStaticPadTemplate with direction
47 * // #GST_PAD_SINK and name "sink"
48 * gst_element_class_add_pad_template (gstelement_class,
49 * gst_static_pad_template_get (&sinktemplate));
50 * // see #GstElementDetails
51 * gst_element_class_set_details (gstelement_class, &details);
55 * #GstBaseSink will handle the prerolling correctly. This means that it will
56 * return #GST_STATE_CHANGE_ASYNC from a state change to PAUSED until the first
57 * buffer arrives in this element. The base class will call the
58 * #GstBaseSinkClass.preroll() vmethod with this preroll buffer and will then
59 * commit the state change to the next asynchronously pending state.
61 * When the element is set to PLAYING, #GstBaseSink will synchronise on the
62 * clock using the times returned from #GstBaseSinkClass.get_times(). If this
63 * function returns #GST_CLOCK_TIME_NONE for the start time, no synchronisation
64 * will be done. Synchronisation can be disabled entirely by setting the object
65 * #GstBaseSink:sync property to %FALSE.
67 * After synchronisation the virtual method #GstBaseSinkClass.render() will be
68 * called. Subclasses should minimally implement this method.
70 * Since 0.10.3 subclasses that synchronise on the clock in the
71 * #GstBaseSinkClass.render() method are supported as well. These classes
72 * typically receive a buffer in the render method and can then potentially
73 * block on the clock while rendering. A typical example is an audiosink.
74 * Since 0.10.11 these subclasses can use gst_base_sink_wait_preroll() to
75 * perform the blocking wait.
77 * Upon receiving the EOS event in the PLAYING state, #GstBaseSink will wait
78 * for the clock to reach the time indicated by the stop time of the last
79 * #GstBaseSinkClass.get_times() call before posting an EOS message. When the
80 * element receives EOS in PAUSED, preroll completes, the event is queued and an
81 * EOS message is posted when going to PLAYING.
83 * #GstBaseSink will internally use the #GST_EVENT_NEWSEGMENT events to schedule
84 * synchronisation and clipping of buffers. Buffers that fall completely outside
85 * of the current segment are dropped. Buffers that fall partially in the
86 * segment are rendered (and prerolled). Subclasses should do any subbuffer
87 * clipping themselves when needed.
89 * #GstBaseSink will by default report the current playback position in
90 * #GST_FORMAT_TIME based on the current clock time and segment information.
91 * If no clock has been set on the element, the query will be forwarded
94 * The #GstBaseSinkClass.set_caps() function will be called when the subclass
95 * should configure itself to process a specific media type.
97 * The #GstBaseSinkClass.start() and #GstBaseSinkClass.stop() virtual methods
98 * will be called when resources should be allocated. Any
99 * #GstBaseSinkClass.preroll(), #GstBaseSinkClass.render() and
100 * #GstBaseSinkClass.set_caps() function will be called between the
101 * #GstBaseSinkClass.start() and #GstBaseSinkClass.stop() calls.
103 * The #GstBaseSinkClass.event() virtual method will be called when an event is
104 * received by #GstBaseSink. Normally this method should only be overriden by
105 * very specific elements (such as file sinks) which need to handle the
106 * newsegment event specially.
108 * #GstBaseSink provides an overridable #GstBaseSinkClass.buffer_alloc()
109 * function that can be used by sinks that want to do reverse negotiation or to
110 * provide custom buffers (hardware buffers for example) to upstream elements.
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 * Since 0.10.15 the #GstBaseSink:async property can be used to instruct the
139 * sink to never perform an ASYNC state change. This feature is mostly usable
140 * when dealing with non-synchronized streams or sparse streams.
142 * Last reviewed on 2007-08-29 (0.10.15)
149 #include <gst/gst_private.h>
151 #include "gstbasesink.h"
152 #include <gst/gstmarshal.h>
153 #include <gst/gst-i18n-lib.h>
155 GST_DEBUG_CATEGORY_STATIC (gst_base_sink_debug);
156 #define GST_CAT_DEFAULT gst_base_sink_debug
158 #define GST_BASE_SINK_GET_PRIVATE(obj) \
159 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_BASE_SINK, GstBaseSinkPrivate))
161 #define GST_FLOW_STEP GST_FLOW_CUSTOM_ERROR
165 gboolean valid; /* if this info is valid */
166 guint32 seqnum; /* the seqnum of the STEP event */
167 GstFormat format; /* the format of the amount */
168 guint64 amount; /* the total amount of data to skip */
169 guint64 position; /* the position in the stepped data */
170 guint64 duration; /* the duration in time of the skipped data */
171 guint64 start; /* running_time of the start */
172 gdouble rate; /* rate of skipping */
173 gdouble start_rate; /* rate before skipping */
174 guint64 start_start; /* start position skipping */
175 guint64 start_stop; /* stop position skipping */
176 gboolean flush; /* if this was a flushing step */
177 gboolean intermediate; /* if this is an intermediate step */
178 gboolean need_preroll; /* if we need preroll after this step */
181 /* FIXME, some stuff in ABI.data and other in Private...
182 * Make up your mind please.
184 struct _GstBaseSinkPrivate
186 gint qos_enabled; /* ATOMIC */
187 gboolean async_enabled;
188 GstClockTimeDiff ts_offset;
189 GstClockTime render_delay;
191 /* start, stop of current buffer, stream time, used to report position */
192 GstClockTime current_sstart;
193 GstClockTime current_sstop;
195 /* start, stop and jitter of current buffer, running time */
196 GstClockTime current_rstart;
197 GstClockTime current_rstop;
198 GstClockTimeDiff current_jitter;
200 /* EOS sync time in running time */
201 GstClockTime eos_rtime;
203 /* last buffer that arrived in time, running time */
204 GstClockTime last_render_time;
205 /* when the last buffer left the sink, running time */
206 GstClockTime last_left;
208 /* running averages go here these are done on running time */
210 GstClockTime avg_duration;
213 /* these are done on system time. avg_jitter and avg_render are
214 * compared to eachother to see if the rendering time takes a
215 * huge amount of the processing, If so we are flooded with
217 GstClockTime last_left_systime;
218 GstClockTime avg_jitter;
219 GstClockTime start, stop;
220 GstClockTime avg_render;
222 /* number of rendered and dropped frames */
227 GstClockTime latency;
229 /* if we already commited the state */
232 /* when we received EOS */
233 gboolean received_eos;
235 /* when we are prerolled and able to report latency */
236 gboolean have_latency;
238 /* the last buffer we prerolled or rendered. Useful for making snapshots */
239 gint enable_last_buffer; /* atomic */
240 GstBuffer *last_buffer;
242 /* caps for pull based scheduling */
245 /* blocksize for pulling */
250 /* seqnum of the stream */
253 gboolean call_preroll;
254 gboolean step_unlock;
256 /* we have a pending and a current step operation */
257 GstStepInfo current_step;
258 GstStepInfo pending_step;
260 /* Cached GstClockID */
261 GstClockID cached_clock_id;
263 /* for throttling and QoS */
264 GstClockTime earliest_in_time;
265 GstClockTime throttle_time;
268 #define DO_RUNNING_AVG(avg,val,size) (((val) + ((size)-1) * (avg)) / (size))
270 /* generic running average, this has a neutral window size */
271 #define UPDATE_RUNNING_AVG(avg,val) DO_RUNNING_AVG(avg,val,8)
273 /* the windows for these running averages are experimentally obtained.
274 * possitive values get averaged more while negative values use a small
275 * window so we can react faster to badness. */
276 #define UPDATE_RUNNING_AVG_P(avg,val) DO_RUNNING_AVG(avg,val,16)
277 #define UPDATE_RUNNING_AVG_N(avg,val) DO_RUNNING_AVG(avg,val,4)
281 _PR_IS_NOTHING = 1 << 0,
282 _PR_IS_BUFFER = 1 << 1,
283 _PR_IS_BUFFERLIST = 1 << 2,
284 _PR_IS_EVENT = 1 << 3
287 #define OBJ_IS_BUFFER(a) ((a) & _PR_IS_BUFFER)
288 #define OBJ_IS_BUFFERLIST(a) ((a) & _PR_IS_BUFFERLIST)
289 #define OBJ_IS_EVENT(a) ((a) & _PR_IS_EVENT)
290 #define OBJ_IS_BUFFERFULL(a) ((a) & (_PR_IS_BUFFER | _PR_IS_BUFFERLIST))
292 /* BaseSink properties */
294 #define DEFAULT_CAN_ACTIVATE_PULL FALSE /* fixme: enable me */
295 #define DEFAULT_CAN_ACTIVATE_PUSH TRUE
297 #define DEFAULT_PREROLL_QUEUE_LEN 0
298 #define DEFAULT_SYNC TRUE
299 #define DEFAULT_MAX_LATENESS -1
300 #define DEFAULT_QOS FALSE
301 #define DEFAULT_ASYNC TRUE
302 #define DEFAULT_TS_OFFSET 0
303 #define DEFAULT_BLOCKSIZE 4096
304 #define DEFAULT_RENDER_DELAY 0
305 #define DEFAULT_ENABLE_LAST_BUFFER TRUE
306 #define DEFAULT_THROTTLE_TIME 0
311 PROP_PREROLL_QUEUE_LEN,
317 PROP_ENABLE_LAST_BUFFER,
325 static GstElementClass *parent_class = NULL;
327 static void gst_base_sink_class_init (GstBaseSinkClass * klass);
328 static void gst_base_sink_init (GstBaseSink * trans, gpointer g_class);
329 static void gst_base_sink_finalize (GObject * object);
332 gst_base_sink_get_type (void)
334 static volatile gsize base_sink_type = 0;
336 if (g_once_init_enter (&base_sink_type)) {
338 static const GTypeInfo base_sink_info = {
339 sizeof (GstBaseSinkClass),
342 (GClassInitFunc) gst_base_sink_class_init,
345 sizeof (GstBaseSink),
347 (GInstanceInitFunc) gst_base_sink_init,
350 _type = g_type_register_static (GST_TYPE_ELEMENT,
351 "GstBaseSink", &base_sink_info, G_TYPE_FLAG_ABSTRACT);
352 g_once_init_leave (&base_sink_type, _type);
354 return base_sink_type;
357 static void gst_base_sink_set_property (GObject * object, guint prop_id,
358 const GValue * value, GParamSpec * pspec);
359 static void gst_base_sink_get_property (GObject * object, guint prop_id,
360 GValue * value, GParamSpec * pspec);
362 static gboolean gst_base_sink_send_event (GstElement * element,
364 static gboolean gst_base_sink_query (GstElement * element, GstQuery * query);
365 static const GstQueryType *gst_base_sink_get_query_types (GstElement * element);
367 static GstCaps *gst_base_sink_get_caps (GstBaseSink * sink);
368 static gboolean gst_base_sink_set_caps (GstBaseSink * sink, GstCaps * caps);
369 static GstFlowReturn gst_base_sink_buffer_alloc (GstBaseSink * sink,
370 guint64 offset, guint size, GstCaps * caps, GstBuffer ** buf);
371 static void gst_base_sink_get_times (GstBaseSink * basesink, GstBuffer * buffer,
372 GstClockTime * start, GstClockTime * end);
373 static gboolean gst_base_sink_set_flushing (GstBaseSink * basesink,
374 GstPad * pad, gboolean flushing);
375 static gboolean gst_base_sink_default_activate_pull (GstBaseSink * basesink,
377 static gboolean gst_base_sink_default_do_seek (GstBaseSink * sink,
378 GstSegment * segment);
379 static gboolean gst_base_sink_default_prepare_seek_segment (GstBaseSink * sink,
380 GstEvent * event, GstSegment * segment);
382 static GstStateChangeReturn gst_base_sink_change_state (GstElement * element,
383 GstStateChange transition);
385 static GstFlowReturn gst_base_sink_chain (GstPad * pad, GstBuffer * buffer);
386 static GstFlowReturn gst_base_sink_chain_list (GstPad * pad,
387 GstBufferList * list);
389 static void gst_base_sink_loop (GstPad * pad);
390 static gboolean gst_base_sink_pad_activate (GstPad * pad);
391 static gboolean gst_base_sink_pad_activate_push (GstPad * pad, gboolean active);
392 static gboolean gst_base_sink_pad_activate_pull (GstPad * pad, gboolean active);
393 static gboolean gst_base_sink_event (GstPad * pad, GstEvent * event);
395 static gboolean gst_base_sink_negotiate_pull (GstBaseSink * basesink);
396 static GstCaps *gst_base_sink_pad_getcaps (GstPad * pad);
397 static gboolean gst_base_sink_pad_setcaps (GstPad * pad, GstCaps * caps);
398 static void gst_base_sink_pad_fixate (GstPad * pad, GstCaps * caps);
399 static GstFlowReturn gst_base_sink_pad_buffer_alloc (GstPad * pad,
400 guint64 offset, guint size, GstCaps * caps, GstBuffer ** buf);
403 /* check if an object was too late */
404 static gboolean gst_base_sink_is_too_late (GstBaseSink * basesink,
405 GstMiniObject * obj, GstClockTime rstart, GstClockTime rstop,
406 GstClockReturn status, GstClockTimeDiff jitter);
407 static GstFlowReturn gst_base_sink_preroll_object (GstBaseSink * basesink,
408 guint8 obj_type, GstMiniObject * obj);
411 gst_base_sink_class_init (GstBaseSinkClass * klass)
413 GObjectClass *gobject_class;
414 GstElementClass *gstelement_class;
416 gobject_class = G_OBJECT_CLASS (klass);
417 gstelement_class = GST_ELEMENT_CLASS (klass);
419 GST_DEBUG_CATEGORY_INIT (gst_base_sink_debug, "basesink", 0,
422 g_type_class_add_private (klass, sizeof (GstBaseSinkPrivate));
424 parent_class = g_type_class_peek_parent (klass);
426 gobject_class->finalize = gst_base_sink_finalize;
427 gobject_class->set_property = gst_base_sink_set_property;
428 gobject_class->get_property = gst_base_sink_get_property;
430 /* FIXME, this next value should be configured using an event from the
431 * upstream element, ie, the BUFFER_SIZE event. */
432 g_object_class_install_property (gobject_class, PROP_PREROLL_QUEUE_LEN,
433 g_param_spec_uint ("preroll-queue-len", "Preroll queue length",
434 "Number of buffers to queue during preroll", 0, G_MAXUINT,
435 DEFAULT_PREROLL_QUEUE_LEN,
436 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
438 g_object_class_install_property (gobject_class, PROP_SYNC,
439 g_param_spec_boolean ("sync", "Sync", "Sync on the clock", DEFAULT_SYNC,
440 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
442 g_object_class_install_property (gobject_class, PROP_MAX_LATENESS,
443 g_param_spec_int64 ("max-lateness", "Max Lateness",
444 "Maximum number of nanoseconds that a buffer can be late before it "
445 "is dropped (-1 unlimited)", -1, G_MAXINT64, DEFAULT_MAX_LATENESS,
446 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
448 g_object_class_install_property (gobject_class, PROP_QOS,
449 g_param_spec_boolean ("qos", "Qos",
450 "Generate Quality-of-Service events upstream", DEFAULT_QOS,
451 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
455 * If set to #TRUE, the basesink will perform asynchronous state changes.
456 * When set to #FALSE, the sink will not signal the parent when it prerolls.
457 * Use this option when dealing with sparse streams or when synchronisation is
462 g_object_class_install_property (gobject_class, PROP_ASYNC,
463 g_param_spec_boolean ("async", "Async",
464 "Go asynchronously to PAUSED", DEFAULT_ASYNC,
465 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
467 * GstBaseSink:ts-offset
469 * Controls the final synchronisation, a negative value will render the buffer
470 * earlier while a positive value delays playback. This property can be
471 * used to fix synchronisation in bad files.
475 g_object_class_install_property (gobject_class, PROP_TS_OFFSET,
476 g_param_spec_int64 ("ts-offset", "TS Offset",
477 "Timestamp offset in nanoseconds", G_MININT64, G_MAXINT64,
478 DEFAULT_TS_OFFSET, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
481 * GstBaseSink:enable-last-buffer
483 * Enable the last-buffer property. If FALSE, basesink doesn't keep a
484 * reference to the last buffer arrived and the last-buffer property is always
485 * set to NULL. This can be useful if you need buffers to be released as soon
486 * as possible, eg. if you're using a buffer pool.
490 g_object_class_install_property (gobject_class, PROP_ENABLE_LAST_BUFFER,
491 g_param_spec_boolean ("enable-last-buffer", "Enable Last Buffer",
492 "Enable the last-buffer property", DEFAULT_ENABLE_LAST_BUFFER,
493 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
496 * GstBaseSink:last-buffer
498 * The last buffer that arrived in the sink and was used for preroll or for
499 * rendering. This property can be used to generate thumbnails. This property
500 * can be NULL when the sink has not yet received a bufer.
504 g_object_class_install_property (gobject_class, PROP_LAST_BUFFER,
505 gst_param_spec_mini_object ("last-buffer", "Last Buffer",
506 "The last buffer received in the sink", GST_TYPE_BUFFER,
507 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
509 * GstBaseSink:blocksize
511 * The amount of bytes to pull when operating in pull mode.
515 /* FIXME 0.11: blocksize property should be int, otherwise min>max.. */
516 g_object_class_install_property (gobject_class, PROP_BLOCKSIZE,
517 g_param_spec_uint ("blocksize", "Block size",
518 "Size in bytes to pull per buffer (0 = default)", 0, G_MAXUINT,
519 DEFAULT_BLOCKSIZE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
521 * GstBaseSink:render-delay
523 * The additional delay between synchronisation and actual rendering of the
524 * media. This property will add additional latency to the device in order to
525 * make other sinks compensate for the delay.
529 g_object_class_install_property (gobject_class, PROP_RENDER_DELAY,
530 g_param_spec_uint64 ("render-delay", "Render Delay",
531 "Additional render delay of the sink in nanoseconds", 0, G_MAXUINT64,
532 DEFAULT_RENDER_DELAY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
534 * GstBaseSink:throttle-time
536 * The time to insert between buffers. This property can be used to control
537 * the maximum amount of buffers per second to render. Setting this property
538 * to a value bigger than 0 will make the sink create THROTTLE QoS events.
542 g_object_class_install_property (gobject_class, PROP_THROTTLE_TIME,
543 g_param_spec_uint64 ("throttle-time", "Throttle time",
544 "The time to keep between rendered buffers (unused)", 0, G_MAXUINT64,
545 DEFAULT_THROTTLE_TIME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
547 gstelement_class->change_state =
548 GST_DEBUG_FUNCPTR (gst_base_sink_change_state);
549 gstelement_class->send_event = GST_DEBUG_FUNCPTR (gst_base_sink_send_event);
550 gstelement_class->query = GST_DEBUG_FUNCPTR (gst_base_sink_query);
551 gstelement_class->get_query_types =
552 GST_DEBUG_FUNCPTR (gst_base_sink_get_query_types);
554 klass->get_caps = GST_DEBUG_FUNCPTR (gst_base_sink_get_caps);
555 klass->set_caps = GST_DEBUG_FUNCPTR (gst_base_sink_set_caps);
556 klass->buffer_alloc = GST_DEBUG_FUNCPTR (gst_base_sink_buffer_alloc);
557 klass->get_times = GST_DEBUG_FUNCPTR (gst_base_sink_get_times);
558 klass->activate_pull =
559 GST_DEBUG_FUNCPTR (gst_base_sink_default_activate_pull);
561 /* Registering debug symbols for function pointers */
562 GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_pad_getcaps);
563 GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_pad_setcaps);
564 GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_pad_fixate);
565 GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_pad_buffer_alloc);
566 GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_pad_activate);
567 GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_pad_activate_push);
568 GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_pad_activate_pull);
569 GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_event);
570 GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_chain);
571 GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_chain_list);
575 gst_base_sink_pad_getcaps (GstPad * pad)
577 GstBaseSinkClass *bclass;
579 GstCaps *caps = NULL;
581 bsink = GST_BASE_SINK (gst_pad_get_parent (pad));
582 bclass = GST_BASE_SINK_GET_CLASS (bsink);
584 if (bsink->pad_mode == GST_ACTIVATE_PULL) {
585 /* if we are operating in pull mode we only accept the negotiated caps */
586 GST_OBJECT_LOCK (pad);
587 if ((caps = GST_PAD_CAPS (pad)))
589 GST_OBJECT_UNLOCK (pad);
592 if (bclass->get_caps)
593 caps = bclass->get_caps (bsink);
596 GstPadTemplate *pad_template;
599 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (bclass),
601 if (pad_template != NULL) {
602 caps = gst_caps_ref (gst_pad_template_get_caps (pad_template));
606 gst_object_unref (bsink);
612 gst_base_sink_pad_setcaps (GstPad * pad, GstCaps * caps)
614 GstBaseSinkClass *bclass;
618 bsink = GST_BASE_SINK (gst_pad_get_parent (pad));
619 bclass = GST_BASE_SINK_GET_CLASS (bsink);
621 if (res && bclass->set_caps)
622 res = bclass->set_caps (bsink, caps);
624 gst_object_unref (bsink);
630 gst_base_sink_pad_fixate (GstPad * pad, GstCaps * caps)
632 GstBaseSinkClass *bclass;
635 bsink = GST_BASE_SINK (gst_pad_get_parent (pad));
636 bclass = GST_BASE_SINK_GET_CLASS (bsink);
639 bclass->fixate (bsink, caps);
641 gst_object_unref (bsink);
645 gst_base_sink_pad_buffer_alloc (GstPad * pad, guint64 offset, guint size,
646 GstCaps * caps, GstBuffer ** buf)
648 GstBaseSinkClass *bclass;
650 GstFlowReturn result = GST_FLOW_OK;
652 bsink = GST_BASE_SINK (gst_pad_get_parent (pad));
653 bclass = GST_BASE_SINK_GET_CLASS (bsink);
655 if (bclass->buffer_alloc)
656 result = bclass->buffer_alloc (bsink, offset, size, caps, buf);
658 *buf = NULL; /* fallback in gstpad.c will allocate generic buffer */
660 gst_object_unref (bsink);
666 gst_base_sink_init (GstBaseSink * basesink, gpointer g_class)
668 GstPadTemplate *pad_template;
669 GstBaseSinkPrivate *priv;
671 basesink->priv = priv = GST_BASE_SINK_GET_PRIVATE (basesink);
674 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (g_class), "sink");
675 g_return_if_fail (pad_template != NULL);
677 basesink->sinkpad = gst_pad_new_from_template (pad_template, "sink");
679 gst_pad_set_getcaps_function (basesink->sinkpad, gst_base_sink_pad_getcaps);
680 gst_pad_set_setcaps_function (basesink->sinkpad, gst_base_sink_pad_setcaps);
681 gst_pad_set_fixatecaps_function (basesink->sinkpad, gst_base_sink_pad_fixate);
682 gst_pad_set_bufferalloc_function (basesink->sinkpad,
683 gst_base_sink_pad_buffer_alloc);
684 gst_pad_set_activate_function (basesink->sinkpad, gst_base_sink_pad_activate);
685 gst_pad_set_activatepush_function (basesink->sinkpad,
686 gst_base_sink_pad_activate_push);
687 gst_pad_set_activatepull_function (basesink->sinkpad,
688 gst_base_sink_pad_activate_pull);
689 gst_pad_set_event_function (basesink->sinkpad, gst_base_sink_event);
690 gst_pad_set_chain_function (basesink->sinkpad, gst_base_sink_chain);
691 gst_pad_set_chain_list_function (basesink->sinkpad, gst_base_sink_chain_list);
692 gst_element_add_pad (GST_ELEMENT_CAST (basesink), basesink->sinkpad);
694 basesink->pad_mode = GST_ACTIVATE_NONE;
695 basesink->preroll_queue = g_queue_new ();
696 basesink->abidata.ABI.clip_segment = gst_segment_new ();
697 priv->have_latency = FALSE;
699 basesink->can_activate_push = DEFAULT_CAN_ACTIVATE_PUSH;
700 basesink->can_activate_pull = DEFAULT_CAN_ACTIVATE_PULL;
702 basesink->sync = DEFAULT_SYNC;
703 basesink->abidata.ABI.max_lateness = DEFAULT_MAX_LATENESS;
704 g_atomic_int_set (&priv->qos_enabled, DEFAULT_QOS);
705 priv->async_enabled = DEFAULT_ASYNC;
706 priv->ts_offset = DEFAULT_TS_OFFSET;
707 priv->render_delay = DEFAULT_RENDER_DELAY;
708 priv->blocksize = DEFAULT_BLOCKSIZE;
709 priv->cached_clock_id = NULL;
710 g_atomic_int_set (&priv->enable_last_buffer, DEFAULT_ENABLE_LAST_BUFFER);
711 priv->throttle_time = DEFAULT_THROTTLE_TIME;
713 GST_OBJECT_FLAG_SET (basesink, GST_ELEMENT_IS_SINK);
717 gst_base_sink_finalize (GObject * object)
719 GstBaseSink *basesink;
721 basesink = GST_BASE_SINK (object);
723 g_queue_free (basesink->preroll_queue);
724 gst_segment_free (basesink->abidata.ABI.clip_segment);
726 G_OBJECT_CLASS (parent_class)->finalize (object);
730 * gst_base_sink_set_sync:
732 * @sync: the new sync value.
734 * Configures @sink to synchronize on the clock or not. When
735 * @sync is FALSE, incomming samples will be played as fast as
736 * possible. If @sync is TRUE, the timestamps of the incomming
737 * buffers will be used to schedule the exact render time of its
743 gst_base_sink_set_sync (GstBaseSink * sink, gboolean sync)
745 g_return_if_fail (GST_IS_BASE_SINK (sink));
747 GST_OBJECT_LOCK (sink);
749 GST_OBJECT_UNLOCK (sink);
753 * gst_base_sink_get_sync:
756 * Checks if @sink is currently configured to synchronize against the
759 * Returns: TRUE if the sink is configured to synchronize against the clock.
764 gst_base_sink_get_sync (GstBaseSink * sink)
768 g_return_val_if_fail (GST_IS_BASE_SINK (sink), FALSE);
770 GST_OBJECT_LOCK (sink);
772 GST_OBJECT_UNLOCK (sink);
778 * gst_base_sink_set_max_lateness:
780 * @max_lateness: the new max lateness value.
782 * Sets the new max lateness value to @max_lateness. This value is
783 * used to decide if a buffer should be dropped or not based on the
784 * buffer timestamp and the current clock time. A value of -1 means
790 gst_base_sink_set_max_lateness (GstBaseSink * sink, gint64 max_lateness)
792 g_return_if_fail (GST_IS_BASE_SINK (sink));
794 GST_OBJECT_LOCK (sink);
795 sink->abidata.ABI.max_lateness = max_lateness;
796 GST_OBJECT_UNLOCK (sink);
800 * gst_base_sink_get_max_lateness:
803 * Gets the max lateness value. See gst_base_sink_set_max_lateness for
806 * Returns: The maximum time in nanoseconds that a buffer can be late
807 * before it is dropped and not rendered. A value of -1 means an
813 gst_base_sink_get_max_lateness (GstBaseSink * sink)
817 g_return_val_if_fail (GST_IS_BASE_SINK (sink), -1);
819 GST_OBJECT_LOCK (sink);
820 res = sink->abidata.ABI.max_lateness;
821 GST_OBJECT_UNLOCK (sink);
827 * gst_base_sink_set_qos_enabled:
829 * @enabled: the new qos value.
831 * Configures @sink to send Quality-of-Service events upstream.
836 gst_base_sink_set_qos_enabled (GstBaseSink * sink, gboolean enabled)
838 g_return_if_fail (GST_IS_BASE_SINK (sink));
840 g_atomic_int_set (&sink->priv->qos_enabled, enabled);
844 * gst_base_sink_is_qos_enabled:
847 * Checks if @sink is currently configured to send Quality-of-Service events
850 * Returns: TRUE if the sink is configured to perform Quality-of-Service.
855 gst_base_sink_is_qos_enabled (GstBaseSink * sink)
859 g_return_val_if_fail (GST_IS_BASE_SINK (sink), FALSE);
861 res = g_atomic_int_get (&sink->priv->qos_enabled);
867 * gst_base_sink_set_async_enabled:
869 * @enabled: the new async value.
871 * Configures @sink to perform all state changes asynchronusly. When async is
872 * disabled, the sink will immediatly go to PAUSED instead of waiting for a
873 * preroll buffer. This feature is usefull if the sink does not synchronize
874 * against the clock or when it is dealing with sparse streams.
879 gst_base_sink_set_async_enabled (GstBaseSink * sink, gboolean enabled)
881 g_return_if_fail (GST_IS_BASE_SINK (sink));
883 GST_PAD_PREROLL_LOCK (sink->sinkpad);
884 g_atomic_int_set (&sink->priv->async_enabled, enabled);
885 GST_LOG_OBJECT (sink, "set async enabled to %d", enabled);
886 GST_PAD_PREROLL_UNLOCK (sink->sinkpad);
890 * gst_base_sink_is_async_enabled:
893 * Checks if @sink is currently configured to perform asynchronous state
896 * Returns: TRUE if the sink is configured to perform asynchronous state
902 gst_base_sink_is_async_enabled (GstBaseSink * sink)
906 g_return_val_if_fail (GST_IS_BASE_SINK (sink), FALSE);
908 res = g_atomic_int_get (&sink->priv->async_enabled);
914 * gst_base_sink_set_ts_offset:
916 * @offset: the new offset
918 * Adjust the synchronisation of @sink with @offset. A negative value will
919 * render buffers earlier than their timestamp. A positive value will delay
920 * rendering. This function can be used to fix playback of badly timestamped
926 gst_base_sink_set_ts_offset (GstBaseSink * sink, GstClockTimeDiff offset)
928 g_return_if_fail (GST_IS_BASE_SINK (sink));
930 GST_OBJECT_LOCK (sink);
931 sink->priv->ts_offset = offset;
932 GST_LOG_OBJECT (sink, "set time offset to %" G_GINT64_FORMAT, offset);
933 GST_OBJECT_UNLOCK (sink);
937 * gst_base_sink_get_ts_offset:
940 * Get the synchronisation offset of @sink.
942 * Returns: The synchronisation offset.
947 gst_base_sink_get_ts_offset (GstBaseSink * sink)
949 GstClockTimeDiff res;
951 g_return_val_if_fail (GST_IS_BASE_SINK (sink), 0);
953 GST_OBJECT_LOCK (sink);
954 res = sink->priv->ts_offset;
955 GST_OBJECT_UNLOCK (sink);
961 * gst_base_sink_get_last_buffer:
964 * Get the last buffer that arrived in the sink and was used for preroll or for
965 * rendering. This property can be used to generate thumbnails.
967 * The #GstCaps on the buffer can be used to determine the type of the buffer.
969 * Free-function: gst_buffer_unref
971 * Returns: (transfer full): a #GstBuffer. gst_buffer_unref() after usage.
972 * This function returns NULL when no buffer has arrived in the sink yet
973 * or when the sink is not in PAUSED or PLAYING.
978 gst_base_sink_get_last_buffer (GstBaseSink * sink)
982 g_return_val_if_fail (GST_IS_BASE_SINK (sink), NULL);
984 GST_OBJECT_LOCK (sink);
985 if ((res = sink->priv->last_buffer))
986 gst_buffer_ref (res);
987 GST_OBJECT_UNLOCK (sink);
992 /* with OBJECT_LOCK */
994 gst_base_sink_set_last_buffer_unlocked (GstBaseSink * sink, GstBuffer * buffer)
998 old = sink->priv->last_buffer;
999 if (G_LIKELY (old != buffer)) {
1000 GST_DEBUG_OBJECT (sink, "setting last buffer to %p", buffer);
1001 if (G_LIKELY (buffer))
1002 gst_buffer_ref (buffer);
1003 sink->priv->last_buffer = buffer;
1007 /* avoid unreffing with the lock because cleanup code might want to take the
1009 if (G_LIKELY (old)) {
1010 GST_OBJECT_UNLOCK (sink);
1011 gst_buffer_unref (old);
1012 GST_OBJECT_LOCK (sink);
1017 gst_base_sink_set_last_buffer (GstBaseSink * sink, GstBuffer * buffer)
1019 if (!g_atomic_int_get (&sink->priv->enable_last_buffer))
1022 GST_OBJECT_LOCK (sink);
1023 gst_base_sink_set_last_buffer_unlocked (sink, buffer);
1024 GST_OBJECT_UNLOCK (sink);
1028 * gst_base_sink_set_last_buffer_enabled:
1030 * @enabled: the new enable-last-buffer value.
1032 * Configures @sink to store the last received buffer in the last-buffer
1038 gst_base_sink_set_last_buffer_enabled (GstBaseSink * sink, gboolean enabled)
1040 g_return_if_fail (GST_IS_BASE_SINK (sink));
1042 /* Only take lock if we change the value */
1043 if (g_atomic_int_compare_and_exchange (&sink->priv->enable_last_buffer,
1044 !enabled, enabled) && !enabled) {
1045 GST_OBJECT_LOCK (sink);
1046 gst_base_sink_set_last_buffer_unlocked (sink, NULL);
1047 GST_OBJECT_UNLOCK (sink);
1052 * gst_base_sink_is_last_buffer_enabled:
1055 * Checks if @sink is currently configured to store the last received buffer in
1056 * the last-buffer property.
1058 * Returns: TRUE if the sink is configured to store the last received buffer.
1063 gst_base_sink_is_last_buffer_enabled (GstBaseSink * sink)
1065 g_return_val_if_fail (GST_IS_BASE_SINK (sink), FALSE);
1067 return g_atomic_int_get (&sink->priv->enable_last_buffer);
1071 * gst_base_sink_get_latency:
1074 * Get the currently configured latency.
1076 * Returns: The configured latency.
1081 gst_base_sink_get_latency (GstBaseSink * sink)
1085 GST_OBJECT_LOCK (sink);
1086 res = sink->priv->latency;
1087 GST_OBJECT_UNLOCK (sink);
1093 * gst_base_sink_query_latency:
1095 * @live: (out) (allow-none): if the sink is live
1096 * @upstream_live: (out) (allow-none): if an upstream element is live
1097 * @min_latency: (out) (allow-none): the min latency of the upstream elements
1098 * @max_latency: (out) (allow-none): the max latency of the upstream elements
1100 * Query the sink for the latency parameters. The latency will be queried from
1101 * the upstream elements. @live will be TRUE if @sink is configured to
1102 * synchronize against the clock. @upstream_live will be TRUE if an upstream
1105 * If both @live and @upstream_live are TRUE, the sink will want to compensate
1106 * for the latency introduced by the upstream elements by setting the
1107 * @min_latency to a strictly possitive value.
1109 * This function is mostly used by subclasses.
1111 * Returns: TRUE if the query succeeded.
1116 gst_base_sink_query_latency (GstBaseSink * sink, gboolean * live,
1117 gboolean * upstream_live, GstClockTime * min_latency,
1118 GstClockTime * max_latency)
1120 gboolean l, us_live, res, have_latency;
1121 GstClockTime min, max, render_delay;
1123 GstClockTime us_min, us_max;
1125 /* we are live when we sync to the clock */
1126 GST_OBJECT_LOCK (sink);
1128 have_latency = sink->priv->have_latency;
1129 render_delay = sink->priv->render_delay;
1130 GST_OBJECT_UNLOCK (sink);
1132 /* assume no latency */
1138 GST_DEBUG_OBJECT (sink, "we are ready for LATENCY query");
1139 /* we are ready for a latency query this is when we preroll or when we are
1141 query = gst_query_new_latency ();
1143 /* ask the peer for the latency */
1144 if ((res = gst_pad_peer_query (sink->sinkpad, query))) {
1145 /* get upstream min and max latency */
1146 gst_query_parse_latency (query, &us_live, &us_min, &us_max);
1149 /* upstream live, use its latency, subclasses should use these
1150 * values to create the complete latency. */
1155 /* we need to add the render delay if we are live */
1157 min += render_delay;
1159 max += render_delay;
1162 gst_query_unref (query);
1164 GST_DEBUG_OBJECT (sink, "we are not yet ready for LATENCY query");
1168 /* not live, we tried to do the query, if it failed we return TRUE anyway */
1172 GST_DEBUG_OBJECT (sink, "latency query failed but we are not live");
1174 GST_DEBUG_OBJECT (sink, "latency query failed and we are live");
1179 GST_DEBUG_OBJECT (sink, "latency query: live: %d, have_latency %d,"
1180 " upstream: %d, min %" GST_TIME_FORMAT ", max %" GST_TIME_FORMAT, l,
1181 have_latency, us_live, GST_TIME_ARGS (min), GST_TIME_ARGS (max));
1186 *upstream_live = us_live;
1196 * gst_base_sink_set_render_delay:
1197 * @sink: a #GstBaseSink
1198 * @delay: the new delay
1200 * Set the render delay in @sink to @delay. The render delay is the time
1201 * between actual rendering of a buffer and its synchronisation time. Some
1202 * devices might delay media rendering which can be compensated for with this
1205 * After calling this function, this sink will report additional latency and
1206 * other sinks will adjust their latency to delay the rendering of their media.
1208 * This function is usually called by subclasses.
1213 gst_base_sink_set_render_delay (GstBaseSink * sink, GstClockTime delay)
1215 GstClockTime old_render_delay;
1217 g_return_if_fail (GST_IS_BASE_SINK (sink));
1219 GST_OBJECT_LOCK (sink);
1220 old_render_delay = sink->priv->render_delay;
1221 sink->priv->render_delay = delay;
1222 GST_LOG_OBJECT (sink, "set render delay to %" GST_TIME_FORMAT,
1223 GST_TIME_ARGS (delay));
1224 GST_OBJECT_UNLOCK (sink);
1226 if (delay != old_render_delay) {
1227 GST_DEBUG_OBJECT (sink, "posting latency changed");
1228 gst_element_post_message (GST_ELEMENT_CAST (sink),
1229 gst_message_new_latency (GST_OBJECT_CAST (sink)));
1234 * gst_base_sink_get_render_delay:
1235 * @sink: a #GstBaseSink
1237 * Get the render delay of @sink. see gst_base_sink_set_render_delay() for more
1238 * information about the render delay.
1240 * Returns: the render delay of @sink.
1245 gst_base_sink_get_render_delay (GstBaseSink * sink)
1247 GstClockTimeDiff res;
1249 g_return_val_if_fail (GST_IS_BASE_SINK (sink), 0);
1251 GST_OBJECT_LOCK (sink);
1252 res = sink->priv->render_delay;
1253 GST_OBJECT_UNLOCK (sink);
1259 * gst_base_sink_set_blocksize:
1260 * @sink: a #GstBaseSink
1261 * @blocksize: the blocksize in bytes
1263 * Set the number of bytes that the sink will pull when it is operating in pull
1268 /* FIXME 0.11: blocksize property should be int, otherwise min>max.. */
1270 gst_base_sink_set_blocksize (GstBaseSink * sink, guint blocksize)
1272 g_return_if_fail (GST_IS_BASE_SINK (sink));
1274 GST_OBJECT_LOCK (sink);
1275 sink->priv->blocksize = blocksize;
1276 GST_LOG_OBJECT (sink, "set blocksize to %u", blocksize);
1277 GST_OBJECT_UNLOCK (sink);
1281 * gst_base_sink_get_blocksize:
1282 * @sink: a #GstBaseSink
1284 * Get the number of bytes that the sink will pull when it is operating in pull
1287 * Returns: the number of bytes @sink will pull in pull mode.
1291 /* FIXME 0.11: blocksize property should be int, otherwise min>max.. */
1293 gst_base_sink_get_blocksize (GstBaseSink * sink)
1297 g_return_val_if_fail (GST_IS_BASE_SINK (sink), 0);
1299 GST_OBJECT_LOCK (sink);
1300 res = sink->priv->blocksize;
1301 GST_OBJECT_UNLOCK (sink);
1307 * gst_base_sink_set_throttle_time:
1308 * @sink: a #GstBaseSink
1309 * @throttle: the throttle time in nanoseconds
1311 * Set the time that will be inserted between rendered buffers. This
1312 * can be used to control the maximum buffers per second that the sink
1318 gst_base_sink_set_throttle_time (GstBaseSink * sink, guint64 throttle)
1320 g_return_if_fail (GST_IS_BASE_SINK (sink));
1322 GST_OBJECT_LOCK (sink);
1323 sink->priv->throttle_time = throttle;
1324 GST_LOG_OBJECT (sink, "set throttle_time to %" G_GUINT64_FORMAT, throttle);
1325 GST_OBJECT_UNLOCK (sink);
1329 * gst_base_sink_get_throttle_time:
1330 * @sink: a #GstBaseSink
1332 * Get the time that will be inserted between frames to control the
1333 * maximum buffers per second.
1335 * Returns: the number of nanoseconds @sink will put between frames.
1340 gst_base_sink_get_throttle_time (GstBaseSink * sink)
1344 g_return_val_if_fail (GST_IS_BASE_SINK (sink), 0);
1346 GST_OBJECT_LOCK (sink);
1347 res = sink->priv->throttle_time;
1348 GST_OBJECT_UNLOCK (sink);
1354 gst_base_sink_set_property (GObject * object, guint prop_id,
1355 const GValue * value, GParamSpec * pspec)
1357 GstBaseSink *sink = GST_BASE_SINK (object);
1360 case PROP_PREROLL_QUEUE_LEN:
1361 /* preroll lock necessary to serialize with finish_preroll */
1362 GST_PAD_PREROLL_LOCK (sink->sinkpad);
1363 g_atomic_int_set (&sink->preroll_queue_max_len, g_value_get_uint (value));
1364 GST_PAD_PREROLL_UNLOCK (sink->sinkpad);
1367 gst_base_sink_set_sync (sink, g_value_get_boolean (value));
1369 case PROP_MAX_LATENESS:
1370 gst_base_sink_set_max_lateness (sink, g_value_get_int64 (value));
1373 gst_base_sink_set_qos_enabled (sink, g_value_get_boolean (value));
1376 gst_base_sink_set_async_enabled (sink, g_value_get_boolean (value));
1378 case PROP_TS_OFFSET:
1379 gst_base_sink_set_ts_offset (sink, g_value_get_int64 (value));
1381 case PROP_BLOCKSIZE:
1382 gst_base_sink_set_blocksize (sink, g_value_get_uint (value));
1384 case PROP_RENDER_DELAY:
1385 gst_base_sink_set_render_delay (sink, g_value_get_uint64 (value));
1387 case PROP_ENABLE_LAST_BUFFER:
1388 gst_base_sink_set_last_buffer_enabled (sink, g_value_get_boolean (value));
1390 case PROP_THROTTLE_TIME:
1391 gst_base_sink_set_throttle_time (sink, g_value_get_uint64 (value));
1394 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1400 gst_base_sink_get_property (GObject * object, guint prop_id, GValue * value,
1403 GstBaseSink *sink = GST_BASE_SINK (object);
1406 case PROP_PREROLL_QUEUE_LEN:
1407 g_value_set_uint (value, g_atomic_int_get (&sink->preroll_queue_max_len));
1410 g_value_set_boolean (value, gst_base_sink_get_sync (sink));
1412 case PROP_MAX_LATENESS:
1413 g_value_set_int64 (value, gst_base_sink_get_max_lateness (sink));
1416 g_value_set_boolean (value, gst_base_sink_is_qos_enabled (sink));
1419 g_value_set_boolean (value, gst_base_sink_is_async_enabled (sink));
1421 case PROP_TS_OFFSET:
1422 g_value_set_int64 (value, gst_base_sink_get_ts_offset (sink));
1424 case PROP_LAST_BUFFER:
1425 gst_value_take_buffer (value, gst_base_sink_get_last_buffer (sink));
1427 case PROP_ENABLE_LAST_BUFFER:
1428 g_value_set_boolean (value, gst_base_sink_is_last_buffer_enabled (sink));
1430 case PROP_BLOCKSIZE:
1431 g_value_set_uint (value, gst_base_sink_get_blocksize (sink));
1433 case PROP_RENDER_DELAY:
1434 g_value_set_uint64 (value, gst_base_sink_get_render_delay (sink));
1436 case PROP_THROTTLE_TIME:
1437 g_value_set_uint64 (value, gst_base_sink_get_throttle_time (sink));
1440 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1447 gst_base_sink_get_caps (GstBaseSink * sink)
1453 gst_base_sink_set_caps (GstBaseSink * sink, GstCaps * caps)
1458 static GstFlowReturn
1459 gst_base_sink_buffer_alloc (GstBaseSink * sink, guint64 offset, guint size,
1460 GstCaps * caps, GstBuffer ** buf)
1466 /* with PREROLL_LOCK, STREAM_LOCK */
1468 gst_base_sink_preroll_queue_flush (GstBaseSink * basesink, GstPad * pad)
1472 GST_DEBUG_OBJECT (basesink, "flushing queue %p", basesink);
1473 while ((obj = g_queue_pop_head (basesink->preroll_queue))) {
1474 GST_DEBUG_OBJECT (basesink, "popped %p", obj);
1475 gst_mini_object_unref (obj);
1477 /* we can't have EOS anymore now */
1478 basesink->eos = FALSE;
1479 basesink->priv->received_eos = FALSE;
1480 basesink->have_preroll = FALSE;
1481 basesink->priv->step_unlock = FALSE;
1482 basesink->eos_queued = FALSE;
1483 basesink->preroll_queued = 0;
1484 basesink->buffers_queued = 0;
1485 basesink->events_queued = 0;
1486 /* can't report latency anymore until we preroll again */
1487 if (basesink->priv->async_enabled) {
1488 GST_OBJECT_LOCK (basesink);
1489 basesink->priv->have_latency = FALSE;
1490 GST_OBJECT_UNLOCK (basesink);
1492 /* and signal any waiters now */
1493 GST_PAD_PREROLL_SIGNAL (pad);
1496 /* with STREAM_LOCK, configures given segment with the event information. */
1498 gst_base_sink_configure_segment (GstBaseSink * basesink, GstPad * pad,
1499 GstEvent * event, GstSegment * segment)
1502 gdouble rate, arate;
1508 /* the newsegment event is needed to bring the buffer timestamps to the
1509 * stream time and to drop samples outside of the playback segment. */
1510 gst_event_parse_new_segment_full (event, &update, &rate, &arate, &format,
1511 &start, &stop, &time);
1513 /* The segment is protected with both the STREAM_LOCK and the OBJECT_LOCK.
1514 * We protect with the OBJECT_LOCK so that we can use the values to
1515 * safely answer a POSITION query. */
1516 GST_OBJECT_LOCK (basesink);
1517 gst_segment_set_newsegment_full (segment, update, rate, arate, format, start,
1520 if (format == GST_FORMAT_TIME) {
1521 GST_DEBUG_OBJECT (basesink,
1522 "configured NEWSEGMENT update %d, rate %lf, applied rate %lf, "
1523 "format GST_FORMAT_TIME, "
1524 "%" GST_TIME_FORMAT " -- %" GST_TIME_FORMAT
1525 ", time %" GST_TIME_FORMAT ", accum %" GST_TIME_FORMAT,
1526 update, rate, arate, GST_TIME_ARGS (segment->start),
1527 GST_TIME_ARGS (segment->stop), GST_TIME_ARGS (segment->time),
1528 GST_TIME_ARGS (segment->accum));
1530 GST_DEBUG_OBJECT (basesink,
1531 "configured NEWSEGMENT update %d, rate %lf, applied rate %lf, "
1533 "%" G_GINT64_FORMAT " -- %" G_GINT64_FORMAT ", time %"
1534 G_GINT64_FORMAT ", accum %" G_GINT64_FORMAT, update, rate, arate,
1535 segment->format, segment->start, segment->stop, segment->time,
1538 GST_OBJECT_UNLOCK (basesink);
1541 /* with PREROLL_LOCK, STREAM_LOCK */
1543 gst_base_sink_commit_state (GstBaseSink * basesink)
1545 /* commit state and proceed to next pending state */
1546 GstState current, next, pending, post_pending;
1547 gboolean post_paused = FALSE;
1548 gboolean post_async_done = FALSE;
1549 gboolean post_playing = FALSE;
1551 /* we are certainly not playing async anymore now */
1552 basesink->playing_async = FALSE;
1554 GST_OBJECT_LOCK (basesink);
1555 current = GST_STATE (basesink);
1556 next = GST_STATE_NEXT (basesink);
1557 pending = GST_STATE_PENDING (basesink);
1558 post_pending = pending;
1561 case GST_STATE_PLAYING:
1563 GstBaseSinkClass *bclass;
1564 GstStateChangeReturn ret;
1566 bclass = GST_BASE_SINK_GET_CLASS (basesink);
1568 GST_DEBUG_OBJECT (basesink, "commiting state to PLAYING");
1570 basesink->need_preroll = FALSE;
1571 post_async_done = TRUE;
1572 basesink->priv->commited = TRUE;
1573 post_playing = TRUE;
1574 /* post PAUSED too when we were READY */
1575 if (current == GST_STATE_READY) {
1579 /* make sure we notify the subclass of async playing */
1580 if (bclass->async_play) {
1581 GST_WARNING_OBJECT (basesink, "deprecated async_play");
1582 ret = bclass->async_play (basesink);
1583 if (ret == GST_STATE_CHANGE_FAILURE)
1588 case GST_STATE_PAUSED:
1589 GST_DEBUG_OBJECT (basesink, "commiting state to PAUSED");
1591 post_async_done = TRUE;
1592 basesink->priv->commited = TRUE;
1593 post_pending = GST_STATE_VOID_PENDING;
1595 case GST_STATE_READY:
1596 case GST_STATE_NULL:
1598 case GST_STATE_VOID_PENDING:
1599 goto nothing_pending;
1604 /* we can report latency queries now */
1605 basesink->priv->have_latency = TRUE;
1607 GST_STATE (basesink) = pending;
1608 GST_STATE_NEXT (basesink) = GST_STATE_VOID_PENDING;
1609 GST_STATE_PENDING (basesink) = GST_STATE_VOID_PENDING;
1610 GST_STATE_RETURN (basesink) = GST_STATE_CHANGE_SUCCESS;
1611 GST_OBJECT_UNLOCK (basesink);
1614 GST_DEBUG_OBJECT (basesink, "posting PAUSED state change message");
1615 gst_element_post_message (GST_ELEMENT_CAST (basesink),
1616 gst_message_new_state_changed (GST_OBJECT_CAST (basesink),
1617 current, next, post_pending));
1619 if (post_async_done) {
1620 GST_DEBUG_OBJECT (basesink, "posting async-done message");
1621 gst_element_post_message (GST_ELEMENT_CAST (basesink),
1622 gst_message_new_async_done (GST_OBJECT_CAST (basesink)));
1625 GST_DEBUG_OBJECT (basesink, "posting PLAYING state change message");
1626 gst_element_post_message (GST_ELEMENT_CAST (basesink),
1627 gst_message_new_state_changed (GST_OBJECT_CAST (basesink),
1628 next, pending, GST_STATE_VOID_PENDING));
1631 GST_STATE_BROADCAST (basesink);
1637 /* Depending on the state, set our vars. We get in this situation when the
1638 * state change function got a change to update the state vars before the
1639 * streaming thread did. This is fine but we need to make sure that we
1640 * update the need_preroll var since it was TRUE when we got here and might
1641 * become FALSE if we got to PLAYING. */
1642 GST_DEBUG_OBJECT (basesink, "nothing to commit, now in %s",
1643 gst_element_state_get_name (current));
1645 case GST_STATE_PLAYING:
1646 basesink->need_preroll = FALSE;
1648 case GST_STATE_PAUSED:
1649 basesink->need_preroll = TRUE;
1652 basesink->need_preroll = FALSE;
1653 basesink->flushing = TRUE;
1656 /* we can report latency queries now */
1657 basesink->priv->have_latency = TRUE;
1658 GST_OBJECT_UNLOCK (basesink);
1663 /* app is going to READY */
1664 GST_DEBUG_OBJECT (basesink, "stopping");
1665 basesink->need_preroll = FALSE;
1666 basesink->flushing = TRUE;
1667 GST_OBJECT_UNLOCK (basesink);
1672 GST_DEBUG_OBJECT (basesink, "async commit failed");
1673 GST_STATE_RETURN (basesink) = GST_STATE_CHANGE_FAILURE;
1674 GST_OBJECT_UNLOCK (basesink);
1680 start_stepping (GstBaseSink * sink, GstSegment * segment,
1681 GstStepInfo * pending, GstStepInfo * current)
1684 GstMessage *message;
1686 GST_DEBUG_OBJECT (sink, "update pending step");
1688 GST_OBJECT_LOCK (sink);
1689 memcpy (current, pending, sizeof (GstStepInfo));
1690 pending->valid = FALSE;
1691 GST_OBJECT_UNLOCK (sink);
1693 /* post message first */
1695 gst_message_new_step_start (GST_OBJECT (sink), TRUE, current->format,
1696 current->amount, current->rate, current->flush, current->intermediate);
1697 gst_message_set_seqnum (message, current->seqnum);
1698 gst_element_post_message (GST_ELEMENT (sink), message);
1700 /* get the running time of where we paused and remember it */
1701 current->start = gst_element_get_start_time (GST_ELEMENT_CAST (sink));
1702 gst_segment_set_running_time (segment, GST_FORMAT_TIME, current->start);
1704 /* set the new rate for the remainder of the segment */
1705 current->start_rate = segment->rate;
1706 segment->rate *= current->rate;
1707 segment->abs_rate = ABS (segment->rate);
1710 if (segment->rate > 0.0)
1711 current->start_stop = segment->stop;
1713 current->start_start = segment->start;
1715 if (current->format == GST_FORMAT_TIME) {
1716 end = current->start + current->amount;
1717 if (!current->flush) {
1718 /* update the segment clipping regions for non-flushing seeks */
1719 if (segment->rate > 0.0) {
1720 segment->stop = gst_segment_to_position (segment, GST_FORMAT_TIME, end);
1721 segment->last_stop = segment->stop;
1725 position = gst_segment_to_position (segment, GST_FORMAT_TIME, end);
1726 segment->time = position;
1727 segment->start = position;
1728 segment->last_stop = position;
1733 GST_DEBUG_OBJECT (sink,
1734 "segment now rate %lf, applied rate %lf, "
1735 "format GST_FORMAT_TIME, "
1736 "%" GST_TIME_FORMAT " -- %" GST_TIME_FORMAT
1737 ", time %" GST_TIME_FORMAT ", accum %" GST_TIME_FORMAT,
1738 segment->rate, segment->applied_rate, GST_TIME_ARGS (segment->start),
1739 GST_TIME_ARGS (segment->stop), GST_TIME_ARGS (segment->time),
1740 GST_TIME_ARGS (segment->accum));
1742 GST_DEBUG_OBJECT (sink, "step started at running_time %" GST_TIME_FORMAT,
1743 GST_TIME_ARGS (current->start));
1745 if (current->amount == -1) {
1746 GST_DEBUG_OBJECT (sink, "step amount == -1, stop stepping");
1747 current->valid = FALSE;
1749 GST_DEBUG_OBJECT (sink, "step amount: %" G_GUINT64_FORMAT ", format: %s, "
1750 "rate: %f", current->amount, gst_format_get_name (current->format),
1756 stop_stepping (GstBaseSink * sink, GstSegment * segment,
1757 GstStepInfo * current, gint64 rstart, gint64 rstop, gboolean eos)
1759 gint64 stop, position;
1760 GstMessage *message;
1762 GST_DEBUG_OBJECT (sink, "step complete");
1764 if (segment->rate > 0.0)
1769 GST_DEBUG_OBJECT (sink,
1770 "step stop at running_time %" GST_TIME_FORMAT, GST_TIME_ARGS (stop));
1773 current->duration = current->position;
1775 current->duration = stop - current->start;
1777 GST_DEBUG_OBJECT (sink, "step elapsed running_time %" GST_TIME_FORMAT,
1778 GST_TIME_ARGS (current->duration));
1780 position = current->start + current->duration;
1782 /* now move the segment to the new running time */
1783 gst_segment_set_running_time (segment, GST_FORMAT_TIME, position);
1785 if (current->flush) {
1786 /* and remove the accumulated time we flushed, start time did not change */
1787 segment->accum = current->start;
1789 /* start time is now the stepped position */
1790 gst_element_set_start_time (GST_ELEMENT_CAST (sink), position);
1793 /* restore the previous rate */
1794 segment->rate = current->start_rate;
1795 segment->abs_rate = ABS (segment->rate);
1797 if (segment->rate > 0.0)
1798 segment->stop = current->start_stop;
1800 segment->start = current->start_start;
1802 /* the clip segment is used for position report in paused... */
1803 memcpy (sink->abidata.ABI.clip_segment, segment, sizeof (GstSegment));
1805 /* post the step done when we know the stepped duration in TIME */
1807 gst_message_new_step_done (GST_OBJECT_CAST (sink), current->format,
1808 current->amount, current->rate, current->flush, current->intermediate,
1809 current->duration, eos);
1810 gst_message_set_seqnum (message, current->seqnum);
1811 gst_element_post_message (GST_ELEMENT_CAST (sink), message);
1813 if (!current->intermediate)
1814 sink->need_preroll = current->need_preroll;
1816 /* and the current step info finished and becomes invalid */
1817 current->valid = FALSE;
1821 handle_stepping (GstBaseSink * sink, GstSegment * segment,
1822 GstStepInfo * current, gint64 * cstart, gint64 * cstop, gint64 * rstart,
1825 gboolean step_end = FALSE;
1827 /* see if we need to skip this buffer because of stepping */
1828 switch (current->format) {
1829 case GST_FORMAT_TIME:
1834 if (segment->rate > 0.0) {
1835 if (segment->stop == *cstop)
1836 *rstop = *rstart + current->amount;
1841 if (segment->start == *cstart)
1842 *rstart = *rstop + current->amount;
1848 end = current->start + current->amount;
1849 current->position = first - current->start;
1851 if (G_UNLIKELY (segment->abs_rate != 1.0))
1852 current->position /= segment->abs_rate;
1854 GST_DEBUG_OBJECT (sink,
1855 "buffer: %" GST_TIME_FORMAT "-%" GST_TIME_FORMAT,
1856 GST_TIME_ARGS (first), GST_TIME_ARGS (last));
1857 GST_DEBUG_OBJECT (sink,
1858 "got time step %" GST_TIME_FORMAT "-%" GST_TIME_FORMAT "/%"
1859 GST_TIME_FORMAT, GST_TIME_ARGS (current->position),
1860 GST_TIME_ARGS (last - current->start),
1861 GST_TIME_ARGS (current->amount));
1863 if ((current->flush && current->position >= current->amount)
1865 GST_DEBUG_OBJECT (sink, "step ended, we need clipping");
1867 if (segment->rate > 0.0) {
1869 *cstart = gst_segment_to_position (segment, GST_FORMAT_TIME, end);
1872 *cstop = gst_segment_to_position (segment, GST_FORMAT_TIME, end);
1875 GST_DEBUG_OBJECT (sink,
1876 "cstart %" GST_TIME_FORMAT ", rstart %" GST_TIME_FORMAT,
1877 GST_TIME_ARGS (*cstart), GST_TIME_ARGS (*rstart));
1878 GST_DEBUG_OBJECT (sink,
1879 "cstop %" GST_TIME_FORMAT ", rstop %" GST_TIME_FORMAT,
1880 GST_TIME_ARGS (*cstop), GST_TIME_ARGS (*rstop));
1883 case GST_FORMAT_BUFFERS:
1884 GST_DEBUG_OBJECT (sink,
1885 "got default step %" G_GUINT64_FORMAT "/%" G_GUINT64_FORMAT,
1886 current->position, current->amount);
1888 if (current->position < current->amount) {
1889 current->position++;
1894 case GST_FORMAT_DEFAULT:
1896 GST_DEBUG_OBJECT (sink,
1897 "got unknown step %" G_GUINT64_FORMAT "/%" G_GUINT64_FORMAT,
1898 current->position, current->amount);
1904 /* with STREAM_LOCK, PREROLL_LOCK
1906 * Returns TRUE if the object needs synchronisation and takes therefore
1907 * part in prerolling.
1909 * rsstart/rsstop contain the start/stop in stream time.
1910 * rrstart/rrstop contain the start/stop in running time.
1913 gst_base_sink_get_sync_times (GstBaseSink * basesink, GstMiniObject * obj,
1914 GstClockTime * rsstart, GstClockTime * rsstop,
1915 GstClockTime * rrstart, GstClockTime * rrstop, gboolean * do_sync,
1916 gboolean * stepped, GstSegment * segment, GstStepInfo * step,
1917 gboolean * step_end, guint8 obj_type)
1919 GstBaseSinkClass *bclass;
1921 GstClockTime start, stop; /* raw start/stop timestamps */
1922 gint64 cstart, cstop; /* clipped raw timestamps */
1923 gint64 rstart, rstop; /* clipped timestamps converted to running time */
1924 GstClockTime sstart, sstop; /* clipped timestamps converted to stream time */
1926 GstBaseSinkPrivate *priv;
1929 priv = basesink->priv;
1931 /* start with nothing */
1932 start = stop = GST_CLOCK_TIME_NONE;
1934 if (G_UNLIKELY (OBJ_IS_EVENT (obj_type))) {
1935 GstEvent *event = GST_EVENT_CAST (obj);
1937 switch (GST_EVENT_TYPE (event)) {
1938 /* EOS event needs syncing */
1941 if (basesink->segment.rate >= 0.0) {
1942 sstart = sstop = priv->current_sstop;
1943 if (!GST_CLOCK_TIME_IS_VALID (sstart)) {
1944 /* we have not seen a buffer yet, use the segment values */
1945 sstart = sstop = gst_segment_to_stream_time (&basesink->segment,
1946 basesink->segment.format, basesink->segment.stop);
1949 sstart = sstop = priv->current_sstart;
1950 if (!GST_CLOCK_TIME_IS_VALID (sstart)) {
1951 /* we have not seen a buffer yet, use the segment values */
1952 sstart = sstop = gst_segment_to_stream_time (&basesink->segment,
1953 basesink->segment.format, basesink->segment.start);
1957 rstart = rstop = priv->eos_rtime;
1958 *do_sync = rstart != -1;
1959 GST_DEBUG_OBJECT (basesink, "sync times for EOS %" GST_TIME_FORMAT,
1960 GST_TIME_ARGS (rstart));
1961 /* if we are stepping, we end now */
1962 *step_end = step->valid;
1967 /* other events do not need syncing */
1968 /* FIXME, maybe NEWSEGMENT might need synchronisation
1969 * since the POSITION query depends on accumulated times and
1970 * we cannot accumulate the current segment before the previous
1980 /* else do buffer sync code */
1981 buffer = GST_BUFFER_CAST (obj);
1983 bclass = GST_BASE_SINK_GET_CLASS (basesink);
1985 /* just get the times to see if we need syncing, if the start returns -1 we
1987 if (bclass->get_times)
1988 bclass->get_times (basesink, buffer, &start, &stop);
1990 if (!GST_CLOCK_TIME_IS_VALID (start)) {
1991 /* we don't need to sync but we still want to get the timestamps for
1992 * tracking the position */
1993 gst_base_sink_get_times (basesink, buffer, &start, &stop);
1999 GST_DEBUG_OBJECT (basesink, "got times start: %" GST_TIME_FORMAT
2000 ", stop: %" GST_TIME_FORMAT ", do_sync %d", GST_TIME_ARGS (start),
2001 GST_TIME_ARGS (stop), *do_sync);
2003 /* collect segment and format for code clarity */
2004 format = segment->format;
2006 /* no timestamp clipping if we did not get a TIME segment format */
2007 if (G_UNLIKELY (format != GST_FORMAT_TIME)) {
2010 /* do running and stream time in TIME format */
2011 format = GST_FORMAT_TIME;
2012 GST_LOG_OBJECT (basesink, "not time format, don't clip");
2016 /* clip, only when we know about time */
2017 if (G_UNLIKELY (!gst_segment_clip (segment, GST_FORMAT_TIME,
2018 (gint64) start, (gint64) stop, &cstart, &cstop))) {
2020 GST_DEBUG_OBJECT (basesink, "step out of segment");
2021 /* when we are stepping, pretend we're at the end of the segment */
2022 if (segment->rate > 0.0) {
2023 cstart = segment->stop;
2024 cstop = segment->stop;
2026 cstart = segment->start;
2027 cstop = segment->start;
2031 goto out_of_segment;
2034 if (G_UNLIKELY (start != cstart || stop != cstop)) {
2035 GST_DEBUG_OBJECT (basesink, "clipped to: start %" GST_TIME_FORMAT
2036 ", stop: %" GST_TIME_FORMAT, GST_TIME_ARGS (cstart),
2037 GST_TIME_ARGS (cstop));
2040 /* set last stop position */
2041 if (G_LIKELY (stop != GST_CLOCK_TIME_NONE && cstop != GST_CLOCK_TIME_NONE))
2042 gst_segment_set_last_stop (segment, GST_FORMAT_TIME, cstop);
2044 gst_segment_set_last_stop (segment, GST_FORMAT_TIME, cstart);
2047 rstart = gst_segment_to_running_time (segment, format, cstart);
2048 rstop = gst_segment_to_running_time (segment, format, cstop);
2050 if (G_UNLIKELY (step->valid)) {
2051 if (!(*step_end = handle_stepping (basesink, segment, step, &cstart, &cstop,
2052 &rstart, &rstop))) {
2053 /* step is still busy, we discard data when we are flushing */
2054 *stepped = step->flush;
2055 GST_DEBUG_OBJECT (basesink, "stepping busy");
2058 /* this can produce wrong values if we accumulated non-TIME segments. If this happens,
2059 * upstream is behaving very badly */
2060 sstart = gst_segment_to_stream_time (segment, format, cstart);
2061 sstop = gst_segment_to_stream_time (segment, format, cstop);
2064 /* eos_done label only called when doing EOS, we also stop stepping then */
2065 if (*step_end && step->flush) {
2066 GST_DEBUG_OBJECT (basesink, "flushing step ended");
2067 stop_stepping (basesink, segment, step, rstart, rstop, eos);
2069 /* re-determine running start times for adjusted segment
2070 * (which has a flushed amount of running/accumulated time removed) */
2071 if (!GST_IS_EVENT (obj)) {
2072 GST_DEBUG_OBJECT (basesink, "refresh sync times");
2083 /* buffers and EOS always need syncing and preroll */
2089 /* we usually clip in the chain function already but stepping could cause
2090 * the segment to be updated later. we return FALSE so that we don't try
2092 GST_LOG_OBJECT (basesink, "buffer skipped, not in segment");
2097 /* with STREAM_LOCK, PREROLL_LOCK, LOCK
2098 * adjust a timestamp with the latency and timestamp offset. This function does
2099 * not adjust for the render delay. */
2101 gst_base_sink_adjust_time (GstBaseSink * basesink, GstClockTime time)
2103 GstClockTimeDiff ts_offset;
2105 /* don't do anything funny with invalid timestamps */
2106 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (time)))
2109 time += basesink->priv->latency;
2111 /* apply offset, be carefull for underflows */
2112 ts_offset = basesink->priv->ts_offset;
2113 if (ts_offset < 0) {
2114 ts_offset = -ts_offset;
2115 if (ts_offset < time)
2122 /* subtract the render delay again, which was included in the latency */
2123 if (time > basesink->priv->render_delay)
2124 time -= basesink->priv->render_delay;
2132 * gst_base_sink_wait_clock:
2134 * @time: the running_time to be reached
2135 * @jitter: (out) (allow-none): the jitter to be filled with time diff, or NULL
2137 * This function will block until @time is reached. It is usually called by
2138 * subclasses that use their own internal synchronisation.
2140 * If @time is not valid, no sycnhronisation is done and #GST_CLOCK_BADTIME is
2141 * returned. Likewise, if synchronisation is disabled in the element or there
2142 * is no clock, no synchronisation is done and #GST_CLOCK_BADTIME is returned.
2144 * This function should only be called with the PREROLL_LOCK held, like when
2145 * receiving an EOS event in the #GstBaseSinkClass.event() vmethod or when
2146 * receiving a buffer in
2147 * the #GstBaseSinkClass.render() vmethod.
2149 * The @time argument should be the running_time of when this method should
2150 * return and is not adjusted with any latency or offset configured in the
2155 * Returns: #GstClockReturn
2158 gst_base_sink_wait_clock (GstBaseSink * sink, GstClockTime time,
2159 GstClockTimeDiff * jitter)
2163 GstClockTime base_time;
2165 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (time)))
2168 GST_OBJECT_LOCK (sink);
2169 if (G_UNLIKELY (!sink->sync))
2172 if (G_UNLIKELY ((clock = GST_ELEMENT_CLOCK (sink)) == NULL))
2175 base_time = GST_ELEMENT_CAST (sink)->base_time;
2176 GST_LOG_OBJECT (sink,
2177 "time %" GST_TIME_FORMAT ", base_time %" GST_TIME_FORMAT,
2178 GST_TIME_ARGS (time), GST_TIME_ARGS (base_time));
2180 /* add base_time to running_time to get the time against the clock */
2183 /* Re-use existing clockid if available */
2184 if (G_LIKELY (sink->priv->cached_clock_id != NULL)) {
2185 if (!gst_clock_single_shot_id_reinit (clock, sink->priv->cached_clock_id,
2187 gst_clock_id_unref (sink->priv->cached_clock_id);
2188 sink->priv->cached_clock_id = gst_clock_new_single_shot_id (clock, time);
2191 sink->priv->cached_clock_id = gst_clock_new_single_shot_id (clock, time);
2192 GST_OBJECT_UNLOCK (sink);
2194 /* A blocking wait is performed on the clock. We save the ClockID
2195 * so we can unlock the entry at any time. While we are blocking, we
2196 * release the PREROLL_LOCK so that other threads can interrupt the
2198 sink->clock_id = sink->priv->cached_clock_id;
2199 /* release the preroll lock while waiting */
2200 GST_PAD_PREROLL_UNLOCK (sink->sinkpad);
2202 ret = gst_clock_id_wait (sink->priv->cached_clock_id, jitter);
2204 GST_PAD_PREROLL_LOCK (sink->sinkpad);
2205 sink->clock_id = NULL;
2209 /* no syncing needed */
2212 GST_DEBUG_OBJECT (sink, "time not valid, no sync needed");
2213 return GST_CLOCK_BADTIME;
2217 GST_DEBUG_OBJECT (sink, "sync disabled");
2218 GST_OBJECT_UNLOCK (sink);
2219 return GST_CLOCK_BADTIME;
2223 GST_DEBUG_OBJECT (sink, "no clock, can't sync");
2224 GST_OBJECT_UNLOCK (sink);
2225 return GST_CLOCK_BADTIME;
2230 * gst_base_sink_wait_preroll:
2233 * If the #GstBaseSinkClass.render() method performs its own synchronisation
2234 * against the clock it must unblock when going from PLAYING to the PAUSED state
2235 * and call this method before continuing to render the remaining data.
2237 * This function will block until a state change to PLAYING happens (in which
2238 * case this function returns #GST_FLOW_OK) or the processing must be stopped due
2239 * to a state change to READY or a FLUSH event (in which case this function
2240 * returns #GST_FLOW_WRONG_STATE).
2242 * This function should only be called with the PREROLL_LOCK held, like in the
2245 * Returns: #GST_FLOW_OK if the preroll completed and processing can
2246 * continue. Any other return value should be returned from the render vmethod.
2251 gst_base_sink_wait_preroll (GstBaseSink * sink)
2253 sink->have_preroll = TRUE;
2254 GST_DEBUG_OBJECT (sink, "waiting in preroll for flush or PLAYING");
2255 /* block until the state changes, or we get a flush, or something */
2256 GST_PAD_PREROLL_WAIT (sink->sinkpad);
2257 sink->have_preroll = FALSE;
2258 if (G_UNLIKELY (sink->flushing))
2260 if (G_UNLIKELY (sink->priv->step_unlock))
2262 GST_DEBUG_OBJECT (sink, "continue after preroll");
2269 GST_DEBUG_OBJECT (sink, "preroll interrupted because of flush");
2270 return GST_FLOW_WRONG_STATE;
2274 sink->priv->step_unlock = FALSE;
2275 GST_DEBUG_OBJECT (sink, "preroll interrupted because of step");
2276 return GST_FLOW_STEP;
2280 static inline guint8
2281 get_object_type (GstMiniObject * obj)
2285 if (G_LIKELY (GST_IS_BUFFER (obj)))
2286 obj_type = _PR_IS_BUFFER;
2287 else if (GST_IS_EVENT (obj))
2288 obj_type = _PR_IS_EVENT;
2289 else if (GST_IS_BUFFER_LIST (obj))
2290 obj_type = _PR_IS_BUFFERLIST;
2292 obj_type = _PR_IS_NOTHING;
2298 * gst_base_sink_do_preroll:
2300 * @obj: (transfer none): the mini object that caused the preroll
2302 * If the @sink spawns its own thread for pulling buffers from upstream it
2303 * should call this method after it has pulled a buffer. If the element needed
2304 * to preroll, this function will perform the preroll and will then block
2305 * until the element state is changed.
2307 * This function should be called with the PREROLL_LOCK held.
2309 * Returns: #GST_FLOW_OK if the preroll completed and processing can
2310 * continue. Any other return value should be returned from the render vmethod.
2315 gst_base_sink_do_preroll (GstBaseSink * sink, GstMiniObject * obj)
2319 while (G_UNLIKELY (sink->need_preroll)) {
2321 GST_DEBUG_OBJECT (sink, "prerolling object %p", obj);
2323 obj_type = get_object_type (obj);
2325 ret = gst_base_sink_preroll_object (sink, obj_type, obj);
2326 if (ret != GST_FLOW_OK)
2327 goto preroll_failed;
2329 /* need to recheck here because the commit state could have
2330 * made us not need the preroll anymore */
2331 if (G_LIKELY (sink->need_preroll)) {
2332 /* block until the state changes, or we get a flush, or something */
2333 ret = gst_base_sink_wait_preroll (sink);
2334 if ((ret != GST_FLOW_OK) && (ret != GST_FLOW_STEP))
2335 goto preroll_failed;
2343 GST_DEBUG_OBJECT (sink, "preroll failed %d", ret);
2349 * gst_base_sink_wait_eos:
2351 * @time: the running_time to be reached
2352 * @jitter: (out) (allow-none): the jitter to be filled with time diff, or NULL
2354 * This function will block until @time is reached. It is usually called by
2355 * subclasses that use their own internal synchronisation but want to let the
2356 * EOS be handled by the base class.
2358 * This function should only be called with the PREROLL_LOCK held, like when
2359 * receiving an EOS event in the ::event vmethod.
2361 * The @time argument should be the running_time of when the EOS should happen
2362 * and will be adjusted with any latency and offset configured in the sink.
2364 * Returns: #GstFlowReturn
2369 gst_base_sink_wait_eos (GstBaseSink * sink, GstClockTime time,
2370 GstClockTimeDiff * jitter)
2372 GstClockReturn status;
2378 GST_DEBUG_OBJECT (sink, "checking preroll");
2380 /* first wait for the playing state before we can continue */
2381 while (G_UNLIKELY (sink->need_preroll)) {
2382 ret = gst_base_sink_wait_preroll (sink);
2383 if ((ret != GST_FLOW_OK) && (ret != GST_FLOW_STEP))
2387 /* preroll done, we can sync since we are in PLAYING now. */
2388 GST_DEBUG_OBJECT (sink, "possibly waiting for clock to reach %"
2389 GST_TIME_FORMAT, GST_TIME_ARGS (time));
2391 /* compensate for latency and ts_offset. We don't adjust for render delay
2392 * because we don't interact with the device on EOS normally. */
2393 stime = gst_base_sink_adjust_time (sink, time);
2395 /* wait for the clock, this can be interrupted because we got shut down or
2397 status = gst_base_sink_wait_clock (sink, stime, jitter);
2399 GST_DEBUG_OBJECT (sink, "clock returned %d", status);
2401 /* invalid time, no clock or sync disabled, just continue then */
2402 if (status == GST_CLOCK_BADTIME)
2405 /* waiting could have been interrupted and we can be flushing now */
2406 if (G_UNLIKELY (sink->flushing))
2409 /* retry if we got unscheduled, which means we did not reach the timeout
2410 * yet. if some other error occures, we continue. */
2411 } while (status == GST_CLOCK_UNSCHEDULED);
2413 GST_DEBUG_OBJECT (sink, "end of stream");
2420 GST_DEBUG_OBJECT (sink, "we are flushing");
2421 return GST_FLOW_WRONG_STATE;
2425 /* with STREAM_LOCK, PREROLL_LOCK
2427 * Make sure we are in PLAYING and synchronize an object to the clock.
2429 * If we need preroll, we are not in PLAYING. We try to commit the state
2430 * if needed and then block if we still are not PLAYING.
2432 * We start waiting on the clock in PLAYING. If we got interrupted, we
2433 * immediatly try to re-preroll.
2435 * Some objects do not need synchronisation (most events) and so this function
2436 * immediatly returns GST_FLOW_OK.
2438 * for objects that arrive later than max-lateness to be synchronized to the
2439 * clock have the @late boolean set to TRUE.
2441 * This function keeps a running average of the jitter (the diff between the
2442 * clock time and the requested sync time). The jitter is negative for
2443 * objects that arrive in time and positive for late buffers.
2445 * does not take ownership of obj.
2447 static GstFlowReturn
2448 gst_base_sink_do_sync (GstBaseSink * basesink, GstPad * pad,
2449 GstMiniObject * obj, gboolean * late, gboolean * step_end, guint8 obj_type)
2451 GstClockTimeDiff jitter = 0;
2453 GstClockReturn status = GST_CLOCK_OK;
2454 GstClockTime rstart, rstop, sstart, sstop, stime;
2456 GstBaseSinkPrivate *priv;
2458 GstStepInfo *current, *pending;
2461 priv = basesink->priv;
2464 sstart = sstop = rstart = rstop = GST_CLOCK_TIME_NONE;
2468 priv->current_rstart = GST_CLOCK_TIME_NONE;
2470 /* get stepping info */
2471 current = &priv->current_step;
2472 pending = &priv->pending_step;
2474 /* get timing information for this object against the render segment */
2475 syncable = gst_base_sink_get_sync_times (basesink, obj,
2476 &sstart, &sstop, &rstart, &rstop, &do_sync, &stepped, &basesink->segment,
2477 current, step_end, obj_type);
2479 if (G_UNLIKELY (stepped))
2482 /* a syncable object needs to participate in preroll and
2483 * clocking. All buffers and EOS are syncable. */
2484 if (G_UNLIKELY (!syncable))
2487 /* store timing info for current object */
2488 priv->current_rstart = rstart;
2489 priv->current_rstop = (GST_CLOCK_TIME_IS_VALID (rstop) ? rstop : rstart);
2491 /* save sync time for eos when the previous object needed sync */
2492 priv->eos_rtime = (do_sync ? priv->current_rstop : GST_CLOCK_TIME_NONE);
2494 if (G_UNLIKELY (priv->earliest_in_time != -1
2495 && rstart < priv->earliest_in_time))
2499 /* first do preroll, this makes sure we commit our state
2500 * to PAUSED and can continue to PLAYING. We cannot perform
2501 * any clock sync in PAUSED because there is no clock. */
2502 ret = gst_base_sink_do_preroll (basesink, obj);
2503 if (G_UNLIKELY (ret != GST_FLOW_OK))
2504 goto preroll_failed;
2506 /* update the segment with a pending step if the current one is invalid and we
2507 * have a new pending one. We only accept new step updates after a preroll */
2508 if (G_UNLIKELY (pending->valid && !current->valid)) {
2509 start_stepping (basesink, &basesink->segment, pending, current);
2513 /* After rendering we store the position of the last buffer so that we can use
2514 * it to report the position. We need to take the lock here. */
2515 GST_OBJECT_LOCK (basesink);
2516 priv->current_sstart = sstart;
2517 priv->current_sstop = (GST_CLOCK_TIME_IS_VALID (sstop) ? sstop : sstart);
2518 GST_OBJECT_UNLOCK (basesink);
2523 /* adjust for latency */
2524 stime = gst_base_sink_adjust_time (basesink, rstart);
2526 /* adjust for render-delay, avoid underflows */
2527 if (GST_CLOCK_TIME_IS_VALID (stime)) {
2528 if (stime > priv->render_delay)
2529 stime -= priv->render_delay;
2534 /* preroll done, we can sync since we are in PLAYING now. */
2535 GST_DEBUG_OBJECT (basesink, "possibly waiting for clock to reach %"
2536 GST_TIME_FORMAT ", adjusted %" GST_TIME_FORMAT,
2537 GST_TIME_ARGS (rstart), GST_TIME_ARGS (stime));
2539 /* This function will return immediatly if start == -1, no clock
2540 * or sync is disabled with GST_CLOCK_BADTIME. */
2541 status = gst_base_sink_wait_clock (basesink, stime, &jitter);
2543 GST_DEBUG_OBJECT (basesink, "clock returned %d, jitter %c%" GST_TIME_FORMAT,
2544 status, (jitter < 0 ? '-' : ' '), GST_TIME_ARGS (ABS (jitter)));
2546 /* invalid time, no clock or sync disabled, just render */
2547 if (status == GST_CLOCK_BADTIME)
2550 /* waiting could have been interrupted and we can be flushing now */
2551 if (G_UNLIKELY (basesink->flushing))
2554 /* check for unlocked by a state change, we are not flushing so
2555 * we can try to preroll on the current buffer. */
2556 if (G_UNLIKELY (status == GST_CLOCK_UNSCHEDULED)) {
2557 GST_DEBUG_OBJECT (basesink, "unscheduled, waiting some more");
2558 priv->call_preroll = TRUE;
2562 /* successful syncing done, record observation */
2563 priv->current_jitter = jitter;
2565 /* check if the object should be dropped */
2566 *late = gst_base_sink_is_too_late (basesink, obj, rstart, rstop,
2575 GST_DEBUG_OBJECT (basesink, "skipped stepped object %p", obj);
2581 GST_DEBUG_OBJECT (basesink, "non syncable object %p", obj);
2586 GST_DEBUG_OBJECT (basesink, "dropped because of QoS %p", obj);
2592 GST_DEBUG_OBJECT (basesink, "we are flushing");
2593 return GST_FLOW_WRONG_STATE;
2597 GST_DEBUG_OBJECT (basesink, "preroll failed");
2604 gst_base_sink_send_qos (GstBaseSink * basesink, GstQOSType type,
2605 gdouble proportion, GstClockTime time, GstClockTimeDiff diff)
2610 /* generate Quality-of-Service event */
2611 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, basesink,
2612 "qos: type %d, proportion: %lf, diff %" G_GINT64_FORMAT ", timestamp %"
2613 GST_TIME_FORMAT, type, proportion, diff, GST_TIME_ARGS (time));
2615 event = gst_event_new_qos_full (type, proportion, diff, time);
2618 res = gst_pad_push_event (basesink->sinkpad, event);
2624 gst_base_sink_perform_qos (GstBaseSink * sink, gboolean dropped)
2626 GstBaseSinkPrivate *priv;
2627 GstClockTime start, stop;
2628 GstClockTimeDiff jitter;
2629 GstClockTime pt, entered, left;
2630 GstClockTime duration;
2635 start = priv->current_rstart;
2637 if (priv->current_step.valid)
2640 /* if Quality-of-Service disabled, do nothing */
2641 if (!g_atomic_int_get (&priv->qos_enabled) ||
2642 !GST_CLOCK_TIME_IS_VALID (start))
2645 stop = priv->current_rstop;
2646 jitter = priv->current_jitter;
2649 /* this is the time the buffer entered the sink */
2650 if (start < -jitter)
2653 entered = start + jitter;
2656 /* this is the time the buffer entered the sink */
2657 entered = start + jitter;
2658 /* this is the time the buffer left the sink */
2659 left = start + jitter;
2662 /* calculate duration of the buffer */
2663 if (GST_CLOCK_TIME_IS_VALID (stop) && stop != start)
2664 duration = stop - start;
2666 duration = GST_CLOCK_TIME_NONE;
2668 /* if we have the time when the last buffer left us, calculate
2669 * processing time */
2670 if (GST_CLOCK_TIME_IS_VALID (priv->last_left)) {
2671 if (entered > priv->last_left) {
2672 pt = entered - priv->last_left;
2680 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, sink, "start: %" GST_TIME_FORMAT
2681 ", stop %" GST_TIME_FORMAT ", entered %" GST_TIME_FORMAT ", left %"
2682 GST_TIME_FORMAT ", pt: %" GST_TIME_FORMAT ", duration %" GST_TIME_FORMAT
2683 ",jitter %" G_GINT64_FORMAT, GST_TIME_ARGS (start), GST_TIME_ARGS (stop),
2684 GST_TIME_ARGS (entered), GST_TIME_ARGS (left), GST_TIME_ARGS (pt),
2685 GST_TIME_ARGS (duration), jitter);
2687 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, sink, "avg_duration: %" GST_TIME_FORMAT
2688 ", avg_pt: %" GST_TIME_FORMAT ", avg_rate: %g",
2689 GST_TIME_ARGS (priv->avg_duration), GST_TIME_ARGS (priv->avg_pt),
2692 /* collect running averages. for first observations, we copy the
2694 if (!GST_CLOCK_TIME_IS_VALID (priv->avg_duration))
2695 priv->avg_duration = duration;
2697 priv->avg_duration = UPDATE_RUNNING_AVG (priv->avg_duration, duration);
2699 if (!GST_CLOCK_TIME_IS_VALID (priv->avg_pt))
2702 priv->avg_pt = UPDATE_RUNNING_AVG (priv->avg_pt, pt);
2704 if (priv->avg_duration != 0)
2706 gst_guint64_to_gdouble (priv->avg_pt) /
2707 gst_guint64_to_gdouble (priv->avg_duration);
2711 if (GST_CLOCK_TIME_IS_VALID (priv->last_left)) {
2712 if (dropped || priv->avg_rate < 0.0) {
2713 priv->avg_rate = rate;
2716 priv->avg_rate = UPDATE_RUNNING_AVG_N (priv->avg_rate, rate);
2718 priv->avg_rate = UPDATE_RUNNING_AVG_P (priv->avg_rate, rate);
2722 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, sink,
2723 "updated: avg_duration: %" GST_TIME_FORMAT ", avg_pt: %" GST_TIME_FORMAT
2724 ", avg_rate: %g", GST_TIME_ARGS (priv->avg_duration),
2725 GST_TIME_ARGS (priv->avg_pt), priv->avg_rate);
2728 if (priv->avg_rate >= 0.0) {
2730 GstClockTimeDiff diff;
2732 /* if we have a valid rate, start sending QoS messages */
2733 if (priv->current_jitter < 0) {
2734 /* make sure we never go below 0 when adding the jitter to the
2736 if (priv->current_rstart < -priv->current_jitter)
2737 priv->current_jitter = -priv->current_rstart;
2740 if (priv->throttle_time > 0) {
2741 diff = priv->throttle_time;
2742 type = GST_QOS_TYPE_THROTTLE;
2744 diff = priv->current_jitter;
2746 type = GST_QOS_TYPE_OVERFLOW;
2748 type = GST_QOS_TYPE_UNDERFLOW;
2751 gst_base_sink_send_qos (sink, type, priv->avg_rate, priv->current_rstart,
2755 /* record when this buffer will leave us */
2756 priv->last_left = left;
2759 /* reset all qos measuring */
2761 gst_base_sink_reset_qos (GstBaseSink * sink)
2763 GstBaseSinkPrivate *priv;
2767 priv->last_render_time = GST_CLOCK_TIME_NONE;
2768 priv->earliest_in_time = GST_CLOCK_TIME_NONE;
2769 priv->last_left = GST_CLOCK_TIME_NONE;
2770 priv->avg_duration = GST_CLOCK_TIME_NONE;
2771 priv->avg_pt = GST_CLOCK_TIME_NONE;
2772 priv->avg_rate = -1.0;
2773 priv->avg_render = GST_CLOCK_TIME_NONE;
2779 /* Checks if the object was scheduled too late.
2781 * rstart/rstop contain the running_time start and stop values
2784 * status and jitter contain the return values from the clock wait.
2786 * returns TRUE if the buffer was too late.
2789 gst_base_sink_is_too_late (GstBaseSink * basesink, GstMiniObject * obj,
2790 GstClockTime rstart, GstClockTime rstop,
2791 GstClockReturn status, GstClockTimeDiff jitter)
2794 gint64 max_lateness;
2795 GstBaseSinkPrivate *priv;
2797 priv = basesink->priv;
2801 /* only for objects that were too late */
2802 if (G_LIKELY (status != GST_CLOCK_EARLY))
2805 max_lateness = basesink->abidata.ABI.max_lateness;
2807 /* check if frame dropping is enabled */
2808 if (max_lateness == -1)
2811 /* only check for buffers */
2812 if (G_UNLIKELY (!GST_IS_BUFFER (obj)))
2815 /* can't do check if we don't have a timestamp */
2816 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (rstart)))
2819 /* we can add a valid stop time */
2820 if (GST_CLOCK_TIME_IS_VALID (rstop))
2821 max_lateness += rstop;
2823 max_lateness += rstart;
2825 /* if the jitter bigger than duration and lateness we are too late */
2826 if ((late = rstart + jitter > max_lateness)) {
2827 GST_CAT_DEBUG_OBJECT (GST_CAT_PERFORMANCE, basesink,
2828 "buffer is too late %" GST_TIME_FORMAT
2829 " > %" GST_TIME_FORMAT, GST_TIME_ARGS (rstart + jitter),
2830 GST_TIME_ARGS (max_lateness));
2831 /* !!emergency!!, if we did not receive anything valid for more than a
2832 * second, render it anyway so the user sees something */
2833 if (GST_CLOCK_TIME_IS_VALID (priv->last_render_time) &&
2834 rstart - priv->last_render_time > GST_SECOND) {
2836 GST_ELEMENT_WARNING (basesink, CORE, CLOCK,
2837 (_("A lot of buffers are being dropped.")),
2838 ("There may be a timestamping problem, or this computer is too slow."));
2839 GST_CAT_DEBUG_OBJECT (GST_CAT_PERFORMANCE, basesink,
2840 "**emergency** last buffer at %" GST_TIME_FORMAT " > GST_SECOND",
2841 GST_TIME_ARGS (priv->last_render_time));
2846 if (!late || !GST_CLOCK_TIME_IS_VALID (priv->last_render_time)) {
2847 priv->last_render_time = rstart;
2848 /* the next allowed input timestamp */
2849 if (priv->throttle_time > 0)
2850 priv->earliest_in_time = rstart + priv->throttle_time;
2857 GST_DEBUG_OBJECT (basesink, "object was scheduled in time");
2862 GST_DEBUG_OBJECT (basesink, "frame dropping disabled");
2867 GST_DEBUG_OBJECT (basesink, "object is not a buffer");
2872 GST_DEBUG_OBJECT (basesink, "buffer has no timestamp");
2877 /* called before and after calling the render vmethod. It keeps track of how
2878 * much time was spent in the render method and is used to check if we are
2881 gst_base_sink_do_render_stats (GstBaseSink * basesink, gboolean start)
2883 GstBaseSinkPrivate *priv;
2885 priv = basesink->priv;
2888 priv->start = gst_util_get_timestamp ();
2890 GstClockTime elapsed;
2892 priv->stop = gst_util_get_timestamp ();
2894 elapsed = GST_CLOCK_DIFF (priv->start, priv->stop);
2896 if (!GST_CLOCK_TIME_IS_VALID (priv->avg_render))
2897 priv->avg_render = elapsed;
2899 priv->avg_render = UPDATE_RUNNING_AVG (priv->avg_render, elapsed);
2901 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, basesink,
2902 "avg_render: %" GST_TIME_FORMAT, GST_TIME_ARGS (priv->avg_render));
2906 /* with STREAM_LOCK, PREROLL_LOCK,
2908 * Synchronize the object on the clock and then render it.
2910 * takes ownership of obj.
2912 static GstFlowReturn
2913 gst_base_sink_render_object (GstBaseSink * basesink, GstPad * pad,
2914 guint8 obj_type, gpointer obj)
2917 GstBaseSinkClass *bclass;
2918 gboolean late, step_end;
2920 GstBaseSinkPrivate *priv;
2922 priv = basesink->priv;
2924 if (OBJ_IS_BUFFERLIST (obj_type)) {
2926 * If buffer list, use the first group buffer within the list
2929 sync_obj = gst_buffer_list_get (GST_BUFFER_LIST_CAST (obj), 0, 0);
2930 g_assert (NULL != sync_obj);
2939 /* synchronize this object, non syncable objects return OK
2942 gst_base_sink_do_sync (basesink, pad, sync_obj, &late, &step_end,
2944 if (G_UNLIKELY (ret != GST_FLOW_OK))
2947 /* and now render, event or buffer/buffer list. */
2948 if (G_LIKELY (OBJ_IS_BUFFERFULL (obj_type))) {
2949 /* drop late buffers unconditionally, let's hope it's unlikely */
2950 if (G_UNLIKELY (late))
2953 bclass = GST_BASE_SINK_GET_CLASS (basesink);
2955 if (G_LIKELY ((OBJ_IS_BUFFERLIST (obj_type) && bclass->render_list) ||
2956 (!OBJ_IS_BUFFERLIST (obj_type) && bclass->render))) {
2959 /* read once, to get same value before and after */
2960 do_qos = g_atomic_int_get (&priv->qos_enabled);
2962 GST_DEBUG_OBJECT (basesink, "rendering object %p", obj);
2964 /* record rendering time for QoS and stats */
2966 gst_base_sink_do_render_stats (basesink, TRUE);
2968 if (!OBJ_IS_BUFFERLIST (obj_type)) {
2971 /* For buffer lists do not set last buffer. Creating buffer
2972 * with meaningful data can be done only with memcpy which will
2973 * significantly affect performance */
2974 buf = GST_BUFFER_CAST (obj);
2975 gst_base_sink_set_last_buffer (basesink, buf);
2977 ret = bclass->render (basesink, buf);
2979 GstBufferList *buflist;
2981 buflist = GST_BUFFER_LIST_CAST (obj);
2983 ret = bclass->render_list (basesink, buflist);
2987 gst_base_sink_do_render_stats (basesink, FALSE);
2989 if (ret == GST_FLOW_STEP)
2992 if (G_UNLIKELY (basesink->flushing))
2997 } else if (G_LIKELY (OBJ_IS_EVENT (obj_type))) {
2998 GstEvent *event = GST_EVENT_CAST (obj);
2999 gboolean event_res = TRUE;
3002 bclass = GST_BASE_SINK_GET_CLASS (basesink);
3004 type = GST_EVENT_TYPE (event);
3006 GST_DEBUG_OBJECT (basesink, "rendering event %p, type %s", obj,
3007 gst_event_type_get_name (type));
3010 event_res = bclass->event (basesink, event);
3012 /* when we get here we could be flushing again when the event handler calls
3013 * _wait_eos(). We have to ignore this object in that case. */
3014 if (G_UNLIKELY (basesink->flushing))
3017 if (G_LIKELY (event_res)) {
3020 seqnum = basesink->priv->seqnum = gst_event_get_seqnum (event);
3021 GST_DEBUG_OBJECT (basesink, "Got seqnum #%" G_GUINT32_FORMAT, seqnum);
3026 GstMessage *message;
3028 /* the EOS event is completely handled so we mark
3029 * ourselves as being in the EOS state. eos is also
3030 * protected by the object lock so we can read it when
3031 * answering the POSITION query. */
3032 GST_OBJECT_LOCK (basesink);
3033 basesink->eos = TRUE;
3034 GST_OBJECT_UNLOCK (basesink);
3036 /* ok, now we can post the message */
3037 GST_DEBUG_OBJECT (basesink, "Now posting EOS");
3039 message = gst_message_new_eos (GST_OBJECT_CAST (basesink));
3040 gst_message_set_seqnum (message, seqnum);
3041 gst_element_post_message (GST_ELEMENT_CAST (basesink), message);
3044 case GST_EVENT_NEWSEGMENT:
3045 /* configure the segment */
3046 gst_base_sink_configure_segment (basesink, pad, event,
3047 &basesink->segment);
3049 case GST_EVENT_SINK_MESSAGE:{
3050 GstMessage *msg = NULL;
3052 gst_event_parse_sink_message (event, &msg);
3055 gst_element_post_message (GST_ELEMENT_CAST (basesink), msg);
3062 g_return_val_if_reached (GST_FLOW_ERROR);
3067 /* the step ended, check if we need to activate a new step */
3068 GST_DEBUG_OBJECT (basesink, "step ended");
3069 stop_stepping (basesink, &basesink->segment, &priv->current_step,
3070 priv->current_rstart, priv->current_rstop, basesink->eos);
3074 gst_base_sink_perform_qos (basesink, late);
3076 GST_DEBUG_OBJECT (basesink, "object unref after render %p", obj);
3077 gst_mini_object_unref (GST_MINI_OBJECT_CAST (obj));
3083 GST_DEBUG_OBJECT (basesink, "do_sync returned %s", gst_flow_get_name (ret));
3089 GST_DEBUG_OBJECT (basesink, "buffer late, dropping");
3091 if (g_atomic_int_get (&priv->qos_enabled)) {
3092 GstMessage *qos_msg;
3093 GstClockTime timestamp, duration;
3095 timestamp = GST_BUFFER_TIMESTAMP (GST_BUFFER_CAST (sync_obj));
3096 duration = GST_BUFFER_DURATION (GST_BUFFER_CAST (sync_obj));
3098 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, basesink,
3099 "qos: dropped buffer rt %" GST_TIME_FORMAT ", st %" GST_TIME_FORMAT
3100 ", ts %" GST_TIME_FORMAT ", dur %" GST_TIME_FORMAT,
3101 GST_TIME_ARGS (priv->current_rstart),
3102 GST_TIME_ARGS (priv->current_sstart), GST_TIME_ARGS (timestamp),
3103 GST_TIME_ARGS (duration));
3104 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, basesink,
3105 "qos: rendered %" G_GUINT64_FORMAT ", dropped %" G_GUINT64_FORMAT,
3106 priv->rendered, priv->dropped);
3109 gst_message_new_qos (GST_OBJECT_CAST (basesink), basesink->sync,
3110 priv->current_rstart, priv->current_sstart, timestamp, duration);
3111 gst_message_set_qos_values (qos_msg, priv->current_jitter, priv->avg_rate,
3113 gst_message_set_qos_stats (qos_msg, GST_FORMAT_BUFFERS, priv->rendered,
3115 gst_element_post_message (GST_ELEMENT_CAST (basesink), qos_msg);
3121 GST_DEBUG_OBJECT (basesink, "we are flushing, ignore object");
3122 gst_mini_object_unref (obj);
3123 return GST_FLOW_WRONG_STATE;
3127 /* with STREAM_LOCK, PREROLL_LOCK
3129 * Perform preroll on the given object. For buffers this means
3130 * calling the preroll subclass method.
3131 * If that succeeds, the state will be commited.
3133 * function does not take ownership of obj.
3135 static GstFlowReturn
3136 gst_base_sink_preroll_object (GstBaseSink * basesink, guint8 obj_type,
3137 GstMiniObject * obj)
3141 GST_DEBUG_OBJECT (basesink, "prerolling object %p", obj);
3143 /* if it's a buffer, we need to call the preroll method */
3144 if (G_LIKELY (OBJ_IS_BUFFERFULL (obj_type) && basesink->priv->call_preroll)) {
3145 GstBaseSinkClass *bclass;
3147 GstClockTime timestamp;
3149 if (OBJ_IS_BUFFERLIST (obj_type)) {
3150 buf = gst_buffer_list_get (GST_BUFFER_LIST_CAST (obj), 0, 0);
3151 g_assert (NULL != buf);
3153 buf = GST_BUFFER_CAST (obj);
3156 timestamp = GST_BUFFER_TIMESTAMP (buf);
3158 GST_DEBUG_OBJECT (basesink, "preroll buffer %" GST_TIME_FORMAT,
3159 GST_TIME_ARGS (timestamp));
3162 * For buffer lists do not set last buffer. Creating buffer
3163 * with meaningful data can be done only with memcpy which will
3164 * significantly affect performance
3166 if (!OBJ_IS_BUFFERLIST (obj_type)) {
3167 gst_base_sink_set_last_buffer (basesink, buf);
3170 bclass = GST_BASE_SINK_GET_CLASS (basesink);
3171 if (bclass->preroll)
3172 if ((ret = bclass->preroll (basesink, buf)) != GST_FLOW_OK)
3173 goto preroll_failed;
3175 basesink->priv->call_preroll = FALSE;
3179 if (G_LIKELY (basesink->playing_async)) {
3180 if (G_UNLIKELY (!gst_base_sink_commit_state (basesink)))
3189 GST_DEBUG_OBJECT (basesink, "preroll failed, abort state");
3190 gst_element_abort_state (GST_ELEMENT_CAST (basesink));
3195 GST_DEBUG_OBJECT (basesink, "stopping while commiting state");
3196 return GST_FLOW_WRONG_STATE;
3200 /* with STREAM_LOCK, PREROLL_LOCK
3202 * Queue an object for rendering.
3203 * The first prerollable object queued will complete the preroll. If the
3204 * preroll queue if filled, we render all the objects in the queue.
3206 * This function takes ownership of the object.
3208 static GstFlowReturn
3209 gst_base_sink_queue_object_unlocked (GstBaseSink * basesink, GstPad * pad,
3210 guint8 obj_type, gpointer obj, gboolean prerollable)
3212 GstFlowReturn ret = GST_FLOW_OK;
3216 if (G_UNLIKELY (basesink->need_preroll)) {
3217 if (G_LIKELY (prerollable))
3218 basesink->preroll_queued++;
3220 length = basesink->preroll_queued;
3222 GST_DEBUG_OBJECT (basesink, "now %d prerolled items", length);
3224 /* first prerollable item needs to finish the preroll */
3226 ret = gst_base_sink_preroll_object (basesink, obj_type, obj);
3227 if (G_UNLIKELY (ret != GST_FLOW_OK))
3228 goto preroll_failed;
3230 /* need to recheck if we need preroll, commmit state during preroll
3231 * could have made us not need more preroll. */
3232 if (G_UNLIKELY (basesink->need_preroll)) {
3233 /* see if we can render now, if we can't add the object to the preroll
3235 if (G_UNLIKELY (length <= basesink->preroll_queue_max_len))
3239 /* we can start rendering (or blocking) the queued object
3241 q = basesink->preroll_queue;
3242 while (G_UNLIKELY (!g_queue_is_empty (q))) {
3246 o = g_queue_pop_head (q);
3247 GST_DEBUG_OBJECT (basesink, "rendering queued object %p", o);
3249 ot = get_object_type (o);
3251 /* do something with the return value */
3252 ret = gst_base_sink_render_object (basesink, pad, ot, o);
3253 if (ret != GST_FLOW_OK)
3254 goto dequeue_failed;
3257 /* now render the object */
3258 ret = gst_base_sink_render_object (basesink, pad, obj_type, obj);
3259 basesink->preroll_queued = 0;
3266 GST_DEBUG_OBJECT (basesink, "preroll failed, reason %s",
3267 gst_flow_get_name (ret));
3268 gst_mini_object_unref (GST_MINI_OBJECT_CAST (obj));
3273 /* add object to the queue and return */
3274 GST_DEBUG_OBJECT (basesink, "need more preroll data %d <= %d",
3275 length, basesink->preroll_queue_max_len);
3276 g_queue_push_tail (basesink->preroll_queue, obj);
3281 GST_DEBUG_OBJECT (basesink, "rendering queued objects failed, reason %s",
3282 gst_flow_get_name (ret));
3283 gst_mini_object_unref (GST_MINI_OBJECT_CAST (obj));
3290 * This function grabs the PREROLL_LOCK and adds the object to
3293 * This function takes ownership of obj.
3295 * Note: Only GstEvent seem to be passed to this private method
3297 static GstFlowReturn
3298 gst_base_sink_queue_object (GstBaseSink * basesink, GstPad * pad,
3299 GstMiniObject * obj, gboolean prerollable)
3303 GST_PAD_PREROLL_LOCK (pad);
3304 if (G_UNLIKELY (basesink->flushing))
3307 if (G_UNLIKELY (basesink->priv->received_eos))
3311 gst_base_sink_queue_object_unlocked (basesink, pad, _PR_IS_EVENT, obj,
3313 GST_PAD_PREROLL_UNLOCK (pad);
3320 GST_DEBUG_OBJECT (basesink, "sink is flushing");
3321 GST_PAD_PREROLL_UNLOCK (pad);
3322 gst_mini_object_unref (obj);
3323 return GST_FLOW_WRONG_STATE;
3327 GST_DEBUG_OBJECT (basesink,
3328 "we are EOS, dropping object, return UNEXPECTED");
3329 GST_PAD_PREROLL_UNLOCK (pad);
3330 gst_mini_object_unref (obj);
3331 return GST_FLOW_UNEXPECTED;
3336 gst_base_sink_flush_start (GstBaseSink * basesink, GstPad * pad)
3338 /* make sure we are not blocked on the clock also clear any pending
3340 gst_base_sink_set_flushing (basesink, pad, TRUE);
3342 /* we grab the stream lock but that is not needed since setting the
3343 * sink to flushing would make sure no state commit is being done
3345 GST_PAD_STREAM_LOCK (pad);
3346 gst_base_sink_reset_qos (basesink);
3347 /* and we need to commit our state again on the next
3348 * prerolled buffer */
3349 basesink->playing_async = TRUE;
3350 if (basesink->priv->async_enabled) {
3351 gst_element_lost_state (GST_ELEMENT_CAST (basesink));
3353 basesink->priv->have_latency = TRUE;
3355 gst_base_sink_set_last_buffer (basesink, NULL);
3356 GST_PAD_STREAM_UNLOCK (pad);
3360 gst_base_sink_flush_stop (GstBaseSink * basesink, GstPad * pad)
3362 /* unset flushing so we can accept new data, this also flushes out any EOS
3364 gst_base_sink_set_flushing (basesink, pad, FALSE);
3366 /* for position reporting */
3367 GST_OBJECT_LOCK (basesink);
3368 basesink->priv->current_sstart = GST_CLOCK_TIME_NONE;
3369 basesink->priv->current_sstop = GST_CLOCK_TIME_NONE;
3370 basesink->priv->eos_rtime = GST_CLOCK_TIME_NONE;
3371 basesink->priv->call_preroll = TRUE;
3372 basesink->priv->current_step.valid = FALSE;
3373 basesink->priv->pending_step.valid = FALSE;
3374 if (basesink->pad_mode == GST_ACTIVATE_PUSH) {
3375 /* we need new segment info after the flush. */
3376 basesink->have_newsegment = FALSE;
3377 gst_segment_init (&basesink->segment, GST_FORMAT_UNDEFINED);
3378 gst_segment_init (basesink->abidata.ABI.clip_segment, GST_FORMAT_UNDEFINED);
3380 GST_OBJECT_UNLOCK (basesink);
3384 gst_base_sink_event (GstPad * pad, GstEvent * event)
3386 GstBaseSink *basesink;
3387 gboolean result = TRUE;
3388 GstBaseSinkClass *bclass;
3390 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
3392 bclass = GST_BASE_SINK_GET_CLASS (basesink);
3394 GST_DEBUG_OBJECT (basesink, "received event %p %" GST_PTR_FORMAT, event,
3397 switch (GST_EVENT_TYPE (event)) {
3402 GST_PAD_PREROLL_LOCK (pad);
3403 if (G_UNLIKELY (basesink->flushing))
3406 if (G_UNLIKELY (basesink->priv->received_eos)) {
3407 /* we can't accept anything when we are EOS */
3409 gst_event_unref (event);
3411 /* we set the received EOS flag here so that we can use it when testing if
3412 * we are prerolled and to refuse more buffers. */
3413 basesink->priv->received_eos = TRUE;
3415 /* EOS is a prerollable object, we call the unlocked version because it
3416 * does not check the received_eos flag. */
3417 ret = gst_base_sink_queue_object_unlocked (basesink, pad,
3418 _PR_IS_EVENT, GST_MINI_OBJECT_CAST (event), TRUE);
3419 if (G_UNLIKELY (ret != GST_FLOW_OK))
3422 GST_PAD_PREROLL_UNLOCK (pad);
3425 case GST_EVENT_NEWSEGMENT:
3430 GST_DEBUG_OBJECT (basesink, "newsegment %p", event);
3432 GST_PAD_PREROLL_LOCK (pad);
3433 if (G_UNLIKELY (basesink->flushing))
3436 gst_event_parse_new_segment_full (event, &update, NULL, NULL, NULL, NULL,
3439 if (G_UNLIKELY (basesink->priv->received_eos && !update)) {
3440 /* we can't accept anything when we are EOS */
3442 gst_event_unref (event);
3444 /* the new segment is a non prerollable item and does not block anything,
3445 * we need to configure the current clipping segment and insert the event
3446 * in the queue to serialize it with the buffers for rendering. */
3447 gst_base_sink_configure_segment (basesink, pad, event,
3448 basesink->abidata.ABI.clip_segment);
3451 gst_base_sink_queue_object_unlocked (basesink, pad,
3452 _PR_IS_EVENT, GST_MINI_OBJECT_CAST (event), FALSE);
3453 if (G_UNLIKELY (ret != GST_FLOW_OK))
3456 GST_OBJECT_LOCK (basesink);
3457 basesink->have_newsegment = TRUE;
3458 GST_OBJECT_UNLOCK (basesink);
3461 GST_PAD_PREROLL_UNLOCK (pad);
3464 case GST_EVENT_FLUSH_START:
3466 bclass->event (basesink, event);
3468 GST_DEBUG_OBJECT (basesink, "flush-start %p", event);
3470 gst_base_sink_flush_start (basesink, pad);
3472 gst_event_unref (event);
3474 case GST_EVENT_FLUSH_STOP:
3476 bclass->event (basesink, event);
3478 GST_DEBUG_OBJECT (basesink, "flush-stop %p", event);
3480 gst_base_sink_flush_stop (basesink, pad);
3482 gst_event_unref (event);
3485 /* other events are sent to queue or subclass depending on if they
3486 * are serialized. */
3487 if (GST_EVENT_IS_SERIALIZED (event)) {
3488 gst_base_sink_queue_object (basesink, pad,
3489 GST_MINI_OBJECT_CAST (event), FALSE);
3492 bclass->event (basesink, event);
3493 gst_event_unref (event);
3498 gst_object_unref (basesink);
3505 GST_DEBUG_OBJECT (basesink, "we are flushing");
3506 GST_PAD_PREROLL_UNLOCK (pad);
3508 gst_event_unref (event);
3513 /* default implementation to calculate the start and end
3514 * timestamps on a buffer, subclasses can override
3517 gst_base_sink_get_times (GstBaseSink * basesink, GstBuffer * buffer,
3518 GstClockTime * start, GstClockTime * end)
3520 GstClockTime timestamp, duration;
3522 timestamp = GST_BUFFER_TIMESTAMP (buffer);
3523 if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
3525 /* get duration to calculate end time */
3526 duration = GST_BUFFER_DURATION (buffer);
3527 if (GST_CLOCK_TIME_IS_VALID (duration)) {
3528 *end = timestamp + duration;
3534 /* must be called with PREROLL_LOCK */
3536 gst_base_sink_needs_preroll (GstBaseSink * basesink)
3538 gboolean is_prerolled, res;
3540 /* we have 2 cases where the PREROLL_LOCK is released:
3541 * 1) we are blocking in the PREROLL_LOCK and thus are prerolled.
3542 * 2) we are syncing on the clock
3544 is_prerolled = basesink->have_preroll || basesink->priv->received_eos;
3545 res = !is_prerolled;
3547 GST_DEBUG_OBJECT (basesink, "have_preroll: %d, EOS: %d => needs preroll: %d",
3548 basesink->have_preroll, basesink->priv->received_eos, res);
3553 /* with STREAM_LOCK, PREROLL_LOCK
3555 * Takes a buffer and compare the timestamps with the last segment.
3556 * If the buffer falls outside of the segment boundaries, drop it.
3557 * Else queue the buffer for preroll and rendering.
3559 * This function takes ownership of the buffer.
3561 static GstFlowReturn
3562 gst_base_sink_chain_unlocked (GstBaseSink * basesink, GstPad * pad,
3563 guint8 obj_type, gpointer obj)
3565 GstBaseSinkClass *bclass;
3566 GstFlowReturn result;
3567 GstClockTime start = GST_CLOCK_TIME_NONE, end = GST_CLOCK_TIME_NONE;
3568 GstSegment *clip_segment;
3569 GstBuffer *time_buf;
3571 if (G_UNLIKELY (basesink->flushing))
3574 if (G_UNLIKELY (basesink->priv->received_eos))
3577 if (OBJ_IS_BUFFERLIST (obj_type)) {
3578 time_buf = gst_buffer_list_get (GST_BUFFER_LIST_CAST (obj), 0, 0);
3579 g_assert (NULL != time_buf);
3581 time_buf = GST_BUFFER_CAST (obj);
3584 /* for code clarity */
3585 clip_segment = basesink->abidata.ABI.clip_segment;
3587 if (G_UNLIKELY (!basesink->have_newsegment)) {
3590 sync = gst_base_sink_get_sync (basesink);
3592 GST_ELEMENT_WARNING (basesink, STREAM, FAILED,
3593 (_("Internal data flow problem.")),
3594 ("Received buffer without a new-segment. Assuming timestamps start from 0."));
3597 /* this means this sink will assume timestamps start from 0 */
3598 GST_OBJECT_LOCK (basesink);
3599 clip_segment->start = 0;
3600 clip_segment->stop = -1;
3601 basesink->segment.start = 0;
3602 basesink->segment.stop = -1;
3603 basesink->have_newsegment = TRUE;
3604 GST_OBJECT_UNLOCK (basesink);
3607 bclass = GST_BASE_SINK_GET_CLASS (basesink);
3609 /* check if the buffer needs to be dropped, we first ask the subclass for the
3611 if (bclass->get_times)
3612 bclass->get_times (basesink, time_buf, &start, &end);
3614 if (!GST_CLOCK_TIME_IS_VALID (start)) {
3615 /* if the subclass does not want sync, we use our own values so that we at
3616 * least clip the buffer to the segment */
3617 gst_base_sink_get_times (basesink, time_buf, &start, &end);
3620 GST_DEBUG_OBJECT (basesink, "got times start: %" GST_TIME_FORMAT
3621 ", end: %" GST_TIME_FORMAT, GST_TIME_ARGS (start), GST_TIME_ARGS (end));
3623 /* a dropped buffer does not participate in anything */
3624 if (GST_CLOCK_TIME_IS_VALID (start) &&
3625 (clip_segment->format == GST_FORMAT_TIME)) {
3626 if (G_UNLIKELY (!gst_segment_clip (clip_segment,
3627 GST_FORMAT_TIME, (gint64) start, (gint64) end, NULL, NULL)))
3628 goto out_of_segment;
3631 /* now we can process the buffer in the queue, this function takes ownership
3633 result = gst_base_sink_queue_object_unlocked (basesink, pad,
3634 obj_type, obj, TRUE);
3640 GST_DEBUG_OBJECT (basesink, "sink is flushing");
3641 gst_mini_object_unref (GST_MINI_OBJECT_CAST (obj));
3642 return GST_FLOW_WRONG_STATE;
3646 GST_DEBUG_OBJECT (basesink,
3647 "we are EOS, dropping object, return UNEXPECTED");
3648 gst_mini_object_unref (GST_MINI_OBJECT_CAST (obj));
3649 return GST_FLOW_UNEXPECTED;
3653 GST_DEBUG_OBJECT (basesink, "dropping buffer, out of clipping segment");
3654 gst_mini_object_unref (GST_MINI_OBJECT_CAST (obj));
3661 static GstFlowReturn
3662 gst_base_sink_chain_main (GstBaseSink * basesink, GstPad * pad,
3663 guint8 obj_type, gpointer obj)
3665 GstFlowReturn result;
3667 if (G_UNLIKELY (basesink->pad_mode != GST_ACTIVATE_PUSH))
3670 GST_PAD_PREROLL_LOCK (pad);
3671 result = gst_base_sink_chain_unlocked (basesink, pad, obj_type, obj);
3672 GST_PAD_PREROLL_UNLOCK (pad);
3680 GST_OBJECT_LOCK (pad);
3681 GST_WARNING_OBJECT (basesink,
3682 "Push on pad %s:%s, but it was not activated in push mode",
3683 GST_DEBUG_PAD_NAME (pad));
3684 GST_OBJECT_UNLOCK (pad);
3685 gst_mini_object_unref (GST_MINI_OBJECT_CAST (obj));
3686 /* we don't post an error message this will signal to the peer
3687 * pushing that EOS is reached. */
3688 result = GST_FLOW_UNEXPECTED;
3693 static GstFlowReturn
3694 gst_base_sink_chain (GstPad * pad, GstBuffer * buf)
3696 GstBaseSink *basesink;
3698 basesink = GST_BASE_SINK (GST_OBJECT_PARENT (pad));
3700 return gst_base_sink_chain_main (basesink, pad, _PR_IS_BUFFER, buf);
3703 static GstFlowReturn
3704 gst_base_sink_chain_list (GstPad * pad, GstBufferList * list)
3706 GstBaseSink *basesink;
3707 GstBaseSinkClass *bclass;
3708 GstFlowReturn result;
3710 basesink = GST_BASE_SINK (GST_OBJECT_PARENT (pad));
3711 bclass = GST_BASE_SINK_GET_CLASS (basesink);
3713 if (G_LIKELY (bclass->render_list)) {
3714 result = gst_base_sink_chain_main (basesink, pad, _PR_IS_BUFFERLIST, list);
3716 GstBufferListIterator *it;
3719 GST_INFO_OBJECT (pad, "chaining each group in list as a merged buffer");
3721 it = gst_buffer_list_iterate (list);
3723 if (gst_buffer_list_iterator_next_group (it)) {
3725 group = gst_buffer_list_iterator_merge_group (it);
3726 if (group == NULL) {
3727 group = gst_buffer_new ();
3728 GST_CAT_INFO_OBJECT (GST_CAT_SCHEDULING, pad, "chaining empty group");
3730 GST_CAT_INFO_OBJECT (GST_CAT_SCHEDULING, pad, "chaining group");
3732 result = gst_base_sink_chain_main (basesink, pad, _PR_IS_BUFFER, group);
3733 } while (result == GST_FLOW_OK
3734 && gst_buffer_list_iterator_next_group (it));
3736 GST_CAT_INFO_OBJECT (GST_CAT_SCHEDULING, pad, "chaining empty group");
3738 gst_base_sink_chain_main (basesink, pad, _PR_IS_BUFFER,
3741 gst_buffer_list_iterator_free (it);
3742 gst_buffer_list_unref (list);
3749 gst_base_sink_default_do_seek (GstBaseSink * sink, GstSegment * segment)
3751 gboolean res = TRUE;
3753 /* update our offset if the start/stop position was updated */
3754 if (segment->format == GST_FORMAT_BYTES) {
3755 segment->time = segment->start;
3756 } else if (segment->start == 0) {
3757 /* seek to start, we can implement a default for this. */
3761 GST_INFO_OBJECT (sink, "Can't do a default seek");
3767 #define SEEK_TYPE_IS_RELATIVE(t) (((t) != GST_SEEK_TYPE_NONE) && ((t) != GST_SEEK_TYPE_SET))
3770 gst_base_sink_default_prepare_seek_segment (GstBaseSink * sink,
3771 GstEvent * event, GstSegment * segment)
3773 /* By default, we try one of 2 things:
3774 * - For absolute seek positions, convert the requested position to our
3775 * configured processing format and place it in the output segment \
3776 * - For relative seek positions, convert our current (input) values to the
3777 * seek format, adjust by the relative seek offset and then convert back to
3778 * the processing format
3780 GstSeekType cur_type, stop_type;
3783 GstFormat seek_format, dest_format;
3786 gboolean res = TRUE;
3788 gst_event_parse_seek (event, &rate, &seek_format, &flags,
3789 &cur_type, &cur, &stop_type, &stop);
3790 dest_format = segment->format;
3792 if (seek_format == dest_format) {
3793 gst_segment_set_seek (segment, rate, seek_format, flags,
3794 cur_type, cur, stop_type, stop, &update);
3798 if (cur_type != GST_SEEK_TYPE_NONE) {
3799 /* FIXME: Handle seek_cur & seek_end by converting the input segment vals */
3801 gst_pad_query_convert (sink->sinkpad, seek_format, cur, &dest_format,
3803 cur_type = GST_SEEK_TYPE_SET;
3806 if (res && stop_type != GST_SEEK_TYPE_NONE) {
3807 /* FIXME: Handle seek_cur & seek_end by converting the input segment vals */
3809 gst_pad_query_convert (sink->sinkpad, seek_format, stop, &dest_format,
3811 stop_type = GST_SEEK_TYPE_SET;
3814 /* And finally, configure our output segment in the desired format */
3815 gst_segment_set_seek (segment, rate, dest_format, flags, cur_type, cur,
3816 stop_type, stop, &update);
3825 GST_DEBUG_OBJECT (sink, "undefined format given, seek aborted.");
3830 /* perform a seek, only executed in pull mode */
3832 gst_base_sink_perform_seek (GstBaseSink * sink, GstPad * pad, GstEvent * event)
3836 GstFormat seek_format, dest_format;
3838 GstSeekType cur_type, stop_type;
3839 gboolean seekseg_configured = FALSE;
3841 gboolean update, res = TRUE;
3842 GstSegment seeksegment;
3844 dest_format = sink->segment.format;
3847 GST_DEBUG_OBJECT (sink, "performing seek with event %p", event);
3848 gst_event_parse_seek (event, &rate, &seek_format, &flags,
3849 &cur_type, &cur, &stop_type, &stop);
3851 flush = flags & GST_SEEK_FLAG_FLUSH;
3853 GST_DEBUG_OBJECT (sink, "performing seek without event");
3858 GST_DEBUG_OBJECT (sink, "flushing upstream");
3859 gst_pad_push_event (pad, gst_event_new_flush_start ());
3860 gst_base_sink_flush_start (sink, pad);
3862 GST_DEBUG_OBJECT (sink, "pausing pulling thread");
3865 GST_PAD_STREAM_LOCK (pad);
3867 /* If we configured the seeksegment above, don't overwrite it now. Otherwise
3868 * copy the current segment info into the temp segment that we can actually
3869 * attempt the seek with. We only update the real segment if the seek suceeds. */
3870 if (!seekseg_configured) {
3871 memcpy (&seeksegment, &sink->segment, sizeof (GstSegment));
3873 /* now configure the final seek segment */
3875 if (sink->segment.format != seek_format) {
3876 /* OK, here's where we give the subclass a chance to convert the relative
3877 * seek into an absolute one in the processing format. We set up any
3878 * absolute seek above, before taking the stream lock. */
3879 if (!gst_base_sink_default_prepare_seek_segment (sink, event,
3881 GST_DEBUG_OBJECT (sink,
3882 "Preparing the seek failed after flushing. " "Aborting seek");
3886 /* The seek format matches our processing format, no need to ask the
3887 * the subclass to configure the segment. */
3888 gst_segment_set_seek (&seeksegment, rate, seek_format, flags,
3889 cur_type, cur, stop_type, stop, &update);
3892 /* Else, no seek event passed, so we're just (re)starting the
3897 GST_DEBUG_OBJECT (sink, "segment configured from %" G_GINT64_FORMAT
3898 " to %" G_GINT64_FORMAT ", position %" G_GINT64_FORMAT,
3899 seeksegment.start, seeksegment.stop, seeksegment.last_stop);
3901 /* do the seek, segment.last_stop contains the new position. */
3902 res = gst_base_sink_default_do_seek (sink, &seeksegment);
3907 GST_DEBUG_OBJECT (sink, "stop flushing upstream");
3908 gst_pad_push_event (pad, gst_event_new_flush_stop ());
3909 gst_base_sink_flush_stop (sink, pad);
3910 } else if (res && sink->abidata.ABI.running) {
3911 /* we are running the current segment and doing a non-flushing seek,
3912 * close the segment first based on the last_stop. */
3913 GST_DEBUG_OBJECT (sink, "closing running segment %" G_GINT64_FORMAT
3914 " to %" G_GINT64_FORMAT, sink->segment.start, sink->segment.last_stop);
3917 /* The subclass must have converted the segment to the processing format
3919 if (res && seeksegment.format != dest_format) {
3920 GST_DEBUG_OBJECT (sink, "Subclass failed to prepare a seek segment "
3921 "in the correct format. Aborting seek.");
3925 /* if successfull seek, we update our real segment and push
3926 * out the new segment. */
3928 memcpy (&sink->segment, &seeksegment, sizeof (GstSegment));
3930 if (sink->segment.flags & GST_SEEK_FLAG_SEGMENT) {
3931 gst_element_post_message (GST_ELEMENT (sink),
3932 gst_message_new_segment_start (GST_OBJECT (sink),
3933 sink->segment.format, sink->segment.last_stop));
3937 sink->priv->discont = TRUE;
3938 sink->abidata.ABI.running = TRUE;
3940 GST_PAD_STREAM_UNLOCK (pad);
3946 set_step_info (GstBaseSink * sink, GstStepInfo * current, GstStepInfo * pending,
3947 guint seqnum, GstFormat format, guint64 amount, gdouble rate,
3948 gboolean flush, gboolean intermediate)
3950 GST_OBJECT_LOCK (sink);
3951 pending->seqnum = seqnum;
3952 pending->format = format;
3953 pending->amount = amount;
3954 pending->position = 0;
3955 pending->rate = rate;
3956 pending->flush = flush;
3957 pending->intermediate = intermediate;
3958 pending->valid = TRUE;
3959 /* flush invalidates the current stepping segment */
3961 current->valid = FALSE;
3962 GST_OBJECT_UNLOCK (sink);
3966 gst_base_sink_perform_step (GstBaseSink * sink, GstPad * pad, GstEvent * event)
3968 GstBaseSinkPrivate *priv;
3969 GstBaseSinkClass *bclass;
3970 gboolean flush, intermediate;
3975 GstStepInfo *pending, *current;
3976 GstMessage *message;
3978 bclass = GST_BASE_SINK_GET_CLASS (sink);
3981 GST_DEBUG_OBJECT (sink, "performing step with event %p", event);
3983 gst_event_parse_step (event, &format, &amount, &rate, &flush, &intermediate);
3984 seqnum = gst_event_get_seqnum (event);
3986 pending = &priv->pending_step;
3987 current = &priv->current_step;
3989 /* post message first */
3990 message = gst_message_new_step_start (GST_OBJECT (sink), FALSE, format,
3991 amount, rate, flush, intermediate);
3992 gst_message_set_seqnum (message, seqnum);
3993 gst_element_post_message (GST_ELEMENT (sink), message);
3996 /* we need to call ::unlock before locking PREROLL_LOCK
3997 * since we lock it before going into ::render */
3999 bclass->unlock (sink);
4001 GST_PAD_PREROLL_LOCK (sink->sinkpad);
4002 /* now that we have the PREROLL lock, clear our unlock request */
4003 if (bclass->unlock_stop)
4004 bclass->unlock_stop (sink);
4006 /* update the stepinfo and make it valid */
4007 set_step_info (sink, current, pending, seqnum, format, amount, rate, flush,
4010 if (sink->priv->async_enabled) {
4011 /* and we need to commit our state again on the next
4012 * prerolled buffer */
4013 sink->playing_async = TRUE;
4014 priv->pending_step.need_preroll = TRUE;
4015 sink->need_preroll = FALSE;
4016 gst_element_lost_state_full (GST_ELEMENT_CAST (sink), FALSE);
4018 sink->priv->have_latency = TRUE;
4019 sink->need_preroll = FALSE;
4021 priv->current_sstart = GST_CLOCK_TIME_NONE;
4022 priv->current_sstop = GST_CLOCK_TIME_NONE;
4023 priv->eos_rtime = GST_CLOCK_TIME_NONE;
4024 priv->call_preroll = TRUE;
4025 gst_base_sink_set_last_buffer (sink, NULL);
4026 gst_base_sink_reset_qos (sink);
4028 if (sink->clock_id) {
4029 gst_clock_id_unschedule (sink->clock_id);
4032 if (sink->have_preroll) {
4033 GST_DEBUG_OBJECT (sink, "signal waiter");
4034 priv->step_unlock = TRUE;
4035 GST_PAD_PREROLL_SIGNAL (sink->sinkpad);
4037 GST_PAD_PREROLL_UNLOCK (sink->sinkpad);
4039 /* update the stepinfo and make it valid */
4040 set_step_info (sink, current, pending, seqnum, format, amount, rate, flush,
4050 gst_base_sink_loop (GstPad * pad)
4052 GstBaseSink *basesink;
4053 GstBuffer *buf = NULL;
4054 GstFlowReturn result;
4058 basesink = GST_BASE_SINK (GST_OBJECT_PARENT (pad));
4060 g_assert (basesink->pad_mode == GST_ACTIVATE_PULL);
4062 if ((blocksize = basesink->priv->blocksize) == 0)
4065 offset = basesink->segment.last_stop;
4067 GST_DEBUG_OBJECT (basesink, "pulling %" G_GUINT64_FORMAT ", %u",
4070 result = gst_pad_pull_range (pad, offset, blocksize, &buf);
4071 if (G_UNLIKELY (result != GST_FLOW_OK))
4074 if (G_UNLIKELY (buf == NULL))
4077 offset += GST_BUFFER_SIZE (buf);
4079 gst_segment_set_last_stop (&basesink->segment, GST_FORMAT_BYTES, offset);
4081 GST_PAD_PREROLL_LOCK (pad);
4082 result = gst_base_sink_chain_unlocked (basesink, pad, _PR_IS_BUFFER, buf);
4083 GST_PAD_PREROLL_UNLOCK (pad);
4084 if (G_UNLIKELY (result != GST_FLOW_OK))
4092 GST_LOG_OBJECT (basesink, "pausing task, reason %s",
4093 gst_flow_get_name (result));
4094 gst_pad_pause_task (pad);
4095 if (result == GST_FLOW_UNEXPECTED) {
4096 /* perform EOS logic */
4097 if (basesink->segment.flags & GST_SEEK_FLAG_SEGMENT) {
4098 gst_element_post_message (GST_ELEMENT_CAST (basesink),
4099 gst_message_new_segment_done (GST_OBJECT_CAST (basesink),
4100 basesink->segment.format, basesink->segment.last_stop));
4102 gst_base_sink_event (pad, gst_event_new_eos ());
4104 } else if (result == GST_FLOW_NOT_LINKED || result <= GST_FLOW_UNEXPECTED) {
4105 /* for fatal errors we post an error message, post the error
4106 * first so the app knows about the error first.
4107 * wrong-state is not a fatal error because it happens due to
4108 * flushing and posting an error message in that case is the
4109 * wrong thing to do, e.g. when basesrc is doing a flushing
4111 GST_ELEMENT_ERROR (basesink, STREAM, FAILED,
4112 (_("Internal data stream error.")),
4113 ("stream stopped, reason %s", gst_flow_get_name (result)));
4114 gst_base_sink_event (pad, gst_event_new_eos ());
4120 GST_LOG_OBJECT (basesink, "no buffer, pausing");
4121 GST_ELEMENT_ERROR (basesink, STREAM, FAILED,
4122 (_("Internal data flow error.")), ("element returned NULL buffer"));
4123 result = GST_FLOW_ERROR;
4129 gst_base_sink_set_flushing (GstBaseSink * basesink, GstPad * pad,
4132 GstBaseSinkClass *bclass;
4134 bclass = GST_BASE_SINK_GET_CLASS (basesink);
4137 /* unlock any subclasses, we need to do this before grabbing the
4138 * PREROLL_LOCK since we hold this lock before going into ::render. */
4140 bclass->unlock (basesink);
4143 GST_PAD_PREROLL_LOCK (pad);
4144 basesink->flushing = flushing;
4146 /* step 1, now that we have the PREROLL lock, clear our unlock request */
4147 if (bclass->unlock_stop)
4148 bclass->unlock_stop (basesink);
4150 /* set need_preroll before we unblock the clock. If the clock is unblocked
4151 * before timing out, we can reuse the buffer for preroll. */
4152 basesink->need_preroll = TRUE;
4154 /* step 2, unblock clock sync (if any) or any other blocking thing */
4155 if (basesink->clock_id) {
4156 gst_clock_id_unschedule (basesink->clock_id);
4159 /* flush out the data thread if it's locked in finish_preroll, this will
4160 * also flush out the EOS state */
4161 GST_DEBUG_OBJECT (basesink,
4162 "flushing out data thread, need preroll to TRUE");
4163 gst_base_sink_preroll_queue_flush (basesink, pad);
4165 GST_PAD_PREROLL_UNLOCK (pad);
4171 gst_base_sink_default_activate_pull (GstBaseSink * basesink, gboolean active)
4177 result = gst_pad_start_task (basesink->sinkpad,
4178 (GstTaskFunction) gst_base_sink_loop, basesink->sinkpad);
4180 /* step 2, make sure streaming finishes */
4181 result = gst_pad_stop_task (basesink->sinkpad);
4188 gst_base_sink_pad_activate (GstPad * pad)
4190 gboolean result = FALSE;
4191 GstBaseSink *basesink;
4193 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
4195 GST_DEBUG_OBJECT (basesink, "Trying pull mode first");
4197 gst_base_sink_set_flushing (basesink, pad, FALSE);
4199 /* we need to have the pull mode enabled */
4200 if (!basesink->can_activate_pull) {
4201 GST_DEBUG_OBJECT (basesink, "pull mode disabled");
4205 /* check if downstreams supports pull mode at all */
4206 if (!gst_pad_check_pull_range (pad)) {
4207 GST_DEBUG_OBJECT (basesink, "pull mode not supported");
4211 /* set the pad mode before starting the task so that it's in the
4212 * correct state for the new thread. also the sink set_caps and get_caps
4213 * function checks this */
4214 basesink->pad_mode = GST_ACTIVATE_PULL;
4216 /* we first try to negotiate a format so that when we try to activate
4217 * downstream, it knows about our format */
4218 if (!gst_base_sink_negotiate_pull (basesink)) {
4219 GST_DEBUG_OBJECT (basesink, "failed to negotiate in pull mode");
4223 /* ok activate now */
4224 if (!gst_pad_activate_pull (pad, TRUE)) {
4225 /* clear any pending caps */
4226 GST_OBJECT_LOCK (basesink);
4227 gst_caps_replace (&basesink->priv->pull_caps, NULL);
4228 GST_OBJECT_UNLOCK (basesink);
4229 GST_DEBUG_OBJECT (basesink, "failed to activate in pull mode");
4233 GST_DEBUG_OBJECT (basesink, "Success activating pull mode");
4237 /* push mode fallback */
4239 GST_DEBUG_OBJECT (basesink, "Falling back to push mode");
4240 if ((result = gst_pad_activate_push (pad, TRUE))) {
4241 GST_DEBUG_OBJECT (basesink, "Success activating push mode");
4246 GST_WARNING_OBJECT (basesink, "Could not activate pad in either mode");
4247 gst_base_sink_set_flushing (basesink, pad, TRUE);
4250 gst_object_unref (basesink);
4256 gst_base_sink_pad_activate_push (GstPad * pad, gboolean active)
4259 GstBaseSink *basesink;
4261 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
4264 if (!basesink->can_activate_push) {
4266 basesink->pad_mode = GST_ACTIVATE_NONE;
4269 basesink->pad_mode = GST_ACTIVATE_PUSH;
4272 if (G_UNLIKELY (basesink->pad_mode != GST_ACTIVATE_PUSH)) {
4273 g_warning ("Internal GStreamer activation error!!!");
4276 gst_base_sink_set_flushing (basesink, pad, TRUE);
4278 basesink->pad_mode = GST_ACTIVATE_NONE;
4282 gst_object_unref (basesink);
4288 gst_base_sink_negotiate_pull (GstBaseSink * basesink)
4295 /* this returns the intersection between our caps and the peer caps. If there
4296 * is no peer, it returns NULL and we can't operate in pull mode so we can
4297 * fail the negotiation. */
4298 caps = gst_pad_get_allowed_caps (GST_BASE_SINK_PAD (basesink));
4299 if (caps == NULL || gst_caps_is_empty (caps))
4300 goto no_caps_possible;
4302 GST_DEBUG_OBJECT (basesink, "allowed caps: %" GST_PTR_FORMAT, caps);
4304 caps = gst_caps_make_writable (caps);
4305 /* get the first (prefered) format */
4306 gst_caps_truncate (caps);
4308 gst_pad_fixate_caps (GST_BASE_SINK_PAD (basesink), caps);
4310 GST_DEBUG_OBJECT (basesink, "fixated to: %" GST_PTR_FORMAT, caps);
4312 if (gst_caps_is_any (caps)) {
4313 GST_DEBUG_OBJECT (basesink, "caps were ANY after fixating, "
4315 /* neither side has template caps in this case, so they are prepared for
4316 pull() without setcaps() */
4318 } else if (gst_caps_is_fixed (caps)) {
4319 if (!gst_pad_set_caps (GST_BASE_SINK_PAD (basesink), caps))
4320 goto could_not_set_caps;
4322 GST_OBJECT_LOCK (basesink);
4323 gst_caps_replace (&basesink->priv->pull_caps, caps);
4324 GST_OBJECT_UNLOCK (basesink);
4329 gst_caps_unref (caps);
4335 GST_INFO_OBJECT (basesink, "Pipeline could not agree on caps");
4336 GST_DEBUG_OBJECT (basesink, "get_allowed_caps() returned EMPTY");
4338 gst_caps_unref (caps);
4343 GST_INFO_OBJECT (basesink, "Could not set caps: %" GST_PTR_FORMAT, caps);
4344 gst_caps_unref (caps);
4349 /* this won't get called until we implement an activate function */
4351 gst_base_sink_pad_activate_pull (GstPad * pad, gboolean active)
4353 gboolean result = FALSE;
4354 GstBaseSink *basesink;
4355 GstBaseSinkClass *bclass;
4357 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
4358 bclass = GST_BASE_SINK_GET_CLASS (basesink);
4364 /* we mark we have a newsegment here because pull based
4365 * mode works just fine without having a newsegment before the
4367 format = GST_FORMAT_BYTES;
4369 gst_segment_init (&basesink->segment, format);
4370 gst_segment_init (basesink->abidata.ABI.clip_segment, format);
4371 GST_OBJECT_LOCK (basesink);
4372 basesink->have_newsegment = TRUE;
4373 GST_OBJECT_UNLOCK (basesink);
4375 /* get the peer duration in bytes */
4376 result = gst_pad_query_peer_duration (pad, &format, &duration);
4378 GST_DEBUG_OBJECT (basesink,
4379 "setting duration in bytes to %" G_GINT64_FORMAT, duration);
4380 gst_segment_set_duration (basesink->abidata.ABI.clip_segment, format,
4382 gst_segment_set_duration (&basesink->segment, format, duration);
4384 GST_DEBUG_OBJECT (basesink, "unknown duration");
4387 if (bclass->activate_pull)
4388 result = bclass->activate_pull (basesink, TRUE);
4393 goto activate_failed;
4396 if (G_UNLIKELY (basesink->pad_mode != GST_ACTIVATE_PULL)) {
4397 g_warning ("Internal GStreamer activation error!!!");
4400 result = gst_base_sink_set_flushing (basesink, pad, TRUE);
4401 if (bclass->activate_pull)
4402 result &= bclass->activate_pull (basesink, FALSE);
4403 basesink->pad_mode = GST_ACTIVATE_NONE;
4404 /* clear any pending caps */
4405 GST_OBJECT_LOCK (basesink);
4406 gst_caps_replace (&basesink->priv->pull_caps, NULL);
4407 GST_OBJECT_UNLOCK (basesink);
4410 gst_object_unref (basesink);
4417 /* reset, as starting the thread failed */
4418 basesink->pad_mode = GST_ACTIVATE_NONE;
4420 GST_ERROR_OBJECT (basesink, "subclass failed to activate in pull mode");
4425 /* send an event to our sinkpad peer. */
4427 gst_base_sink_send_event (GstElement * element, GstEvent * event)
4430 GstBaseSink *basesink = GST_BASE_SINK (element);
4431 gboolean forward, result = TRUE;
4432 GstActivateMode mode;
4434 GST_OBJECT_LOCK (element);
4435 /* get the pad and the scheduling mode */
4436 pad = gst_object_ref (basesink->sinkpad);
4437 mode = basesink->pad_mode;
4438 GST_OBJECT_UNLOCK (element);
4440 /* only push UPSTREAM events upstream */
4441 forward = GST_EVENT_IS_UPSTREAM (event);
4443 GST_DEBUG_OBJECT (basesink, "handling event %p %" GST_PTR_FORMAT, event,
4446 switch (GST_EVENT_TYPE (event)) {
4447 case GST_EVENT_LATENCY:
4449 GstClockTime latency;
4451 gst_event_parse_latency (event, &latency);
4453 /* store the latency. We use this to adjust the running_time before syncing
4454 * it to the clock. */
4455 GST_OBJECT_LOCK (element);
4456 basesink->priv->latency = latency;
4457 if (!basesink->priv->have_latency)
4459 GST_OBJECT_UNLOCK (element);
4460 GST_DEBUG_OBJECT (basesink, "latency set to %" GST_TIME_FORMAT,
4461 GST_TIME_ARGS (latency));
4463 /* We forward this event so that all elements know about the global pipeline
4464 * latency. This is interesting for an element when it wants to figure out
4465 * when a particular piece of data will be rendered. */
4468 case GST_EVENT_SEEK:
4469 /* in pull mode we will execute the seek */
4470 if (mode == GST_ACTIVATE_PULL)
4471 result = gst_base_sink_perform_seek (basesink, pad, event);
4473 case GST_EVENT_STEP:
4474 result = gst_base_sink_perform_step (basesink, pad, event);
4482 result = gst_pad_push_event (pad, event);
4484 /* not forwarded, unref the event */
4485 gst_event_unref (event);
4488 gst_object_unref (pad);
4493 gst_base_sink_get_position (GstBaseSink * basesink, GstFormat format,
4494 gint64 * cur, gboolean * upstream)
4496 GstClock *clock = NULL;
4497 gboolean res = FALSE;
4498 GstFormat oformat, tformat;
4499 GstSegment *segment;
4500 GstClockTime now, latency;
4501 GstClockTimeDiff base;
4502 gint64 time, accum, duration;
4505 gboolean last_seen, with_clock, in_paused;
4507 GST_OBJECT_LOCK (basesink);
4508 /* we can only get the segment when we are not NULL or READY */
4509 if (!basesink->have_newsegment)
4513 /* when not in PLAYING or when we're busy with a state change, we
4514 * cannot read from the clock so we report time based on the
4515 * last seen timestamp. */
4516 if (GST_STATE (basesink) != GST_STATE_PLAYING ||
4517 GST_STATE_PENDING (basesink) != GST_STATE_VOID_PENDING) {
4521 /* we don't use the clip segment in pull mode, when seeking we update the
4522 * main segment directly with the new segment values without it having to be
4523 * activated by the rendering after preroll */
4524 if (basesink->pad_mode == GST_ACTIVATE_PUSH)
4525 segment = basesink->abidata.ABI.clip_segment;
4527 segment = &basesink->segment;
4529 /* our intermediate time format */
4530 tformat = GST_FORMAT_TIME;
4531 /* get the format in the segment */
4532 oformat = segment->format;
4534 /* report with last seen position when EOS */
4535 last_seen = basesink->eos;
4537 /* assume we will use the clock for getting the current position */
4539 if (basesink->sync == FALSE)
4542 /* and we need a clock */
4543 if (G_UNLIKELY ((clock = GST_ELEMENT_CLOCK (basesink)) == NULL))
4546 gst_object_ref (clock);
4548 /* collect all data we need holding the lock */
4549 if (GST_CLOCK_TIME_IS_VALID (segment->time))
4550 time = segment->time;
4554 if (GST_CLOCK_TIME_IS_VALID (segment->stop))
4555 duration = segment->stop - segment->start;
4559 accum = segment->accum;
4560 rate = segment->rate * segment->applied_rate;
4561 latency = basesink->priv->latency;
4563 if (oformat == GST_FORMAT_TIME) {
4566 start = basesink->priv->current_sstart;
4567 stop = basesink->priv->current_sstop;
4570 /* in paused we use the last position as a lower bound */
4571 if (stop == -1 || segment->rate > 0.0)
4576 /* in playing, use last stop time as upper bound */
4577 if (start == -1 || segment->rate > 0.0)
4583 /* convert last stop to stream time */
4584 last = gst_segment_to_stream_time (segment, oformat, segment->last_stop);
4588 /* in paused, use start_time */
4589 base = GST_ELEMENT_START_TIME (basesink);
4590 GST_DEBUG_OBJECT (basesink, "in paused, using start time %" GST_TIME_FORMAT,
4591 GST_TIME_ARGS (base));
4592 } else if (with_clock) {
4593 /* else use clock when needed */
4594 base = GST_ELEMENT_CAST (basesink)->base_time;
4595 GST_DEBUG_OBJECT (basesink, "using clock and base time %" GST_TIME_FORMAT,
4596 GST_TIME_ARGS (base));
4598 /* else, no sync or clock -> no base time */
4599 GST_DEBUG_OBJECT (basesink, "no sync or no clock");
4603 /* no base, we can't calculate running_time, use last seem timestamp to report
4608 /* need to release the object lock before we can get the time,
4609 * a clock might take the LOCK of the provider, which could be
4610 * a basesink subclass. */
4611 GST_OBJECT_UNLOCK (basesink);
4614 /* in EOS or when no valid stream_time, report the value of last seen
4617 /* no timestamp, we need to ask upstream */
4618 GST_DEBUG_OBJECT (basesink, "no last seen timestamp, asking upstream");
4623 GST_DEBUG_OBJECT (basesink, "using last seen timestamp %" GST_TIME_FORMAT,
4624 GST_TIME_ARGS (last));
4627 if (oformat != tformat) {
4628 /* convert accum, time and duration to time */
4629 if (!gst_pad_query_convert (basesink->sinkpad, oformat, accum, &tformat,
4631 goto convert_failed;
4632 if (!gst_pad_query_convert (basesink->sinkpad, oformat, duration,
4633 &tformat, &duration))
4634 goto convert_failed;
4635 if (!gst_pad_query_convert (basesink->sinkpad, oformat, time, &tformat,
4637 goto convert_failed;
4638 if (!gst_pad_query_convert (basesink->sinkpad, oformat, last, &tformat,
4640 goto convert_failed;
4642 /* assume time format from now on */
4646 if (!in_paused && with_clock) {
4647 now = gst_clock_get_time (clock);
4653 /* subtract base time and accumulated time from the clock time.
4654 * Make sure we don't go negative. This is the current time in
4655 * the segment which we need to scale with the combined
4656 * rate and applied rate. */
4659 if (GST_CLOCK_DIFF (base, now) < 0)
4662 /* for negative rates we need to count back from the segment
4667 *cur = time + gst_guint64_to_gdouble (now - base) * rate;
4670 /* never report less than segment values in paused */
4672 *cur = MAX (last, *cur);
4674 /* never report more than last seen position in playing */
4676 *cur = MIN (last, *cur);
4679 GST_DEBUG_OBJECT (basesink,
4680 "now %" GST_TIME_FORMAT " - base %" GST_TIME_FORMAT " - accum %"
4681 GST_TIME_FORMAT " + time %" GST_TIME_FORMAT " last %" GST_TIME_FORMAT,
4682 GST_TIME_ARGS (now), GST_TIME_ARGS (base), GST_TIME_ARGS (accum),
4683 GST_TIME_ARGS (time), GST_TIME_ARGS (last));
4686 if (oformat != format) {
4687 /* convert to final format */
4688 if (!gst_pad_query_convert (basesink->sinkpad, oformat, *cur, &format, cur))
4689 goto convert_failed;
4695 GST_DEBUG_OBJECT (basesink, "res: %d, POSITION: %" GST_TIME_FORMAT,
4696 res, GST_TIME_ARGS (*cur));
4699 gst_object_unref (clock);
4706 /* in NULL or READY we always return FALSE and -1 */
4707 GST_DEBUG_OBJECT (basesink, "position in wrong state, return -1");
4710 GST_OBJECT_UNLOCK (basesink);
4715 GST_DEBUG_OBJECT (basesink, "convert failed, try upstream");
4723 gst_base_sink_get_duration (GstBaseSink * basesink, GstFormat format,
4724 gint64 * dur, gboolean * upstream)
4726 gboolean res = FALSE;
4728 if (basesink->pad_mode == GST_ACTIVATE_PULL) {
4729 GstFormat uformat = GST_FORMAT_BYTES;
4732 /* get the duration in bytes, in pull mode that's all we are sure to
4733 * know. We have to explicitly get this value from upstream instead of
4734 * using our cached value because it might change. Duration caching
4735 * should be done at a higher level. */
4736 res = gst_pad_query_peer_duration (basesink->sinkpad, &uformat, &uduration);
4738 gst_segment_set_duration (&basesink->segment, uformat, uduration);
4739 if (format != uformat) {
4740 /* convert to the requested format */
4741 res = gst_pad_query_convert (basesink->sinkpad, uformat, uduration,
4755 static const GstQueryType *
4756 gst_base_sink_get_query_types (GstElement * element)
4758 static const GstQueryType query_types[] = {
4770 gst_base_sink_query (GstElement * element, GstQuery * query)
4772 gboolean res = FALSE;
4774 GstBaseSink *basesink = GST_BASE_SINK (element);
4776 switch (GST_QUERY_TYPE (query)) {
4777 case GST_QUERY_POSITION:
4781 gboolean upstream = FALSE;
4783 gst_query_parse_position (query, &format, NULL);
4785 GST_DEBUG_OBJECT (basesink, "position query in format %s",
4786 gst_format_get_name (format));
4788 /* first try to get the position based on the clock */
4790 gst_base_sink_get_position (basesink, format, &cur, &upstream))) {
4791 gst_query_set_position (query, format, cur);
4792 } else if (upstream) {
4793 /* fallback to peer query */
4794 res = gst_pad_peer_query (basesink->sinkpad, query);
4797 /* we can handle a few things if upstream failed */
4798 if (format == GST_FORMAT_PERCENT) {
4800 GstFormat uformat = GST_FORMAT_TIME;
4802 res = gst_base_sink_get_position (basesink, GST_FORMAT_TIME, &cur,
4804 if (!res && upstream) {
4805 res = gst_pad_query_peer_position (basesink->sinkpad, &uformat,
4809 res = gst_base_sink_get_duration (basesink, GST_FORMAT_TIME, &dur,
4811 if (!res && upstream) {
4812 res = gst_pad_query_peer_duration (basesink->sinkpad, &uformat,
4819 pos = gst_util_uint64_scale (100 * GST_FORMAT_PERCENT_SCALE, cur,
4821 gst_query_set_position (query, GST_FORMAT_PERCENT, pos);
4827 case GST_QUERY_DURATION:
4831 gboolean upstream = FALSE;
4833 gst_query_parse_duration (query, &format, NULL);
4835 GST_DEBUG_OBJECT (basesink, "duration query in format %s",
4836 gst_format_get_name (format));
4839 gst_base_sink_get_duration (basesink, format, &dur, &upstream))) {
4840 gst_query_set_duration (query, format, dur);
4841 } else if (upstream) {
4842 /* fallback to peer query */
4843 res = gst_pad_peer_query (basesink->sinkpad, query);
4846 /* we can handle a few things if upstream failed */
4847 if (format == GST_FORMAT_PERCENT) {
4848 gst_query_set_duration (query, GST_FORMAT_PERCENT,
4849 GST_FORMAT_PERCENT_MAX);
4855 case GST_QUERY_LATENCY:
4857 gboolean live, us_live;
4858 GstClockTime min, max;
4860 if ((res = gst_base_sink_query_latency (basesink, &live, &us_live, &min,
4862 gst_query_set_latency (query, live, min, max);
4866 case GST_QUERY_JITTER:
4868 case GST_QUERY_RATE:
4869 /* gst_query_set_rate (query, basesink->segment_rate); */
4872 case GST_QUERY_SEGMENT:
4874 if (basesink->pad_mode == GST_ACTIVATE_PULL) {
4875 gst_query_set_segment (query, basesink->segment.rate,
4876 GST_FORMAT_TIME, basesink->segment.start, basesink->segment.stop);
4879 res = gst_pad_peer_query (basesink->sinkpad, query);
4883 case GST_QUERY_SEEKING:
4884 case GST_QUERY_CONVERT:
4885 case GST_QUERY_FORMATS:
4887 res = gst_pad_peer_query (basesink->sinkpad, query);
4890 GST_DEBUG_OBJECT (basesink, "query %s returns %d",
4891 GST_QUERY_TYPE_NAME (query), res);
4895 static GstStateChangeReturn
4896 gst_base_sink_change_state (GstElement * element, GstStateChange transition)
4898 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
4899 GstBaseSink *basesink = GST_BASE_SINK (element);
4900 GstBaseSinkClass *bclass;
4901 GstBaseSinkPrivate *priv;
4903 priv = basesink->priv;
4905 bclass = GST_BASE_SINK_GET_CLASS (basesink);
4907 switch (transition) {
4908 case GST_STATE_CHANGE_NULL_TO_READY:
4910 if (!bclass->start (basesink))
4913 case GST_STATE_CHANGE_READY_TO_PAUSED:
4914 /* need to complete preroll before this state change completes, there
4915 * is no data flow in READY so we can safely assume we need to preroll. */
4916 GST_PAD_PREROLL_LOCK (basesink->sinkpad);
4917 GST_DEBUG_OBJECT (basesink, "READY to PAUSED");
4918 basesink->have_newsegment = FALSE;
4919 gst_segment_init (&basesink->segment, GST_FORMAT_UNDEFINED);
4920 gst_segment_init (basesink->abidata.ABI.clip_segment,
4921 GST_FORMAT_UNDEFINED);
4922 basesink->offset = 0;
4923 basesink->have_preroll = FALSE;
4924 priv->step_unlock = FALSE;
4925 basesink->need_preroll = TRUE;
4926 basesink->playing_async = TRUE;
4927 priv->current_sstart = GST_CLOCK_TIME_NONE;
4928 priv->current_sstop = GST_CLOCK_TIME_NONE;
4929 priv->eos_rtime = GST_CLOCK_TIME_NONE;
4931 basesink->eos = FALSE;
4932 priv->received_eos = FALSE;
4933 gst_base_sink_reset_qos (basesink);
4934 priv->commited = FALSE;
4935 priv->call_preroll = TRUE;
4936 priv->current_step.valid = FALSE;
4937 priv->pending_step.valid = FALSE;
4938 if (priv->async_enabled) {
4939 GST_DEBUG_OBJECT (basesink, "doing async state change");
4940 /* when async enabled, post async-start message and return ASYNC from
4941 * the state change function */
4942 ret = GST_STATE_CHANGE_ASYNC;
4943 gst_element_post_message (GST_ELEMENT_CAST (basesink),
4944 gst_message_new_async_start (GST_OBJECT_CAST (basesink), FALSE));
4946 priv->have_latency = TRUE;
4948 GST_PAD_PREROLL_UNLOCK (basesink->sinkpad);
4950 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
4951 GST_PAD_PREROLL_LOCK (basesink->sinkpad);
4952 if (!gst_base_sink_needs_preroll (basesink)) {
4953 GST_DEBUG_OBJECT (basesink, "PAUSED to PLAYING, don't need preroll");
4954 /* no preroll needed anymore now. */
4955 basesink->playing_async = FALSE;
4956 basesink->need_preroll = FALSE;
4957 if (basesink->eos) {
4958 GstMessage *message;
4960 /* need to post EOS message here */
4961 GST_DEBUG_OBJECT (basesink, "Now posting EOS");
4962 message = gst_message_new_eos (GST_OBJECT_CAST (basesink));
4963 gst_message_set_seqnum (message, basesink->priv->seqnum);
4964 gst_element_post_message (GST_ELEMENT_CAST (basesink), message);
4966 GST_DEBUG_OBJECT (basesink, "signal preroll");
4967 GST_PAD_PREROLL_SIGNAL (basesink->sinkpad);
4970 GST_DEBUG_OBJECT (basesink, "PAUSED to PLAYING, we are not prerolled");
4971 basesink->need_preroll = TRUE;
4972 basesink->playing_async = TRUE;
4973 priv->call_preroll = TRUE;
4974 priv->commited = FALSE;
4975 if (priv->async_enabled) {
4976 GST_DEBUG_OBJECT (basesink, "doing async state change");
4977 ret = GST_STATE_CHANGE_ASYNC;
4978 gst_element_post_message (GST_ELEMENT_CAST (basesink),
4979 gst_message_new_async_start (GST_OBJECT_CAST (basesink), FALSE));
4982 GST_PAD_PREROLL_UNLOCK (basesink->sinkpad);
4989 GstStateChangeReturn bret;
4991 bret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
4992 if (G_UNLIKELY (bret == GST_STATE_CHANGE_FAILURE))
4993 goto activate_failed;
4996 switch (transition) {
4997 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
4998 GST_DEBUG_OBJECT (basesink, "PLAYING to PAUSED");
4999 /* FIXME, make sure we cannot enter _render first */
5001 /* we need to call ::unlock before locking PREROLL_LOCK
5002 * since we lock it before going into ::render */
5004 bclass->unlock (basesink);
5006 GST_PAD_PREROLL_LOCK (basesink->sinkpad);
5007 GST_DEBUG_OBJECT (basesink, "got preroll lock");
5008 /* now that we have the PREROLL lock, clear our unlock request */
5009 if (bclass->unlock_stop)
5010 bclass->unlock_stop (basesink);
5012 /* we need preroll again and we set the flag before unlocking the clockid
5013 * because if the clockid is unlocked before a current buffer expired, we
5014 * can use that buffer to preroll with */
5015 basesink->need_preroll = TRUE;
5017 if (basesink->clock_id) {
5018 GST_DEBUG_OBJECT (basesink, "unschedule clock");
5019 gst_clock_id_unschedule (basesink->clock_id);
5022 /* if we don't have a preroll buffer we need to wait for a preroll and
5024 if (!gst_base_sink_needs_preroll (basesink)) {
5025 GST_DEBUG_OBJECT (basesink, "PLAYING to PAUSED, we are prerolled");
5026 basesink->playing_async = FALSE;
5028 if (GST_STATE_TARGET (GST_ELEMENT (basesink)) <= GST_STATE_READY) {
5029 GST_DEBUG_OBJECT (basesink, "element is <= READY");
5030 ret = GST_STATE_CHANGE_SUCCESS;
5032 GST_DEBUG_OBJECT (basesink,
5033 "PLAYING to PAUSED, we are not prerolled");
5034 basesink->playing_async = TRUE;
5035 priv->commited = FALSE;
5036 priv->call_preroll = TRUE;
5037 if (priv->async_enabled) {
5038 GST_DEBUG_OBJECT (basesink, "doing async state change");
5039 ret = GST_STATE_CHANGE_ASYNC;
5040 gst_element_post_message (GST_ELEMENT_CAST (basesink),
5041 gst_message_new_async_start (GST_OBJECT_CAST (basesink),
5046 GST_DEBUG_OBJECT (basesink, "rendered: %" G_GUINT64_FORMAT
5047 ", dropped: %" G_GUINT64_FORMAT, priv->rendered, priv->dropped);
5049 gst_base_sink_reset_qos (basesink);
5050 GST_PAD_PREROLL_UNLOCK (basesink->sinkpad);
5052 case GST_STATE_CHANGE_PAUSED_TO_READY:
5053 GST_PAD_PREROLL_LOCK (basesink->sinkpad);
5054 /* start by reseting our position state with the object lock so that the
5055 * position query gets the right idea. We do this before we post the
5056 * messages so that the message handlers pick this up. */
5057 GST_OBJECT_LOCK (basesink);
5058 basesink->have_newsegment = FALSE;
5059 priv->current_sstart = GST_CLOCK_TIME_NONE;
5060 priv->current_sstop = GST_CLOCK_TIME_NONE;
5061 priv->have_latency = FALSE;
5062 if (priv->cached_clock_id) {
5063 gst_clock_id_unref (priv->cached_clock_id);
5064 priv->cached_clock_id = NULL;
5066 GST_OBJECT_UNLOCK (basesink);
5068 gst_base_sink_set_last_buffer (basesink, NULL);
5069 priv->call_preroll = FALSE;
5071 if (!priv->commited) {
5072 if (priv->async_enabled) {
5073 GST_DEBUG_OBJECT (basesink, "PAUSED to READY, posting async-done");
5075 gst_element_post_message (GST_ELEMENT_CAST (basesink),
5076 gst_message_new_state_changed (GST_OBJECT_CAST (basesink),
5077 GST_STATE_PLAYING, GST_STATE_PAUSED, GST_STATE_READY));
5079 gst_element_post_message (GST_ELEMENT_CAST (basesink),
5080 gst_message_new_async_done (GST_OBJECT_CAST (basesink)));
5082 priv->commited = TRUE;
5084 GST_DEBUG_OBJECT (basesink, "PAUSED to READY, don't need_preroll");
5086 GST_PAD_PREROLL_UNLOCK (basesink->sinkpad);
5088 case GST_STATE_CHANGE_READY_TO_NULL:
5090 if (!bclass->stop (basesink)) {
5091 GST_WARNING_OBJECT (basesink, "failed to stop");
5094 gst_base_sink_set_last_buffer (basesink, NULL);
5095 priv->call_preroll = FALSE;
5106 GST_DEBUG_OBJECT (basesink, "failed to start");
5107 return GST_STATE_CHANGE_FAILURE;
5111 GST_DEBUG_OBJECT (basesink,
5112 "element failed to change states -- activation problem?");
5113 return GST_STATE_CHANGE_FAILURE;