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_in_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;
264 #define DO_RUNNING_AVG(avg,val,size) (((val) + ((size)-1) * (avg)) / (size))
266 /* generic running average, this has a neutral window size */
267 #define UPDATE_RUNNING_AVG(avg,val) DO_RUNNING_AVG(avg,val,8)
269 /* the windows for these running averages are experimentally obtained.
270 * possitive values get averaged more while negative values use a small
271 * window so we can react faster to badness. */
272 #define UPDATE_RUNNING_AVG_P(avg,val) DO_RUNNING_AVG(avg,val,16)
273 #define UPDATE_RUNNING_AVG_N(avg,val) DO_RUNNING_AVG(avg,val,4)
277 _PR_IS_NOTHING = 1 << 0,
278 _PR_IS_BUFFER = 1 << 1,
279 _PR_IS_BUFFERLIST = 1 << 2,
280 _PR_IS_EVENT = 1 << 3
283 #define OBJ_IS_BUFFER(a) ((a) & _PR_IS_BUFFER)
284 #define OBJ_IS_BUFFERLIST(a) ((a) & _PR_IS_BUFFERLIST)
285 #define OBJ_IS_EVENT(a) ((a) & _PR_IS_EVENT)
286 #define OBJ_IS_BUFFERFULL(a) ((a) & (_PR_IS_BUFFER | _PR_IS_BUFFERLIST))
288 /* BaseSink properties */
290 #define DEFAULT_CAN_ACTIVATE_PULL FALSE /* fixme: enable me */
291 #define DEFAULT_CAN_ACTIVATE_PUSH TRUE
293 #define DEFAULT_PREROLL_QUEUE_LEN 0
294 #define DEFAULT_SYNC TRUE
295 #define DEFAULT_MAX_LATENESS -1
296 #define DEFAULT_QOS FALSE
297 #define DEFAULT_ASYNC TRUE
298 #define DEFAULT_TS_OFFSET 0
299 #define DEFAULT_BLOCKSIZE 4096
300 #define DEFAULT_RENDER_DELAY 0
301 #define DEFAULT_ENABLE_LAST_BUFFER TRUE
306 PROP_PREROLL_QUEUE_LEN,
312 PROP_ENABLE_LAST_BUFFER,
319 static GstElementClass *parent_class = NULL;
321 static void gst_base_sink_class_init (GstBaseSinkClass * klass);
322 static void gst_base_sink_init (GstBaseSink * trans, gpointer g_class);
323 static void gst_base_sink_finalize (GObject * object);
326 gst_base_sink_get_type (void)
328 static volatile gsize base_sink_type = 0;
330 if (g_once_init_enter (&base_sink_type)) {
332 static const GTypeInfo base_sink_info = {
333 sizeof (GstBaseSinkClass),
336 (GClassInitFunc) gst_base_sink_class_init,
339 sizeof (GstBaseSink),
341 (GInstanceInitFunc) gst_base_sink_init,
344 _type = g_type_register_static (GST_TYPE_ELEMENT,
345 "GstBaseSink", &base_sink_info, G_TYPE_FLAG_ABSTRACT);
346 g_once_init_leave (&base_sink_type, _type);
348 return base_sink_type;
351 static void gst_base_sink_set_property (GObject * object, guint prop_id,
352 const GValue * value, GParamSpec * pspec);
353 static void gst_base_sink_get_property (GObject * object, guint prop_id,
354 GValue * value, GParamSpec * pspec);
356 static gboolean gst_base_sink_send_event (GstElement * element,
358 static gboolean gst_base_sink_query (GstElement * element, GstQuery * query);
359 static const GstQueryType *gst_base_sink_get_query_types (GstElement * element);
361 static GstCaps *gst_base_sink_get_caps (GstBaseSink * sink);
362 static gboolean gst_base_sink_set_caps (GstBaseSink * sink, GstCaps * caps);
363 static GstFlowReturn gst_base_sink_buffer_alloc (GstBaseSink * sink,
364 guint64 offset, guint size, GstCaps * caps, GstBuffer ** buf);
365 static void gst_base_sink_get_times (GstBaseSink * basesink, GstBuffer * buffer,
366 GstClockTime * start, GstClockTime * end);
367 static gboolean gst_base_sink_set_flushing (GstBaseSink * basesink,
368 GstPad * pad, gboolean flushing);
369 static gboolean gst_base_sink_default_activate_pull (GstBaseSink * basesink,
371 static gboolean gst_base_sink_default_do_seek (GstBaseSink * sink,
372 GstSegment * segment);
373 static gboolean gst_base_sink_default_prepare_seek_segment (GstBaseSink * sink,
374 GstEvent * event, GstSegment * segment);
376 static GstStateChangeReturn gst_base_sink_change_state (GstElement * element,
377 GstStateChange transition);
379 static GstFlowReturn gst_base_sink_chain (GstPad * pad, GstBuffer * buffer);
380 static GstFlowReturn gst_base_sink_chain_list (GstPad * pad,
381 GstBufferList * list);
383 static void gst_base_sink_loop (GstPad * pad);
384 static gboolean gst_base_sink_pad_activate (GstPad * pad);
385 static gboolean gst_base_sink_pad_activate_push (GstPad * pad, gboolean active);
386 static gboolean gst_base_sink_pad_activate_pull (GstPad * pad, gboolean active);
387 static gboolean gst_base_sink_event (GstPad * pad, GstEvent * event);
389 static gboolean gst_base_sink_negotiate_pull (GstBaseSink * basesink);
390 static GstCaps *gst_base_sink_pad_getcaps (GstPad * pad);
391 static gboolean gst_base_sink_pad_setcaps (GstPad * pad, GstCaps * caps);
392 static void gst_base_sink_pad_fixate (GstPad * pad, GstCaps * caps);
393 static GstFlowReturn gst_base_sink_pad_buffer_alloc (GstPad * pad,
394 guint64 offset, guint size, GstCaps * caps, GstBuffer ** buf);
397 /* check if an object was too late */
398 static gboolean gst_base_sink_is_too_late (GstBaseSink * basesink,
399 GstMiniObject * obj, GstClockTime start, GstClockTime stop,
400 GstClockReturn status, GstClockTimeDiff jitter);
401 static GstFlowReturn gst_base_sink_preroll_object (GstBaseSink * basesink,
402 guint8 obj_type, GstMiniObject * obj);
405 gst_base_sink_class_init (GstBaseSinkClass * klass)
407 GObjectClass *gobject_class;
408 GstElementClass *gstelement_class;
410 gobject_class = G_OBJECT_CLASS (klass);
411 gstelement_class = GST_ELEMENT_CLASS (klass);
413 GST_DEBUG_CATEGORY_INIT (gst_base_sink_debug, "basesink", 0,
416 g_type_class_add_private (klass, sizeof (GstBaseSinkPrivate));
418 parent_class = g_type_class_peek_parent (klass);
420 gobject_class->finalize = gst_base_sink_finalize;
421 gobject_class->set_property = gst_base_sink_set_property;
422 gobject_class->get_property = gst_base_sink_get_property;
424 /* FIXME, this next value should be configured using an event from the
425 * upstream element, ie, the BUFFER_SIZE event. */
426 g_object_class_install_property (gobject_class, PROP_PREROLL_QUEUE_LEN,
427 g_param_spec_uint ("preroll-queue-len", "Preroll queue length",
428 "Number of buffers to queue during preroll", 0, G_MAXUINT,
429 DEFAULT_PREROLL_QUEUE_LEN,
430 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
432 g_object_class_install_property (gobject_class, PROP_SYNC,
433 g_param_spec_boolean ("sync", "Sync", "Sync on the clock", DEFAULT_SYNC,
434 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
436 g_object_class_install_property (gobject_class, PROP_MAX_LATENESS,
437 g_param_spec_int64 ("max-lateness", "Max Lateness",
438 "Maximum number of nanoseconds that a buffer can be late before it "
439 "is dropped (-1 unlimited)", -1, G_MAXINT64, DEFAULT_MAX_LATENESS,
440 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
442 g_object_class_install_property (gobject_class, PROP_QOS,
443 g_param_spec_boolean ("qos", "Qos",
444 "Generate Quality-of-Service events upstream", DEFAULT_QOS,
445 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
449 * If set to #TRUE, the basesink will perform asynchronous state changes.
450 * When set to #FALSE, the sink will not signal the parent when it prerolls.
451 * Use this option when dealing with sparse streams or when synchronisation is
456 g_object_class_install_property (gobject_class, PROP_ASYNC,
457 g_param_spec_boolean ("async", "Async",
458 "Go asynchronously to PAUSED", DEFAULT_ASYNC,
459 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
461 * GstBaseSink:ts-offset
463 * Controls the final synchronisation, a negative value will render the buffer
464 * earlier while a positive value delays playback. This property can be
465 * used to fix synchronisation in bad files.
469 g_object_class_install_property (gobject_class, PROP_TS_OFFSET,
470 g_param_spec_int64 ("ts-offset", "TS Offset",
471 "Timestamp offset in nanoseconds", G_MININT64, G_MAXINT64,
472 DEFAULT_TS_OFFSET, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
475 * GstBaseSink:enable-last-buffer
477 * Enable the last-buffer property. If FALSE, basesink doesn't keep a
478 * reference to the last buffer arrived and the last-buffer property is always
479 * set to NULL. This can be useful if you need buffers to be released as soon
480 * as possible, eg. if you're using a buffer pool.
484 g_object_class_install_property (gobject_class, PROP_ENABLE_LAST_BUFFER,
485 g_param_spec_boolean ("enable-last-buffer", "Enable Last Buffer",
486 "Enable the last-buffer property", DEFAULT_ENABLE_LAST_BUFFER,
487 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
490 * GstBaseSink:last-buffer
492 * The last buffer that arrived in the sink and was used for preroll or for
493 * rendering. This property can be used to generate thumbnails. This property
494 * can be NULL when the sink has not yet received a bufer.
498 g_object_class_install_property (gobject_class, PROP_LAST_BUFFER,
499 gst_param_spec_mini_object ("last-buffer", "Last Buffer",
500 "The last buffer received in the sink", GST_TYPE_BUFFER,
501 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
503 * GstBaseSink:blocksize
505 * The amount of bytes to pull when operating in pull mode.
509 g_object_class_install_property (gobject_class, PROP_BLOCKSIZE,
510 g_param_spec_uint ("blocksize", "Block size",
511 "Size in bytes to pull per buffer (0 = default)", 0, G_MAXUINT,
512 DEFAULT_BLOCKSIZE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
514 * GstBaseSink:render-delay
516 * The additional delay between synchronisation and actual rendering of the
517 * media. This property will add additional latency to the device in order to
518 * make other sinks compensate for the delay.
522 g_object_class_install_property (gobject_class, PROP_RENDER_DELAY,
523 g_param_spec_uint64 ("render-delay", "Render Delay",
524 "Additional render delay of the sink in nanoseconds", 0, G_MAXUINT64,
525 DEFAULT_RENDER_DELAY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
527 gstelement_class->change_state =
528 GST_DEBUG_FUNCPTR (gst_base_sink_change_state);
529 gstelement_class->send_event = GST_DEBUG_FUNCPTR (gst_base_sink_send_event);
530 gstelement_class->query = GST_DEBUG_FUNCPTR (gst_base_sink_query);
531 gstelement_class->get_query_types =
532 GST_DEBUG_FUNCPTR (gst_base_sink_get_query_types);
534 klass->get_caps = GST_DEBUG_FUNCPTR (gst_base_sink_get_caps);
535 klass->set_caps = GST_DEBUG_FUNCPTR (gst_base_sink_set_caps);
536 klass->buffer_alloc = GST_DEBUG_FUNCPTR (gst_base_sink_buffer_alloc);
537 klass->get_times = GST_DEBUG_FUNCPTR (gst_base_sink_get_times);
538 klass->activate_pull =
539 GST_DEBUG_FUNCPTR (gst_base_sink_default_activate_pull);
541 /* Registering debug symbols for function pointers */
542 GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_pad_getcaps);
543 GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_pad_setcaps);
544 GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_pad_fixate);
545 GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_pad_buffer_alloc);
546 GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_pad_activate);
547 GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_pad_activate_push);
548 GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_pad_activate_pull);
549 GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_event);
550 GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_chain);
551 GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_chain_list);
555 gst_base_sink_pad_getcaps (GstPad * pad)
557 GstBaseSinkClass *bclass;
559 GstCaps *caps = NULL;
561 bsink = GST_BASE_SINK (gst_pad_get_parent (pad));
562 bclass = GST_BASE_SINK_GET_CLASS (bsink);
564 if (bsink->pad_mode == GST_ACTIVATE_PULL) {
565 /* if we are operating in pull mode we only accept the negotiated caps */
566 GST_OBJECT_LOCK (pad);
567 if ((caps = GST_PAD_CAPS (pad)))
569 GST_OBJECT_UNLOCK (pad);
572 if (bclass->get_caps)
573 caps = bclass->get_caps (bsink);
576 GstPadTemplate *pad_template;
579 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (bclass),
581 if (pad_template != NULL) {
582 caps = gst_caps_ref (gst_pad_template_get_caps (pad_template));
586 gst_object_unref (bsink);
592 gst_base_sink_pad_setcaps (GstPad * pad, GstCaps * caps)
594 GstBaseSinkClass *bclass;
598 bsink = GST_BASE_SINK (gst_pad_get_parent (pad));
599 bclass = GST_BASE_SINK_GET_CLASS (bsink);
601 if (res && bclass->set_caps)
602 res = bclass->set_caps (bsink, caps);
604 gst_object_unref (bsink);
610 gst_base_sink_pad_fixate (GstPad * pad, GstCaps * caps)
612 GstBaseSinkClass *bclass;
615 bsink = GST_BASE_SINK (gst_pad_get_parent (pad));
616 bclass = GST_BASE_SINK_GET_CLASS (bsink);
619 bclass->fixate (bsink, caps);
621 gst_object_unref (bsink);
625 gst_base_sink_pad_buffer_alloc (GstPad * pad, guint64 offset, guint size,
626 GstCaps * caps, GstBuffer ** buf)
628 GstBaseSinkClass *bclass;
630 GstFlowReturn result = GST_FLOW_OK;
632 bsink = GST_BASE_SINK (gst_pad_get_parent (pad));
633 bclass = GST_BASE_SINK_GET_CLASS (bsink);
635 if (bclass->buffer_alloc)
636 result = bclass->buffer_alloc (bsink, offset, size, caps, buf);
638 *buf = NULL; /* fallback in gstpad.c will allocate generic buffer */
640 gst_object_unref (bsink);
646 gst_base_sink_init (GstBaseSink * basesink, gpointer g_class)
648 GstPadTemplate *pad_template;
649 GstBaseSinkPrivate *priv;
651 basesink->priv = priv = GST_BASE_SINK_GET_PRIVATE (basesink);
654 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (g_class), "sink");
655 g_return_if_fail (pad_template != NULL);
657 basesink->sinkpad = gst_pad_new_from_template (pad_template, "sink");
659 gst_pad_set_getcaps_function (basesink->sinkpad, gst_base_sink_pad_getcaps);
660 gst_pad_set_setcaps_function (basesink->sinkpad, gst_base_sink_pad_setcaps);
661 gst_pad_set_fixatecaps_function (basesink->sinkpad, gst_base_sink_pad_fixate);
662 gst_pad_set_bufferalloc_function (basesink->sinkpad,
663 gst_base_sink_pad_buffer_alloc);
664 gst_pad_set_activate_function (basesink->sinkpad, gst_base_sink_pad_activate);
665 gst_pad_set_activatepush_function (basesink->sinkpad,
666 gst_base_sink_pad_activate_push);
667 gst_pad_set_activatepull_function (basesink->sinkpad,
668 gst_base_sink_pad_activate_pull);
669 gst_pad_set_event_function (basesink->sinkpad, gst_base_sink_event);
670 gst_pad_set_chain_function (basesink->sinkpad, gst_base_sink_chain);
671 gst_pad_set_chain_list_function (basesink->sinkpad, gst_base_sink_chain_list);
672 gst_element_add_pad (GST_ELEMENT_CAST (basesink), basesink->sinkpad);
674 basesink->pad_mode = GST_ACTIVATE_NONE;
675 basesink->preroll_queue = g_queue_new ();
676 basesink->abidata.ABI.clip_segment = gst_segment_new ();
677 priv->have_latency = FALSE;
679 basesink->can_activate_push = DEFAULT_CAN_ACTIVATE_PUSH;
680 basesink->can_activate_pull = DEFAULT_CAN_ACTIVATE_PULL;
682 basesink->sync = DEFAULT_SYNC;
683 basesink->abidata.ABI.max_lateness = DEFAULT_MAX_LATENESS;
684 g_atomic_int_set (&priv->qos_enabled, DEFAULT_QOS);
685 priv->async_enabled = DEFAULT_ASYNC;
686 priv->ts_offset = DEFAULT_TS_OFFSET;
687 priv->render_delay = DEFAULT_RENDER_DELAY;
688 priv->blocksize = DEFAULT_BLOCKSIZE;
689 priv->cached_clock_id = NULL;
690 g_atomic_int_set (&priv->enable_last_buffer, DEFAULT_ENABLE_LAST_BUFFER);
692 GST_OBJECT_FLAG_SET (basesink, GST_ELEMENT_IS_SINK);
696 gst_base_sink_finalize (GObject * object)
698 GstBaseSink *basesink;
700 basesink = GST_BASE_SINK (object);
702 g_queue_free (basesink->preroll_queue);
703 gst_segment_free (basesink->abidata.ABI.clip_segment);
705 G_OBJECT_CLASS (parent_class)->finalize (object);
709 * gst_base_sink_set_sync:
711 * @sync: the new sync value.
713 * Configures @sink to synchronize on the clock or not. When
714 * @sync is FALSE, incomming samples will be played as fast as
715 * possible. If @sync is TRUE, the timestamps of the incomming
716 * buffers will be used to schedule the exact render time of its
722 gst_base_sink_set_sync (GstBaseSink * sink, gboolean sync)
724 g_return_if_fail (GST_IS_BASE_SINK (sink));
726 GST_OBJECT_LOCK (sink);
728 GST_OBJECT_UNLOCK (sink);
732 * gst_base_sink_get_sync:
735 * Checks if @sink is currently configured to synchronize against the
738 * Returns: TRUE if the sink is configured to synchronize against the clock.
743 gst_base_sink_get_sync (GstBaseSink * sink)
747 g_return_val_if_fail (GST_IS_BASE_SINK (sink), FALSE);
749 GST_OBJECT_LOCK (sink);
751 GST_OBJECT_UNLOCK (sink);
757 * gst_base_sink_set_max_lateness:
759 * @max_lateness: the new max lateness value.
761 * Sets the new max lateness value to @max_lateness. This value is
762 * used to decide if a buffer should be dropped or not based on the
763 * buffer timestamp and the current clock time. A value of -1 means
769 gst_base_sink_set_max_lateness (GstBaseSink * sink, gint64 max_lateness)
771 g_return_if_fail (GST_IS_BASE_SINK (sink));
773 GST_OBJECT_LOCK (sink);
774 sink->abidata.ABI.max_lateness = max_lateness;
775 GST_OBJECT_UNLOCK (sink);
779 * gst_base_sink_get_max_lateness:
782 * Gets the max lateness value. See gst_base_sink_set_max_lateness for
785 * Returns: The maximum time in nanoseconds that a buffer can be late
786 * before it is dropped and not rendered. A value of -1 means an
792 gst_base_sink_get_max_lateness (GstBaseSink * sink)
796 g_return_val_if_fail (GST_IS_BASE_SINK (sink), -1);
798 GST_OBJECT_LOCK (sink);
799 res = sink->abidata.ABI.max_lateness;
800 GST_OBJECT_UNLOCK (sink);
806 * gst_base_sink_set_qos_enabled:
808 * @enabled: the new qos value.
810 * Configures @sink to send Quality-of-Service events upstream.
815 gst_base_sink_set_qos_enabled (GstBaseSink * sink, gboolean enabled)
817 g_return_if_fail (GST_IS_BASE_SINK (sink));
819 g_atomic_int_set (&sink->priv->qos_enabled, enabled);
823 * gst_base_sink_is_qos_enabled:
826 * Checks if @sink is currently configured to send Quality-of-Service events
829 * Returns: TRUE if the sink is configured to perform Quality-of-Service.
834 gst_base_sink_is_qos_enabled (GstBaseSink * sink)
838 g_return_val_if_fail (GST_IS_BASE_SINK (sink), FALSE);
840 res = g_atomic_int_get (&sink->priv->qos_enabled);
846 * gst_base_sink_set_async_enabled:
848 * @enabled: the new async value.
850 * Configures @sink to perform all state changes asynchronusly. When async is
851 * disabled, the sink will immediatly go to PAUSED instead of waiting for a
852 * preroll buffer. This feature is usefull if the sink does not synchronize
853 * against the clock or when it is dealing with sparse streams.
858 gst_base_sink_set_async_enabled (GstBaseSink * sink, gboolean enabled)
860 g_return_if_fail (GST_IS_BASE_SINK (sink));
862 GST_PAD_PREROLL_LOCK (sink->sinkpad);
863 g_atomic_int_set (&sink->priv->async_enabled, enabled);
864 GST_LOG_OBJECT (sink, "set async enabled to %d", enabled);
865 GST_PAD_PREROLL_UNLOCK (sink->sinkpad);
869 * gst_base_sink_is_async_enabled:
872 * Checks if @sink is currently configured to perform asynchronous state
875 * Returns: TRUE if the sink is configured to perform asynchronous state
881 gst_base_sink_is_async_enabled (GstBaseSink * sink)
885 g_return_val_if_fail (GST_IS_BASE_SINK (sink), FALSE);
887 res = g_atomic_int_get (&sink->priv->async_enabled);
893 * gst_base_sink_set_ts_offset:
895 * @offset: the new offset
897 * Adjust the synchronisation of @sink with @offset. A negative value will
898 * render buffers earlier than their timestamp. A positive value will delay
899 * rendering. This function can be used to fix playback of badly timestamped
905 gst_base_sink_set_ts_offset (GstBaseSink * sink, GstClockTimeDiff offset)
907 g_return_if_fail (GST_IS_BASE_SINK (sink));
909 GST_OBJECT_LOCK (sink);
910 sink->priv->ts_offset = offset;
911 GST_LOG_OBJECT (sink, "set time offset to %" G_GINT64_FORMAT, offset);
912 GST_OBJECT_UNLOCK (sink);
916 * gst_base_sink_get_ts_offset:
919 * Get the synchronisation offset of @sink.
921 * Returns: The synchronisation offset.
926 gst_base_sink_get_ts_offset (GstBaseSink * sink)
928 GstClockTimeDiff res;
930 g_return_val_if_fail (GST_IS_BASE_SINK (sink), 0);
932 GST_OBJECT_LOCK (sink);
933 res = sink->priv->ts_offset;
934 GST_OBJECT_UNLOCK (sink);
940 * gst_base_sink_get_last_buffer:
943 * Get the last buffer that arrived in the sink and was used for preroll or for
944 * rendering. This property can be used to generate thumbnails.
946 * The #GstCaps on the buffer can be used to determine the type of the buffer.
948 * Free-function: gst_buffer_unref
950 * Returns: (transfer full): a #GstBuffer. gst_buffer_unref() after usage.
951 * This function returns NULL when no buffer has arrived in the sink yet
952 * or when the sink is not in PAUSED or PLAYING.
957 gst_base_sink_get_last_buffer (GstBaseSink * sink)
961 g_return_val_if_fail (GST_IS_BASE_SINK (sink), NULL);
963 GST_OBJECT_LOCK (sink);
964 if ((res = sink->priv->last_buffer))
965 gst_buffer_ref (res);
966 GST_OBJECT_UNLOCK (sink);
971 /* with OBJECT_LOCK */
973 gst_base_sink_set_last_buffer_unlocked (GstBaseSink * sink, GstBuffer * buffer)
977 old = sink->priv->last_buffer;
978 if (G_LIKELY (old != buffer)) {
979 GST_DEBUG_OBJECT (sink, "setting last buffer to %p", buffer);
980 if (G_LIKELY (buffer))
981 gst_buffer_ref (buffer);
982 sink->priv->last_buffer = buffer;
986 /* avoid unreffing with the lock because cleanup code might want to take the
988 if (G_LIKELY (old)) {
989 GST_OBJECT_UNLOCK (sink);
990 gst_buffer_unref (old);
991 GST_OBJECT_LOCK (sink);
996 gst_base_sink_set_last_buffer (GstBaseSink * sink, GstBuffer * buffer)
998 if (!g_atomic_int_get (&sink->priv->enable_last_buffer))
1001 GST_OBJECT_LOCK (sink);
1002 gst_base_sink_set_last_buffer_unlocked (sink, buffer);
1003 GST_OBJECT_UNLOCK (sink);
1007 * gst_base_sink_set_last_buffer_enabled:
1009 * @enabled: the new enable-last-buffer value.
1011 * Configures @sink to store the last received buffer in the last-buffer
1017 gst_base_sink_set_last_buffer_enabled (GstBaseSink * sink, gboolean enabled)
1019 g_return_if_fail (GST_IS_BASE_SINK (sink));
1021 /* Only take lock if we change the value */
1022 if (g_atomic_int_compare_and_exchange (&sink->priv->enable_last_buffer,
1023 !enabled, enabled) && !enabled) {
1024 GST_OBJECT_LOCK (sink);
1025 gst_base_sink_set_last_buffer_unlocked (sink, NULL);
1026 GST_OBJECT_UNLOCK (sink);
1031 * gst_base_sink_is_last_buffer_enabled:
1034 * Checks if @sink is currently configured to store the last received buffer in
1035 * the last-buffer property.
1037 * Returns: TRUE if the sink is configured to store the last received buffer.
1042 gst_base_sink_is_last_buffer_enabled (GstBaseSink * sink)
1044 g_return_val_if_fail (GST_IS_BASE_SINK (sink), FALSE);
1046 return g_atomic_int_get (&sink->priv->enable_last_buffer);
1050 * gst_base_sink_get_latency:
1053 * Get the currently configured latency.
1055 * Returns: The configured latency.
1060 gst_base_sink_get_latency (GstBaseSink * sink)
1064 GST_OBJECT_LOCK (sink);
1065 res = sink->priv->latency;
1066 GST_OBJECT_UNLOCK (sink);
1072 * gst_base_sink_query_latency:
1074 * @live: (out) (allow-none): if the sink is live
1075 * @upstream_live: (out) (allow-none): if an upstream element is live
1076 * @min_latency: (out) (allow-none): the min latency of the upstream elements
1077 * @max_latency: (out) (allow-none): the max latency of the upstream elements
1079 * Query the sink for the latency parameters. The latency will be queried from
1080 * the upstream elements. @live will be TRUE if @sink is configured to
1081 * synchronize against the clock. @upstream_live will be TRUE if an upstream
1084 * If both @live and @upstream_live are TRUE, the sink will want to compensate
1085 * for the latency introduced by the upstream elements by setting the
1086 * @min_latency to a strictly possitive value.
1088 * This function is mostly used by subclasses.
1090 * Returns: TRUE if the query succeeded.
1095 gst_base_sink_query_latency (GstBaseSink * sink, gboolean * live,
1096 gboolean * upstream_live, GstClockTime * min_latency,
1097 GstClockTime * max_latency)
1099 gboolean l, us_live, res, have_latency;
1100 GstClockTime min, max, render_delay;
1102 GstClockTime us_min, us_max;
1104 /* we are live when we sync to the clock */
1105 GST_OBJECT_LOCK (sink);
1107 have_latency = sink->priv->have_latency;
1108 render_delay = sink->priv->render_delay;
1109 GST_OBJECT_UNLOCK (sink);
1111 /* assume no latency */
1117 GST_DEBUG_OBJECT (sink, "we are ready for LATENCY query");
1118 /* we are ready for a latency query this is when we preroll or when we are
1120 query = gst_query_new_latency ();
1122 /* ask the peer for the latency */
1123 if ((res = gst_pad_peer_query (sink->sinkpad, query))) {
1124 /* get upstream min and max latency */
1125 gst_query_parse_latency (query, &us_live, &us_min, &us_max);
1128 /* upstream live, use its latency, subclasses should use these
1129 * values to create the complete latency. */
1134 /* we need to add the render delay if we are live */
1136 min += render_delay;
1138 max += render_delay;
1141 gst_query_unref (query);
1143 GST_DEBUG_OBJECT (sink, "we are not yet ready for LATENCY query");
1147 /* not live, we tried to do the query, if it failed we return TRUE anyway */
1151 GST_DEBUG_OBJECT (sink, "latency query failed but we are not live");
1153 GST_DEBUG_OBJECT (sink, "latency query failed and we are live");
1158 GST_DEBUG_OBJECT (sink, "latency query: live: %d, have_latency %d,"
1159 " upstream: %d, min %" GST_TIME_FORMAT ", max %" GST_TIME_FORMAT, l,
1160 have_latency, us_live, GST_TIME_ARGS (min), GST_TIME_ARGS (max));
1165 *upstream_live = us_live;
1175 * gst_base_sink_set_render_delay:
1176 * @sink: a #GstBaseSink
1177 * @delay: the new delay
1179 * Set the render delay in @sink to @delay. The render delay is the time
1180 * between actual rendering of a buffer and its synchronisation time. Some
1181 * devices might delay media rendering which can be compensated for with this
1184 * After calling this function, this sink will report additional latency and
1185 * other sinks will adjust their latency to delay the rendering of their media.
1187 * This function is usually called by subclasses.
1192 gst_base_sink_set_render_delay (GstBaseSink * sink, GstClockTime delay)
1194 GstClockTime old_render_delay;
1196 g_return_if_fail (GST_IS_BASE_SINK (sink));
1198 GST_OBJECT_LOCK (sink);
1199 old_render_delay = sink->priv->render_delay;
1200 sink->priv->render_delay = delay;
1201 GST_LOG_OBJECT (sink, "set render delay to %" GST_TIME_FORMAT,
1202 GST_TIME_ARGS (delay));
1203 GST_OBJECT_UNLOCK (sink);
1205 if (delay != old_render_delay) {
1206 GST_DEBUG_OBJECT (sink, "posting latency changed");
1207 gst_element_post_message (GST_ELEMENT_CAST (sink),
1208 gst_message_new_latency (GST_OBJECT_CAST (sink)));
1213 * gst_base_sink_get_render_delay:
1214 * @sink: a #GstBaseSink
1216 * Get the render delay of @sink. see gst_base_sink_set_render_delay() for more
1217 * information about the render delay.
1219 * Returns: the render delay of @sink.
1224 gst_base_sink_get_render_delay (GstBaseSink * sink)
1226 GstClockTimeDiff res;
1228 g_return_val_if_fail (GST_IS_BASE_SINK (sink), 0);
1230 GST_OBJECT_LOCK (sink);
1231 res = sink->priv->render_delay;
1232 GST_OBJECT_UNLOCK (sink);
1238 * gst_base_sink_set_blocksize:
1239 * @sink: a #GstBaseSink
1240 * @blocksize: the blocksize in bytes
1242 * Set the number of bytes that the sink will pull when it is operating in pull
1248 gst_base_sink_set_blocksize (GstBaseSink * sink, guint blocksize)
1250 g_return_if_fail (GST_IS_BASE_SINK (sink));
1252 GST_OBJECT_LOCK (sink);
1253 sink->priv->blocksize = blocksize;
1254 GST_LOG_OBJECT (sink, "set blocksize to %u", blocksize);
1255 GST_OBJECT_UNLOCK (sink);
1259 * gst_base_sink_get_blocksize:
1260 * @sink: a #GstBaseSink
1262 * Get the number of bytes that the sink will pull when it is operating in pull
1265 * Returns: the number of bytes @sink will pull in pull mode.
1270 gst_base_sink_get_blocksize (GstBaseSink * sink)
1274 g_return_val_if_fail (GST_IS_BASE_SINK (sink), 0);
1276 GST_OBJECT_LOCK (sink);
1277 res = sink->priv->blocksize;
1278 GST_OBJECT_UNLOCK (sink);
1284 gst_base_sink_set_property (GObject * object, guint prop_id,
1285 const GValue * value, GParamSpec * pspec)
1287 GstBaseSink *sink = GST_BASE_SINK (object);
1290 case PROP_PREROLL_QUEUE_LEN:
1291 /* preroll lock necessary to serialize with finish_preroll */
1292 GST_PAD_PREROLL_LOCK (sink->sinkpad);
1293 g_atomic_int_set (&sink->preroll_queue_max_len, g_value_get_uint (value));
1294 GST_PAD_PREROLL_UNLOCK (sink->sinkpad);
1297 gst_base_sink_set_sync (sink, g_value_get_boolean (value));
1299 case PROP_MAX_LATENESS:
1300 gst_base_sink_set_max_lateness (sink, g_value_get_int64 (value));
1303 gst_base_sink_set_qos_enabled (sink, g_value_get_boolean (value));
1306 gst_base_sink_set_async_enabled (sink, g_value_get_boolean (value));
1308 case PROP_TS_OFFSET:
1309 gst_base_sink_set_ts_offset (sink, g_value_get_int64 (value));
1311 case PROP_BLOCKSIZE:
1312 gst_base_sink_set_blocksize (sink, g_value_get_uint (value));
1314 case PROP_RENDER_DELAY:
1315 gst_base_sink_set_render_delay (sink, g_value_get_uint64 (value));
1317 case PROP_ENABLE_LAST_BUFFER:
1318 gst_base_sink_set_last_buffer_enabled (sink, g_value_get_boolean (value));
1321 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1327 gst_base_sink_get_property (GObject * object, guint prop_id, GValue * value,
1330 GstBaseSink *sink = GST_BASE_SINK (object);
1333 case PROP_PREROLL_QUEUE_LEN:
1334 g_value_set_uint (value, g_atomic_int_get (&sink->preroll_queue_max_len));
1337 g_value_set_boolean (value, gst_base_sink_get_sync (sink));
1339 case PROP_MAX_LATENESS:
1340 g_value_set_int64 (value, gst_base_sink_get_max_lateness (sink));
1343 g_value_set_boolean (value, gst_base_sink_is_qos_enabled (sink));
1346 g_value_set_boolean (value, gst_base_sink_is_async_enabled (sink));
1348 case PROP_TS_OFFSET:
1349 g_value_set_int64 (value, gst_base_sink_get_ts_offset (sink));
1351 case PROP_LAST_BUFFER:
1352 gst_value_take_buffer (value, gst_base_sink_get_last_buffer (sink));
1354 case PROP_ENABLE_LAST_BUFFER:
1355 g_value_set_boolean (value, gst_base_sink_is_last_buffer_enabled (sink));
1357 case PROP_BLOCKSIZE:
1358 g_value_set_uint (value, gst_base_sink_get_blocksize (sink));
1360 case PROP_RENDER_DELAY:
1361 g_value_set_uint64 (value, gst_base_sink_get_render_delay (sink));
1364 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1371 gst_base_sink_get_caps (GstBaseSink * sink)
1377 gst_base_sink_set_caps (GstBaseSink * sink, GstCaps * caps)
1382 static GstFlowReturn
1383 gst_base_sink_buffer_alloc (GstBaseSink * sink, guint64 offset, guint size,
1384 GstCaps * caps, GstBuffer ** buf)
1390 /* with PREROLL_LOCK, STREAM_LOCK */
1392 gst_base_sink_preroll_queue_flush (GstBaseSink * basesink, GstPad * pad)
1396 GST_DEBUG_OBJECT (basesink, "flushing queue %p", basesink);
1397 while ((obj = g_queue_pop_head (basesink->preroll_queue))) {
1398 GST_DEBUG_OBJECT (basesink, "popped %p", obj);
1399 gst_mini_object_unref (obj);
1401 /* we can't have EOS anymore now */
1402 basesink->eos = FALSE;
1403 basesink->priv->received_eos = FALSE;
1404 basesink->have_preroll = FALSE;
1405 basesink->priv->step_unlock = FALSE;
1406 basesink->eos_queued = FALSE;
1407 basesink->preroll_queued = 0;
1408 basesink->buffers_queued = 0;
1409 basesink->events_queued = 0;
1410 /* can't report latency anymore until we preroll again */
1411 if (basesink->priv->async_enabled) {
1412 GST_OBJECT_LOCK (basesink);
1413 basesink->priv->have_latency = FALSE;
1414 GST_OBJECT_UNLOCK (basesink);
1416 /* and signal any waiters now */
1417 GST_PAD_PREROLL_SIGNAL (pad);
1420 /* with STREAM_LOCK, configures given segment with the event information. */
1422 gst_base_sink_configure_segment (GstBaseSink * basesink, GstPad * pad,
1423 GstEvent * event, GstSegment * segment)
1426 gdouble rate, arate;
1432 /* the newsegment event is needed to bring the buffer timestamps to the
1433 * stream time and to drop samples outside of the playback segment. */
1434 gst_event_parse_new_segment_full (event, &update, &rate, &arate, &format,
1435 &start, &stop, &time);
1437 /* The segment is protected with both the STREAM_LOCK and the OBJECT_LOCK.
1438 * We protect with the OBJECT_LOCK so that we can use the values to
1439 * safely answer a POSITION query. */
1440 GST_OBJECT_LOCK (basesink);
1441 gst_segment_set_newsegment_full (segment, update, rate, arate, format, start,
1444 if (format == GST_FORMAT_TIME) {
1445 GST_DEBUG_OBJECT (basesink,
1446 "configured NEWSEGMENT update %d, rate %lf, applied rate %lf, "
1447 "format GST_FORMAT_TIME, "
1448 "%" GST_TIME_FORMAT " -- %" GST_TIME_FORMAT
1449 ", time %" GST_TIME_FORMAT ", accum %" GST_TIME_FORMAT,
1450 update, rate, arate, GST_TIME_ARGS (segment->start),
1451 GST_TIME_ARGS (segment->stop), GST_TIME_ARGS (segment->time),
1452 GST_TIME_ARGS (segment->accum));
1454 GST_DEBUG_OBJECT (basesink,
1455 "configured NEWSEGMENT update %d, rate %lf, applied rate %lf, "
1457 "%" G_GINT64_FORMAT " -- %" G_GINT64_FORMAT ", time %"
1458 G_GINT64_FORMAT ", accum %" G_GINT64_FORMAT, update, rate, arate,
1459 segment->format, segment->start, segment->stop, segment->time,
1462 GST_OBJECT_UNLOCK (basesink);
1465 /* with PREROLL_LOCK, STREAM_LOCK */
1467 gst_base_sink_commit_state (GstBaseSink * basesink)
1469 /* commit state and proceed to next pending state */
1470 GstState current, next, pending, post_pending;
1471 gboolean post_paused = FALSE;
1472 gboolean post_async_done = FALSE;
1473 gboolean post_playing = FALSE;
1475 /* we are certainly not playing async anymore now */
1476 basesink->playing_async = FALSE;
1478 GST_OBJECT_LOCK (basesink);
1479 current = GST_STATE (basesink);
1480 next = GST_STATE_NEXT (basesink);
1481 pending = GST_STATE_PENDING (basesink);
1482 post_pending = pending;
1485 case GST_STATE_PLAYING:
1487 GstBaseSinkClass *bclass;
1488 GstStateChangeReturn ret;
1490 bclass = GST_BASE_SINK_GET_CLASS (basesink);
1492 GST_DEBUG_OBJECT (basesink, "commiting state to PLAYING");
1494 basesink->need_preroll = FALSE;
1495 post_async_done = TRUE;
1496 basesink->priv->commited = TRUE;
1497 post_playing = TRUE;
1498 /* post PAUSED too when we were READY */
1499 if (current == GST_STATE_READY) {
1503 /* make sure we notify the subclass of async playing */
1504 if (bclass->async_play) {
1505 GST_WARNING_OBJECT (basesink, "deprecated async_play");
1506 ret = bclass->async_play (basesink);
1507 if (ret == GST_STATE_CHANGE_FAILURE)
1512 case GST_STATE_PAUSED:
1513 GST_DEBUG_OBJECT (basesink, "commiting state to PAUSED");
1515 post_async_done = TRUE;
1516 basesink->priv->commited = TRUE;
1517 post_pending = GST_STATE_VOID_PENDING;
1519 case GST_STATE_READY:
1520 case GST_STATE_NULL:
1522 case GST_STATE_VOID_PENDING:
1523 goto nothing_pending;
1528 /* we can report latency queries now */
1529 basesink->priv->have_latency = TRUE;
1531 GST_STATE (basesink) = pending;
1532 GST_STATE_NEXT (basesink) = GST_STATE_VOID_PENDING;
1533 GST_STATE_PENDING (basesink) = GST_STATE_VOID_PENDING;
1534 GST_STATE_RETURN (basesink) = GST_STATE_CHANGE_SUCCESS;
1535 GST_OBJECT_UNLOCK (basesink);
1538 GST_DEBUG_OBJECT (basesink, "posting PAUSED state change message");
1539 gst_element_post_message (GST_ELEMENT_CAST (basesink),
1540 gst_message_new_state_changed (GST_OBJECT_CAST (basesink),
1541 current, next, post_pending));
1543 if (post_async_done) {
1544 GST_DEBUG_OBJECT (basesink, "posting async-done message");
1545 gst_element_post_message (GST_ELEMENT_CAST (basesink),
1546 gst_message_new_async_done (GST_OBJECT_CAST (basesink)));
1549 GST_DEBUG_OBJECT (basesink, "posting PLAYING state change message");
1550 gst_element_post_message (GST_ELEMENT_CAST (basesink),
1551 gst_message_new_state_changed (GST_OBJECT_CAST (basesink),
1552 next, pending, GST_STATE_VOID_PENDING));
1555 GST_STATE_BROADCAST (basesink);
1561 /* Depending on the state, set our vars. We get in this situation when the
1562 * state change function got a change to update the state vars before the
1563 * streaming thread did. This is fine but we need to make sure that we
1564 * update the need_preroll var since it was TRUE when we got here and might
1565 * become FALSE if we got to PLAYING. */
1566 GST_DEBUG_OBJECT (basesink, "nothing to commit, now in %s",
1567 gst_element_state_get_name (current));
1569 case GST_STATE_PLAYING:
1570 basesink->need_preroll = FALSE;
1572 case GST_STATE_PAUSED:
1573 basesink->need_preroll = TRUE;
1576 basesink->need_preroll = FALSE;
1577 basesink->flushing = TRUE;
1580 /* we can report latency queries now */
1581 basesink->priv->have_latency = TRUE;
1582 GST_OBJECT_UNLOCK (basesink);
1587 /* app is going to READY */
1588 GST_DEBUG_OBJECT (basesink, "stopping");
1589 basesink->need_preroll = FALSE;
1590 basesink->flushing = TRUE;
1591 GST_OBJECT_UNLOCK (basesink);
1596 GST_DEBUG_OBJECT (basesink, "async commit failed");
1597 GST_STATE_RETURN (basesink) = GST_STATE_CHANGE_FAILURE;
1598 GST_OBJECT_UNLOCK (basesink);
1604 start_stepping (GstBaseSink * sink, GstSegment * segment,
1605 GstStepInfo * pending, GstStepInfo * current)
1608 GstMessage *message;
1610 GST_DEBUG_OBJECT (sink, "update pending step");
1612 GST_OBJECT_LOCK (sink);
1613 memcpy (current, pending, sizeof (GstStepInfo));
1614 pending->valid = FALSE;
1615 GST_OBJECT_UNLOCK (sink);
1617 /* post message first */
1619 gst_message_new_step_start (GST_OBJECT (sink), TRUE, current->format,
1620 current->amount, current->rate, current->flush, current->intermediate);
1621 gst_message_set_seqnum (message, current->seqnum);
1622 gst_element_post_message (GST_ELEMENT (sink), message);
1624 /* get the running time of where we paused and remember it */
1625 current->start = gst_element_get_start_time (GST_ELEMENT_CAST (sink));
1626 gst_segment_set_running_time (segment, GST_FORMAT_TIME, current->start);
1628 /* set the new rate for the remainder of the segment */
1629 current->start_rate = segment->rate;
1630 segment->rate *= current->rate;
1631 segment->abs_rate = ABS (segment->rate);
1634 if (segment->rate > 0.0)
1635 current->start_stop = segment->stop;
1637 current->start_start = segment->start;
1639 if (current->format == GST_FORMAT_TIME) {
1640 end = current->start + current->amount;
1641 if (!current->flush) {
1642 /* update the segment clipping regions for non-flushing seeks */
1643 if (segment->rate > 0.0) {
1644 segment->stop = gst_segment_to_position (segment, GST_FORMAT_TIME, end);
1645 segment->last_stop = segment->stop;
1649 position = gst_segment_to_position (segment, GST_FORMAT_TIME, end);
1650 segment->time = position;
1651 segment->start = position;
1652 segment->last_stop = position;
1657 GST_DEBUG_OBJECT (sink,
1658 "segment now rate %lf, applied rate %lf, "
1659 "format GST_FORMAT_TIME, "
1660 "%" GST_TIME_FORMAT " -- %" GST_TIME_FORMAT
1661 ", time %" GST_TIME_FORMAT ", accum %" GST_TIME_FORMAT,
1662 segment->rate, segment->applied_rate, GST_TIME_ARGS (segment->start),
1663 GST_TIME_ARGS (segment->stop), GST_TIME_ARGS (segment->time),
1664 GST_TIME_ARGS (segment->accum));
1666 GST_DEBUG_OBJECT (sink, "step started at running_time %" GST_TIME_FORMAT,
1667 GST_TIME_ARGS (current->start));
1669 if (current->amount == -1) {
1670 GST_DEBUG_OBJECT (sink, "step amount == -1, stop stepping");
1671 current->valid = FALSE;
1673 GST_DEBUG_OBJECT (sink, "step amount: %" G_GUINT64_FORMAT ", format: %s, "
1674 "rate: %f", current->amount, gst_format_get_name (current->format),
1680 stop_stepping (GstBaseSink * sink, GstSegment * segment,
1681 GstStepInfo * current, gint64 rstart, gint64 rstop, gboolean eos)
1683 gint64 stop, position;
1684 GstMessage *message;
1686 GST_DEBUG_OBJECT (sink, "step complete");
1688 if (segment->rate > 0.0)
1693 GST_DEBUG_OBJECT (sink,
1694 "step stop at running_time %" GST_TIME_FORMAT, GST_TIME_ARGS (stop));
1697 current->duration = current->position;
1699 current->duration = stop - current->start;
1701 GST_DEBUG_OBJECT (sink, "step elapsed running_time %" GST_TIME_FORMAT,
1702 GST_TIME_ARGS (current->duration));
1704 position = current->start + current->duration;
1706 /* now move the segment to the new running time */
1707 gst_segment_set_running_time (segment, GST_FORMAT_TIME, position);
1709 if (current->flush) {
1710 /* and remove the accumulated time we flushed, start time did not change */
1711 segment->accum = current->start;
1713 /* start time is now the stepped position */
1714 gst_element_set_start_time (GST_ELEMENT_CAST (sink), position);
1717 /* restore the previous rate */
1718 segment->rate = current->start_rate;
1719 segment->abs_rate = ABS (segment->rate);
1721 if (segment->rate > 0.0)
1722 segment->stop = current->start_stop;
1724 segment->start = current->start_start;
1726 /* the clip segment is used for position report in paused... */
1727 memcpy (sink->abidata.ABI.clip_segment, segment, sizeof (GstSegment));
1729 /* post the step done when we know the stepped duration in TIME */
1731 gst_message_new_step_done (GST_OBJECT_CAST (sink), current->format,
1732 current->amount, current->rate, current->flush, current->intermediate,
1733 current->duration, eos);
1734 gst_message_set_seqnum (message, current->seqnum);
1735 gst_element_post_message (GST_ELEMENT_CAST (sink), message);
1737 if (!current->intermediate)
1738 sink->need_preroll = current->need_preroll;
1740 /* and the current step info finished and becomes invalid */
1741 current->valid = FALSE;
1745 handle_stepping (GstBaseSink * sink, GstSegment * segment,
1746 GstStepInfo * current, gint64 * cstart, gint64 * cstop, gint64 * rstart,
1749 gboolean step_end = FALSE;
1751 /* see if we need to skip this buffer because of stepping */
1752 switch (current->format) {
1753 case GST_FORMAT_TIME:
1758 if (segment->rate > 0.0) {
1759 if (segment->stop == *cstop)
1760 *rstop = *rstart + current->amount;
1765 if (segment->start == *cstart)
1766 *rstart = *rstop + current->amount;
1772 end = current->start + current->amount;
1773 current->position = first - current->start;
1775 if (G_UNLIKELY (segment->abs_rate != 1.0))
1776 current->position /= segment->abs_rate;
1778 GST_DEBUG_OBJECT (sink,
1779 "buffer: %" GST_TIME_FORMAT "-%" GST_TIME_FORMAT,
1780 GST_TIME_ARGS (first), GST_TIME_ARGS (last));
1781 GST_DEBUG_OBJECT (sink,
1782 "got time step %" GST_TIME_FORMAT "-%" GST_TIME_FORMAT "/%"
1783 GST_TIME_FORMAT, GST_TIME_ARGS (current->position),
1784 GST_TIME_ARGS (last - current->start),
1785 GST_TIME_ARGS (current->amount));
1787 if ((current->flush && current->position >= current->amount)
1789 GST_DEBUG_OBJECT (sink, "step ended, we need clipping");
1791 if (segment->rate > 0.0) {
1793 *cstart = gst_segment_to_position (segment, GST_FORMAT_TIME, end);
1796 *cstop = gst_segment_to_position (segment, GST_FORMAT_TIME, end);
1799 GST_DEBUG_OBJECT (sink,
1800 "cstart %" GST_TIME_FORMAT ", rstart %" GST_TIME_FORMAT,
1801 GST_TIME_ARGS (*cstart), GST_TIME_ARGS (*rstart));
1802 GST_DEBUG_OBJECT (sink,
1803 "cstop %" GST_TIME_FORMAT ", rstop %" GST_TIME_FORMAT,
1804 GST_TIME_ARGS (*cstop), GST_TIME_ARGS (*rstop));
1807 case GST_FORMAT_BUFFERS:
1808 GST_DEBUG_OBJECT (sink,
1809 "got default step %" G_GUINT64_FORMAT "/%" G_GUINT64_FORMAT,
1810 current->position, current->amount);
1812 if (current->position < current->amount) {
1813 current->position++;
1818 case GST_FORMAT_DEFAULT:
1820 GST_DEBUG_OBJECT (sink,
1821 "got unknown step %" G_GUINT64_FORMAT "/%" G_GUINT64_FORMAT,
1822 current->position, current->amount);
1828 /* with STREAM_LOCK, PREROLL_LOCK
1830 * Returns TRUE if the object needs synchronisation and takes therefore
1831 * part in prerolling.
1833 * rsstart/rsstop contain the start/stop in stream time.
1834 * rrstart/rrstop contain the start/stop in running time.
1837 gst_base_sink_get_sync_times (GstBaseSink * basesink, GstMiniObject * obj,
1838 GstClockTime * rsstart, GstClockTime * rsstop,
1839 GstClockTime * rrstart, GstClockTime * rrstop, gboolean * do_sync,
1840 gboolean * stepped, GstSegment * segment, GstStepInfo * step,
1841 gboolean * step_end, guint8 obj_type)
1843 GstBaseSinkClass *bclass;
1845 GstClockTime start, stop; /* raw start/stop timestamps */
1846 gint64 cstart, cstop; /* clipped raw timestamps */
1847 gint64 rstart, rstop; /* clipped timestamps converted to running time */
1848 GstClockTime sstart, sstop; /* clipped timestamps converted to stream time */
1850 GstBaseSinkPrivate *priv;
1853 priv = basesink->priv;
1855 /* start with nothing */
1856 start = stop = GST_CLOCK_TIME_NONE;
1858 if (G_UNLIKELY (OBJ_IS_EVENT (obj_type))) {
1859 GstEvent *event = GST_EVENT_CAST (obj);
1861 switch (GST_EVENT_TYPE (event)) {
1862 /* EOS event needs syncing */
1865 if (basesink->segment.rate >= 0.0) {
1866 sstart = sstop = priv->current_sstop;
1867 if (!GST_CLOCK_TIME_IS_VALID (sstart)) {
1868 /* we have not seen a buffer yet, use the segment values */
1869 sstart = sstop = gst_segment_to_stream_time (&basesink->segment,
1870 basesink->segment.format, basesink->segment.stop);
1873 sstart = sstop = priv->current_sstart;
1874 if (!GST_CLOCK_TIME_IS_VALID (sstart)) {
1875 /* we have not seen a buffer yet, use the segment values */
1876 sstart = sstop = gst_segment_to_stream_time (&basesink->segment,
1877 basesink->segment.format, basesink->segment.start);
1881 rstart = rstop = priv->eos_rtime;
1882 *do_sync = rstart != -1;
1883 GST_DEBUG_OBJECT (basesink, "sync times for EOS %" GST_TIME_FORMAT,
1884 GST_TIME_ARGS (rstart));
1885 /* if we are stepping, we end now */
1886 *step_end = step->valid;
1891 /* other events do not need syncing */
1892 /* FIXME, maybe NEWSEGMENT might need synchronisation
1893 * since the POSITION query depends on accumulated times and
1894 * we cannot accumulate the current segment before the previous
1904 /* else do buffer sync code */
1905 buffer = GST_BUFFER_CAST (obj);
1907 bclass = GST_BASE_SINK_GET_CLASS (basesink);
1909 /* just get the times to see if we need syncing, if the start returns -1 we
1911 if (bclass->get_times)
1912 bclass->get_times (basesink, buffer, &start, &stop);
1914 if (!GST_CLOCK_TIME_IS_VALID (start)) {
1915 /* we don't need to sync but we still want to get the timestamps for
1916 * tracking the position */
1917 gst_base_sink_get_times (basesink, buffer, &start, &stop);
1923 GST_DEBUG_OBJECT (basesink, "got times start: %" GST_TIME_FORMAT
1924 ", stop: %" GST_TIME_FORMAT ", do_sync %d", GST_TIME_ARGS (start),
1925 GST_TIME_ARGS (stop), *do_sync);
1927 /* collect segment and format for code clarity */
1928 format = segment->format;
1930 /* no timestamp clipping if we did not get a TIME segment format */
1931 if (G_UNLIKELY (format != GST_FORMAT_TIME)) {
1934 /* do running and stream time in TIME format */
1935 format = GST_FORMAT_TIME;
1936 GST_LOG_OBJECT (basesink, "not time format, don't clip");
1940 /* clip, only when we know about time */
1941 if (G_UNLIKELY (!gst_segment_clip (segment, GST_FORMAT_TIME,
1942 (gint64) start, (gint64) stop, &cstart, &cstop))) {
1944 GST_DEBUG_OBJECT (basesink, "step out of segment");
1945 /* when we are stepping, pretend we're at the end of the segment */
1946 if (segment->rate > 0.0) {
1947 cstart = segment->stop;
1948 cstop = segment->stop;
1950 cstart = segment->start;
1951 cstop = segment->start;
1955 goto out_of_segment;
1958 if (G_UNLIKELY (start != cstart || stop != cstop)) {
1959 GST_DEBUG_OBJECT (basesink, "clipped to: start %" GST_TIME_FORMAT
1960 ", stop: %" GST_TIME_FORMAT, GST_TIME_ARGS (cstart),
1961 GST_TIME_ARGS (cstop));
1964 /* set last stop position */
1965 if (G_LIKELY (cstop != GST_CLOCK_TIME_NONE))
1966 gst_segment_set_last_stop (segment, GST_FORMAT_TIME, cstop);
1968 gst_segment_set_last_stop (segment, GST_FORMAT_TIME, cstart);
1971 rstart = gst_segment_to_running_time (segment, format, cstart);
1972 rstop = gst_segment_to_running_time (segment, format, cstop);
1974 if (G_UNLIKELY (step->valid)) {
1975 if (!(*step_end = handle_stepping (basesink, segment, step, &cstart, &cstop,
1976 &rstart, &rstop))) {
1977 /* step is still busy, we discard data when we are flushing */
1978 *stepped = step->flush;
1979 GST_DEBUG_OBJECT (basesink, "stepping busy");
1982 /* this can produce wrong values if we accumulated non-TIME segments. If this happens,
1983 * upstream is behaving very badly */
1984 sstart = gst_segment_to_stream_time (segment, format, cstart);
1985 sstop = gst_segment_to_stream_time (segment, format, cstop);
1988 /* eos_done label only called when doing EOS, we also stop stepping then */
1989 if (*step_end && step->flush) {
1990 GST_DEBUG_OBJECT (basesink, "flushing step ended");
1991 stop_stepping (basesink, segment, step, rstart, rstop, eos);
1993 /* re-determine running start times for adjusted segment
1994 * (which has a flushed amount of running/accumulated time removed) */
1995 if (!GST_IS_EVENT (obj)) {
1996 GST_DEBUG_OBJECT (basesink, "refresh sync times");
2007 /* buffers and EOS always need syncing and preroll */
2013 /* we usually clip in the chain function already but stepping could cause
2014 * the segment to be updated later. we return FALSE so that we don't try
2016 GST_LOG_OBJECT (basesink, "buffer skipped, not in segment");
2021 /* with STREAM_LOCK, PREROLL_LOCK, LOCK
2022 * adjust a timestamp with the latency and timestamp offset. This function does
2023 * not adjust for the render delay. */
2025 gst_base_sink_adjust_time (GstBaseSink * basesink, GstClockTime time)
2027 GstClockTimeDiff ts_offset;
2029 /* don't do anything funny with invalid timestamps */
2030 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (time)))
2033 time += basesink->priv->latency;
2035 /* apply offset, be carefull for underflows */
2036 ts_offset = basesink->priv->ts_offset;
2037 if (ts_offset < 0) {
2038 ts_offset = -ts_offset;
2039 if (ts_offset < time)
2046 /* subtract the render delay again, which was included in the latency */
2047 if (time > basesink->priv->render_delay)
2048 time -= basesink->priv->render_delay;
2056 * gst_base_sink_wait_clock:
2058 * @time: the running_time to be reached
2059 * @jitter: (out) (allow-none): the jitter to be filled with time diff, or NULL
2061 * This function will block until @time is reached. It is usually called by
2062 * subclasses that use their own internal synchronisation.
2064 * If @time is not valid, no sycnhronisation is done and #GST_CLOCK_BADTIME is
2065 * returned. Likewise, if synchronisation is disabled in the element or there
2066 * is no clock, no synchronisation is done and #GST_CLOCK_BADTIME is returned.
2068 * This function should only be called with the PREROLL_LOCK held, like when
2069 * receiving an EOS event in the #GstBaseSinkClass.event() vmethod or when
2070 * receiving a buffer in
2071 * the #GstBaseSinkClass.render() vmethod.
2073 * The @time argument should be the running_time of when this method should
2074 * return and is not adjusted with any latency or offset configured in the
2079 * Returns: #GstClockReturn
2082 gst_base_sink_wait_clock (GstBaseSink * sink, GstClockTime time,
2083 GstClockTimeDiff * jitter)
2087 GstClockTime base_time;
2089 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (time)))
2092 GST_OBJECT_LOCK (sink);
2093 if (G_UNLIKELY (!sink->sync))
2096 if (G_UNLIKELY ((clock = GST_ELEMENT_CLOCK (sink)) == NULL))
2099 base_time = GST_ELEMENT_CAST (sink)->base_time;
2100 GST_LOG_OBJECT (sink,
2101 "time %" GST_TIME_FORMAT ", base_time %" GST_TIME_FORMAT,
2102 GST_TIME_ARGS (time), GST_TIME_ARGS (base_time));
2104 /* add base_time to running_time to get the time against the clock */
2107 /* Re-use existing clockid if available */
2108 if (G_LIKELY (sink->priv->cached_clock_id != NULL)) {
2109 if (!gst_clock_single_shot_id_reinit (clock, sink->priv->cached_clock_id,
2111 gst_clock_id_unref (sink->priv->cached_clock_id);
2112 sink->priv->cached_clock_id = gst_clock_new_single_shot_id (clock, time);
2115 sink->priv->cached_clock_id = gst_clock_new_single_shot_id (clock, time);
2116 GST_OBJECT_UNLOCK (sink);
2118 /* A blocking wait is performed on the clock. We save the ClockID
2119 * so we can unlock the entry at any time. While we are blocking, we
2120 * release the PREROLL_LOCK so that other threads can interrupt the
2122 sink->clock_id = sink->priv->cached_clock_id;
2123 /* release the preroll lock while waiting */
2124 GST_PAD_PREROLL_UNLOCK (sink->sinkpad);
2126 ret = gst_clock_id_wait (sink->priv->cached_clock_id, jitter);
2128 GST_PAD_PREROLL_LOCK (sink->sinkpad);
2129 sink->clock_id = NULL;
2133 /* no syncing needed */
2136 GST_DEBUG_OBJECT (sink, "time not valid, no sync needed");
2137 return GST_CLOCK_BADTIME;
2141 GST_DEBUG_OBJECT (sink, "sync disabled");
2142 GST_OBJECT_UNLOCK (sink);
2143 return GST_CLOCK_BADTIME;
2147 GST_DEBUG_OBJECT (sink, "no clock, can't sync");
2148 GST_OBJECT_UNLOCK (sink);
2149 return GST_CLOCK_BADTIME;
2154 * gst_base_sink_wait_preroll:
2157 * If the #GstBaseSinkClass.render() method performs its own synchronisation
2158 * against the clock it must unblock when going from PLAYING to the PAUSED state
2159 * and call this method before continuing to render the remaining data.
2161 * This function will block until a state change to PLAYING happens (in which
2162 * case this function returns #GST_FLOW_OK) or the processing must be stopped due
2163 * to a state change to READY or a FLUSH event (in which case this function
2164 * returns #GST_FLOW_WRONG_STATE).
2166 * This function should only be called with the PREROLL_LOCK held, like in the
2169 * Returns: #GST_FLOW_OK if the preroll completed and processing can
2170 * continue. Any other return value should be returned from the render vmethod.
2175 gst_base_sink_wait_preroll (GstBaseSink * sink)
2177 sink->have_preroll = TRUE;
2178 GST_DEBUG_OBJECT (sink, "waiting in preroll for flush or PLAYING");
2179 /* block until the state changes, or we get a flush, or something */
2180 GST_PAD_PREROLL_WAIT (sink->sinkpad);
2181 sink->have_preroll = FALSE;
2182 if (G_UNLIKELY (sink->flushing))
2184 if (G_UNLIKELY (sink->priv->step_unlock))
2186 GST_DEBUG_OBJECT (sink, "continue after preroll");
2193 GST_DEBUG_OBJECT (sink, "preroll interrupted because of flush");
2194 return GST_FLOW_WRONG_STATE;
2198 sink->priv->step_unlock = FALSE;
2199 GST_DEBUG_OBJECT (sink, "preroll interrupted because of step");
2200 return GST_FLOW_STEP;
2204 static inline guint8
2205 get_object_type (GstMiniObject * obj)
2209 if (G_LIKELY (GST_IS_BUFFER (obj)))
2210 obj_type = _PR_IS_BUFFER;
2211 else if (GST_IS_EVENT (obj))
2212 obj_type = _PR_IS_EVENT;
2213 else if (GST_IS_BUFFER_LIST (obj))
2214 obj_type = _PR_IS_BUFFERLIST;
2216 obj_type = _PR_IS_NOTHING;
2222 * gst_base_sink_do_preroll:
2224 * @obj: (transfer none): the mini object that caused the preroll
2226 * If the @sink spawns its own thread for pulling buffers from upstream it
2227 * should call this method after it has pulled a buffer. If the element needed
2228 * to preroll, this function will perform the preroll and will then block
2229 * until the element state is changed.
2231 * This function should be called with the PREROLL_LOCK held.
2233 * Returns: #GST_FLOW_OK if the preroll completed and processing can
2234 * continue. Any other return value should be returned from the render vmethod.
2239 gst_base_sink_do_preroll (GstBaseSink * sink, GstMiniObject * obj)
2243 while (G_UNLIKELY (sink->need_preroll)) {
2245 GST_DEBUG_OBJECT (sink, "prerolling object %p", obj);
2247 obj_type = get_object_type (obj);
2249 ret = gst_base_sink_preroll_object (sink, obj_type, obj);
2250 if (ret != GST_FLOW_OK)
2251 goto preroll_failed;
2253 /* need to recheck here because the commit state could have
2254 * made us not need the preroll anymore */
2255 if (G_LIKELY (sink->need_preroll)) {
2256 /* block until the state changes, or we get a flush, or something */
2257 ret = gst_base_sink_wait_preroll (sink);
2258 if ((ret != GST_FLOW_OK) && (ret != GST_FLOW_STEP))
2259 goto preroll_failed;
2267 GST_DEBUG_OBJECT (sink, "preroll failed %d", ret);
2273 * gst_base_sink_wait_eos:
2275 * @time: the running_time to be reached
2276 * @jitter: (out) (allow-none): the jitter to be filled with time diff, or NULL
2278 * This function will block until @time is reached. It is usually called by
2279 * subclasses that use their own internal synchronisation but want to let the
2280 * EOS be handled by the base class.
2282 * This function should only be called with the PREROLL_LOCK held, like when
2283 * receiving an EOS event in the ::event vmethod.
2285 * The @time argument should be the running_time of when the EOS should happen
2286 * and will be adjusted with any latency and offset configured in the sink.
2288 * Returns: #GstFlowReturn
2293 gst_base_sink_wait_eos (GstBaseSink * sink, GstClockTime time,
2294 GstClockTimeDiff * jitter)
2296 GstClockReturn status;
2302 GST_DEBUG_OBJECT (sink, "checking preroll");
2304 /* first wait for the playing state before we can continue */
2305 while (G_UNLIKELY (sink->need_preroll)) {
2306 ret = gst_base_sink_wait_preroll (sink);
2307 if ((ret != GST_FLOW_OK) && (ret != GST_FLOW_STEP))
2311 /* preroll done, we can sync since we are in PLAYING now. */
2312 GST_DEBUG_OBJECT (sink, "possibly waiting for clock to reach %"
2313 GST_TIME_FORMAT, GST_TIME_ARGS (time));
2315 /* compensate for latency and ts_offset. We don't adjust for render delay
2316 * because we don't interact with the device on EOS normally. */
2317 stime = gst_base_sink_adjust_time (sink, time);
2319 /* wait for the clock, this can be interrupted because we got shut down or
2321 status = gst_base_sink_wait_clock (sink, stime, jitter);
2323 GST_DEBUG_OBJECT (sink, "clock returned %d", status);
2325 /* invalid time, no clock or sync disabled, just continue then */
2326 if (status == GST_CLOCK_BADTIME)
2329 /* waiting could have been interrupted and we can be flushing now */
2330 if (G_UNLIKELY (sink->flushing))
2333 /* retry if we got unscheduled, which means we did not reach the timeout
2334 * yet. if some other error occures, we continue. */
2335 } while (status == GST_CLOCK_UNSCHEDULED);
2337 GST_DEBUG_OBJECT (sink, "end of stream");
2344 GST_DEBUG_OBJECT (sink, "we are flushing");
2345 return GST_FLOW_WRONG_STATE;
2349 /* with STREAM_LOCK, PREROLL_LOCK
2351 * Make sure we are in PLAYING and synchronize an object to the clock.
2353 * If we need preroll, we are not in PLAYING. We try to commit the state
2354 * if needed and then block if we still are not PLAYING.
2356 * We start waiting on the clock in PLAYING. If we got interrupted, we
2357 * immediatly try to re-preroll.
2359 * Some objects do not need synchronisation (most events) and so this function
2360 * immediatly returns GST_FLOW_OK.
2362 * for objects that arrive later than max-lateness to be synchronized to the
2363 * clock have the @late boolean set to TRUE.
2365 * This function keeps a running average of the jitter (the diff between the
2366 * clock time and the requested sync time). The jitter is negative for
2367 * objects that arrive in time and positive for late buffers.
2369 * does not take ownership of obj.
2371 static GstFlowReturn
2372 gst_base_sink_do_sync (GstBaseSink * basesink, GstPad * pad,
2373 GstMiniObject * obj, gboolean * late, gboolean * step_end, guint8 obj_type)
2375 GstClockTimeDiff jitter = 0;
2377 GstClockReturn status = GST_CLOCK_OK;
2378 GstClockTime rstart, rstop, sstart, sstop, stime;
2380 GstBaseSinkPrivate *priv;
2382 GstStepInfo *current, *pending;
2385 priv = basesink->priv;
2388 sstart = sstop = rstart = rstop = GST_CLOCK_TIME_NONE;
2392 priv->current_rstart = GST_CLOCK_TIME_NONE;
2394 /* get stepping info */
2395 current = &priv->current_step;
2396 pending = &priv->pending_step;
2398 /* get timing information for this object against the render segment */
2399 syncable = gst_base_sink_get_sync_times (basesink, obj,
2400 &sstart, &sstop, &rstart, &rstop, &do_sync, &stepped, &basesink->segment,
2401 current, step_end, obj_type);
2403 if (G_UNLIKELY (stepped))
2406 /* a syncable object needs to participate in preroll and
2407 * clocking. All buffers and EOS are syncable. */
2408 if (G_UNLIKELY (!syncable))
2411 /* store timing info for current object */
2412 priv->current_rstart = rstart;
2413 priv->current_rstop = (GST_CLOCK_TIME_IS_VALID (rstop) ? rstop : rstart);
2415 /* save sync time for eos when the previous object needed sync */
2416 priv->eos_rtime = (do_sync ? priv->current_rstop : GST_CLOCK_TIME_NONE);
2419 /* first do preroll, this makes sure we commit our state
2420 * to PAUSED and can continue to PLAYING. We cannot perform
2421 * any clock sync in PAUSED because there is no clock. */
2422 ret = gst_base_sink_do_preroll (basesink, obj);
2423 if (G_UNLIKELY (ret != GST_FLOW_OK))
2424 goto preroll_failed;
2426 /* update the segment with a pending step if the current one is invalid and we
2427 * have a new pending one. We only accept new step updates after a preroll */
2428 if (G_UNLIKELY (pending->valid && !current->valid)) {
2429 start_stepping (basesink, &basesink->segment, pending, current);
2433 /* After rendering we store the position of the last buffer so that we can use
2434 * it to report the position. We need to take the lock here. */
2435 GST_OBJECT_LOCK (basesink);
2436 priv->current_sstart = sstart;
2437 priv->current_sstop = (GST_CLOCK_TIME_IS_VALID (sstop) ? sstop : sstart);
2438 GST_OBJECT_UNLOCK (basesink);
2443 /* adjust for latency */
2444 stime = gst_base_sink_adjust_time (basesink, rstart);
2446 /* adjust for render-delay, avoid underflows */
2447 if (GST_CLOCK_TIME_IS_VALID (stime)) {
2448 if (stime > priv->render_delay)
2449 stime -= priv->render_delay;
2454 /* preroll done, we can sync since we are in PLAYING now. */
2455 GST_DEBUG_OBJECT (basesink, "possibly waiting for clock to reach %"
2456 GST_TIME_FORMAT ", adjusted %" GST_TIME_FORMAT,
2457 GST_TIME_ARGS (rstart), GST_TIME_ARGS (stime));
2459 /* This function will return immediatly if start == -1, no clock
2460 * or sync is disabled with GST_CLOCK_BADTIME. */
2461 status = gst_base_sink_wait_clock (basesink, stime, &jitter);
2463 GST_DEBUG_OBJECT (basesink, "clock returned %d, jitter %c%" GST_TIME_FORMAT,
2464 status, (jitter < 0 ? '-' : ' '), GST_TIME_ARGS (ABS (jitter)));
2466 /* invalid time, no clock or sync disabled, just render */
2467 if (status == GST_CLOCK_BADTIME)
2470 /* waiting could have been interrupted and we can be flushing now */
2471 if (G_UNLIKELY (basesink->flushing))
2474 /* check for unlocked by a state change, we are not flushing so
2475 * we can try to preroll on the current buffer. */
2476 if (G_UNLIKELY (status == GST_CLOCK_UNSCHEDULED)) {
2477 GST_DEBUG_OBJECT (basesink, "unscheduled, waiting some more");
2478 priv->call_preroll = TRUE;
2482 /* successful syncing done, record observation */
2483 priv->current_jitter = jitter;
2485 /* check if the object should be dropped */
2486 *late = gst_base_sink_is_too_late (basesink, obj, rstart, rstop,
2495 GST_DEBUG_OBJECT (basesink, "skipped stepped object %p", obj);
2501 GST_DEBUG_OBJECT (basesink, "non syncable object %p", obj);
2506 GST_DEBUG_OBJECT (basesink, "we are flushing");
2507 return GST_FLOW_WRONG_STATE;
2511 GST_DEBUG_OBJECT (basesink, "preroll failed");
2518 gst_base_sink_send_qos (GstBaseSink * basesink,
2519 gdouble proportion, GstClockTime time, GstClockTimeDiff diff)
2524 /* generate Quality-of-Service event */
2525 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, basesink,
2526 "qos: proportion: %lf, diff %" G_GINT64_FORMAT ", timestamp %"
2527 GST_TIME_FORMAT, proportion, diff, GST_TIME_ARGS (time));
2529 event = gst_event_new_qos (proportion, diff, time);
2532 res = gst_pad_push_event (basesink->sinkpad, event);
2538 gst_base_sink_perform_qos (GstBaseSink * sink, gboolean dropped)
2540 GstBaseSinkPrivate *priv;
2541 GstClockTime start, stop;
2542 GstClockTimeDiff jitter;
2543 GstClockTime pt, entered, left;
2544 GstClockTime duration;
2549 start = priv->current_rstart;
2551 if (priv->current_step.valid)
2554 /* if Quality-of-Service disabled, do nothing */
2555 if (!g_atomic_int_get (&priv->qos_enabled) ||
2556 !GST_CLOCK_TIME_IS_VALID (start))
2559 stop = priv->current_rstop;
2560 jitter = priv->current_jitter;
2563 /* this is the time the buffer entered the sink */
2564 if (start < -jitter)
2567 entered = start + jitter;
2570 /* this is the time the buffer entered the sink */
2571 entered = start + jitter;
2572 /* this is the time the buffer left the sink */
2573 left = start + jitter;
2576 /* calculate duration of the buffer */
2577 if (GST_CLOCK_TIME_IS_VALID (stop))
2578 duration = stop - start;
2580 duration = GST_CLOCK_TIME_NONE;
2582 /* if we have the time when the last buffer left us, calculate
2583 * processing time */
2584 if (GST_CLOCK_TIME_IS_VALID (priv->last_left)) {
2585 if (entered > priv->last_left) {
2586 pt = entered - priv->last_left;
2594 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, sink, "start: %" GST_TIME_FORMAT
2595 ", entered %" GST_TIME_FORMAT ", left %" GST_TIME_FORMAT ", pt: %"
2596 GST_TIME_FORMAT ", duration %" GST_TIME_FORMAT ",jitter %"
2597 G_GINT64_FORMAT, GST_TIME_ARGS (start), GST_TIME_ARGS (entered),
2598 GST_TIME_ARGS (left), GST_TIME_ARGS (pt), GST_TIME_ARGS (duration),
2601 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, sink, "avg_duration: %" GST_TIME_FORMAT
2602 ", avg_pt: %" GST_TIME_FORMAT ", avg_rate: %g",
2603 GST_TIME_ARGS (priv->avg_duration), GST_TIME_ARGS (priv->avg_pt),
2606 /* collect running averages. for first observations, we copy the
2608 if (!GST_CLOCK_TIME_IS_VALID (priv->avg_duration))
2609 priv->avg_duration = duration;
2611 priv->avg_duration = UPDATE_RUNNING_AVG (priv->avg_duration, duration);
2613 if (!GST_CLOCK_TIME_IS_VALID (priv->avg_pt))
2616 priv->avg_pt = UPDATE_RUNNING_AVG (priv->avg_pt, pt);
2618 if (priv->avg_duration != 0)
2620 gst_guint64_to_gdouble (priv->avg_pt) /
2621 gst_guint64_to_gdouble (priv->avg_duration);
2625 if (GST_CLOCK_TIME_IS_VALID (priv->last_left)) {
2626 if (dropped || priv->avg_rate < 0.0) {
2627 priv->avg_rate = rate;
2630 priv->avg_rate = UPDATE_RUNNING_AVG_N (priv->avg_rate, rate);
2632 priv->avg_rate = UPDATE_RUNNING_AVG_P (priv->avg_rate, rate);
2636 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, sink,
2637 "updated: avg_duration: %" GST_TIME_FORMAT ", avg_pt: %" GST_TIME_FORMAT
2638 ", avg_rate: %g", GST_TIME_ARGS (priv->avg_duration),
2639 GST_TIME_ARGS (priv->avg_pt), priv->avg_rate);
2642 if (priv->avg_rate >= 0.0) {
2643 /* if we have a valid rate, start sending QoS messages */
2644 if (priv->current_jitter < 0) {
2645 /* make sure we never go below 0 when adding the jitter to the
2647 if (priv->current_rstart < -priv->current_jitter)
2648 priv->current_jitter = -priv->current_rstart;
2650 gst_base_sink_send_qos (sink, priv->avg_rate, priv->current_rstart,
2651 priv->current_jitter);
2654 /* record when this buffer will leave us */
2655 priv->last_left = left;
2658 /* reset all qos measuring */
2660 gst_base_sink_reset_qos (GstBaseSink * sink)
2662 GstBaseSinkPrivate *priv;
2666 priv->last_in_time = GST_CLOCK_TIME_NONE;
2667 priv->last_left = GST_CLOCK_TIME_NONE;
2668 priv->avg_duration = GST_CLOCK_TIME_NONE;
2669 priv->avg_pt = GST_CLOCK_TIME_NONE;
2670 priv->avg_rate = -1.0;
2671 priv->avg_render = GST_CLOCK_TIME_NONE;
2677 /* Checks if the object was scheduled too late.
2679 * start/stop contain the raw timestamp start and stop values
2682 * status and jitter contain the return values from the clock wait.
2684 * returns TRUE if the buffer was too late.
2687 gst_base_sink_is_too_late (GstBaseSink * basesink, GstMiniObject * obj,
2688 GstClockTime start, GstClockTime stop,
2689 GstClockReturn status, GstClockTimeDiff jitter)
2692 gint64 max_lateness;
2693 GstBaseSinkPrivate *priv;
2695 priv = basesink->priv;
2699 /* only for objects that were too late */
2700 if (G_LIKELY (status != GST_CLOCK_EARLY))
2703 max_lateness = basesink->abidata.ABI.max_lateness;
2705 /* check if frame dropping is enabled */
2706 if (max_lateness == -1)
2709 /* only check for buffers */
2710 if (G_UNLIKELY (!GST_IS_BUFFER (obj)))
2713 /* can't do check if we don't have a timestamp */
2714 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (start)))
2717 /* we can add a valid stop time */
2718 if (GST_CLOCK_TIME_IS_VALID (stop))
2719 max_lateness += stop;
2721 max_lateness += start;
2723 /* if the jitter bigger than duration and lateness we are too late */
2724 if ((late = start + jitter > max_lateness)) {
2725 GST_CAT_DEBUG_OBJECT (GST_CAT_PERFORMANCE, basesink,
2726 "buffer is too late %" GST_TIME_FORMAT
2727 " > %" GST_TIME_FORMAT, GST_TIME_ARGS (start + jitter),
2728 GST_TIME_ARGS (max_lateness));
2729 /* !!emergency!!, if we did not receive anything valid for more than a
2730 * second, render it anyway so the user sees something */
2731 if (GST_CLOCK_TIME_IS_VALID (priv->last_in_time) &&
2732 start - priv->last_in_time > GST_SECOND) {
2734 GST_ELEMENT_WARNING (basesink, CORE, CLOCK,
2735 (_("A lot of buffers are being dropped.")),
2736 ("There may be a timestamping problem, or this computer is too slow."));
2737 GST_CAT_DEBUG_OBJECT (GST_CAT_PERFORMANCE, basesink,
2738 "**emergency** last buffer at %" GST_TIME_FORMAT " > GST_SECOND",
2739 GST_TIME_ARGS (priv->last_in_time));
2744 if (!late || !GST_CLOCK_TIME_IS_VALID (priv->last_in_time)) {
2745 priv->last_in_time = start;
2752 GST_DEBUG_OBJECT (basesink, "object was scheduled in time");
2757 GST_DEBUG_OBJECT (basesink, "frame dropping disabled");
2762 GST_DEBUG_OBJECT (basesink, "object is not a buffer");
2767 GST_DEBUG_OBJECT (basesink, "buffer has no timestamp");
2772 /* called before and after calling the render vmethod. It keeps track of how
2773 * much time was spent in the render method and is used to check if we are
2776 gst_base_sink_do_render_stats (GstBaseSink * basesink, gboolean start)
2778 GstBaseSinkPrivate *priv;
2780 priv = basesink->priv;
2783 priv->start = gst_util_get_timestamp ();
2785 GstClockTime elapsed;
2787 priv->stop = gst_util_get_timestamp ();
2789 elapsed = GST_CLOCK_DIFF (priv->start, priv->stop);
2791 if (!GST_CLOCK_TIME_IS_VALID (priv->avg_render))
2792 priv->avg_render = elapsed;
2794 priv->avg_render = UPDATE_RUNNING_AVG (priv->avg_render, elapsed);
2796 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, basesink,
2797 "avg_render: %" GST_TIME_FORMAT, GST_TIME_ARGS (priv->avg_render));
2801 /* with STREAM_LOCK, PREROLL_LOCK,
2803 * Synchronize the object on the clock and then render it.
2805 * takes ownership of obj.
2807 static GstFlowReturn
2808 gst_base_sink_render_object (GstBaseSink * basesink, GstPad * pad,
2809 guint8 obj_type, gpointer obj)
2812 GstBaseSinkClass *bclass;
2813 gboolean late, step_end;
2815 GstBaseSinkPrivate *priv;
2817 priv = basesink->priv;
2819 if (OBJ_IS_BUFFERLIST (obj_type)) {
2821 * If buffer list, use the first group buffer within the list
2824 sync_obj = gst_buffer_list_get (GST_BUFFER_LIST_CAST (obj), 0, 0);
2825 g_assert (NULL != sync_obj);
2834 /* synchronize this object, non syncable objects return OK
2837 gst_base_sink_do_sync (basesink, pad, sync_obj, &late, &step_end,
2839 if (G_UNLIKELY (ret != GST_FLOW_OK))
2842 /* and now render, event or buffer/buffer list. */
2843 if (G_LIKELY (OBJ_IS_BUFFERFULL (obj_type))) {
2844 /* drop late buffers unconditionally, let's hope it's unlikely */
2845 if (G_UNLIKELY (late))
2848 bclass = GST_BASE_SINK_GET_CLASS (basesink);
2850 if (G_LIKELY ((OBJ_IS_BUFFERLIST (obj_type) && bclass->render_list) ||
2851 (!OBJ_IS_BUFFERLIST (obj_type) && bclass->render))) {
2854 /* read once, to get same value before and after */
2855 do_qos = g_atomic_int_get (&priv->qos_enabled);
2857 GST_DEBUG_OBJECT (basesink, "rendering object %p", obj);
2859 /* record rendering time for QoS and stats */
2861 gst_base_sink_do_render_stats (basesink, TRUE);
2863 if (!OBJ_IS_BUFFERLIST (obj_type)) {
2866 /* For buffer lists do not set last buffer. Creating buffer
2867 * with meaningful data can be done only with memcpy which will
2868 * significantly affect performance */
2869 buf = GST_BUFFER_CAST (obj);
2870 gst_base_sink_set_last_buffer (basesink, buf);
2872 ret = bclass->render (basesink, buf);
2874 GstBufferList *buflist;
2876 buflist = GST_BUFFER_LIST_CAST (obj);
2878 ret = bclass->render_list (basesink, buflist);
2882 gst_base_sink_do_render_stats (basesink, FALSE);
2884 if (ret == GST_FLOW_STEP)
2887 if (G_UNLIKELY (basesink->flushing))
2892 } else if (G_LIKELY (OBJ_IS_EVENT (obj_type))) {
2893 GstEvent *event = GST_EVENT_CAST (obj);
2894 gboolean event_res = TRUE;
2897 bclass = GST_BASE_SINK_GET_CLASS (basesink);
2899 type = GST_EVENT_TYPE (event);
2901 GST_DEBUG_OBJECT (basesink, "rendering event %p, type %s", obj,
2902 gst_event_type_get_name (type));
2905 event_res = bclass->event (basesink, event);
2907 /* when we get here we could be flushing again when the event handler calls
2908 * _wait_eos(). We have to ignore this object in that case. */
2909 if (G_UNLIKELY (basesink->flushing))
2912 if (G_LIKELY (event_res)) {
2915 seqnum = basesink->priv->seqnum = gst_event_get_seqnum (event);
2916 GST_DEBUG_OBJECT (basesink, "Got seqnum #%" G_GUINT32_FORMAT, seqnum);
2921 GstMessage *message;
2923 /* the EOS event is completely handled so we mark
2924 * ourselves as being in the EOS state. eos is also
2925 * protected by the object lock so we can read it when
2926 * answering the POSITION query. */
2927 GST_OBJECT_LOCK (basesink);
2928 basesink->eos = TRUE;
2929 GST_OBJECT_UNLOCK (basesink);
2931 /* ok, now we can post the message */
2932 GST_DEBUG_OBJECT (basesink, "Now posting EOS");
2934 message = gst_message_new_eos (GST_OBJECT_CAST (basesink));
2935 gst_message_set_seqnum (message, seqnum);
2936 gst_element_post_message (GST_ELEMENT_CAST (basesink), message);
2939 case GST_EVENT_NEWSEGMENT:
2940 /* configure the segment */
2941 gst_base_sink_configure_segment (basesink, pad, event,
2942 &basesink->segment);
2944 case GST_EVENT_SINK_MESSAGE:{
2945 GstMessage *msg = NULL;
2947 gst_event_parse_sink_message (event, &msg);
2950 gst_element_post_message (GST_ELEMENT_CAST (basesink), msg);
2957 g_return_val_if_reached (GST_FLOW_ERROR);
2962 /* the step ended, check if we need to activate a new step */
2963 GST_DEBUG_OBJECT (basesink, "step ended");
2964 stop_stepping (basesink, &basesink->segment, &priv->current_step,
2965 priv->current_rstart, priv->current_rstop, basesink->eos);
2969 gst_base_sink_perform_qos (basesink, late);
2971 GST_DEBUG_OBJECT (basesink, "object unref after render %p", obj);
2972 gst_mini_object_unref (GST_MINI_OBJECT_CAST (obj));
2978 GST_DEBUG_OBJECT (basesink, "do_sync returned %s", gst_flow_get_name (ret));
2984 GST_DEBUG_OBJECT (basesink, "buffer late, dropping");
2986 if (g_atomic_int_get (&priv->qos_enabled)) {
2987 GstMessage *qos_msg;
2988 GstClockTime timestamp, duration;
2990 timestamp = GST_BUFFER_TIMESTAMP (GST_BUFFER_CAST (sync_obj));
2991 duration = GST_BUFFER_DURATION (GST_BUFFER_CAST (sync_obj));
2993 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, basesink,
2994 "qos: dropped buffer rt %" GST_TIME_FORMAT ", st %" GST_TIME_FORMAT
2995 ", ts %" GST_TIME_FORMAT ", dur %" GST_TIME_FORMAT,
2996 GST_TIME_ARGS (priv->current_rstart),
2997 GST_TIME_ARGS (priv->current_sstart), GST_TIME_ARGS (timestamp),
2998 GST_TIME_ARGS (duration));
2999 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, basesink,
3000 "qos: rendered %" G_GUINT64_FORMAT ", dropped %" G_GUINT64_FORMAT,
3001 priv->rendered, priv->dropped);
3004 gst_message_new_qos (GST_OBJECT_CAST (basesink), basesink->sync,
3005 priv->current_rstart, priv->current_sstart, timestamp, duration);
3006 gst_message_set_qos_values (qos_msg, priv->current_jitter, priv->avg_rate,
3008 gst_message_set_qos_stats (qos_msg, GST_FORMAT_BUFFERS, priv->rendered,
3010 gst_element_post_message (GST_ELEMENT_CAST (basesink), qos_msg);
3016 GST_DEBUG_OBJECT (basesink, "we are flushing, ignore object");
3017 gst_mini_object_unref (obj);
3018 return GST_FLOW_WRONG_STATE;
3022 /* with STREAM_LOCK, PREROLL_LOCK
3024 * Perform preroll on the given object. For buffers this means
3025 * calling the preroll subclass method.
3026 * If that succeeds, the state will be commited.
3028 * function does not take ownership of obj.
3030 static GstFlowReturn
3031 gst_base_sink_preroll_object (GstBaseSink * basesink, guint8 obj_type,
3032 GstMiniObject * obj)
3036 GST_DEBUG_OBJECT (basesink, "prerolling object %p", obj);
3038 /* if it's a buffer, we need to call the preroll method */
3039 if (G_LIKELY (OBJ_IS_BUFFERFULL (obj_type) && basesink->priv->call_preroll)) {
3040 GstBaseSinkClass *bclass;
3042 GstClockTime timestamp;
3044 if (OBJ_IS_BUFFERLIST (obj_type)) {
3045 buf = gst_buffer_list_get (GST_BUFFER_LIST_CAST (obj), 0, 0);
3046 g_assert (NULL != buf);
3048 buf = GST_BUFFER_CAST (obj);
3051 timestamp = GST_BUFFER_TIMESTAMP (buf);
3053 GST_DEBUG_OBJECT (basesink, "preroll buffer %" GST_TIME_FORMAT,
3054 GST_TIME_ARGS (timestamp));
3057 * For buffer lists do not set last buffer. Creating buffer
3058 * with meaningful data can be done only with memcpy which will
3059 * significantly affect performance
3061 if (!OBJ_IS_BUFFERLIST (obj_type)) {
3062 gst_base_sink_set_last_buffer (basesink, buf);
3065 bclass = GST_BASE_SINK_GET_CLASS (basesink);
3066 if (bclass->preroll)
3067 if ((ret = bclass->preroll (basesink, buf)) != GST_FLOW_OK)
3068 goto preroll_failed;
3070 basesink->priv->call_preroll = FALSE;
3074 if (G_LIKELY (basesink->playing_async)) {
3075 if (G_UNLIKELY (!gst_base_sink_commit_state (basesink)))
3084 GST_DEBUG_OBJECT (basesink, "preroll failed, abort state");
3085 gst_element_abort_state (GST_ELEMENT_CAST (basesink));
3090 GST_DEBUG_OBJECT (basesink, "stopping while commiting state");
3091 return GST_FLOW_WRONG_STATE;
3095 /* with STREAM_LOCK, PREROLL_LOCK
3097 * Queue an object for rendering.
3098 * The first prerollable object queued will complete the preroll. If the
3099 * preroll queue if filled, we render all the objects in the queue.
3101 * This function takes ownership of the object.
3103 static GstFlowReturn
3104 gst_base_sink_queue_object_unlocked (GstBaseSink * basesink, GstPad * pad,
3105 guint8 obj_type, gpointer obj, gboolean prerollable)
3107 GstFlowReturn ret = GST_FLOW_OK;
3111 if (G_UNLIKELY (basesink->need_preroll)) {
3112 if (G_LIKELY (prerollable))
3113 basesink->preroll_queued++;
3115 length = basesink->preroll_queued;
3117 GST_DEBUG_OBJECT (basesink, "now %d prerolled items", length);
3119 /* first prerollable item needs to finish the preroll */
3121 ret = gst_base_sink_preroll_object (basesink, obj_type, obj);
3122 if (G_UNLIKELY (ret != GST_FLOW_OK))
3123 goto preroll_failed;
3125 /* need to recheck if we need preroll, commmit state during preroll
3126 * could have made us not need more preroll. */
3127 if (G_UNLIKELY (basesink->need_preroll)) {
3128 /* see if we can render now, if we can't add the object to the preroll
3130 if (G_UNLIKELY (length <= basesink->preroll_queue_max_len))
3134 /* we can start rendering (or blocking) the queued object
3136 q = basesink->preroll_queue;
3137 while (G_UNLIKELY (!g_queue_is_empty (q))) {
3141 o = g_queue_pop_head (q);
3142 GST_DEBUG_OBJECT (basesink, "rendering queued object %p", o);
3144 ot = get_object_type (o);
3146 /* do something with the return value */
3147 ret = gst_base_sink_render_object (basesink, pad, ot, o);
3148 if (ret != GST_FLOW_OK)
3149 goto dequeue_failed;
3152 /* now render the object */
3153 ret = gst_base_sink_render_object (basesink, pad, obj_type, obj);
3154 basesink->preroll_queued = 0;
3161 GST_DEBUG_OBJECT (basesink, "preroll failed, reason %s",
3162 gst_flow_get_name (ret));
3163 gst_mini_object_unref (GST_MINI_OBJECT_CAST (obj));
3168 /* add object to the queue and return */
3169 GST_DEBUG_OBJECT (basesink, "need more preroll data %d <= %d",
3170 length, basesink->preroll_queue_max_len);
3171 g_queue_push_tail (basesink->preroll_queue, obj);
3176 GST_DEBUG_OBJECT (basesink, "rendering queued objects failed, reason %s",
3177 gst_flow_get_name (ret));
3178 gst_mini_object_unref (GST_MINI_OBJECT_CAST (obj));
3185 * This function grabs the PREROLL_LOCK and adds the object to
3188 * This function takes ownership of obj.
3190 * Note: Only GstEvent seem to be passed to this private method
3192 static GstFlowReturn
3193 gst_base_sink_queue_object (GstBaseSink * basesink, GstPad * pad,
3194 GstMiniObject * obj, gboolean prerollable)
3198 GST_PAD_PREROLL_LOCK (pad);
3199 if (G_UNLIKELY (basesink->flushing))
3202 if (G_UNLIKELY (basesink->priv->received_eos))
3206 gst_base_sink_queue_object_unlocked (basesink, pad, _PR_IS_EVENT, obj,
3208 GST_PAD_PREROLL_UNLOCK (pad);
3215 GST_DEBUG_OBJECT (basesink, "sink is flushing");
3216 GST_PAD_PREROLL_UNLOCK (pad);
3217 gst_mini_object_unref (obj);
3218 return GST_FLOW_WRONG_STATE;
3222 GST_DEBUG_OBJECT (basesink,
3223 "we are EOS, dropping object, return UNEXPECTED");
3224 GST_PAD_PREROLL_UNLOCK (pad);
3225 gst_mini_object_unref (obj);
3226 return GST_FLOW_UNEXPECTED;
3231 gst_base_sink_flush_start (GstBaseSink * basesink, GstPad * pad)
3233 /* make sure we are not blocked on the clock also clear any pending
3235 gst_base_sink_set_flushing (basesink, pad, TRUE);
3237 /* we grab the stream lock but that is not needed since setting the
3238 * sink to flushing would make sure no state commit is being done
3240 GST_PAD_STREAM_LOCK (pad);
3241 gst_base_sink_reset_qos (basesink);
3242 if (basesink->priv->async_enabled) {
3243 /* and we need to commit our state again on the next
3244 * prerolled buffer */
3245 basesink->playing_async = TRUE;
3246 gst_element_lost_state (GST_ELEMENT_CAST (basesink));
3248 basesink->priv->have_latency = TRUE;
3250 gst_base_sink_set_last_buffer (basesink, NULL);
3251 GST_PAD_STREAM_UNLOCK (pad);
3255 gst_base_sink_flush_stop (GstBaseSink * basesink, GstPad * pad)
3257 /* unset flushing so we can accept new data, this also flushes out any EOS
3259 gst_base_sink_set_flushing (basesink, pad, FALSE);
3261 /* for position reporting */
3262 GST_OBJECT_LOCK (basesink);
3263 basesink->priv->current_sstart = GST_CLOCK_TIME_NONE;
3264 basesink->priv->current_sstop = GST_CLOCK_TIME_NONE;
3265 basesink->priv->eos_rtime = GST_CLOCK_TIME_NONE;
3266 basesink->priv->call_preroll = TRUE;
3267 basesink->priv->current_step.valid = FALSE;
3268 basesink->priv->pending_step.valid = FALSE;
3269 if (basesink->pad_mode == GST_ACTIVATE_PUSH) {
3270 /* we need new segment info after the flush. */
3271 basesink->have_newsegment = FALSE;
3272 gst_segment_init (&basesink->segment, GST_FORMAT_UNDEFINED);
3273 gst_segment_init (basesink->abidata.ABI.clip_segment, GST_FORMAT_UNDEFINED);
3275 GST_OBJECT_UNLOCK (basesink);
3279 gst_base_sink_event (GstPad * pad, GstEvent * event)
3281 GstBaseSink *basesink;
3282 gboolean result = TRUE;
3283 GstBaseSinkClass *bclass;
3285 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
3287 bclass = GST_BASE_SINK_GET_CLASS (basesink);
3289 GST_DEBUG_OBJECT (basesink, "reveived event %p %" GST_PTR_FORMAT, event,
3292 switch (GST_EVENT_TYPE (event)) {
3297 GST_PAD_PREROLL_LOCK (pad);
3298 if (G_UNLIKELY (basesink->flushing))
3301 if (G_UNLIKELY (basesink->priv->received_eos)) {
3302 /* we can't accept anything when we are EOS */
3304 gst_event_unref (event);
3306 /* we set the received EOS flag here so that we can use it when testing if
3307 * we are prerolled and to refuse more buffers. */
3308 basesink->priv->received_eos = TRUE;
3310 /* EOS is a prerollable object, we call the unlocked version because it
3311 * does not check the received_eos flag. */
3312 ret = gst_base_sink_queue_object_unlocked (basesink, pad,
3313 _PR_IS_EVENT, GST_MINI_OBJECT_CAST (event), TRUE);
3314 if (G_UNLIKELY (ret != GST_FLOW_OK))
3317 GST_PAD_PREROLL_UNLOCK (pad);
3320 case GST_EVENT_NEWSEGMENT:
3325 GST_DEBUG_OBJECT (basesink, "newsegment %p", event);
3327 GST_PAD_PREROLL_LOCK (pad);
3328 if (G_UNLIKELY (basesink->flushing))
3331 gst_event_parse_new_segment_full (event, &update, NULL, NULL, NULL, NULL,
3334 if (G_UNLIKELY (basesink->priv->received_eos && !update)) {
3335 /* we can't accept anything when we are EOS */
3337 gst_event_unref (event);
3339 /* the new segment is a non prerollable item and does not block anything,
3340 * we need to configure the current clipping segment and insert the event
3341 * in the queue to serialize it with the buffers for rendering. */
3342 gst_base_sink_configure_segment (basesink, pad, event,
3343 basesink->abidata.ABI.clip_segment);
3346 gst_base_sink_queue_object_unlocked (basesink, pad,
3347 _PR_IS_EVENT, GST_MINI_OBJECT_CAST (event), FALSE);
3348 if (G_UNLIKELY (ret != GST_FLOW_OK))
3351 GST_OBJECT_LOCK (basesink);
3352 basesink->have_newsegment = TRUE;
3353 GST_OBJECT_UNLOCK (basesink);
3356 GST_PAD_PREROLL_UNLOCK (pad);
3359 case GST_EVENT_FLUSH_START:
3361 bclass->event (basesink, event);
3363 GST_DEBUG_OBJECT (basesink, "flush-start %p", event);
3365 gst_base_sink_flush_start (basesink, pad);
3367 gst_event_unref (event);
3369 case GST_EVENT_FLUSH_STOP:
3371 bclass->event (basesink, event);
3373 GST_DEBUG_OBJECT (basesink, "flush-stop %p", event);
3375 gst_base_sink_flush_stop (basesink, pad);
3377 gst_event_unref (event);
3380 /* other events are sent to queue or subclass depending on if they
3381 * are serialized. */
3382 if (GST_EVENT_IS_SERIALIZED (event)) {
3383 gst_base_sink_queue_object (basesink, pad,
3384 GST_MINI_OBJECT_CAST (event), FALSE);
3387 bclass->event (basesink, event);
3388 gst_event_unref (event);
3393 gst_object_unref (basesink);
3400 GST_DEBUG_OBJECT (basesink, "we are flushing");
3401 GST_PAD_PREROLL_UNLOCK (pad);
3403 gst_event_unref (event);
3408 /* default implementation to calculate the start and end
3409 * timestamps on a buffer, subclasses can override
3412 gst_base_sink_get_times (GstBaseSink * basesink, GstBuffer * buffer,
3413 GstClockTime * start, GstClockTime * end)
3415 GstClockTime timestamp, duration;
3417 timestamp = GST_BUFFER_TIMESTAMP (buffer);
3418 if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
3420 /* get duration to calculate end time */
3421 duration = GST_BUFFER_DURATION (buffer);
3422 if (GST_CLOCK_TIME_IS_VALID (duration)) {
3423 *end = timestamp + duration;
3429 /* must be called with PREROLL_LOCK */
3431 gst_base_sink_needs_preroll (GstBaseSink * basesink)
3433 gboolean is_prerolled, res;
3435 /* we have 2 cases where the PREROLL_LOCK is released:
3436 * 1) we are blocking in the PREROLL_LOCK and thus are prerolled.
3437 * 2) we are syncing on the clock
3439 is_prerolled = basesink->have_preroll || basesink->priv->received_eos;
3440 res = !is_prerolled;
3442 GST_DEBUG_OBJECT (basesink, "have_preroll: %d, EOS: %d => needs preroll: %d",
3443 basesink->have_preroll, basesink->priv->received_eos, res);
3448 /* with STREAM_LOCK, PREROLL_LOCK
3450 * Takes a buffer and compare the timestamps with the last segment.
3451 * If the buffer falls outside of the segment boundaries, drop it.
3452 * Else queue the buffer for preroll and rendering.
3454 * This function takes ownership of the buffer.
3456 static GstFlowReturn
3457 gst_base_sink_chain_unlocked (GstBaseSink * basesink, GstPad * pad,
3458 guint8 obj_type, gpointer obj)
3460 GstBaseSinkClass *bclass;
3461 GstFlowReturn result;
3462 GstClockTime start = GST_CLOCK_TIME_NONE, end = GST_CLOCK_TIME_NONE;
3463 GstSegment *clip_segment;
3464 GstBuffer *time_buf;
3466 if (G_UNLIKELY (basesink->flushing))
3469 if (G_UNLIKELY (basesink->priv->received_eos))
3472 if (OBJ_IS_BUFFERLIST (obj_type)) {
3473 time_buf = gst_buffer_list_get (GST_BUFFER_LIST_CAST (obj), 0, 0);
3474 g_assert (NULL != time_buf);
3476 time_buf = GST_BUFFER_CAST (obj);
3479 /* for code clarity */
3480 clip_segment = basesink->abidata.ABI.clip_segment;
3482 if (G_UNLIKELY (!basesink->have_newsegment)) {
3485 sync = gst_base_sink_get_sync (basesink);
3487 GST_ELEMENT_WARNING (basesink, STREAM, FAILED,
3488 (_("Internal data flow problem.")),
3489 ("Received buffer without a new-segment. Assuming timestamps start from 0."));
3492 /* this means this sink will assume timestamps start from 0 */
3493 GST_OBJECT_LOCK (basesink);
3494 clip_segment->start = 0;
3495 clip_segment->stop = -1;
3496 basesink->segment.start = 0;
3497 basesink->segment.stop = -1;
3498 basesink->have_newsegment = TRUE;
3499 GST_OBJECT_UNLOCK (basesink);
3502 bclass = GST_BASE_SINK_GET_CLASS (basesink);
3504 /* check if the buffer needs to be dropped, we first ask the subclass for the
3506 if (bclass->get_times)
3507 bclass->get_times (basesink, time_buf, &start, &end);
3509 if (!GST_CLOCK_TIME_IS_VALID (start)) {
3510 /* if the subclass does not want sync, we use our own values so that we at
3511 * least clip the buffer to the segment */
3512 gst_base_sink_get_times (basesink, time_buf, &start, &end);
3515 GST_DEBUG_OBJECT (basesink, "got times start: %" GST_TIME_FORMAT
3516 ", end: %" GST_TIME_FORMAT, GST_TIME_ARGS (start), GST_TIME_ARGS (end));
3518 /* a dropped buffer does not participate in anything */
3519 if (GST_CLOCK_TIME_IS_VALID (start) &&
3520 (clip_segment->format == GST_FORMAT_TIME)) {
3521 if (G_UNLIKELY (!gst_segment_clip (clip_segment,
3522 GST_FORMAT_TIME, (gint64) start, (gint64) end, NULL, NULL)))
3523 goto out_of_segment;
3526 /* now we can process the buffer in the queue, this function takes ownership
3528 result = gst_base_sink_queue_object_unlocked (basesink, pad,
3529 obj_type, obj, TRUE);
3535 GST_DEBUG_OBJECT (basesink, "sink is flushing");
3536 gst_mini_object_unref (GST_MINI_OBJECT_CAST (obj));
3537 return GST_FLOW_WRONG_STATE;
3541 GST_DEBUG_OBJECT (basesink,
3542 "we are EOS, dropping object, return UNEXPECTED");
3543 gst_mini_object_unref (GST_MINI_OBJECT_CAST (obj));
3544 return GST_FLOW_UNEXPECTED;
3548 GST_DEBUG_OBJECT (basesink, "dropping buffer, out of clipping segment");
3549 gst_mini_object_unref (GST_MINI_OBJECT_CAST (obj));
3556 static GstFlowReturn
3557 gst_base_sink_chain_main (GstBaseSink * basesink, GstPad * pad,
3558 guint8 obj_type, gpointer obj)
3560 GstFlowReturn result;
3562 if (G_UNLIKELY (basesink->pad_mode != GST_ACTIVATE_PUSH))
3565 GST_PAD_PREROLL_LOCK (pad);
3566 result = gst_base_sink_chain_unlocked (basesink, pad, obj_type, obj);
3567 GST_PAD_PREROLL_UNLOCK (pad);
3575 GST_OBJECT_LOCK (pad);
3576 GST_WARNING_OBJECT (basesink,
3577 "Push on pad %s:%s, but it was not activated in push mode",
3578 GST_DEBUG_PAD_NAME (pad));
3579 GST_OBJECT_UNLOCK (pad);
3580 gst_mini_object_unref (GST_MINI_OBJECT_CAST (obj));
3581 /* we don't post an error message this will signal to the peer
3582 * pushing that EOS is reached. */
3583 result = GST_FLOW_UNEXPECTED;
3588 static GstFlowReturn
3589 gst_base_sink_chain (GstPad * pad, GstBuffer * buf)
3591 GstBaseSink *basesink;
3593 basesink = GST_BASE_SINK (GST_OBJECT_PARENT (pad));
3595 return gst_base_sink_chain_main (basesink, pad, _PR_IS_BUFFER, buf);
3598 static GstFlowReturn
3599 gst_base_sink_chain_list (GstPad * pad, GstBufferList * list)
3601 GstBaseSink *basesink;
3602 GstBaseSinkClass *bclass;
3603 GstFlowReturn result;
3605 basesink = GST_BASE_SINK (GST_OBJECT_PARENT (pad));
3606 bclass = GST_BASE_SINK_GET_CLASS (basesink);
3608 if (G_LIKELY (bclass->render_list)) {
3609 result = gst_base_sink_chain_main (basesink, pad, _PR_IS_BUFFERLIST, list);
3611 GstBufferListIterator *it;
3614 GST_INFO_OBJECT (pad, "chaining each group in list as a merged buffer");
3616 it = gst_buffer_list_iterate (list);
3618 if (gst_buffer_list_iterator_next_group (it)) {
3620 group = gst_buffer_list_iterator_merge_group (it);
3621 if (group == NULL) {
3622 group = gst_buffer_new ();
3623 GST_CAT_INFO_OBJECT (GST_CAT_SCHEDULING, pad, "chaining empty group");
3625 GST_CAT_INFO_OBJECT (GST_CAT_SCHEDULING, pad, "chaining group");
3627 result = gst_base_sink_chain_main (basesink, pad, _PR_IS_BUFFER, group);
3628 } while (result == GST_FLOW_OK
3629 && gst_buffer_list_iterator_next_group (it));
3631 GST_CAT_INFO_OBJECT (GST_CAT_SCHEDULING, pad, "chaining empty group");
3633 gst_base_sink_chain_main (basesink, pad, _PR_IS_BUFFER,
3636 gst_buffer_list_iterator_free (it);
3637 gst_buffer_list_unref (list);
3644 gst_base_sink_default_do_seek (GstBaseSink * sink, GstSegment * segment)
3646 gboolean res = TRUE;
3648 /* update our offset if the start/stop position was updated */
3649 if (segment->format == GST_FORMAT_BYTES) {
3650 segment->time = segment->start;
3651 } else if (segment->start == 0) {
3652 /* seek to start, we can implement a default for this. */
3656 GST_INFO_OBJECT (sink, "Can't do a default seek");
3662 #define SEEK_TYPE_IS_RELATIVE(t) (((t) != GST_SEEK_TYPE_NONE) && ((t) != GST_SEEK_TYPE_SET))
3665 gst_base_sink_default_prepare_seek_segment (GstBaseSink * sink,
3666 GstEvent * event, GstSegment * segment)
3668 /* By default, we try one of 2 things:
3669 * - For absolute seek positions, convert the requested position to our
3670 * configured processing format and place it in the output segment \
3671 * - For relative seek positions, convert our current (input) values to the
3672 * seek format, adjust by the relative seek offset and then convert back to
3673 * the processing format
3675 GstSeekType cur_type, stop_type;
3678 GstFormat seek_format, dest_format;
3681 gboolean res = TRUE;
3683 gst_event_parse_seek (event, &rate, &seek_format, &flags,
3684 &cur_type, &cur, &stop_type, &stop);
3685 dest_format = segment->format;
3687 if (seek_format == dest_format) {
3688 gst_segment_set_seek (segment, rate, seek_format, flags,
3689 cur_type, cur, stop_type, stop, &update);
3693 if (cur_type != GST_SEEK_TYPE_NONE) {
3694 /* FIXME: Handle seek_cur & seek_end by converting the input segment vals */
3696 gst_pad_query_convert (sink->sinkpad, seek_format, cur, &dest_format,
3698 cur_type = GST_SEEK_TYPE_SET;
3701 if (res && stop_type != GST_SEEK_TYPE_NONE) {
3702 /* FIXME: Handle seek_cur & seek_end by converting the input segment vals */
3704 gst_pad_query_convert (sink->sinkpad, seek_format, stop, &dest_format,
3706 stop_type = GST_SEEK_TYPE_SET;
3709 /* And finally, configure our output segment in the desired format */
3710 gst_segment_set_seek (segment, rate, dest_format, flags, cur_type, cur,
3711 stop_type, stop, &update);
3720 GST_DEBUG_OBJECT (sink, "undefined format given, seek aborted.");
3725 /* perform a seek, only executed in pull mode */
3727 gst_base_sink_perform_seek (GstBaseSink * sink, GstPad * pad, GstEvent * event)
3731 GstFormat seek_format, dest_format;
3733 GstSeekType cur_type, stop_type;
3734 gboolean seekseg_configured = FALSE;
3736 gboolean update, res = TRUE;
3737 GstSegment seeksegment;
3739 dest_format = sink->segment.format;
3742 GST_DEBUG_OBJECT (sink, "performing seek with event %p", event);
3743 gst_event_parse_seek (event, &rate, &seek_format, &flags,
3744 &cur_type, &cur, &stop_type, &stop);
3746 flush = flags & GST_SEEK_FLAG_FLUSH;
3748 GST_DEBUG_OBJECT (sink, "performing seek without event");
3753 GST_DEBUG_OBJECT (sink, "flushing upstream");
3754 gst_pad_push_event (pad, gst_event_new_flush_start ());
3755 gst_base_sink_flush_start (sink, pad);
3757 GST_DEBUG_OBJECT (sink, "pausing pulling thread");
3760 GST_PAD_STREAM_LOCK (pad);
3762 /* If we configured the seeksegment above, don't overwrite it now. Otherwise
3763 * copy the current segment info into the temp segment that we can actually
3764 * attempt the seek with. We only update the real segment if the seek suceeds. */
3765 if (!seekseg_configured) {
3766 memcpy (&seeksegment, &sink->segment, sizeof (GstSegment));
3768 /* now configure the final seek segment */
3770 if (sink->segment.format != seek_format) {
3771 /* OK, here's where we give the subclass a chance to convert the relative
3772 * seek into an absolute one in the processing format. We set up any
3773 * absolute seek above, before taking the stream lock. */
3774 if (!gst_base_sink_default_prepare_seek_segment (sink, event,
3776 GST_DEBUG_OBJECT (sink,
3777 "Preparing the seek failed after flushing. " "Aborting seek");
3781 /* The seek format matches our processing format, no need to ask the
3782 * the subclass to configure the segment. */
3783 gst_segment_set_seek (&seeksegment, rate, seek_format, flags,
3784 cur_type, cur, stop_type, stop, &update);
3787 /* Else, no seek event passed, so we're just (re)starting the
3792 GST_DEBUG_OBJECT (sink, "segment configured from %" G_GINT64_FORMAT
3793 " to %" G_GINT64_FORMAT ", position %" G_GINT64_FORMAT,
3794 seeksegment.start, seeksegment.stop, seeksegment.last_stop);
3796 /* do the seek, segment.last_stop contains the new position. */
3797 res = gst_base_sink_default_do_seek (sink, &seeksegment);
3802 GST_DEBUG_OBJECT (sink, "stop flushing upstream");
3803 gst_pad_push_event (pad, gst_event_new_flush_stop ());
3804 gst_base_sink_flush_stop (sink, pad);
3805 } else if (res && sink->abidata.ABI.running) {
3806 /* we are running the current segment and doing a non-flushing seek,
3807 * close the segment first based on the last_stop. */
3808 GST_DEBUG_OBJECT (sink, "closing running segment %" G_GINT64_FORMAT
3809 " to %" G_GINT64_FORMAT, sink->segment.start, sink->segment.last_stop);
3812 /* The subclass must have converted the segment to the processing format
3814 if (res && seeksegment.format != dest_format) {
3815 GST_DEBUG_OBJECT (sink, "Subclass failed to prepare a seek segment "
3816 "in the correct format. Aborting seek.");
3820 /* if successfull seek, we update our real segment and push
3821 * out the new segment. */
3823 memcpy (&sink->segment, &seeksegment, sizeof (GstSegment));
3825 if (sink->segment.flags & GST_SEEK_FLAG_SEGMENT) {
3826 gst_element_post_message (GST_ELEMENT (sink),
3827 gst_message_new_segment_start (GST_OBJECT (sink),
3828 sink->segment.format, sink->segment.last_stop));
3832 sink->priv->discont = TRUE;
3833 sink->abidata.ABI.running = TRUE;
3835 GST_PAD_STREAM_UNLOCK (pad);
3841 set_step_info (GstBaseSink * sink, GstStepInfo * current, GstStepInfo * pending,
3842 guint seqnum, GstFormat format, guint64 amount, gdouble rate,
3843 gboolean flush, gboolean intermediate)
3845 GST_OBJECT_LOCK (sink);
3846 pending->seqnum = seqnum;
3847 pending->format = format;
3848 pending->amount = amount;
3849 pending->position = 0;
3850 pending->rate = rate;
3851 pending->flush = flush;
3852 pending->intermediate = intermediate;
3853 pending->valid = TRUE;
3854 /* flush invalidates the current stepping segment */
3856 current->valid = FALSE;
3857 GST_OBJECT_UNLOCK (sink);
3861 gst_base_sink_perform_step (GstBaseSink * sink, GstPad * pad, GstEvent * event)
3863 GstBaseSinkPrivate *priv;
3864 GstBaseSinkClass *bclass;
3865 gboolean flush, intermediate;
3870 GstStepInfo *pending, *current;
3871 GstMessage *message;
3873 bclass = GST_BASE_SINK_GET_CLASS (sink);
3876 GST_DEBUG_OBJECT (sink, "performing step with event %p", event);
3878 gst_event_parse_step (event, &format, &amount, &rate, &flush, &intermediate);
3879 seqnum = gst_event_get_seqnum (event);
3881 pending = &priv->pending_step;
3882 current = &priv->current_step;
3884 /* post message first */
3885 message = gst_message_new_step_start (GST_OBJECT (sink), FALSE, format,
3886 amount, rate, flush, intermediate);
3887 gst_message_set_seqnum (message, seqnum);
3888 gst_element_post_message (GST_ELEMENT (sink), message);
3891 /* we need to call ::unlock before locking PREROLL_LOCK
3892 * since we lock it before going into ::render */
3894 bclass->unlock (sink);
3896 GST_PAD_PREROLL_LOCK (sink->sinkpad);
3897 /* now that we have the PREROLL lock, clear our unlock request */
3898 if (bclass->unlock_stop)
3899 bclass->unlock_stop (sink);
3901 /* update the stepinfo and make it valid */
3902 set_step_info (sink, current, pending, seqnum, format, amount, rate, flush,
3905 if (sink->priv->async_enabled) {
3906 /* and we need to commit our state again on the next
3907 * prerolled buffer */
3908 sink->playing_async = TRUE;
3909 priv->pending_step.need_preroll = TRUE;
3910 sink->need_preroll = FALSE;
3911 gst_element_lost_state_full (GST_ELEMENT_CAST (sink), FALSE);
3913 sink->priv->have_latency = TRUE;
3914 sink->need_preroll = FALSE;
3916 priv->current_sstart = GST_CLOCK_TIME_NONE;
3917 priv->current_sstop = GST_CLOCK_TIME_NONE;
3918 priv->eos_rtime = GST_CLOCK_TIME_NONE;
3919 priv->call_preroll = TRUE;
3920 gst_base_sink_set_last_buffer (sink, NULL);
3921 gst_base_sink_reset_qos (sink);
3923 if (sink->clock_id) {
3924 gst_clock_id_unschedule (sink->clock_id);
3927 if (sink->have_preroll) {
3928 GST_DEBUG_OBJECT (sink, "signal waiter");
3929 priv->step_unlock = TRUE;
3930 GST_PAD_PREROLL_SIGNAL (sink->sinkpad);
3932 GST_PAD_PREROLL_UNLOCK (sink->sinkpad);
3934 /* update the stepinfo and make it valid */
3935 set_step_info (sink, current, pending, seqnum, format, amount, rate, flush,
3945 gst_base_sink_loop (GstPad * pad)
3947 GstBaseSink *basesink;
3948 GstBuffer *buf = NULL;
3949 GstFlowReturn result;
3953 basesink = GST_BASE_SINK (GST_OBJECT_PARENT (pad));
3955 g_assert (basesink->pad_mode == GST_ACTIVATE_PULL);
3957 if ((blocksize = basesink->priv->blocksize) == 0)
3960 offset = basesink->segment.last_stop;
3962 GST_DEBUG_OBJECT (basesink, "pulling %" G_GUINT64_FORMAT ", %u",
3965 result = gst_pad_pull_range (pad, offset, blocksize, &buf);
3966 if (G_UNLIKELY (result != GST_FLOW_OK))
3969 if (G_UNLIKELY (buf == NULL))
3972 offset += GST_BUFFER_SIZE (buf);
3974 gst_segment_set_last_stop (&basesink->segment, GST_FORMAT_BYTES, offset);
3976 GST_PAD_PREROLL_LOCK (pad);
3977 result = gst_base_sink_chain_unlocked (basesink, pad, _PR_IS_BUFFER, buf);
3978 GST_PAD_PREROLL_UNLOCK (pad);
3979 if (G_UNLIKELY (result != GST_FLOW_OK))
3987 GST_LOG_OBJECT (basesink, "pausing task, reason %s",
3988 gst_flow_get_name (result));
3989 gst_pad_pause_task (pad);
3990 if (result == GST_FLOW_UNEXPECTED) {
3991 /* perform EOS logic */
3992 if (basesink->segment.flags & GST_SEEK_FLAG_SEGMENT) {
3993 gst_element_post_message (GST_ELEMENT_CAST (basesink),
3994 gst_message_new_segment_done (GST_OBJECT_CAST (basesink),
3995 basesink->segment.format, basesink->segment.last_stop));
3997 gst_base_sink_event (pad, gst_event_new_eos ());
3999 } else if (result == GST_FLOW_NOT_LINKED || result <= GST_FLOW_UNEXPECTED) {
4000 /* for fatal errors we post an error message, post the error
4001 * first so the app knows about the error first.
4002 * wrong-state is not a fatal error because it happens due to
4003 * flushing and posting an error message in that case is the
4004 * wrong thing to do, e.g. when basesrc is doing a flushing
4006 GST_ELEMENT_ERROR (basesink, STREAM, FAILED,
4007 (_("Internal data stream error.")),
4008 ("stream stopped, reason %s", gst_flow_get_name (result)));
4009 gst_base_sink_event (pad, gst_event_new_eos ());
4015 GST_LOG_OBJECT (basesink, "no buffer, pausing");
4016 GST_ELEMENT_ERROR (basesink, STREAM, FAILED,
4017 (_("Internal data flow error.")), ("element returned NULL buffer"));
4018 result = GST_FLOW_ERROR;
4024 gst_base_sink_set_flushing (GstBaseSink * basesink, GstPad * pad,
4027 GstBaseSinkClass *bclass;
4029 bclass = GST_BASE_SINK_GET_CLASS (basesink);
4032 /* unlock any subclasses, we need to do this before grabbing the
4033 * PREROLL_LOCK since we hold this lock before going into ::render. */
4035 bclass->unlock (basesink);
4038 GST_PAD_PREROLL_LOCK (pad);
4039 basesink->flushing = flushing;
4041 /* step 1, now that we have the PREROLL lock, clear our unlock request */
4042 if (bclass->unlock_stop)
4043 bclass->unlock_stop (basesink);
4045 /* set need_preroll before we unblock the clock. If the clock is unblocked
4046 * before timing out, we can reuse the buffer for preroll. */
4047 basesink->need_preroll = TRUE;
4049 /* step 2, unblock clock sync (if any) or any other blocking thing */
4050 if (basesink->clock_id) {
4051 gst_clock_id_unschedule (basesink->clock_id);
4054 /* flush out the data thread if it's locked in finish_preroll, this will
4055 * also flush out the EOS state */
4056 GST_DEBUG_OBJECT (basesink,
4057 "flushing out data thread, need preroll to TRUE");
4058 gst_base_sink_preroll_queue_flush (basesink, pad);
4060 GST_PAD_PREROLL_UNLOCK (pad);
4066 gst_base_sink_default_activate_pull (GstBaseSink * basesink, gboolean active)
4072 result = gst_pad_start_task (basesink->sinkpad,
4073 (GstTaskFunction) gst_base_sink_loop, basesink->sinkpad);
4075 /* step 2, make sure streaming finishes */
4076 result = gst_pad_stop_task (basesink->sinkpad);
4083 gst_base_sink_pad_activate (GstPad * pad)
4085 gboolean result = FALSE;
4086 GstBaseSink *basesink;
4088 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
4090 GST_DEBUG_OBJECT (basesink, "Trying pull mode first");
4092 gst_base_sink_set_flushing (basesink, pad, FALSE);
4094 /* we need to have the pull mode enabled */
4095 if (!basesink->can_activate_pull) {
4096 GST_DEBUG_OBJECT (basesink, "pull mode disabled");
4100 /* check if downstreams supports pull mode at all */
4101 if (!gst_pad_check_pull_range (pad)) {
4102 GST_DEBUG_OBJECT (basesink, "pull mode not supported");
4106 /* set the pad mode before starting the task so that it's in the
4107 * correct state for the new thread. also the sink set_caps and get_caps
4108 * function checks this */
4109 basesink->pad_mode = GST_ACTIVATE_PULL;
4111 /* we first try to negotiate a format so that when we try to activate
4112 * downstream, it knows about our format */
4113 if (!gst_base_sink_negotiate_pull (basesink)) {
4114 GST_DEBUG_OBJECT (basesink, "failed to negotiate in pull mode");
4118 /* ok activate now */
4119 if (!gst_pad_activate_pull (pad, TRUE)) {
4120 /* clear any pending caps */
4121 GST_OBJECT_LOCK (basesink);
4122 gst_caps_replace (&basesink->priv->pull_caps, NULL);
4123 GST_OBJECT_UNLOCK (basesink);
4124 GST_DEBUG_OBJECT (basesink, "failed to activate in pull mode");
4128 GST_DEBUG_OBJECT (basesink, "Success activating pull mode");
4132 /* push mode fallback */
4134 GST_DEBUG_OBJECT (basesink, "Falling back to push mode");
4135 if ((result = gst_pad_activate_push (pad, TRUE))) {
4136 GST_DEBUG_OBJECT (basesink, "Success activating push mode");
4141 GST_WARNING_OBJECT (basesink, "Could not activate pad in either mode");
4142 gst_base_sink_set_flushing (basesink, pad, TRUE);
4145 gst_object_unref (basesink);
4151 gst_base_sink_pad_activate_push (GstPad * pad, gboolean active)
4154 GstBaseSink *basesink;
4156 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
4159 if (!basesink->can_activate_push) {
4161 basesink->pad_mode = GST_ACTIVATE_NONE;
4164 basesink->pad_mode = GST_ACTIVATE_PUSH;
4167 if (G_UNLIKELY (basesink->pad_mode != GST_ACTIVATE_PUSH)) {
4168 g_warning ("Internal GStreamer activation error!!!");
4171 gst_base_sink_set_flushing (basesink, pad, TRUE);
4173 basesink->pad_mode = GST_ACTIVATE_NONE;
4177 gst_object_unref (basesink);
4183 gst_base_sink_negotiate_pull (GstBaseSink * basesink)
4190 /* this returns the intersection between our caps and the peer caps. If there
4191 * is no peer, it returns NULL and we can't operate in pull mode so we can
4192 * fail the negotiation. */
4193 caps = gst_pad_get_allowed_caps (GST_BASE_SINK_PAD (basesink));
4194 if (caps == NULL || gst_caps_is_empty (caps))
4195 goto no_caps_possible;
4197 GST_DEBUG_OBJECT (basesink, "allowed caps: %" GST_PTR_FORMAT, caps);
4199 caps = gst_caps_make_writable (caps);
4200 /* get the first (prefered) format */
4201 gst_caps_truncate (caps);
4203 gst_pad_fixate_caps (GST_BASE_SINK_PAD (basesink), caps);
4205 GST_DEBUG_OBJECT (basesink, "fixated to: %" GST_PTR_FORMAT, caps);
4207 if (gst_caps_is_any (caps)) {
4208 GST_DEBUG_OBJECT (basesink, "caps were ANY after fixating, "
4210 /* neither side has template caps in this case, so they are prepared for
4211 pull() without setcaps() */
4213 } else if (gst_caps_is_fixed (caps)) {
4214 if (!gst_pad_set_caps (GST_BASE_SINK_PAD (basesink), caps))
4215 goto could_not_set_caps;
4217 GST_OBJECT_LOCK (basesink);
4218 gst_caps_replace (&basesink->priv->pull_caps, caps);
4219 GST_OBJECT_UNLOCK (basesink);
4224 gst_caps_unref (caps);
4230 GST_INFO_OBJECT (basesink, "Pipeline could not agree on caps");
4231 GST_DEBUG_OBJECT (basesink, "get_allowed_caps() returned EMPTY");
4233 gst_caps_unref (caps);
4238 GST_INFO_OBJECT (basesink, "Could not set caps: %" GST_PTR_FORMAT, caps);
4239 gst_caps_unref (caps);
4244 /* this won't get called until we implement an activate function */
4246 gst_base_sink_pad_activate_pull (GstPad * pad, gboolean active)
4248 gboolean result = FALSE;
4249 GstBaseSink *basesink;
4250 GstBaseSinkClass *bclass;
4252 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
4253 bclass = GST_BASE_SINK_GET_CLASS (basesink);
4259 /* we mark we have a newsegment here because pull based
4260 * mode works just fine without having a newsegment before the
4262 format = GST_FORMAT_BYTES;
4264 gst_segment_init (&basesink->segment, format);
4265 gst_segment_init (basesink->abidata.ABI.clip_segment, format);
4266 GST_OBJECT_LOCK (basesink);
4267 basesink->have_newsegment = TRUE;
4268 GST_OBJECT_UNLOCK (basesink);
4270 /* get the peer duration in bytes */
4271 result = gst_pad_query_peer_duration (pad, &format, &duration);
4273 GST_DEBUG_OBJECT (basesink,
4274 "setting duration in bytes to %" G_GINT64_FORMAT, duration);
4275 gst_segment_set_duration (basesink->abidata.ABI.clip_segment, format,
4277 gst_segment_set_duration (&basesink->segment, format, duration);
4279 GST_DEBUG_OBJECT (basesink, "unknown duration");
4282 if (bclass->activate_pull)
4283 result = bclass->activate_pull (basesink, TRUE);
4288 goto activate_failed;
4291 if (G_UNLIKELY (basesink->pad_mode != GST_ACTIVATE_PULL)) {
4292 g_warning ("Internal GStreamer activation error!!!");
4295 result = gst_base_sink_set_flushing (basesink, pad, TRUE);
4296 if (bclass->activate_pull)
4297 result &= bclass->activate_pull (basesink, FALSE);
4298 basesink->pad_mode = GST_ACTIVATE_NONE;
4299 /* clear any pending caps */
4300 GST_OBJECT_LOCK (basesink);
4301 gst_caps_replace (&basesink->priv->pull_caps, NULL);
4302 GST_OBJECT_UNLOCK (basesink);
4305 gst_object_unref (basesink);
4312 /* reset, as starting the thread failed */
4313 basesink->pad_mode = GST_ACTIVATE_NONE;
4315 GST_ERROR_OBJECT (basesink, "subclass failed to activate in pull mode");
4320 /* send an event to our sinkpad peer. */
4322 gst_base_sink_send_event (GstElement * element, GstEvent * event)
4325 GstBaseSink *basesink = GST_BASE_SINK (element);
4326 gboolean forward, result = TRUE;
4327 GstActivateMode mode;
4329 GST_OBJECT_LOCK (element);
4330 /* get the pad and the scheduling mode */
4331 pad = gst_object_ref (basesink->sinkpad);
4332 mode = basesink->pad_mode;
4333 GST_OBJECT_UNLOCK (element);
4335 /* only push UPSTREAM events upstream */
4336 forward = GST_EVENT_IS_UPSTREAM (event);
4338 GST_DEBUG_OBJECT (basesink, "handling event %p %" GST_PTR_FORMAT, event,
4341 switch (GST_EVENT_TYPE (event)) {
4342 case GST_EVENT_LATENCY:
4344 GstClockTime latency;
4346 gst_event_parse_latency (event, &latency);
4348 /* store the latency. We use this to adjust the running_time before syncing
4349 * it to the clock. */
4350 GST_OBJECT_LOCK (element);
4351 basesink->priv->latency = latency;
4352 if (!basesink->priv->have_latency)
4354 GST_OBJECT_UNLOCK (element);
4355 GST_DEBUG_OBJECT (basesink, "latency set to %" GST_TIME_FORMAT,
4356 GST_TIME_ARGS (latency));
4358 /* We forward this event so that all elements know about the global pipeline
4359 * latency. This is interesting for an element when it wants to figure out
4360 * when a particular piece of data will be rendered. */
4363 case GST_EVENT_SEEK:
4364 /* in pull mode we will execute the seek */
4365 if (mode == GST_ACTIVATE_PULL)
4366 result = gst_base_sink_perform_seek (basesink, pad, event);
4368 case GST_EVENT_STEP:
4369 result = gst_base_sink_perform_step (basesink, pad, event);
4377 result = gst_pad_push_event (pad, event);
4379 /* not forwarded, unref the event */
4380 gst_event_unref (event);
4383 gst_object_unref (pad);
4388 gst_base_sink_get_position (GstBaseSink * basesink, GstFormat format,
4389 gint64 * cur, gboolean * upstream)
4391 GstClock *clock = NULL;
4392 gboolean res = FALSE;
4393 GstFormat oformat, tformat;
4394 GstSegment *segment;
4395 GstClockTime now, latency;
4396 GstClockTimeDiff base;
4397 gint64 time, accum, duration;
4400 gboolean last_seen, with_clock, in_paused;
4402 GST_OBJECT_LOCK (basesink);
4403 /* we can only get the segment when we are not NULL or READY */
4404 if (!basesink->have_newsegment)
4408 /* when not in PLAYING or when we're busy with a state change, we
4409 * cannot read from the clock so we report time based on the
4410 * last seen timestamp. */
4411 if (GST_STATE (basesink) != GST_STATE_PLAYING ||
4412 GST_STATE_PENDING (basesink) != GST_STATE_VOID_PENDING) {
4416 /* we don't use the clip segment in pull mode, when seeking we update the
4417 * main segment directly with the new segment values without it having to be
4418 * activated by the rendering after preroll */
4419 if (basesink->pad_mode == GST_ACTIVATE_PUSH)
4420 segment = basesink->abidata.ABI.clip_segment;
4422 segment = &basesink->segment;
4424 /* our intermediate time format */
4425 tformat = GST_FORMAT_TIME;
4426 /* get the format in the segment */
4427 oformat = segment->format;
4429 /* report with last seen position when EOS */
4430 last_seen = basesink->eos;
4432 /* assume we will use the clock for getting the current position */
4434 if (basesink->sync == FALSE)
4437 /* and we need a clock */
4438 if (G_UNLIKELY ((clock = GST_ELEMENT_CLOCK (basesink)) == NULL))
4441 gst_object_ref (clock);
4443 /* collect all data we need holding the lock */
4444 if (GST_CLOCK_TIME_IS_VALID (segment->time))
4445 time = segment->time;
4449 if (GST_CLOCK_TIME_IS_VALID (segment->stop))
4450 duration = segment->stop - segment->start;
4454 accum = segment->accum;
4455 rate = segment->rate * segment->applied_rate;
4456 latency = basesink->priv->latency;
4458 if (oformat == GST_FORMAT_TIME) {
4461 start = basesink->priv->current_sstart;
4462 stop = basesink->priv->current_sstop;
4465 /* in paused we use the last position as a lower bound */
4466 if (stop == -1 || segment->rate > 0.0)
4471 /* in playing, use last stop time as upper bound */
4472 if (start == -1 || segment->rate > 0.0)
4478 /* convert last stop to stream time */
4479 last = gst_segment_to_stream_time (segment, oformat, segment->last_stop);
4483 /* in paused, use start_time */
4484 base = GST_ELEMENT_START_TIME (basesink);
4485 GST_DEBUG_OBJECT (basesink, "in paused, using start time %" GST_TIME_FORMAT,
4486 GST_TIME_ARGS (base));
4487 } else if (with_clock) {
4488 /* else use clock when needed */
4489 base = GST_ELEMENT_CAST (basesink)->base_time;
4490 GST_DEBUG_OBJECT (basesink, "using clock and base time %" GST_TIME_FORMAT,
4491 GST_TIME_ARGS (base));
4493 /* else, no sync or clock -> no base time */
4494 GST_DEBUG_OBJECT (basesink, "no sync or no clock");
4498 /* no base, we can't calculate running_time, use last seem timestamp to report
4503 /* need to release the object lock before we can get the time,
4504 * a clock might take the LOCK of the provider, which could be
4505 * a basesink subclass. */
4506 GST_OBJECT_UNLOCK (basesink);
4509 /* in EOS or when no valid stream_time, report the value of last seen
4512 /* no timestamp, we need to ask upstream */
4513 GST_DEBUG_OBJECT (basesink, "no last seen timestamp, asking upstream");
4518 GST_DEBUG_OBJECT (basesink, "using last seen timestamp %" GST_TIME_FORMAT,
4519 GST_TIME_ARGS (last));
4522 if (oformat != tformat) {
4523 /* convert accum, time and duration to time */
4524 if (!gst_pad_query_convert (basesink->sinkpad, oformat, accum, &tformat,
4526 goto convert_failed;
4527 if (!gst_pad_query_convert (basesink->sinkpad, oformat, duration,
4528 &tformat, &duration))
4529 goto convert_failed;
4530 if (!gst_pad_query_convert (basesink->sinkpad, oformat, time, &tformat,
4532 goto convert_failed;
4533 if (!gst_pad_query_convert (basesink->sinkpad, oformat, last, &tformat,
4535 goto convert_failed;
4537 /* assume time format from now on */
4541 if (!in_paused && with_clock) {
4542 now = gst_clock_get_time (clock);
4548 /* subtract base time and accumulated time from the clock time.
4549 * Make sure we don't go negative. This is the current time in
4550 * the segment which we need to scale with the combined
4551 * rate and applied rate. */
4554 if (GST_CLOCK_DIFF (base, now) < 0)
4557 /* for negative rates we need to count back from the segment
4562 *cur = time + gst_guint64_to_gdouble (now - base) * rate;
4565 /* never report less than segment values in paused */
4567 *cur = MAX (last, *cur);
4569 /* never report more than last seen position in playing */
4571 *cur = MIN (last, *cur);
4574 GST_DEBUG_OBJECT (basesink,
4575 "now %" GST_TIME_FORMAT " - base %" GST_TIME_FORMAT " - accum %"
4576 GST_TIME_FORMAT " + time %" GST_TIME_FORMAT " last %" GST_TIME_FORMAT,
4577 GST_TIME_ARGS (now), GST_TIME_ARGS (base), GST_TIME_ARGS (accum),
4578 GST_TIME_ARGS (time), GST_TIME_ARGS (last));
4581 if (oformat != format) {
4582 /* convert to final format */
4583 if (!gst_pad_query_convert (basesink->sinkpad, oformat, *cur, &format, cur))
4584 goto convert_failed;
4590 GST_DEBUG_OBJECT (basesink, "res: %d, POSITION: %" GST_TIME_FORMAT,
4591 res, GST_TIME_ARGS (*cur));
4594 gst_object_unref (clock);
4601 /* in NULL or READY we always return FALSE and -1 */
4602 GST_DEBUG_OBJECT (basesink, "position in wrong state, return -1");
4605 GST_OBJECT_UNLOCK (basesink);
4610 GST_DEBUG_OBJECT (basesink, "convert failed, try upstream");
4618 gst_base_sink_get_duration (GstBaseSink * basesink, GstFormat format,
4619 gint64 * dur, gboolean * upstream)
4621 gboolean res = FALSE;
4623 if (basesink->pad_mode == GST_ACTIVATE_PULL) {
4624 GstFormat uformat = GST_FORMAT_BYTES;
4627 /* get the duration in bytes, in pull mode that's all we are sure to
4628 * know. We have to explicitly get this value from upstream instead of
4629 * using our cached value because it might change. Duration caching
4630 * should be done at a higher level. */
4631 res = gst_pad_query_peer_duration (basesink->sinkpad, &uformat, &uduration);
4633 gst_segment_set_duration (&basesink->segment, uformat, uduration);
4634 if (format != uformat) {
4635 /* convert to the requested format */
4636 res = gst_pad_query_convert (basesink->sinkpad, uformat, uduration,
4650 static const GstQueryType *
4651 gst_base_sink_get_query_types (GstElement * element)
4653 static const GstQueryType query_types[] = {
4665 gst_base_sink_query (GstElement * element, GstQuery * query)
4667 gboolean res = FALSE;
4669 GstBaseSink *basesink = GST_BASE_SINK (element);
4671 switch (GST_QUERY_TYPE (query)) {
4672 case GST_QUERY_POSITION:
4676 gboolean upstream = FALSE;
4678 gst_query_parse_position (query, &format, NULL);
4680 GST_DEBUG_OBJECT (basesink, "position query in format %s",
4681 gst_format_get_name (format));
4683 /* first try to get the position based on the clock */
4685 gst_base_sink_get_position (basesink, format, &cur, &upstream))) {
4686 gst_query_set_position (query, format, cur);
4687 } else if (upstream) {
4688 /* fallback to peer query */
4689 res = gst_pad_peer_query (basesink->sinkpad, query);
4692 /* we can handle a few things if upstream failed */
4693 if (format == GST_FORMAT_PERCENT) {
4695 GstFormat uformat = GST_FORMAT_TIME;
4697 res = gst_base_sink_get_position (basesink, GST_FORMAT_TIME, &cur,
4699 if (!res && upstream) {
4700 res = gst_pad_query_peer_position (basesink->sinkpad, &uformat,
4704 res = gst_base_sink_get_duration (basesink, GST_FORMAT_TIME, &dur,
4706 if (!res && upstream) {
4707 res = gst_pad_query_peer_duration (basesink->sinkpad, &uformat,
4714 pos = gst_util_uint64_scale (100 * GST_FORMAT_PERCENT_SCALE, cur,
4716 gst_query_set_position (query, GST_FORMAT_PERCENT, pos);
4722 case GST_QUERY_DURATION:
4726 gboolean upstream = FALSE;
4728 gst_query_parse_duration (query, &format, NULL);
4730 GST_DEBUG_OBJECT (basesink, "duration query in format %s",
4731 gst_format_get_name (format));
4734 gst_base_sink_get_duration (basesink, format, &dur, &upstream))) {
4735 gst_query_set_duration (query, format, dur);
4736 } else if (upstream) {
4737 /* fallback to peer query */
4738 res = gst_pad_peer_query (basesink->sinkpad, query);
4741 /* we can handle a few things if upstream failed */
4742 if (format == GST_FORMAT_PERCENT) {
4743 gst_query_set_duration (query, GST_FORMAT_PERCENT,
4744 GST_FORMAT_PERCENT_MAX);
4750 case GST_QUERY_LATENCY:
4752 gboolean live, us_live;
4753 GstClockTime min, max;
4755 if ((res = gst_base_sink_query_latency (basesink, &live, &us_live, &min,
4757 gst_query_set_latency (query, live, min, max);
4761 case GST_QUERY_JITTER:
4763 case GST_QUERY_RATE:
4764 /* gst_query_set_rate (query, basesink->segment_rate); */
4767 case GST_QUERY_SEGMENT:
4769 if (basesink->pad_mode == GST_ACTIVATE_PULL) {
4770 gst_query_set_segment (query, basesink->segment.rate,
4771 GST_FORMAT_TIME, basesink->segment.start, basesink->segment.stop);
4774 res = gst_pad_peer_query (basesink->sinkpad, query);
4778 case GST_QUERY_SEEKING:
4779 case GST_QUERY_CONVERT:
4780 case GST_QUERY_FORMATS:
4782 res = gst_pad_peer_query (basesink->sinkpad, query);
4785 GST_DEBUG_OBJECT (basesink, "query %s returns %d",
4786 GST_QUERY_TYPE_NAME (query), res);
4790 static GstStateChangeReturn
4791 gst_base_sink_change_state (GstElement * element, GstStateChange transition)
4793 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
4794 GstBaseSink *basesink = GST_BASE_SINK (element);
4795 GstBaseSinkClass *bclass;
4796 GstBaseSinkPrivate *priv;
4798 priv = basesink->priv;
4800 bclass = GST_BASE_SINK_GET_CLASS (basesink);
4802 switch (transition) {
4803 case GST_STATE_CHANGE_NULL_TO_READY:
4805 if (!bclass->start (basesink))
4808 case GST_STATE_CHANGE_READY_TO_PAUSED:
4809 /* need to complete preroll before this state change completes, there
4810 * is no data flow in READY so we can safely assume we need to preroll. */
4811 GST_PAD_PREROLL_LOCK (basesink->sinkpad);
4812 GST_DEBUG_OBJECT (basesink, "READY to PAUSED");
4813 basesink->have_newsegment = FALSE;
4814 gst_segment_init (&basesink->segment, GST_FORMAT_UNDEFINED);
4815 gst_segment_init (basesink->abidata.ABI.clip_segment,
4816 GST_FORMAT_UNDEFINED);
4817 basesink->offset = 0;
4818 basesink->have_preroll = FALSE;
4819 priv->step_unlock = FALSE;
4820 basesink->need_preroll = TRUE;
4821 basesink->playing_async = TRUE;
4822 priv->current_sstart = GST_CLOCK_TIME_NONE;
4823 priv->current_sstop = GST_CLOCK_TIME_NONE;
4824 priv->eos_rtime = GST_CLOCK_TIME_NONE;
4826 basesink->eos = FALSE;
4827 priv->received_eos = FALSE;
4828 gst_base_sink_reset_qos (basesink);
4829 priv->commited = FALSE;
4830 priv->call_preroll = TRUE;
4831 priv->current_step.valid = FALSE;
4832 priv->pending_step.valid = FALSE;
4833 if (priv->async_enabled) {
4834 GST_DEBUG_OBJECT (basesink, "doing async state change");
4835 /* when async enabled, post async-start message and return ASYNC from
4836 * the state change function */
4837 ret = GST_STATE_CHANGE_ASYNC;
4838 gst_element_post_message (GST_ELEMENT_CAST (basesink),
4839 gst_message_new_async_start (GST_OBJECT_CAST (basesink), FALSE));
4841 priv->have_latency = TRUE;
4843 GST_PAD_PREROLL_UNLOCK (basesink->sinkpad);
4845 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
4846 GST_PAD_PREROLL_LOCK (basesink->sinkpad);
4847 if (!gst_base_sink_needs_preroll (basesink)) {
4848 GST_DEBUG_OBJECT (basesink, "PAUSED to PLAYING, don't need preroll");
4849 /* no preroll needed anymore now. */
4850 basesink->playing_async = FALSE;
4851 basesink->need_preroll = FALSE;
4852 if (basesink->eos) {
4853 GstMessage *message;
4855 /* need to post EOS message here */
4856 GST_DEBUG_OBJECT (basesink, "Now posting EOS");
4857 message = gst_message_new_eos (GST_OBJECT_CAST (basesink));
4858 gst_message_set_seqnum (message, basesink->priv->seqnum);
4859 gst_element_post_message (GST_ELEMENT_CAST (basesink), message);
4861 GST_DEBUG_OBJECT (basesink, "signal preroll");
4862 GST_PAD_PREROLL_SIGNAL (basesink->sinkpad);
4865 GST_DEBUG_OBJECT (basesink, "PAUSED to PLAYING, we are not prerolled");
4866 basesink->need_preroll = TRUE;
4867 basesink->playing_async = TRUE;
4868 priv->call_preroll = TRUE;
4869 priv->commited = FALSE;
4870 if (priv->async_enabled) {
4871 GST_DEBUG_OBJECT (basesink, "doing async state change");
4872 ret = GST_STATE_CHANGE_ASYNC;
4873 gst_element_post_message (GST_ELEMENT_CAST (basesink),
4874 gst_message_new_async_start (GST_OBJECT_CAST (basesink), FALSE));
4877 GST_PAD_PREROLL_UNLOCK (basesink->sinkpad);
4884 GstStateChangeReturn bret;
4886 bret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
4887 if (G_UNLIKELY (bret == GST_STATE_CHANGE_FAILURE))
4888 goto activate_failed;
4891 switch (transition) {
4892 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
4893 GST_DEBUG_OBJECT (basesink, "PLAYING to PAUSED");
4894 /* FIXME, make sure we cannot enter _render first */
4896 /* we need to call ::unlock before locking PREROLL_LOCK
4897 * since we lock it before going into ::render */
4899 bclass->unlock (basesink);
4901 GST_PAD_PREROLL_LOCK (basesink->sinkpad);
4902 GST_DEBUG_OBJECT (basesink, "got preroll lock");
4903 /* now that we have the PREROLL lock, clear our unlock request */
4904 if (bclass->unlock_stop)
4905 bclass->unlock_stop (basesink);
4907 /* we need preroll again and we set the flag before unlocking the clockid
4908 * because if the clockid is unlocked before a current buffer expired, we
4909 * can use that buffer to preroll with */
4910 basesink->need_preroll = TRUE;
4912 if (basesink->clock_id) {
4913 GST_DEBUG_OBJECT (basesink, "unschedule clock");
4914 gst_clock_id_unschedule (basesink->clock_id);
4917 /* if we don't have a preroll buffer we need to wait for a preroll and
4919 if (!gst_base_sink_needs_preroll (basesink)) {
4920 GST_DEBUG_OBJECT (basesink, "PLAYING to PAUSED, we are prerolled");
4921 basesink->playing_async = FALSE;
4923 if (GST_STATE_TARGET (GST_ELEMENT (basesink)) <= GST_STATE_READY) {
4924 GST_DEBUG_OBJECT (basesink, "element is <= READY");
4925 ret = GST_STATE_CHANGE_SUCCESS;
4927 GST_DEBUG_OBJECT (basesink,
4928 "PLAYING to PAUSED, we are not prerolled");
4929 basesink->playing_async = TRUE;
4930 priv->commited = FALSE;
4931 priv->call_preroll = TRUE;
4932 if (priv->async_enabled) {
4933 GST_DEBUG_OBJECT (basesink, "doing async state change");
4934 ret = GST_STATE_CHANGE_ASYNC;
4935 gst_element_post_message (GST_ELEMENT_CAST (basesink),
4936 gst_message_new_async_start (GST_OBJECT_CAST (basesink),
4941 GST_DEBUG_OBJECT (basesink, "rendered: %" G_GUINT64_FORMAT
4942 ", dropped: %" G_GUINT64_FORMAT, priv->rendered, priv->dropped);
4944 gst_base_sink_reset_qos (basesink);
4945 GST_PAD_PREROLL_UNLOCK (basesink->sinkpad);
4947 case GST_STATE_CHANGE_PAUSED_TO_READY:
4948 GST_PAD_PREROLL_LOCK (basesink->sinkpad);
4949 /* start by reseting our position state with the object lock so that the
4950 * position query gets the right idea. We do this before we post the
4951 * messages so that the message handlers pick this up. */
4952 GST_OBJECT_LOCK (basesink);
4953 basesink->have_newsegment = FALSE;
4954 priv->current_sstart = GST_CLOCK_TIME_NONE;
4955 priv->current_sstop = GST_CLOCK_TIME_NONE;
4956 priv->have_latency = FALSE;
4957 if (priv->cached_clock_id) {
4958 gst_clock_id_unref (priv->cached_clock_id);
4959 priv->cached_clock_id = NULL;
4961 GST_OBJECT_UNLOCK (basesink);
4963 gst_base_sink_set_last_buffer (basesink, NULL);
4964 priv->call_preroll = FALSE;
4966 if (!priv->commited) {
4967 if (priv->async_enabled) {
4968 GST_DEBUG_OBJECT (basesink, "PAUSED to READY, posting async-done");
4970 gst_element_post_message (GST_ELEMENT_CAST (basesink),
4971 gst_message_new_state_changed (GST_OBJECT_CAST (basesink),
4972 GST_STATE_PLAYING, GST_STATE_PAUSED, GST_STATE_READY));
4974 gst_element_post_message (GST_ELEMENT_CAST (basesink),
4975 gst_message_new_async_done (GST_OBJECT_CAST (basesink)));
4977 priv->commited = TRUE;
4979 GST_DEBUG_OBJECT (basesink, "PAUSED to READY, don't need_preroll");
4981 GST_PAD_PREROLL_UNLOCK (basesink->sinkpad);
4983 case GST_STATE_CHANGE_READY_TO_NULL:
4985 if (!bclass->stop (basesink)) {
4986 GST_WARNING_OBJECT (basesink, "failed to stop");
4989 gst_base_sink_set_last_buffer (basesink, NULL);
4990 priv->call_preroll = FALSE;
5001 GST_DEBUG_OBJECT (basesink, "failed to start");
5002 return GST_STATE_CHANGE_FAILURE;
5006 GST_DEBUG_OBJECT (basesink,
5007 "element failed to change states -- activation problem?");
5008 return GST_STATE_CHANGE_FAILURE;