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 GstClockTime throttle_time;
267 #define DO_RUNNING_AVG(avg,val,size) (((val) + ((size)-1) * (avg)) / (size))
269 /* generic running average, this has a neutral window size */
270 #define UPDATE_RUNNING_AVG(avg,val) DO_RUNNING_AVG(avg,val,8)
272 /* the windows for these running averages are experimentally obtained.
273 * possitive values get averaged more while negative values use a small
274 * window so we can react faster to badness. */
275 #define UPDATE_RUNNING_AVG_P(avg,val) DO_RUNNING_AVG(avg,val,16)
276 #define UPDATE_RUNNING_AVG_N(avg,val) DO_RUNNING_AVG(avg,val,4)
280 _PR_IS_NOTHING = 1 << 0,
281 _PR_IS_BUFFER = 1 << 1,
282 _PR_IS_BUFFERLIST = 1 << 2,
283 _PR_IS_EVENT = 1 << 3
286 #define OBJ_IS_BUFFER(a) ((a) & _PR_IS_BUFFER)
287 #define OBJ_IS_BUFFERLIST(a) ((a) & _PR_IS_BUFFERLIST)
288 #define OBJ_IS_EVENT(a) ((a) & _PR_IS_EVENT)
289 #define OBJ_IS_BUFFERFULL(a) ((a) & (_PR_IS_BUFFER | _PR_IS_BUFFERLIST))
291 /* BaseSink properties */
293 #define DEFAULT_CAN_ACTIVATE_PULL FALSE /* fixme: enable me */
294 #define DEFAULT_CAN_ACTIVATE_PUSH TRUE
296 #define DEFAULT_PREROLL_QUEUE_LEN 0
297 #define DEFAULT_SYNC TRUE
298 #define DEFAULT_MAX_LATENESS -1
299 #define DEFAULT_QOS FALSE
300 #define DEFAULT_ASYNC TRUE
301 #define DEFAULT_TS_OFFSET 0
302 #define DEFAULT_BLOCKSIZE 4096
303 #define DEFAULT_RENDER_DELAY 0
304 #define DEFAULT_ENABLE_LAST_BUFFER TRUE
305 #define DEFAULT_THROTTLE_TIME 0
310 PROP_PREROLL_QUEUE_LEN,
316 PROP_ENABLE_LAST_BUFFER,
324 static GstElementClass *parent_class = NULL;
326 static void gst_base_sink_class_init (GstBaseSinkClass * klass);
327 static void gst_base_sink_init (GstBaseSink * trans, gpointer g_class);
328 static void gst_base_sink_finalize (GObject * object);
331 gst_base_sink_get_type (void)
333 static volatile gsize base_sink_type = 0;
335 if (g_once_init_enter (&base_sink_type)) {
337 static const GTypeInfo base_sink_info = {
338 sizeof (GstBaseSinkClass),
341 (GClassInitFunc) gst_base_sink_class_init,
344 sizeof (GstBaseSink),
346 (GInstanceInitFunc) gst_base_sink_init,
349 _type = g_type_register_static (GST_TYPE_ELEMENT,
350 "GstBaseSink", &base_sink_info, G_TYPE_FLAG_ABSTRACT);
351 g_once_init_leave (&base_sink_type, _type);
353 return base_sink_type;
356 static void gst_base_sink_set_property (GObject * object, guint prop_id,
357 const GValue * value, GParamSpec * pspec);
358 static void gst_base_sink_get_property (GObject * object, guint prop_id,
359 GValue * value, GParamSpec * pspec);
361 static gboolean gst_base_sink_send_event (GstElement * element,
363 static gboolean gst_base_sink_query (GstElement * element, GstQuery * query);
364 static const GstQueryType *gst_base_sink_get_query_types (GstElement * element);
366 static GstCaps *gst_base_sink_get_caps (GstBaseSink * sink);
367 static gboolean gst_base_sink_set_caps (GstBaseSink * sink, GstCaps * caps);
368 static GstFlowReturn gst_base_sink_buffer_alloc (GstBaseSink * sink,
369 guint64 offset, guint size, GstCaps * caps, GstBuffer ** buf);
370 static void gst_base_sink_get_times (GstBaseSink * basesink, GstBuffer * buffer,
371 GstClockTime * start, GstClockTime * end);
372 static gboolean gst_base_sink_set_flushing (GstBaseSink * basesink,
373 GstPad * pad, gboolean flushing);
374 static gboolean gst_base_sink_default_activate_pull (GstBaseSink * basesink,
376 static gboolean gst_base_sink_default_do_seek (GstBaseSink * sink,
377 GstSegment * segment);
378 static gboolean gst_base_sink_default_prepare_seek_segment (GstBaseSink * sink,
379 GstEvent * event, GstSegment * segment);
381 static GstStateChangeReturn gst_base_sink_change_state (GstElement * element,
382 GstStateChange transition);
384 static GstFlowReturn gst_base_sink_chain (GstPad * pad, GstBuffer * buffer);
385 static GstFlowReturn gst_base_sink_chain_list (GstPad * pad,
386 GstBufferList * list);
388 static void gst_base_sink_loop (GstPad * pad);
389 static gboolean gst_base_sink_pad_activate (GstPad * pad);
390 static gboolean gst_base_sink_pad_activate_push (GstPad * pad, gboolean active);
391 static gboolean gst_base_sink_pad_activate_pull (GstPad * pad, gboolean active);
392 static gboolean gst_base_sink_event (GstPad * pad, GstEvent * event);
394 static gboolean gst_base_sink_negotiate_pull (GstBaseSink * basesink);
395 static GstCaps *gst_base_sink_pad_getcaps (GstPad * pad);
396 static gboolean gst_base_sink_pad_setcaps (GstPad * pad, GstCaps * caps);
397 static void gst_base_sink_pad_fixate (GstPad * pad, GstCaps * caps);
398 static GstFlowReturn gst_base_sink_pad_buffer_alloc (GstPad * pad,
399 guint64 offset, guint size, GstCaps * caps, GstBuffer ** buf);
402 /* check if an object was too late */
403 static gboolean gst_base_sink_is_too_late (GstBaseSink * basesink,
404 GstMiniObject * obj, GstClockTime start, GstClockTime stop,
405 GstClockReturn status, GstClockTimeDiff jitter);
406 static GstFlowReturn gst_base_sink_preroll_object (GstBaseSink * basesink,
407 guint8 obj_type, GstMiniObject * obj);
410 gst_base_sink_class_init (GstBaseSinkClass * klass)
412 GObjectClass *gobject_class;
413 GstElementClass *gstelement_class;
415 gobject_class = G_OBJECT_CLASS (klass);
416 gstelement_class = GST_ELEMENT_CLASS (klass);
418 GST_DEBUG_CATEGORY_INIT (gst_base_sink_debug, "basesink", 0,
421 g_type_class_add_private (klass, sizeof (GstBaseSinkPrivate));
423 parent_class = g_type_class_peek_parent (klass);
425 gobject_class->finalize = gst_base_sink_finalize;
426 gobject_class->set_property = gst_base_sink_set_property;
427 gobject_class->get_property = gst_base_sink_get_property;
429 /* FIXME, this next value should be configured using an event from the
430 * upstream element, ie, the BUFFER_SIZE event. */
431 g_object_class_install_property (gobject_class, PROP_PREROLL_QUEUE_LEN,
432 g_param_spec_uint ("preroll-queue-len", "Preroll queue length",
433 "Number of buffers to queue during preroll", 0, G_MAXUINT,
434 DEFAULT_PREROLL_QUEUE_LEN,
435 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
437 g_object_class_install_property (gobject_class, PROP_SYNC,
438 g_param_spec_boolean ("sync", "Sync", "Sync on the clock", DEFAULT_SYNC,
439 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
441 g_object_class_install_property (gobject_class, PROP_MAX_LATENESS,
442 g_param_spec_int64 ("max-lateness", "Max Lateness",
443 "Maximum number of nanoseconds that a buffer can be late before it "
444 "is dropped (-1 unlimited)", -1, G_MAXINT64, DEFAULT_MAX_LATENESS,
445 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
447 g_object_class_install_property (gobject_class, PROP_QOS,
448 g_param_spec_boolean ("qos", "Qos",
449 "Generate Quality-of-Service events upstream", DEFAULT_QOS,
450 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
454 * If set to #TRUE, the basesink will perform asynchronous state changes.
455 * When set to #FALSE, the sink will not signal the parent when it prerolls.
456 * Use this option when dealing with sparse streams or when synchronisation is
461 g_object_class_install_property (gobject_class, PROP_ASYNC,
462 g_param_spec_boolean ("async", "Async",
463 "Go asynchronously to PAUSED", DEFAULT_ASYNC,
464 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
466 * GstBaseSink:ts-offset
468 * Controls the final synchronisation, a negative value will render the buffer
469 * earlier while a positive value delays playback. This property can be
470 * used to fix synchronisation in bad files.
474 g_object_class_install_property (gobject_class, PROP_TS_OFFSET,
475 g_param_spec_int64 ("ts-offset", "TS Offset",
476 "Timestamp offset in nanoseconds", G_MININT64, G_MAXINT64,
477 DEFAULT_TS_OFFSET, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
480 * GstBaseSink:enable-last-buffer
482 * Enable the last-buffer property. If FALSE, basesink doesn't keep a
483 * reference to the last buffer arrived and the last-buffer property is always
484 * set to NULL. This can be useful if you need buffers to be released as soon
485 * as possible, eg. if you're using a buffer pool.
489 g_object_class_install_property (gobject_class, PROP_ENABLE_LAST_BUFFER,
490 g_param_spec_boolean ("enable-last-buffer", "Enable Last Buffer",
491 "Enable the last-buffer property", DEFAULT_ENABLE_LAST_BUFFER,
492 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
495 * GstBaseSink:last-buffer
497 * The last buffer that arrived in the sink and was used for preroll or for
498 * rendering. This property can be used to generate thumbnails. This property
499 * can be NULL when the sink has not yet received a bufer.
503 g_object_class_install_property (gobject_class, PROP_LAST_BUFFER,
504 gst_param_spec_mini_object ("last-buffer", "Last Buffer",
505 "The last buffer received in the sink", GST_TYPE_BUFFER,
506 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
508 * GstBaseSink:blocksize
510 * The amount of bytes to pull when operating in pull mode.
514 /* FIXME 0.11: blocksize property should be int, otherwise min>max.. */
515 g_object_class_install_property (gobject_class, PROP_BLOCKSIZE,
516 g_param_spec_uint ("blocksize", "Block size",
517 "Size in bytes to pull per buffer (0 = default)", 0, G_MAXUINT,
518 DEFAULT_BLOCKSIZE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
520 * GstBaseSink:render-delay
522 * The additional delay between synchronisation and actual rendering of the
523 * media. This property will add additional latency to the device in order to
524 * make other sinks compensate for the delay.
528 g_object_class_install_property (gobject_class, PROP_RENDER_DELAY,
529 g_param_spec_uint64 ("render-delay", "Render Delay",
530 "Additional render delay of the sink in nanoseconds", 0, G_MAXUINT64,
531 DEFAULT_RENDER_DELAY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
533 * GstBaseSink:throttle-time
535 * The time to insert between buffers. This property can be used to control
536 * the maximum amount of buffers per second to render. Setting this property
537 * to a value bigger than 0 will make the sink create THROTTLE QoS events.
541 g_object_class_install_property (gobject_class, PROP_THROTTLE_TIME,
542 g_param_spec_uint64 ("throttle-time", "Throttle time",
543 "The time to keep between rendered buffers (unused)", 0, G_MAXUINT64,
544 DEFAULT_THROTTLE_TIME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
546 gstelement_class->change_state =
547 GST_DEBUG_FUNCPTR (gst_base_sink_change_state);
548 gstelement_class->send_event = GST_DEBUG_FUNCPTR (gst_base_sink_send_event);
549 gstelement_class->query = GST_DEBUG_FUNCPTR (gst_base_sink_query);
550 gstelement_class->get_query_types =
551 GST_DEBUG_FUNCPTR (gst_base_sink_get_query_types);
553 klass->get_caps = GST_DEBUG_FUNCPTR (gst_base_sink_get_caps);
554 klass->set_caps = GST_DEBUG_FUNCPTR (gst_base_sink_set_caps);
555 klass->buffer_alloc = GST_DEBUG_FUNCPTR (gst_base_sink_buffer_alloc);
556 klass->get_times = GST_DEBUG_FUNCPTR (gst_base_sink_get_times);
557 klass->activate_pull =
558 GST_DEBUG_FUNCPTR (gst_base_sink_default_activate_pull);
560 /* Registering debug symbols for function pointers */
561 GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_pad_getcaps);
562 GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_pad_setcaps);
563 GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_pad_fixate);
564 GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_pad_buffer_alloc);
565 GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_pad_activate);
566 GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_pad_activate_push);
567 GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_pad_activate_pull);
568 GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_event);
569 GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_chain);
570 GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_chain_list);
574 gst_base_sink_pad_getcaps (GstPad * pad)
576 GstBaseSinkClass *bclass;
578 GstCaps *caps = NULL;
580 bsink = GST_BASE_SINK (gst_pad_get_parent (pad));
581 bclass = GST_BASE_SINK_GET_CLASS (bsink);
583 if (bsink->pad_mode == GST_ACTIVATE_PULL) {
584 /* if we are operating in pull mode we only accept the negotiated caps */
585 GST_OBJECT_LOCK (pad);
586 if ((caps = GST_PAD_CAPS (pad)))
588 GST_OBJECT_UNLOCK (pad);
591 if (bclass->get_caps)
592 caps = bclass->get_caps (bsink);
595 GstPadTemplate *pad_template;
598 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (bclass),
600 if (pad_template != NULL) {
601 caps = gst_caps_ref (gst_pad_template_get_caps (pad_template));
605 gst_object_unref (bsink);
611 gst_base_sink_pad_setcaps (GstPad * pad, GstCaps * caps)
613 GstBaseSinkClass *bclass;
617 bsink = GST_BASE_SINK (gst_pad_get_parent (pad));
618 bclass = GST_BASE_SINK_GET_CLASS (bsink);
620 if (res && bclass->set_caps)
621 res = bclass->set_caps (bsink, caps);
623 gst_object_unref (bsink);
629 gst_base_sink_pad_fixate (GstPad * pad, GstCaps * caps)
631 GstBaseSinkClass *bclass;
634 bsink = GST_BASE_SINK (gst_pad_get_parent (pad));
635 bclass = GST_BASE_SINK_GET_CLASS (bsink);
638 bclass->fixate (bsink, caps);
640 gst_object_unref (bsink);
644 gst_base_sink_pad_buffer_alloc (GstPad * pad, guint64 offset, guint size,
645 GstCaps * caps, GstBuffer ** buf)
647 GstBaseSinkClass *bclass;
649 GstFlowReturn result = GST_FLOW_OK;
651 bsink = GST_BASE_SINK (gst_pad_get_parent (pad));
652 bclass = GST_BASE_SINK_GET_CLASS (bsink);
654 if (bclass->buffer_alloc)
655 result = bclass->buffer_alloc (bsink, offset, size, caps, buf);
657 *buf = NULL; /* fallback in gstpad.c will allocate generic buffer */
659 gst_object_unref (bsink);
665 gst_base_sink_init (GstBaseSink * basesink, gpointer g_class)
667 GstPadTemplate *pad_template;
668 GstBaseSinkPrivate *priv;
670 basesink->priv = priv = GST_BASE_SINK_GET_PRIVATE (basesink);
673 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (g_class), "sink");
674 g_return_if_fail (pad_template != NULL);
676 basesink->sinkpad = gst_pad_new_from_template (pad_template, "sink");
678 gst_pad_set_getcaps_function (basesink->sinkpad, gst_base_sink_pad_getcaps);
679 gst_pad_set_setcaps_function (basesink->sinkpad, gst_base_sink_pad_setcaps);
680 gst_pad_set_fixatecaps_function (basesink->sinkpad, gst_base_sink_pad_fixate);
681 gst_pad_set_bufferalloc_function (basesink->sinkpad,
682 gst_base_sink_pad_buffer_alloc);
683 gst_pad_set_activate_function (basesink->sinkpad, gst_base_sink_pad_activate);
684 gst_pad_set_activatepush_function (basesink->sinkpad,
685 gst_base_sink_pad_activate_push);
686 gst_pad_set_activatepull_function (basesink->sinkpad,
687 gst_base_sink_pad_activate_pull);
688 gst_pad_set_event_function (basesink->sinkpad, gst_base_sink_event);
689 gst_pad_set_chain_function (basesink->sinkpad, gst_base_sink_chain);
690 gst_pad_set_chain_list_function (basesink->sinkpad, gst_base_sink_chain_list);
691 gst_element_add_pad (GST_ELEMENT_CAST (basesink), basesink->sinkpad);
693 basesink->pad_mode = GST_ACTIVATE_NONE;
694 basesink->preroll_queue = g_queue_new ();
695 basesink->abidata.ABI.clip_segment = gst_segment_new ();
696 priv->have_latency = FALSE;
698 basesink->can_activate_push = DEFAULT_CAN_ACTIVATE_PUSH;
699 basesink->can_activate_pull = DEFAULT_CAN_ACTIVATE_PULL;
701 basesink->sync = DEFAULT_SYNC;
702 basesink->abidata.ABI.max_lateness = DEFAULT_MAX_LATENESS;
703 g_atomic_int_set (&priv->qos_enabled, DEFAULT_QOS);
704 priv->async_enabled = DEFAULT_ASYNC;
705 priv->ts_offset = DEFAULT_TS_OFFSET;
706 priv->render_delay = DEFAULT_RENDER_DELAY;
707 priv->blocksize = DEFAULT_BLOCKSIZE;
708 priv->cached_clock_id = NULL;
709 g_atomic_int_set (&priv->enable_last_buffer, DEFAULT_ENABLE_LAST_BUFFER);
710 priv->throttle_time = DEFAULT_THROTTLE_TIME;
712 GST_OBJECT_FLAG_SET (basesink, GST_ELEMENT_IS_SINK);
716 gst_base_sink_finalize (GObject * object)
718 GstBaseSink *basesink;
720 basesink = GST_BASE_SINK (object);
722 g_queue_free (basesink->preroll_queue);
723 gst_segment_free (basesink->abidata.ABI.clip_segment);
725 G_OBJECT_CLASS (parent_class)->finalize (object);
729 * gst_base_sink_set_sync:
731 * @sync: the new sync value.
733 * Configures @sink to synchronize on the clock or not. When
734 * @sync is FALSE, incomming samples will be played as fast as
735 * possible. If @sync is TRUE, the timestamps of the incomming
736 * buffers will be used to schedule the exact render time of its
742 gst_base_sink_set_sync (GstBaseSink * sink, gboolean sync)
744 g_return_if_fail (GST_IS_BASE_SINK (sink));
746 GST_OBJECT_LOCK (sink);
748 GST_OBJECT_UNLOCK (sink);
752 * gst_base_sink_get_sync:
755 * Checks if @sink is currently configured to synchronize against the
758 * Returns: TRUE if the sink is configured to synchronize against the clock.
763 gst_base_sink_get_sync (GstBaseSink * sink)
767 g_return_val_if_fail (GST_IS_BASE_SINK (sink), FALSE);
769 GST_OBJECT_LOCK (sink);
771 GST_OBJECT_UNLOCK (sink);
777 * gst_base_sink_set_max_lateness:
779 * @max_lateness: the new max lateness value.
781 * Sets the new max lateness value to @max_lateness. This value is
782 * used to decide if a buffer should be dropped or not based on the
783 * buffer timestamp and the current clock time. A value of -1 means
789 gst_base_sink_set_max_lateness (GstBaseSink * sink, gint64 max_lateness)
791 g_return_if_fail (GST_IS_BASE_SINK (sink));
793 GST_OBJECT_LOCK (sink);
794 sink->abidata.ABI.max_lateness = max_lateness;
795 GST_OBJECT_UNLOCK (sink);
799 * gst_base_sink_get_max_lateness:
802 * Gets the max lateness value. See gst_base_sink_set_max_lateness for
805 * Returns: The maximum time in nanoseconds that a buffer can be late
806 * before it is dropped and not rendered. A value of -1 means an
812 gst_base_sink_get_max_lateness (GstBaseSink * sink)
816 g_return_val_if_fail (GST_IS_BASE_SINK (sink), -1);
818 GST_OBJECT_LOCK (sink);
819 res = sink->abidata.ABI.max_lateness;
820 GST_OBJECT_UNLOCK (sink);
826 * gst_base_sink_set_qos_enabled:
828 * @enabled: the new qos value.
830 * Configures @sink to send Quality-of-Service events upstream.
835 gst_base_sink_set_qos_enabled (GstBaseSink * sink, gboolean enabled)
837 g_return_if_fail (GST_IS_BASE_SINK (sink));
839 g_atomic_int_set (&sink->priv->qos_enabled, enabled);
843 * gst_base_sink_is_qos_enabled:
846 * Checks if @sink is currently configured to send Quality-of-Service events
849 * Returns: TRUE if the sink is configured to perform Quality-of-Service.
854 gst_base_sink_is_qos_enabled (GstBaseSink * sink)
858 g_return_val_if_fail (GST_IS_BASE_SINK (sink), FALSE);
860 res = g_atomic_int_get (&sink->priv->qos_enabled);
866 * gst_base_sink_set_async_enabled:
868 * @enabled: the new async value.
870 * Configures @sink to perform all state changes asynchronusly. When async is
871 * disabled, the sink will immediatly go to PAUSED instead of waiting for a
872 * preroll buffer. This feature is usefull if the sink does not synchronize
873 * against the clock or when it is dealing with sparse streams.
878 gst_base_sink_set_async_enabled (GstBaseSink * sink, gboolean enabled)
880 g_return_if_fail (GST_IS_BASE_SINK (sink));
882 GST_PAD_PREROLL_LOCK (sink->sinkpad);
883 g_atomic_int_set (&sink->priv->async_enabled, enabled);
884 GST_LOG_OBJECT (sink, "set async enabled to %d", enabled);
885 GST_PAD_PREROLL_UNLOCK (sink->sinkpad);
889 * gst_base_sink_is_async_enabled:
892 * Checks if @sink is currently configured to perform asynchronous state
895 * Returns: TRUE if the sink is configured to perform asynchronous state
901 gst_base_sink_is_async_enabled (GstBaseSink * sink)
905 g_return_val_if_fail (GST_IS_BASE_SINK (sink), FALSE);
907 res = g_atomic_int_get (&sink->priv->async_enabled);
913 * gst_base_sink_set_ts_offset:
915 * @offset: the new offset
917 * Adjust the synchronisation of @sink with @offset. A negative value will
918 * render buffers earlier than their timestamp. A positive value will delay
919 * rendering. This function can be used to fix playback of badly timestamped
925 gst_base_sink_set_ts_offset (GstBaseSink * sink, GstClockTimeDiff offset)
927 g_return_if_fail (GST_IS_BASE_SINK (sink));
929 GST_OBJECT_LOCK (sink);
930 sink->priv->ts_offset = offset;
931 GST_LOG_OBJECT (sink, "set time offset to %" G_GINT64_FORMAT, offset);
932 GST_OBJECT_UNLOCK (sink);
936 * gst_base_sink_get_ts_offset:
939 * Get the synchronisation offset of @sink.
941 * Returns: The synchronisation offset.
946 gst_base_sink_get_ts_offset (GstBaseSink * sink)
948 GstClockTimeDiff res;
950 g_return_val_if_fail (GST_IS_BASE_SINK (sink), 0);
952 GST_OBJECT_LOCK (sink);
953 res = sink->priv->ts_offset;
954 GST_OBJECT_UNLOCK (sink);
960 * gst_base_sink_get_last_buffer:
963 * Get the last buffer that arrived in the sink and was used for preroll or for
964 * rendering. This property can be used to generate thumbnails.
966 * The #GstCaps on the buffer can be used to determine the type of the buffer.
968 * Free-function: gst_buffer_unref
970 * Returns: (transfer full): a #GstBuffer. gst_buffer_unref() after usage.
971 * This function returns NULL when no buffer has arrived in the sink yet
972 * or when the sink is not in PAUSED or PLAYING.
977 gst_base_sink_get_last_buffer (GstBaseSink * sink)
981 g_return_val_if_fail (GST_IS_BASE_SINK (sink), NULL);
983 GST_OBJECT_LOCK (sink);
984 if ((res = sink->priv->last_buffer))
985 gst_buffer_ref (res);
986 GST_OBJECT_UNLOCK (sink);
991 /* with OBJECT_LOCK */
993 gst_base_sink_set_last_buffer_unlocked (GstBaseSink * sink, GstBuffer * buffer)
997 old = sink->priv->last_buffer;
998 if (G_LIKELY (old != buffer)) {
999 GST_DEBUG_OBJECT (sink, "setting last buffer to %p", buffer);
1000 if (G_LIKELY (buffer))
1001 gst_buffer_ref (buffer);
1002 sink->priv->last_buffer = buffer;
1006 /* avoid unreffing with the lock because cleanup code might want to take the
1008 if (G_LIKELY (old)) {
1009 GST_OBJECT_UNLOCK (sink);
1010 gst_buffer_unref (old);
1011 GST_OBJECT_LOCK (sink);
1016 gst_base_sink_set_last_buffer (GstBaseSink * sink, GstBuffer * buffer)
1018 if (!g_atomic_int_get (&sink->priv->enable_last_buffer))
1021 GST_OBJECT_LOCK (sink);
1022 gst_base_sink_set_last_buffer_unlocked (sink, buffer);
1023 GST_OBJECT_UNLOCK (sink);
1027 * gst_base_sink_set_last_buffer_enabled:
1029 * @enabled: the new enable-last-buffer value.
1031 * Configures @sink to store the last received buffer in the last-buffer
1037 gst_base_sink_set_last_buffer_enabled (GstBaseSink * sink, gboolean enabled)
1039 g_return_if_fail (GST_IS_BASE_SINK (sink));
1041 /* Only take lock if we change the value */
1042 if (g_atomic_int_compare_and_exchange (&sink->priv->enable_last_buffer,
1043 !enabled, enabled) && !enabled) {
1044 GST_OBJECT_LOCK (sink);
1045 gst_base_sink_set_last_buffer_unlocked (sink, NULL);
1046 GST_OBJECT_UNLOCK (sink);
1051 * gst_base_sink_is_last_buffer_enabled:
1054 * Checks if @sink is currently configured to store the last received buffer in
1055 * the last-buffer property.
1057 * Returns: TRUE if the sink is configured to store the last received buffer.
1062 gst_base_sink_is_last_buffer_enabled (GstBaseSink * sink)
1064 g_return_val_if_fail (GST_IS_BASE_SINK (sink), FALSE);
1066 return g_atomic_int_get (&sink->priv->enable_last_buffer);
1070 * gst_base_sink_get_latency:
1073 * Get the currently configured latency.
1075 * Returns: The configured latency.
1080 gst_base_sink_get_latency (GstBaseSink * sink)
1084 GST_OBJECT_LOCK (sink);
1085 res = sink->priv->latency;
1086 GST_OBJECT_UNLOCK (sink);
1092 * gst_base_sink_query_latency:
1094 * @live: (out) (allow-none): if the sink is live
1095 * @upstream_live: (out) (allow-none): if an upstream element is live
1096 * @min_latency: (out) (allow-none): the min latency of the upstream elements
1097 * @max_latency: (out) (allow-none): the max latency of the upstream elements
1099 * Query the sink for the latency parameters. The latency will be queried from
1100 * the upstream elements. @live will be TRUE if @sink is configured to
1101 * synchronize against the clock. @upstream_live will be TRUE if an upstream
1104 * If both @live and @upstream_live are TRUE, the sink will want to compensate
1105 * for the latency introduced by the upstream elements by setting the
1106 * @min_latency to a strictly possitive value.
1108 * This function is mostly used by subclasses.
1110 * Returns: TRUE if the query succeeded.
1115 gst_base_sink_query_latency (GstBaseSink * sink, gboolean * live,
1116 gboolean * upstream_live, GstClockTime * min_latency,
1117 GstClockTime * max_latency)
1119 gboolean l, us_live, res, have_latency;
1120 GstClockTime min, max, render_delay;
1122 GstClockTime us_min, us_max;
1124 /* we are live when we sync to the clock */
1125 GST_OBJECT_LOCK (sink);
1127 have_latency = sink->priv->have_latency;
1128 render_delay = sink->priv->render_delay;
1129 GST_OBJECT_UNLOCK (sink);
1131 /* assume no latency */
1137 GST_DEBUG_OBJECT (sink, "we are ready for LATENCY query");
1138 /* we are ready for a latency query this is when we preroll or when we are
1140 query = gst_query_new_latency ();
1142 /* ask the peer for the latency */
1143 if ((res = gst_pad_peer_query (sink->sinkpad, query))) {
1144 /* get upstream min and max latency */
1145 gst_query_parse_latency (query, &us_live, &us_min, &us_max);
1148 /* upstream live, use its latency, subclasses should use these
1149 * values to create the complete latency. */
1154 /* we need to add the render delay if we are live */
1156 min += render_delay;
1158 max += render_delay;
1161 gst_query_unref (query);
1163 GST_DEBUG_OBJECT (sink, "we are not yet ready for LATENCY query");
1167 /* not live, we tried to do the query, if it failed we return TRUE anyway */
1171 GST_DEBUG_OBJECT (sink, "latency query failed but we are not live");
1173 GST_DEBUG_OBJECT (sink, "latency query failed and we are live");
1178 GST_DEBUG_OBJECT (sink, "latency query: live: %d, have_latency %d,"
1179 " upstream: %d, min %" GST_TIME_FORMAT ", max %" GST_TIME_FORMAT, l,
1180 have_latency, us_live, GST_TIME_ARGS (min), GST_TIME_ARGS (max));
1185 *upstream_live = us_live;
1195 * gst_base_sink_set_render_delay:
1196 * @sink: a #GstBaseSink
1197 * @delay: the new delay
1199 * Set the render delay in @sink to @delay. The render delay is the time
1200 * between actual rendering of a buffer and its synchronisation time. Some
1201 * devices might delay media rendering which can be compensated for with this
1204 * After calling this function, this sink will report additional latency and
1205 * other sinks will adjust their latency to delay the rendering of their media.
1207 * This function is usually called by subclasses.
1212 gst_base_sink_set_render_delay (GstBaseSink * sink, GstClockTime delay)
1214 GstClockTime old_render_delay;
1216 g_return_if_fail (GST_IS_BASE_SINK (sink));
1218 GST_OBJECT_LOCK (sink);
1219 old_render_delay = sink->priv->render_delay;
1220 sink->priv->render_delay = delay;
1221 GST_LOG_OBJECT (sink, "set render delay to %" GST_TIME_FORMAT,
1222 GST_TIME_ARGS (delay));
1223 GST_OBJECT_UNLOCK (sink);
1225 if (delay != old_render_delay) {
1226 GST_DEBUG_OBJECT (sink, "posting latency changed");
1227 gst_element_post_message (GST_ELEMENT_CAST (sink),
1228 gst_message_new_latency (GST_OBJECT_CAST (sink)));
1233 * gst_base_sink_get_render_delay:
1234 * @sink: a #GstBaseSink
1236 * Get the render delay of @sink. see gst_base_sink_set_render_delay() for more
1237 * information about the render delay.
1239 * Returns: the render delay of @sink.
1244 gst_base_sink_get_render_delay (GstBaseSink * sink)
1246 GstClockTimeDiff res;
1248 g_return_val_if_fail (GST_IS_BASE_SINK (sink), 0);
1250 GST_OBJECT_LOCK (sink);
1251 res = sink->priv->render_delay;
1252 GST_OBJECT_UNLOCK (sink);
1258 * gst_base_sink_set_blocksize:
1259 * @sink: a #GstBaseSink
1260 * @blocksize: the blocksize in bytes
1262 * Set the number of bytes that the sink will pull when it is operating in pull
1267 /* FIXME 0.11: blocksize property should be int, otherwise min>max.. */
1269 gst_base_sink_set_blocksize (GstBaseSink * sink, guint blocksize)
1271 g_return_if_fail (GST_IS_BASE_SINK (sink));
1273 GST_OBJECT_LOCK (sink);
1274 sink->priv->blocksize = blocksize;
1275 GST_LOG_OBJECT (sink, "set blocksize to %u", blocksize);
1276 GST_OBJECT_UNLOCK (sink);
1280 * gst_base_sink_get_blocksize:
1281 * @sink: a #GstBaseSink
1283 * Get the number of bytes that the sink will pull when it is operating in pull
1286 * Returns: the number of bytes @sink will pull in pull mode.
1290 /* FIXME 0.11: blocksize property should be int, otherwise min>max.. */
1292 gst_base_sink_get_blocksize (GstBaseSink * sink)
1296 g_return_val_if_fail (GST_IS_BASE_SINK (sink), 0);
1298 GST_OBJECT_LOCK (sink);
1299 res = sink->priv->blocksize;
1300 GST_OBJECT_UNLOCK (sink);
1306 * gst_base_sink_set_throttle_time:
1307 * @sink: a #GstBaseSink
1308 * @throttle: the throttle time in nanoseconds
1310 * Set the time that will be inserted between rendered buffers. This
1311 * can be used to control the maximum buffers per second that the sink
1317 gst_base_sink_set_throttle_time (GstBaseSink * sink, guint64 throttle)
1319 g_return_if_fail (GST_IS_BASE_SINK (sink));
1321 GST_OBJECT_LOCK (sink);
1322 sink->priv->throttle_time = throttle;
1323 GST_LOG_OBJECT (sink, "set throttle_time to %" G_GUINT64_FORMAT, throttle);
1324 GST_OBJECT_UNLOCK (sink);
1328 * gst_base_sink_get_throttle_time:
1329 * @sink: a #GstBaseSink
1331 * Get the time that will be inserted between frames to control the
1332 * maximum buffers per second.
1334 * Returns: the number of nanoseconds @sink will put between frames.
1339 gst_base_sink_get_throttle_time (GstBaseSink * sink)
1343 g_return_val_if_fail (GST_IS_BASE_SINK (sink), 0);
1345 GST_OBJECT_LOCK (sink);
1346 res = sink->priv->throttle_time;
1347 GST_OBJECT_UNLOCK (sink);
1353 gst_base_sink_set_property (GObject * object, guint prop_id,
1354 const GValue * value, GParamSpec * pspec)
1356 GstBaseSink *sink = GST_BASE_SINK (object);
1359 case PROP_PREROLL_QUEUE_LEN:
1360 /* preroll lock necessary to serialize with finish_preroll */
1361 GST_PAD_PREROLL_LOCK (sink->sinkpad);
1362 g_atomic_int_set (&sink->preroll_queue_max_len, g_value_get_uint (value));
1363 GST_PAD_PREROLL_UNLOCK (sink->sinkpad);
1366 gst_base_sink_set_sync (sink, g_value_get_boolean (value));
1368 case PROP_MAX_LATENESS:
1369 gst_base_sink_set_max_lateness (sink, g_value_get_int64 (value));
1372 gst_base_sink_set_qos_enabled (sink, g_value_get_boolean (value));
1375 gst_base_sink_set_async_enabled (sink, g_value_get_boolean (value));
1377 case PROP_TS_OFFSET:
1378 gst_base_sink_set_ts_offset (sink, g_value_get_int64 (value));
1380 case PROP_BLOCKSIZE:
1381 gst_base_sink_set_blocksize (sink, g_value_get_uint (value));
1383 case PROP_RENDER_DELAY:
1384 gst_base_sink_set_render_delay (sink, g_value_get_uint64 (value));
1386 case PROP_ENABLE_LAST_BUFFER:
1387 gst_base_sink_set_last_buffer_enabled (sink, g_value_get_boolean (value));
1389 case PROP_THROTTLE_TIME:
1390 gst_base_sink_set_throttle_time (sink, g_value_get_uint64 (value));
1393 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1399 gst_base_sink_get_property (GObject * object, guint prop_id, GValue * value,
1402 GstBaseSink *sink = GST_BASE_SINK (object);
1405 case PROP_PREROLL_QUEUE_LEN:
1406 g_value_set_uint (value, g_atomic_int_get (&sink->preroll_queue_max_len));
1409 g_value_set_boolean (value, gst_base_sink_get_sync (sink));
1411 case PROP_MAX_LATENESS:
1412 g_value_set_int64 (value, gst_base_sink_get_max_lateness (sink));
1415 g_value_set_boolean (value, gst_base_sink_is_qos_enabled (sink));
1418 g_value_set_boolean (value, gst_base_sink_is_async_enabled (sink));
1420 case PROP_TS_OFFSET:
1421 g_value_set_int64 (value, gst_base_sink_get_ts_offset (sink));
1423 case PROP_LAST_BUFFER:
1424 gst_value_take_buffer (value, gst_base_sink_get_last_buffer (sink));
1426 case PROP_ENABLE_LAST_BUFFER:
1427 g_value_set_boolean (value, gst_base_sink_is_last_buffer_enabled (sink));
1429 case PROP_BLOCKSIZE:
1430 g_value_set_uint (value, gst_base_sink_get_blocksize (sink));
1432 case PROP_RENDER_DELAY:
1433 g_value_set_uint64 (value, gst_base_sink_get_render_delay (sink));
1435 case PROP_THROTTLE_TIME:
1436 g_value_set_uint64 (value, gst_base_sink_get_throttle_time (sink));
1439 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1446 gst_base_sink_get_caps (GstBaseSink * sink)
1452 gst_base_sink_set_caps (GstBaseSink * sink, GstCaps * caps)
1457 static GstFlowReturn
1458 gst_base_sink_buffer_alloc (GstBaseSink * sink, guint64 offset, guint size,
1459 GstCaps * caps, GstBuffer ** buf)
1465 /* with PREROLL_LOCK, STREAM_LOCK */
1467 gst_base_sink_preroll_queue_flush (GstBaseSink * basesink, GstPad * pad)
1471 GST_DEBUG_OBJECT (basesink, "flushing queue %p", basesink);
1472 while ((obj = g_queue_pop_head (basesink->preroll_queue))) {
1473 GST_DEBUG_OBJECT (basesink, "popped %p", obj);
1474 gst_mini_object_unref (obj);
1476 /* we can't have EOS anymore now */
1477 basesink->eos = FALSE;
1478 basesink->priv->received_eos = FALSE;
1479 basesink->have_preroll = FALSE;
1480 basesink->priv->step_unlock = FALSE;
1481 basesink->eos_queued = FALSE;
1482 basesink->preroll_queued = 0;
1483 basesink->buffers_queued = 0;
1484 basesink->events_queued = 0;
1485 /* can't report latency anymore until we preroll again */
1486 if (basesink->priv->async_enabled) {
1487 GST_OBJECT_LOCK (basesink);
1488 basesink->priv->have_latency = FALSE;
1489 GST_OBJECT_UNLOCK (basesink);
1491 /* and signal any waiters now */
1492 GST_PAD_PREROLL_SIGNAL (pad);
1495 /* with STREAM_LOCK, configures given segment with the event information. */
1497 gst_base_sink_configure_segment (GstBaseSink * basesink, GstPad * pad,
1498 GstEvent * event, GstSegment * segment)
1501 gdouble rate, arate;
1507 /* the newsegment event is needed to bring the buffer timestamps to the
1508 * stream time and to drop samples outside of the playback segment. */
1509 gst_event_parse_new_segment_full (event, &update, &rate, &arate, &format,
1510 &start, &stop, &time);
1512 /* The segment is protected with both the STREAM_LOCK and the OBJECT_LOCK.
1513 * We protect with the OBJECT_LOCK so that we can use the values to
1514 * safely answer a POSITION query. */
1515 GST_OBJECT_LOCK (basesink);
1516 gst_segment_set_newsegment_full (segment, update, rate, arate, format, start,
1519 if (format == GST_FORMAT_TIME) {
1520 GST_DEBUG_OBJECT (basesink,
1521 "configured NEWSEGMENT update %d, rate %lf, applied rate %lf, "
1522 "format GST_FORMAT_TIME, "
1523 "%" GST_TIME_FORMAT " -- %" GST_TIME_FORMAT
1524 ", time %" GST_TIME_FORMAT ", accum %" GST_TIME_FORMAT,
1525 update, rate, arate, GST_TIME_ARGS (segment->start),
1526 GST_TIME_ARGS (segment->stop), GST_TIME_ARGS (segment->time),
1527 GST_TIME_ARGS (segment->accum));
1529 GST_DEBUG_OBJECT (basesink,
1530 "configured NEWSEGMENT update %d, rate %lf, applied rate %lf, "
1532 "%" G_GINT64_FORMAT " -- %" G_GINT64_FORMAT ", time %"
1533 G_GINT64_FORMAT ", accum %" G_GINT64_FORMAT, update, rate, arate,
1534 segment->format, segment->start, segment->stop, segment->time,
1537 GST_OBJECT_UNLOCK (basesink);
1540 /* with PREROLL_LOCK, STREAM_LOCK */
1542 gst_base_sink_commit_state (GstBaseSink * basesink)
1544 /* commit state and proceed to next pending state */
1545 GstState current, next, pending, post_pending;
1546 gboolean post_paused = FALSE;
1547 gboolean post_async_done = FALSE;
1548 gboolean post_playing = FALSE;
1550 /* we are certainly not playing async anymore now */
1551 basesink->playing_async = FALSE;
1553 GST_OBJECT_LOCK (basesink);
1554 current = GST_STATE (basesink);
1555 next = GST_STATE_NEXT (basesink);
1556 pending = GST_STATE_PENDING (basesink);
1557 post_pending = pending;
1560 case GST_STATE_PLAYING:
1562 GstBaseSinkClass *bclass;
1563 GstStateChangeReturn ret;
1565 bclass = GST_BASE_SINK_GET_CLASS (basesink);
1567 GST_DEBUG_OBJECT (basesink, "commiting state to PLAYING");
1569 basesink->need_preroll = FALSE;
1570 post_async_done = TRUE;
1571 basesink->priv->commited = TRUE;
1572 post_playing = TRUE;
1573 /* post PAUSED too when we were READY */
1574 if (current == GST_STATE_READY) {
1578 /* make sure we notify the subclass of async playing */
1579 if (bclass->async_play) {
1580 GST_WARNING_OBJECT (basesink, "deprecated async_play");
1581 ret = bclass->async_play (basesink);
1582 if (ret == GST_STATE_CHANGE_FAILURE)
1587 case GST_STATE_PAUSED:
1588 GST_DEBUG_OBJECT (basesink, "commiting state to PAUSED");
1590 post_async_done = TRUE;
1591 basesink->priv->commited = TRUE;
1592 post_pending = GST_STATE_VOID_PENDING;
1594 case GST_STATE_READY:
1595 case GST_STATE_NULL:
1597 case GST_STATE_VOID_PENDING:
1598 goto nothing_pending;
1603 /* we can report latency queries now */
1604 basesink->priv->have_latency = TRUE;
1606 GST_STATE (basesink) = pending;
1607 GST_STATE_NEXT (basesink) = GST_STATE_VOID_PENDING;
1608 GST_STATE_PENDING (basesink) = GST_STATE_VOID_PENDING;
1609 GST_STATE_RETURN (basesink) = GST_STATE_CHANGE_SUCCESS;
1610 GST_OBJECT_UNLOCK (basesink);
1613 GST_DEBUG_OBJECT (basesink, "posting PAUSED state change message");
1614 gst_element_post_message (GST_ELEMENT_CAST (basesink),
1615 gst_message_new_state_changed (GST_OBJECT_CAST (basesink),
1616 current, next, post_pending));
1618 if (post_async_done) {
1619 GST_DEBUG_OBJECT (basesink, "posting async-done message");
1620 gst_element_post_message (GST_ELEMENT_CAST (basesink),
1621 gst_message_new_async_done (GST_OBJECT_CAST (basesink)));
1624 GST_DEBUG_OBJECT (basesink, "posting PLAYING state change message");
1625 gst_element_post_message (GST_ELEMENT_CAST (basesink),
1626 gst_message_new_state_changed (GST_OBJECT_CAST (basesink),
1627 next, pending, GST_STATE_VOID_PENDING));
1630 GST_STATE_BROADCAST (basesink);
1636 /* Depending on the state, set our vars. We get in this situation when the
1637 * state change function got a change to update the state vars before the
1638 * streaming thread did. This is fine but we need to make sure that we
1639 * update the need_preroll var since it was TRUE when we got here and might
1640 * become FALSE if we got to PLAYING. */
1641 GST_DEBUG_OBJECT (basesink, "nothing to commit, now in %s",
1642 gst_element_state_get_name (current));
1644 case GST_STATE_PLAYING:
1645 basesink->need_preroll = FALSE;
1647 case GST_STATE_PAUSED:
1648 basesink->need_preroll = TRUE;
1651 basesink->need_preroll = FALSE;
1652 basesink->flushing = TRUE;
1655 /* we can report latency queries now */
1656 basesink->priv->have_latency = TRUE;
1657 GST_OBJECT_UNLOCK (basesink);
1662 /* app is going to READY */
1663 GST_DEBUG_OBJECT (basesink, "stopping");
1664 basesink->need_preroll = FALSE;
1665 basesink->flushing = TRUE;
1666 GST_OBJECT_UNLOCK (basesink);
1671 GST_DEBUG_OBJECT (basesink, "async commit failed");
1672 GST_STATE_RETURN (basesink) = GST_STATE_CHANGE_FAILURE;
1673 GST_OBJECT_UNLOCK (basesink);
1679 start_stepping (GstBaseSink * sink, GstSegment * segment,
1680 GstStepInfo * pending, GstStepInfo * current)
1683 GstMessage *message;
1685 GST_DEBUG_OBJECT (sink, "update pending step");
1687 GST_OBJECT_LOCK (sink);
1688 memcpy (current, pending, sizeof (GstStepInfo));
1689 pending->valid = FALSE;
1690 GST_OBJECT_UNLOCK (sink);
1692 /* post message first */
1694 gst_message_new_step_start (GST_OBJECT (sink), TRUE, current->format,
1695 current->amount, current->rate, current->flush, current->intermediate);
1696 gst_message_set_seqnum (message, current->seqnum);
1697 gst_element_post_message (GST_ELEMENT (sink), message);
1699 /* get the running time of where we paused and remember it */
1700 current->start = gst_element_get_start_time (GST_ELEMENT_CAST (sink));
1701 gst_segment_set_running_time (segment, GST_FORMAT_TIME, current->start);
1703 /* set the new rate for the remainder of the segment */
1704 current->start_rate = segment->rate;
1705 segment->rate *= current->rate;
1706 segment->abs_rate = ABS (segment->rate);
1709 if (segment->rate > 0.0)
1710 current->start_stop = segment->stop;
1712 current->start_start = segment->start;
1714 if (current->format == GST_FORMAT_TIME) {
1715 end = current->start + current->amount;
1716 if (!current->flush) {
1717 /* update the segment clipping regions for non-flushing seeks */
1718 if (segment->rate > 0.0) {
1719 segment->stop = gst_segment_to_position (segment, GST_FORMAT_TIME, end);
1720 segment->last_stop = segment->stop;
1724 position = gst_segment_to_position (segment, GST_FORMAT_TIME, end);
1725 segment->time = position;
1726 segment->start = position;
1727 segment->last_stop = position;
1732 GST_DEBUG_OBJECT (sink,
1733 "segment now rate %lf, applied rate %lf, "
1734 "format GST_FORMAT_TIME, "
1735 "%" GST_TIME_FORMAT " -- %" GST_TIME_FORMAT
1736 ", time %" GST_TIME_FORMAT ", accum %" GST_TIME_FORMAT,
1737 segment->rate, segment->applied_rate, GST_TIME_ARGS (segment->start),
1738 GST_TIME_ARGS (segment->stop), GST_TIME_ARGS (segment->time),
1739 GST_TIME_ARGS (segment->accum));
1741 GST_DEBUG_OBJECT (sink, "step started at running_time %" GST_TIME_FORMAT,
1742 GST_TIME_ARGS (current->start));
1744 if (current->amount == -1) {
1745 GST_DEBUG_OBJECT (sink, "step amount == -1, stop stepping");
1746 current->valid = FALSE;
1748 GST_DEBUG_OBJECT (sink, "step amount: %" G_GUINT64_FORMAT ", format: %s, "
1749 "rate: %f", current->amount, gst_format_get_name (current->format),
1755 stop_stepping (GstBaseSink * sink, GstSegment * segment,
1756 GstStepInfo * current, gint64 rstart, gint64 rstop, gboolean eos)
1758 gint64 stop, position;
1759 GstMessage *message;
1761 GST_DEBUG_OBJECT (sink, "step complete");
1763 if (segment->rate > 0.0)
1768 GST_DEBUG_OBJECT (sink,
1769 "step stop at running_time %" GST_TIME_FORMAT, GST_TIME_ARGS (stop));
1772 current->duration = current->position;
1774 current->duration = stop - current->start;
1776 GST_DEBUG_OBJECT (sink, "step elapsed running_time %" GST_TIME_FORMAT,
1777 GST_TIME_ARGS (current->duration));
1779 position = current->start + current->duration;
1781 /* now move the segment to the new running time */
1782 gst_segment_set_running_time (segment, GST_FORMAT_TIME, position);
1784 if (current->flush) {
1785 /* and remove the accumulated time we flushed, start time did not change */
1786 segment->accum = current->start;
1788 /* start time is now the stepped position */
1789 gst_element_set_start_time (GST_ELEMENT_CAST (sink), position);
1792 /* restore the previous rate */
1793 segment->rate = current->start_rate;
1794 segment->abs_rate = ABS (segment->rate);
1796 if (segment->rate > 0.0)
1797 segment->stop = current->start_stop;
1799 segment->start = current->start_start;
1801 /* the clip segment is used for position report in paused... */
1802 memcpy (sink->abidata.ABI.clip_segment, segment, sizeof (GstSegment));
1804 /* post the step done when we know the stepped duration in TIME */
1806 gst_message_new_step_done (GST_OBJECT_CAST (sink), current->format,
1807 current->amount, current->rate, current->flush, current->intermediate,
1808 current->duration, eos);
1809 gst_message_set_seqnum (message, current->seqnum);
1810 gst_element_post_message (GST_ELEMENT_CAST (sink), message);
1812 if (!current->intermediate)
1813 sink->need_preroll = current->need_preroll;
1815 /* and the current step info finished and becomes invalid */
1816 current->valid = FALSE;
1820 handle_stepping (GstBaseSink * sink, GstSegment * segment,
1821 GstStepInfo * current, gint64 * cstart, gint64 * cstop, gint64 * rstart,
1824 gboolean step_end = FALSE;
1826 /* see if we need to skip this buffer because of stepping */
1827 switch (current->format) {
1828 case GST_FORMAT_TIME:
1833 if (segment->rate > 0.0) {
1834 if (segment->stop == *cstop)
1835 *rstop = *rstart + current->amount;
1840 if (segment->start == *cstart)
1841 *rstart = *rstop + current->amount;
1847 end = current->start + current->amount;
1848 current->position = first - current->start;
1850 if (G_UNLIKELY (segment->abs_rate != 1.0))
1851 current->position /= segment->abs_rate;
1853 GST_DEBUG_OBJECT (sink,
1854 "buffer: %" GST_TIME_FORMAT "-%" GST_TIME_FORMAT,
1855 GST_TIME_ARGS (first), GST_TIME_ARGS (last));
1856 GST_DEBUG_OBJECT (sink,
1857 "got time step %" GST_TIME_FORMAT "-%" GST_TIME_FORMAT "/%"
1858 GST_TIME_FORMAT, GST_TIME_ARGS (current->position),
1859 GST_TIME_ARGS (last - current->start),
1860 GST_TIME_ARGS (current->amount));
1862 if ((current->flush && current->position >= current->amount)
1864 GST_DEBUG_OBJECT (sink, "step ended, we need clipping");
1866 if (segment->rate > 0.0) {
1868 *cstart = gst_segment_to_position (segment, GST_FORMAT_TIME, end);
1871 *cstop = gst_segment_to_position (segment, GST_FORMAT_TIME, end);
1874 GST_DEBUG_OBJECT (sink,
1875 "cstart %" GST_TIME_FORMAT ", rstart %" GST_TIME_FORMAT,
1876 GST_TIME_ARGS (*cstart), GST_TIME_ARGS (*rstart));
1877 GST_DEBUG_OBJECT (sink,
1878 "cstop %" GST_TIME_FORMAT ", rstop %" GST_TIME_FORMAT,
1879 GST_TIME_ARGS (*cstop), GST_TIME_ARGS (*rstop));
1882 case GST_FORMAT_BUFFERS:
1883 GST_DEBUG_OBJECT (sink,
1884 "got default step %" G_GUINT64_FORMAT "/%" G_GUINT64_FORMAT,
1885 current->position, current->amount);
1887 if (current->position < current->amount) {
1888 current->position++;
1893 case GST_FORMAT_DEFAULT:
1895 GST_DEBUG_OBJECT (sink,
1896 "got unknown step %" G_GUINT64_FORMAT "/%" G_GUINT64_FORMAT,
1897 current->position, current->amount);
1903 /* with STREAM_LOCK, PREROLL_LOCK
1905 * Returns TRUE if the object needs synchronisation and takes therefore
1906 * part in prerolling.
1908 * rsstart/rsstop contain the start/stop in stream time.
1909 * rrstart/rrstop contain the start/stop in running time.
1912 gst_base_sink_get_sync_times (GstBaseSink * basesink, GstMiniObject * obj,
1913 GstClockTime * rsstart, GstClockTime * rsstop,
1914 GstClockTime * rrstart, GstClockTime * rrstop, gboolean * do_sync,
1915 gboolean * stepped, GstSegment * segment, GstStepInfo * step,
1916 gboolean * step_end, guint8 obj_type)
1918 GstBaseSinkClass *bclass;
1920 GstClockTime start, stop; /* raw start/stop timestamps */
1921 gint64 cstart, cstop; /* clipped raw timestamps */
1922 gint64 rstart, rstop; /* clipped timestamps converted to running time */
1923 GstClockTime sstart, sstop; /* clipped timestamps converted to stream time */
1925 GstBaseSinkPrivate *priv;
1928 priv = basesink->priv;
1930 /* start with nothing */
1931 start = stop = GST_CLOCK_TIME_NONE;
1933 if (G_UNLIKELY (OBJ_IS_EVENT (obj_type))) {
1934 GstEvent *event = GST_EVENT_CAST (obj);
1936 switch (GST_EVENT_TYPE (event)) {
1937 /* EOS event needs syncing */
1940 if (basesink->segment.rate >= 0.0) {
1941 sstart = sstop = priv->current_sstop;
1942 if (!GST_CLOCK_TIME_IS_VALID (sstart)) {
1943 /* we have not seen a buffer yet, use the segment values */
1944 sstart = sstop = gst_segment_to_stream_time (&basesink->segment,
1945 basesink->segment.format, basesink->segment.stop);
1948 sstart = sstop = priv->current_sstart;
1949 if (!GST_CLOCK_TIME_IS_VALID (sstart)) {
1950 /* we have not seen a buffer yet, use the segment values */
1951 sstart = sstop = gst_segment_to_stream_time (&basesink->segment,
1952 basesink->segment.format, basesink->segment.start);
1956 rstart = rstop = priv->eos_rtime;
1957 *do_sync = rstart != -1;
1958 GST_DEBUG_OBJECT (basesink, "sync times for EOS %" GST_TIME_FORMAT,
1959 GST_TIME_ARGS (rstart));
1960 /* if we are stepping, we end now */
1961 *step_end = step->valid;
1966 /* other events do not need syncing */
1967 /* FIXME, maybe NEWSEGMENT might need synchronisation
1968 * since the POSITION query depends on accumulated times and
1969 * we cannot accumulate the current segment before the previous
1979 /* else do buffer sync code */
1980 buffer = GST_BUFFER_CAST (obj);
1982 bclass = GST_BASE_SINK_GET_CLASS (basesink);
1984 /* just get the times to see if we need syncing, if the start returns -1 we
1986 if (bclass->get_times)
1987 bclass->get_times (basesink, buffer, &start, &stop);
1989 if (!GST_CLOCK_TIME_IS_VALID (start)) {
1990 /* we don't need to sync but we still want to get the timestamps for
1991 * tracking the position */
1992 gst_base_sink_get_times (basesink, buffer, &start, &stop);
1998 GST_DEBUG_OBJECT (basesink, "got times start: %" GST_TIME_FORMAT
1999 ", stop: %" GST_TIME_FORMAT ", do_sync %d", GST_TIME_ARGS (start),
2000 GST_TIME_ARGS (stop), *do_sync);
2002 /* collect segment and format for code clarity */
2003 format = segment->format;
2005 /* no timestamp clipping if we did not get a TIME segment format */
2006 if (G_UNLIKELY (format != GST_FORMAT_TIME)) {
2009 /* do running and stream time in TIME format */
2010 format = GST_FORMAT_TIME;
2011 GST_LOG_OBJECT (basesink, "not time format, don't clip");
2015 /* clip, only when we know about time */
2016 if (G_UNLIKELY (!gst_segment_clip (segment, GST_FORMAT_TIME,
2017 (gint64) start, (gint64) stop, &cstart, &cstop))) {
2019 GST_DEBUG_OBJECT (basesink, "step out of segment");
2020 /* when we are stepping, pretend we're at the end of the segment */
2021 if (segment->rate > 0.0) {
2022 cstart = segment->stop;
2023 cstop = segment->stop;
2025 cstart = segment->start;
2026 cstop = segment->start;
2030 goto out_of_segment;
2033 if (G_UNLIKELY (start != cstart || stop != cstop)) {
2034 GST_DEBUG_OBJECT (basesink, "clipped to: start %" GST_TIME_FORMAT
2035 ", stop: %" GST_TIME_FORMAT, GST_TIME_ARGS (cstart),
2036 GST_TIME_ARGS (cstop));
2039 /* set last stop position */
2040 if (G_LIKELY (stop != GST_CLOCK_TIME_NONE && cstop != GST_CLOCK_TIME_NONE))
2041 gst_segment_set_last_stop (segment, GST_FORMAT_TIME, cstop);
2043 gst_segment_set_last_stop (segment, GST_FORMAT_TIME, cstart);
2046 rstart = gst_segment_to_running_time (segment, format, cstart);
2047 rstop = gst_segment_to_running_time (segment, format, cstop);
2049 if (G_UNLIKELY (step->valid)) {
2050 if (!(*step_end = handle_stepping (basesink, segment, step, &cstart, &cstop,
2051 &rstart, &rstop))) {
2052 /* step is still busy, we discard data when we are flushing */
2053 *stepped = step->flush;
2054 GST_DEBUG_OBJECT (basesink, "stepping busy");
2057 /* this can produce wrong values if we accumulated non-TIME segments. If this happens,
2058 * upstream is behaving very badly */
2059 sstart = gst_segment_to_stream_time (segment, format, cstart);
2060 sstop = gst_segment_to_stream_time (segment, format, cstop);
2063 /* eos_done label only called when doing EOS, we also stop stepping then */
2064 if (*step_end && step->flush) {
2065 GST_DEBUG_OBJECT (basesink, "flushing step ended");
2066 stop_stepping (basesink, segment, step, rstart, rstop, eos);
2068 /* re-determine running start times for adjusted segment
2069 * (which has a flushed amount of running/accumulated time removed) */
2070 if (!GST_IS_EVENT (obj)) {
2071 GST_DEBUG_OBJECT (basesink, "refresh sync times");
2082 /* buffers and EOS always need syncing and preroll */
2088 /* we usually clip in the chain function already but stepping could cause
2089 * the segment to be updated later. we return FALSE so that we don't try
2091 GST_LOG_OBJECT (basesink, "buffer skipped, not in segment");
2096 /* with STREAM_LOCK, PREROLL_LOCK, LOCK
2097 * adjust a timestamp with the latency and timestamp offset. This function does
2098 * not adjust for the render delay. */
2100 gst_base_sink_adjust_time (GstBaseSink * basesink, GstClockTime time)
2102 GstClockTimeDiff ts_offset;
2104 /* don't do anything funny with invalid timestamps */
2105 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (time)))
2108 time += basesink->priv->latency;
2110 /* apply offset, be carefull for underflows */
2111 ts_offset = basesink->priv->ts_offset;
2112 if (ts_offset < 0) {
2113 ts_offset = -ts_offset;
2114 if (ts_offset < time)
2121 /* subtract the render delay again, which was included in the latency */
2122 if (time > basesink->priv->render_delay)
2123 time -= basesink->priv->render_delay;
2131 * gst_base_sink_wait_clock:
2133 * @time: the running_time to be reached
2134 * @jitter: (out) (allow-none): the jitter to be filled with time diff, or NULL
2136 * This function will block until @time is reached. It is usually called by
2137 * subclasses that use their own internal synchronisation.
2139 * If @time is not valid, no sycnhronisation is done and #GST_CLOCK_BADTIME is
2140 * returned. Likewise, if synchronisation is disabled in the element or there
2141 * is no clock, no synchronisation is done and #GST_CLOCK_BADTIME is returned.
2143 * This function should only be called with the PREROLL_LOCK held, like when
2144 * receiving an EOS event in the #GstBaseSinkClass.event() vmethod or when
2145 * receiving a buffer in
2146 * the #GstBaseSinkClass.render() vmethod.
2148 * The @time argument should be the running_time of when this method should
2149 * return and is not adjusted with any latency or offset configured in the
2154 * Returns: #GstClockReturn
2157 gst_base_sink_wait_clock (GstBaseSink * sink, GstClockTime time,
2158 GstClockTimeDiff * jitter)
2162 GstClockTime base_time;
2164 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (time)))
2167 GST_OBJECT_LOCK (sink);
2168 if (G_UNLIKELY (!sink->sync))
2171 if (G_UNLIKELY ((clock = GST_ELEMENT_CLOCK (sink)) == NULL))
2174 base_time = GST_ELEMENT_CAST (sink)->base_time;
2175 GST_LOG_OBJECT (sink,
2176 "time %" GST_TIME_FORMAT ", base_time %" GST_TIME_FORMAT,
2177 GST_TIME_ARGS (time), GST_TIME_ARGS (base_time));
2179 /* add base_time to running_time to get the time against the clock */
2182 /* Re-use existing clockid if available */
2183 if (G_LIKELY (sink->priv->cached_clock_id != NULL)) {
2184 if (!gst_clock_single_shot_id_reinit (clock, sink->priv->cached_clock_id,
2186 gst_clock_id_unref (sink->priv->cached_clock_id);
2187 sink->priv->cached_clock_id = gst_clock_new_single_shot_id (clock, time);
2190 sink->priv->cached_clock_id = gst_clock_new_single_shot_id (clock, time);
2191 GST_OBJECT_UNLOCK (sink);
2193 /* A blocking wait is performed on the clock. We save the ClockID
2194 * so we can unlock the entry at any time. While we are blocking, we
2195 * release the PREROLL_LOCK so that other threads can interrupt the
2197 sink->clock_id = sink->priv->cached_clock_id;
2198 /* release the preroll lock while waiting */
2199 GST_PAD_PREROLL_UNLOCK (sink->sinkpad);
2201 ret = gst_clock_id_wait (sink->priv->cached_clock_id, jitter);
2203 GST_PAD_PREROLL_LOCK (sink->sinkpad);
2204 sink->clock_id = NULL;
2208 /* no syncing needed */
2211 GST_DEBUG_OBJECT (sink, "time not valid, no sync needed");
2212 return GST_CLOCK_BADTIME;
2216 GST_DEBUG_OBJECT (sink, "sync disabled");
2217 GST_OBJECT_UNLOCK (sink);
2218 return GST_CLOCK_BADTIME;
2222 GST_DEBUG_OBJECT (sink, "no clock, can't sync");
2223 GST_OBJECT_UNLOCK (sink);
2224 return GST_CLOCK_BADTIME;
2229 * gst_base_sink_wait_preroll:
2232 * If the #GstBaseSinkClass.render() method performs its own synchronisation
2233 * against the clock it must unblock when going from PLAYING to the PAUSED state
2234 * and call this method before continuing to render the remaining data.
2236 * This function will block until a state change to PLAYING happens (in which
2237 * case this function returns #GST_FLOW_OK) or the processing must be stopped due
2238 * to a state change to READY or a FLUSH event (in which case this function
2239 * returns #GST_FLOW_WRONG_STATE).
2241 * This function should only be called with the PREROLL_LOCK held, like in the
2244 * Returns: #GST_FLOW_OK if the preroll completed and processing can
2245 * continue. Any other return value should be returned from the render vmethod.
2250 gst_base_sink_wait_preroll (GstBaseSink * sink)
2252 sink->have_preroll = TRUE;
2253 GST_DEBUG_OBJECT (sink, "waiting in preroll for flush or PLAYING");
2254 /* block until the state changes, or we get a flush, or something */
2255 GST_PAD_PREROLL_WAIT (sink->sinkpad);
2256 sink->have_preroll = FALSE;
2257 if (G_UNLIKELY (sink->flushing))
2259 if (G_UNLIKELY (sink->priv->step_unlock))
2261 GST_DEBUG_OBJECT (sink, "continue after preroll");
2268 GST_DEBUG_OBJECT (sink, "preroll interrupted because of flush");
2269 return GST_FLOW_WRONG_STATE;
2273 sink->priv->step_unlock = FALSE;
2274 GST_DEBUG_OBJECT (sink, "preroll interrupted because of step");
2275 return GST_FLOW_STEP;
2279 static inline guint8
2280 get_object_type (GstMiniObject * obj)
2284 if (G_LIKELY (GST_IS_BUFFER (obj)))
2285 obj_type = _PR_IS_BUFFER;
2286 else if (GST_IS_EVENT (obj))
2287 obj_type = _PR_IS_EVENT;
2288 else if (GST_IS_BUFFER_LIST (obj))
2289 obj_type = _PR_IS_BUFFERLIST;
2291 obj_type = _PR_IS_NOTHING;
2297 * gst_base_sink_do_preroll:
2299 * @obj: (transfer none): the mini object that caused the preroll
2301 * If the @sink spawns its own thread for pulling buffers from upstream it
2302 * should call this method after it has pulled a buffer. If the element needed
2303 * to preroll, this function will perform the preroll and will then block
2304 * until the element state is changed.
2306 * This function should be called with the PREROLL_LOCK held.
2308 * Returns: #GST_FLOW_OK if the preroll completed and processing can
2309 * continue. Any other return value should be returned from the render vmethod.
2314 gst_base_sink_do_preroll (GstBaseSink * sink, GstMiniObject * obj)
2318 while (G_UNLIKELY (sink->need_preroll)) {
2320 GST_DEBUG_OBJECT (sink, "prerolling object %p", obj);
2322 obj_type = get_object_type (obj);
2324 ret = gst_base_sink_preroll_object (sink, obj_type, obj);
2325 if (ret != GST_FLOW_OK)
2326 goto preroll_failed;
2328 /* need to recheck here because the commit state could have
2329 * made us not need the preroll anymore */
2330 if (G_LIKELY (sink->need_preroll)) {
2331 /* block until the state changes, or we get a flush, or something */
2332 ret = gst_base_sink_wait_preroll (sink);
2333 if ((ret != GST_FLOW_OK) && (ret != GST_FLOW_STEP))
2334 goto preroll_failed;
2342 GST_DEBUG_OBJECT (sink, "preroll failed %d", ret);
2348 * gst_base_sink_wait_eos:
2350 * @time: the running_time to be reached
2351 * @jitter: (out) (allow-none): the jitter to be filled with time diff, or NULL
2353 * This function will block until @time is reached. It is usually called by
2354 * subclasses that use their own internal synchronisation but want to let the
2355 * EOS be handled by the base class.
2357 * This function should only be called with the PREROLL_LOCK held, like when
2358 * receiving an EOS event in the ::event vmethod.
2360 * The @time argument should be the running_time of when the EOS should happen
2361 * and will be adjusted with any latency and offset configured in the sink.
2363 * Returns: #GstFlowReturn
2368 gst_base_sink_wait_eos (GstBaseSink * sink, GstClockTime time,
2369 GstClockTimeDiff * jitter)
2371 GstClockReturn status;
2377 GST_DEBUG_OBJECT (sink, "checking preroll");
2379 /* first wait for the playing state before we can continue */
2380 while (G_UNLIKELY (sink->need_preroll)) {
2381 ret = gst_base_sink_wait_preroll (sink);
2382 if ((ret != GST_FLOW_OK) && (ret != GST_FLOW_STEP))
2386 /* preroll done, we can sync since we are in PLAYING now. */
2387 GST_DEBUG_OBJECT (sink, "possibly waiting for clock to reach %"
2388 GST_TIME_FORMAT, GST_TIME_ARGS (time));
2390 /* compensate for latency and ts_offset. We don't adjust for render delay
2391 * because we don't interact with the device on EOS normally. */
2392 stime = gst_base_sink_adjust_time (sink, time);
2394 /* wait for the clock, this can be interrupted because we got shut down or
2396 status = gst_base_sink_wait_clock (sink, stime, jitter);
2398 GST_DEBUG_OBJECT (sink, "clock returned %d", status);
2400 /* invalid time, no clock or sync disabled, just continue then */
2401 if (status == GST_CLOCK_BADTIME)
2404 /* waiting could have been interrupted and we can be flushing now */
2405 if (G_UNLIKELY (sink->flushing))
2408 /* retry if we got unscheduled, which means we did not reach the timeout
2409 * yet. if some other error occures, we continue. */
2410 } while (status == GST_CLOCK_UNSCHEDULED);
2412 GST_DEBUG_OBJECT (sink, "end of stream");
2419 GST_DEBUG_OBJECT (sink, "we are flushing");
2420 return GST_FLOW_WRONG_STATE;
2424 /* with STREAM_LOCK, PREROLL_LOCK
2426 * Make sure we are in PLAYING and synchronize an object to the clock.
2428 * If we need preroll, we are not in PLAYING. We try to commit the state
2429 * if needed and then block if we still are not PLAYING.
2431 * We start waiting on the clock in PLAYING. If we got interrupted, we
2432 * immediatly try to re-preroll.
2434 * Some objects do not need synchronisation (most events) and so this function
2435 * immediatly returns GST_FLOW_OK.
2437 * for objects that arrive later than max-lateness to be synchronized to the
2438 * clock have the @late boolean set to TRUE.
2440 * This function keeps a running average of the jitter (the diff between the
2441 * clock time and the requested sync time). The jitter is negative for
2442 * objects that arrive in time and positive for late buffers.
2444 * does not take ownership of obj.
2446 static GstFlowReturn
2447 gst_base_sink_do_sync (GstBaseSink * basesink, GstPad * pad,
2448 GstMiniObject * obj, gboolean * late, gboolean * step_end, guint8 obj_type)
2450 GstClockTimeDiff jitter = 0;
2452 GstClockReturn status = GST_CLOCK_OK;
2453 GstClockTime rstart, rstop, sstart, sstop, stime;
2455 GstBaseSinkPrivate *priv;
2457 GstStepInfo *current, *pending;
2460 priv = basesink->priv;
2463 sstart = sstop = rstart = rstop = GST_CLOCK_TIME_NONE;
2467 priv->current_rstart = GST_CLOCK_TIME_NONE;
2469 /* get stepping info */
2470 current = &priv->current_step;
2471 pending = &priv->pending_step;
2473 /* get timing information for this object against the render segment */
2474 syncable = gst_base_sink_get_sync_times (basesink, obj,
2475 &sstart, &sstop, &rstart, &rstop, &do_sync, &stepped, &basesink->segment,
2476 current, step_end, obj_type);
2478 if (G_UNLIKELY (stepped))
2481 /* a syncable object needs to participate in preroll and
2482 * clocking. All buffers and EOS are syncable. */
2483 if (G_UNLIKELY (!syncable))
2486 /* store timing info for current object */
2487 priv->current_rstart = rstart;
2488 priv->current_rstop = (GST_CLOCK_TIME_IS_VALID (rstop) ? rstop : rstart);
2490 /* save sync time for eos when the previous object needed sync */
2491 priv->eos_rtime = (do_sync ? priv->current_rstop : GST_CLOCK_TIME_NONE);
2494 /* first do preroll, this makes sure we commit our state
2495 * to PAUSED and can continue to PLAYING. We cannot perform
2496 * any clock sync in PAUSED because there is no clock. */
2497 ret = gst_base_sink_do_preroll (basesink, obj);
2498 if (G_UNLIKELY (ret != GST_FLOW_OK))
2499 goto preroll_failed;
2501 /* update the segment with a pending step if the current one is invalid and we
2502 * have a new pending one. We only accept new step updates after a preroll */
2503 if (G_UNLIKELY (pending->valid && !current->valid)) {
2504 start_stepping (basesink, &basesink->segment, pending, current);
2508 /* After rendering we store the position of the last buffer so that we can use
2509 * it to report the position. We need to take the lock here. */
2510 GST_OBJECT_LOCK (basesink);
2511 priv->current_sstart = sstart;
2512 priv->current_sstop = (GST_CLOCK_TIME_IS_VALID (sstop) ? sstop : sstart);
2513 GST_OBJECT_UNLOCK (basesink);
2518 /* adjust for latency */
2519 stime = gst_base_sink_adjust_time (basesink, rstart);
2521 /* adjust for render-delay, avoid underflows */
2522 if (GST_CLOCK_TIME_IS_VALID (stime)) {
2523 if (stime > priv->render_delay)
2524 stime -= priv->render_delay;
2529 /* preroll done, we can sync since we are in PLAYING now. */
2530 GST_DEBUG_OBJECT (basesink, "possibly waiting for clock to reach %"
2531 GST_TIME_FORMAT ", adjusted %" GST_TIME_FORMAT,
2532 GST_TIME_ARGS (rstart), GST_TIME_ARGS (stime));
2534 /* This function will return immediatly if start == -1, no clock
2535 * or sync is disabled with GST_CLOCK_BADTIME. */
2536 status = gst_base_sink_wait_clock (basesink, stime, &jitter);
2538 GST_DEBUG_OBJECT (basesink, "clock returned %d, jitter %c%" GST_TIME_FORMAT,
2539 status, (jitter < 0 ? '-' : ' '), GST_TIME_ARGS (ABS (jitter)));
2541 /* invalid time, no clock or sync disabled, just render */
2542 if (status == GST_CLOCK_BADTIME)
2545 /* waiting could have been interrupted and we can be flushing now */
2546 if (G_UNLIKELY (basesink->flushing))
2549 /* check for unlocked by a state change, we are not flushing so
2550 * we can try to preroll on the current buffer. */
2551 if (G_UNLIKELY (status == GST_CLOCK_UNSCHEDULED)) {
2552 GST_DEBUG_OBJECT (basesink, "unscheduled, waiting some more");
2553 priv->call_preroll = TRUE;
2557 /* successful syncing done, record observation */
2558 priv->current_jitter = jitter;
2560 /* check if the object should be dropped */
2561 *late = gst_base_sink_is_too_late (basesink, obj, rstart, rstop,
2570 GST_DEBUG_OBJECT (basesink, "skipped stepped object %p", obj);
2576 GST_DEBUG_OBJECT (basesink, "non syncable object %p", obj);
2581 GST_DEBUG_OBJECT (basesink, "we are flushing");
2582 return GST_FLOW_WRONG_STATE;
2586 GST_DEBUG_OBJECT (basesink, "preroll failed");
2593 gst_base_sink_send_qos (GstBaseSink * basesink,
2594 gdouble proportion, GstClockTime time, GstClockTimeDiff diff)
2599 /* generate Quality-of-Service event */
2600 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, basesink,
2601 "qos: proportion: %lf, diff %" G_GINT64_FORMAT ", timestamp %"
2602 GST_TIME_FORMAT, proportion, diff, GST_TIME_ARGS (time));
2604 event = gst_event_new_qos (proportion, diff, time);
2607 res = gst_pad_push_event (basesink->sinkpad, event);
2613 gst_base_sink_perform_qos (GstBaseSink * sink, gboolean dropped)
2615 GstBaseSinkPrivate *priv;
2616 GstClockTime start, stop;
2617 GstClockTimeDiff jitter;
2618 GstClockTime pt, entered, left;
2619 GstClockTime duration;
2624 start = priv->current_rstart;
2626 if (priv->current_step.valid)
2629 /* if Quality-of-Service disabled, do nothing */
2630 if (!g_atomic_int_get (&priv->qos_enabled) ||
2631 !GST_CLOCK_TIME_IS_VALID (start))
2634 stop = priv->current_rstop;
2635 jitter = priv->current_jitter;
2638 /* this is the time the buffer entered the sink */
2639 if (start < -jitter)
2642 entered = start + jitter;
2645 /* this is the time the buffer entered the sink */
2646 entered = start + jitter;
2647 /* this is the time the buffer left the sink */
2648 left = start + jitter;
2651 /* calculate duration of the buffer */
2652 if (GST_CLOCK_TIME_IS_VALID (stop))
2653 duration = stop - start;
2655 duration = GST_CLOCK_TIME_NONE;
2657 /* if we have the time when the last buffer left us, calculate
2658 * processing time */
2659 if (GST_CLOCK_TIME_IS_VALID (priv->last_left)) {
2660 if (entered > priv->last_left) {
2661 pt = entered - priv->last_left;
2669 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, sink, "start: %" GST_TIME_FORMAT
2670 ", entered %" GST_TIME_FORMAT ", left %" GST_TIME_FORMAT ", pt: %"
2671 GST_TIME_FORMAT ", duration %" GST_TIME_FORMAT ",jitter %"
2672 G_GINT64_FORMAT, GST_TIME_ARGS (start), GST_TIME_ARGS (entered),
2673 GST_TIME_ARGS (left), GST_TIME_ARGS (pt), GST_TIME_ARGS (duration),
2676 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, sink, "avg_duration: %" GST_TIME_FORMAT
2677 ", avg_pt: %" GST_TIME_FORMAT ", avg_rate: %g",
2678 GST_TIME_ARGS (priv->avg_duration), GST_TIME_ARGS (priv->avg_pt),
2681 /* collect running averages. for first observations, we copy the
2683 if (!GST_CLOCK_TIME_IS_VALID (priv->avg_duration))
2684 priv->avg_duration = duration;
2686 priv->avg_duration = UPDATE_RUNNING_AVG (priv->avg_duration, duration);
2688 if (!GST_CLOCK_TIME_IS_VALID (priv->avg_pt))
2691 priv->avg_pt = UPDATE_RUNNING_AVG (priv->avg_pt, pt);
2693 if (priv->avg_duration != 0)
2695 gst_guint64_to_gdouble (priv->avg_pt) /
2696 gst_guint64_to_gdouble (priv->avg_duration);
2700 if (GST_CLOCK_TIME_IS_VALID (priv->last_left)) {
2701 if (dropped || priv->avg_rate < 0.0) {
2702 priv->avg_rate = rate;
2705 priv->avg_rate = UPDATE_RUNNING_AVG_N (priv->avg_rate, rate);
2707 priv->avg_rate = UPDATE_RUNNING_AVG_P (priv->avg_rate, rate);
2711 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, sink,
2712 "updated: avg_duration: %" GST_TIME_FORMAT ", avg_pt: %" GST_TIME_FORMAT
2713 ", avg_rate: %g", GST_TIME_ARGS (priv->avg_duration),
2714 GST_TIME_ARGS (priv->avg_pt), priv->avg_rate);
2717 if (priv->avg_rate >= 0.0) {
2718 /* if we have a valid rate, start sending QoS messages */
2719 if (priv->current_jitter < 0) {
2720 /* make sure we never go below 0 when adding the jitter to the
2722 if (priv->current_rstart < -priv->current_jitter)
2723 priv->current_jitter = -priv->current_rstart;
2725 gst_base_sink_send_qos (sink, priv->avg_rate, priv->current_rstart,
2726 priv->current_jitter);
2729 /* record when this buffer will leave us */
2730 priv->last_left = left;
2733 /* reset all qos measuring */
2735 gst_base_sink_reset_qos (GstBaseSink * sink)
2737 GstBaseSinkPrivate *priv;
2741 priv->last_in_time = GST_CLOCK_TIME_NONE;
2742 priv->last_left = GST_CLOCK_TIME_NONE;
2743 priv->avg_duration = GST_CLOCK_TIME_NONE;
2744 priv->avg_pt = GST_CLOCK_TIME_NONE;
2745 priv->avg_rate = -1.0;
2746 priv->avg_render = GST_CLOCK_TIME_NONE;
2752 /* Checks if the object was scheduled too late.
2754 * start/stop contain the raw timestamp start and stop values
2757 * status and jitter contain the return values from the clock wait.
2759 * returns TRUE if the buffer was too late.
2762 gst_base_sink_is_too_late (GstBaseSink * basesink, GstMiniObject * obj,
2763 GstClockTime start, GstClockTime stop,
2764 GstClockReturn status, GstClockTimeDiff jitter)
2767 gint64 max_lateness;
2768 GstBaseSinkPrivate *priv;
2770 priv = basesink->priv;
2774 /* only for objects that were too late */
2775 if (G_LIKELY (status != GST_CLOCK_EARLY))
2778 max_lateness = basesink->abidata.ABI.max_lateness;
2780 /* check if frame dropping is enabled */
2781 if (max_lateness == -1)
2784 /* only check for buffers */
2785 if (G_UNLIKELY (!GST_IS_BUFFER (obj)))
2788 /* can't do check if we don't have a timestamp */
2789 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (start)))
2792 /* we can add a valid stop time */
2793 if (GST_CLOCK_TIME_IS_VALID (stop))
2794 max_lateness += stop;
2796 max_lateness += start;
2798 /* if the jitter bigger than duration and lateness we are too late */
2799 if ((late = start + jitter > max_lateness)) {
2800 GST_CAT_DEBUG_OBJECT (GST_CAT_PERFORMANCE, basesink,
2801 "buffer is too late %" GST_TIME_FORMAT
2802 " > %" GST_TIME_FORMAT, GST_TIME_ARGS (start + jitter),
2803 GST_TIME_ARGS (max_lateness));
2804 /* !!emergency!!, if we did not receive anything valid for more than a
2805 * second, render it anyway so the user sees something */
2806 if (GST_CLOCK_TIME_IS_VALID (priv->last_in_time) &&
2807 start - priv->last_in_time > GST_SECOND) {
2809 GST_ELEMENT_WARNING (basesink, CORE, CLOCK,
2810 (_("A lot of buffers are being dropped.")),
2811 ("There may be a timestamping problem, or this computer is too slow."));
2812 GST_CAT_DEBUG_OBJECT (GST_CAT_PERFORMANCE, basesink,
2813 "**emergency** last buffer at %" GST_TIME_FORMAT " > GST_SECOND",
2814 GST_TIME_ARGS (priv->last_in_time));
2819 if (!late || !GST_CLOCK_TIME_IS_VALID (priv->last_in_time)) {
2820 priv->last_in_time = start;
2827 GST_DEBUG_OBJECT (basesink, "object was scheduled in time");
2832 GST_DEBUG_OBJECT (basesink, "frame dropping disabled");
2837 GST_DEBUG_OBJECT (basesink, "object is not a buffer");
2842 GST_DEBUG_OBJECT (basesink, "buffer has no timestamp");
2847 /* called before and after calling the render vmethod. It keeps track of how
2848 * much time was spent in the render method and is used to check if we are
2851 gst_base_sink_do_render_stats (GstBaseSink * basesink, gboolean start)
2853 GstBaseSinkPrivate *priv;
2855 priv = basesink->priv;
2858 priv->start = gst_util_get_timestamp ();
2860 GstClockTime elapsed;
2862 priv->stop = gst_util_get_timestamp ();
2864 elapsed = GST_CLOCK_DIFF (priv->start, priv->stop);
2866 if (!GST_CLOCK_TIME_IS_VALID (priv->avg_render))
2867 priv->avg_render = elapsed;
2869 priv->avg_render = UPDATE_RUNNING_AVG (priv->avg_render, elapsed);
2871 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, basesink,
2872 "avg_render: %" GST_TIME_FORMAT, GST_TIME_ARGS (priv->avg_render));
2876 /* with STREAM_LOCK, PREROLL_LOCK,
2878 * Synchronize the object on the clock and then render it.
2880 * takes ownership of obj.
2882 static GstFlowReturn
2883 gst_base_sink_render_object (GstBaseSink * basesink, GstPad * pad,
2884 guint8 obj_type, gpointer obj)
2887 GstBaseSinkClass *bclass;
2888 gboolean late, step_end;
2890 GstBaseSinkPrivate *priv;
2892 priv = basesink->priv;
2894 if (OBJ_IS_BUFFERLIST (obj_type)) {
2896 * If buffer list, use the first group buffer within the list
2899 sync_obj = gst_buffer_list_get (GST_BUFFER_LIST_CAST (obj), 0, 0);
2900 g_assert (NULL != sync_obj);
2909 /* synchronize this object, non syncable objects return OK
2912 gst_base_sink_do_sync (basesink, pad, sync_obj, &late, &step_end,
2914 if (G_UNLIKELY (ret != GST_FLOW_OK))
2917 /* and now render, event or buffer/buffer list. */
2918 if (G_LIKELY (OBJ_IS_BUFFERFULL (obj_type))) {
2919 /* drop late buffers unconditionally, let's hope it's unlikely */
2920 if (G_UNLIKELY (late))
2923 bclass = GST_BASE_SINK_GET_CLASS (basesink);
2925 if (G_LIKELY ((OBJ_IS_BUFFERLIST (obj_type) && bclass->render_list) ||
2926 (!OBJ_IS_BUFFERLIST (obj_type) && bclass->render))) {
2929 /* read once, to get same value before and after */
2930 do_qos = g_atomic_int_get (&priv->qos_enabled);
2932 GST_DEBUG_OBJECT (basesink, "rendering object %p", obj);
2934 /* record rendering time for QoS and stats */
2936 gst_base_sink_do_render_stats (basesink, TRUE);
2938 if (!OBJ_IS_BUFFERLIST (obj_type)) {
2941 /* For buffer lists do not set last buffer. Creating buffer
2942 * with meaningful data can be done only with memcpy which will
2943 * significantly affect performance */
2944 buf = GST_BUFFER_CAST (obj);
2945 gst_base_sink_set_last_buffer (basesink, buf);
2947 ret = bclass->render (basesink, buf);
2949 GstBufferList *buflist;
2951 buflist = GST_BUFFER_LIST_CAST (obj);
2953 ret = bclass->render_list (basesink, buflist);
2957 gst_base_sink_do_render_stats (basesink, FALSE);
2959 if (ret == GST_FLOW_STEP)
2962 if (G_UNLIKELY (basesink->flushing))
2967 } else if (G_LIKELY (OBJ_IS_EVENT (obj_type))) {
2968 GstEvent *event = GST_EVENT_CAST (obj);
2969 gboolean event_res = TRUE;
2972 bclass = GST_BASE_SINK_GET_CLASS (basesink);
2974 type = GST_EVENT_TYPE (event);
2976 GST_DEBUG_OBJECT (basesink, "rendering event %p, type %s", obj,
2977 gst_event_type_get_name (type));
2980 event_res = bclass->event (basesink, event);
2982 /* when we get here we could be flushing again when the event handler calls
2983 * _wait_eos(). We have to ignore this object in that case. */
2984 if (G_UNLIKELY (basesink->flushing))
2987 if (G_LIKELY (event_res)) {
2990 seqnum = basesink->priv->seqnum = gst_event_get_seqnum (event);
2991 GST_DEBUG_OBJECT (basesink, "Got seqnum #%" G_GUINT32_FORMAT, seqnum);
2996 GstMessage *message;
2998 /* the EOS event is completely handled so we mark
2999 * ourselves as being in the EOS state. eos is also
3000 * protected by the object lock so we can read it when
3001 * answering the POSITION query. */
3002 GST_OBJECT_LOCK (basesink);
3003 basesink->eos = TRUE;
3004 GST_OBJECT_UNLOCK (basesink);
3006 /* ok, now we can post the message */
3007 GST_DEBUG_OBJECT (basesink, "Now posting EOS");
3009 message = gst_message_new_eos (GST_OBJECT_CAST (basesink));
3010 gst_message_set_seqnum (message, seqnum);
3011 gst_element_post_message (GST_ELEMENT_CAST (basesink), message);
3014 case GST_EVENT_NEWSEGMENT:
3015 /* configure the segment */
3016 gst_base_sink_configure_segment (basesink, pad, event,
3017 &basesink->segment);
3019 case GST_EVENT_SINK_MESSAGE:{
3020 GstMessage *msg = NULL;
3022 gst_event_parse_sink_message (event, &msg);
3025 gst_element_post_message (GST_ELEMENT_CAST (basesink), msg);
3032 g_return_val_if_reached (GST_FLOW_ERROR);
3037 /* the step ended, check if we need to activate a new step */
3038 GST_DEBUG_OBJECT (basesink, "step ended");
3039 stop_stepping (basesink, &basesink->segment, &priv->current_step,
3040 priv->current_rstart, priv->current_rstop, basesink->eos);
3044 gst_base_sink_perform_qos (basesink, late);
3046 GST_DEBUG_OBJECT (basesink, "object unref after render %p", obj);
3047 gst_mini_object_unref (GST_MINI_OBJECT_CAST (obj));
3053 GST_DEBUG_OBJECT (basesink, "do_sync returned %s", gst_flow_get_name (ret));
3059 GST_DEBUG_OBJECT (basesink, "buffer late, dropping");
3061 if (g_atomic_int_get (&priv->qos_enabled)) {
3062 GstMessage *qos_msg;
3063 GstClockTime timestamp, duration;
3065 timestamp = GST_BUFFER_TIMESTAMP (GST_BUFFER_CAST (sync_obj));
3066 duration = GST_BUFFER_DURATION (GST_BUFFER_CAST (sync_obj));
3068 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, basesink,
3069 "qos: dropped buffer rt %" GST_TIME_FORMAT ", st %" GST_TIME_FORMAT
3070 ", ts %" GST_TIME_FORMAT ", dur %" GST_TIME_FORMAT,
3071 GST_TIME_ARGS (priv->current_rstart),
3072 GST_TIME_ARGS (priv->current_sstart), GST_TIME_ARGS (timestamp),
3073 GST_TIME_ARGS (duration));
3074 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, basesink,
3075 "qos: rendered %" G_GUINT64_FORMAT ", dropped %" G_GUINT64_FORMAT,
3076 priv->rendered, priv->dropped);
3079 gst_message_new_qos (GST_OBJECT_CAST (basesink), basesink->sync,
3080 priv->current_rstart, priv->current_sstart, timestamp, duration);
3081 gst_message_set_qos_values (qos_msg, priv->current_jitter, priv->avg_rate,
3083 gst_message_set_qos_stats (qos_msg, GST_FORMAT_BUFFERS, priv->rendered,
3085 gst_element_post_message (GST_ELEMENT_CAST (basesink), qos_msg);
3091 GST_DEBUG_OBJECT (basesink, "we are flushing, ignore object");
3092 gst_mini_object_unref (obj);
3093 return GST_FLOW_WRONG_STATE;
3097 /* with STREAM_LOCK, PREROLL_LOCK
3099 * Perform preroll on the given object. For buffers this means
3100 * calling the preroll subclass method.
3101 * If that succeeds, the state will be commited.
3103 * function does not take ownership of obj.
3105 static GstFlowReturn
3106 gst_base_sink_preroll_object (GstBaseSink * basesink, guint8 obj_type,
3107 GstMiniObject * obj)
3111 GST_DEBUG_OBJECT (basesink, "prerolling object %p", obj);
3113 /* if it's a buffer, we need to call the preroll method */
3114 if (G_LIKELY (OBJ_IS_BUFFERFULL (obj_type) && basesink->priv->call_preroll)) {
3115 GstBaseSinkClass *bclass;
3117 GstClockTime timestamp;
3119 if (OBJ_IS_BUFFERLIST (obj_type)) {
3120 buf = gst_buffer_list_get (GST_BUFFER_LIST_CAST (obj), 0, 0);
3121 g_assert (NULL != buf);
3123 buf = GST_BUFFER_CAST (obj);
3126 timestamp = GST_BUFFER_TIMESTAMP (buf);
3128 GST_DEBUG_OBJECT (basesink, "preroll buffer %" GST_TIME_FORMAT,
3129 GST_TIME_ARGS (timestamp));
3132 * For buffer lists do not set last buffer. Creating buffer
3133 * with meaningful data can be done only with memcpy which will
3134 * significantly affect performance
3136 if (!OBJ_IS_BUFFERLIST (obj_type)) {
3137 gst_base_sink_set_last_buffer (basesink, buf);
3140 bclass = GST_BASE_SINK_GET_CLASS (basesink);
3141 if (bclass->preroll)
3142 if ((ret = bclass->preroll (basesink, buf)) != GST_FLOW_OK)
3143 goto preroll_failed;
3145 basesink->priv->call_preroll = FALSE;
3149 if (G_LIKELY (basesink->playing_async)) {
3150 if (G_UNLIKELY (!gst_base_sink_commit_state (basesink)))
3159 GST_DEBUG_OBJECT (basesink, "preroll failed, abort state");
3160 gst_element_abort_state (GST_ELEMENT_CAST (basesink));
3165 GST_DEBUG_OBJECT (basesink, "stopping while commiting state");
3166 return GST_FLOW_WRONG_STATE;
3170 /* with STREAM_LOCK, PREROLL_LOCK
3172 * Queue an object for rendering.
3173 * The first prerollable object queued will complete the preroll. If the
3174 * preroll queue if filled, we render all the objects in the queue.
3176 * This function takes ownership of the object.
3178 static GstFlowReturn
3179 gst_base_sink_queue_object_unlocked (GstBaseSink * basesink, GstPad * pad,
3180 guint8 obj_type, gpointer obj, gboolean prerollable)
3182 GstFlowReturn ret = GST_FLOW_OK;
3186 if (G_UNLIKELY (basesink->need_preroll)) {
3187 if (G_LIKELY (prerollable))
3188 basesink->preroll_queued++;
3190 length = basesink->preroll_queued;
3192 GST_DEBUG_OBJECT (basesink, "now %d prerolled items", length);
3194 /* first prerollable item needs to finish the preroll */
3196 ret = gst_base_sink_preroll_object (basesink, obj_type, obj);
3197 if (G_UNLIKELY (ret != GST_FLOW_OK))
3198 goto preroll_failed;
3200 /* need to recheck if we need preroll, commmit state during preroll
3201 * could have made us not need more preroll. */
3202 if (G_UNLIKELY (basesink->need_preroll)) {
3203 /* see if we can render now, if we can't add the object to the preroll
3205 if (G_UNLIKELY (length <= basesink->preroll_queue_max_len))
3209 /* we can start rendering (or blocking) the queued object
3211 q = basesink->preroll_queue;
3212 while (G_UNLIKELY (!g_queue_is_empty (q))) {
3216 o = g_queue_pop_head (q);
3217 GST_DEBUG_OBJECT (basesink, "rendering queued object %p", o);
3219 ot = get_object_type (o);
3221 /* do something with the return value */
3222 ret = gst_base_sink_render_object (basesink, pad, ot, o);
3223 if (ret != GST_FLOW_OK)
3224 goto dequeue_failed;
3227 /* now render the object */
3228 ret = gst_base_sink_render_object (basesink, pad, obj_type, obj);
3229 basesink->preroll_queued = 0;
3236 GST_DEBUG_OBJECT (basesink, "preroll failed, reason %s",
3237 gst_flow_get_name (ret));
3238 gst_mini_object_unref (GST_MINI_OBJECT_CAST (obj));
3243 /* add object to the queue and return */
3244 GST_DEBUG_OBJECT (basesink, "need more preroll data %d <= %d",
3245 length, basesink->preroll_queue_max_len);
3246 g_queue_push_tail (basesink->preroll_queue, obj);
3251 GST_DEBUG_OBJECT (basesink, "rendering queued objects failed, reason %s",
3252 gst_flow_get_name (ret));
3253 gst_mini_object_unref (GST_MINI_OBJECT_CAST (obj));
3260 * This function grabs the PREROLL_LOCK and adds the object to
3263 * This function takes ownership of obj.
3265 * Note: Only GstEvent seem to be passed to this private method
3267 static GstFlowReturn
3268 gst_base_sink_queue_object (GstBaseSink * basesink, GstPad * pad,
3269 GstMiniObject * obj, gboolean prerollable)
3273 GST_PAD_PREROLL_LOCK (pad);
3274 if (G_UNLIKELY (basesink->flushing))
3277 if (G_UNLIKELY (basesink->priv->received_eos))
3281 gst_base_sink_queue_object_unlocked (basesink, pad, _PR_IS_EVENT, obj,
3283 GST_PAD_PREROLL_UNLOCK (pad);
3290 GST_DEBUG_OBJECT (basesink, "sink is flushing");
3291 GST_PAD_PREROLL_UNLOCK (pad);
3292 gst_mini_object_unref (obj);
3293 return GST_FLOW_WRONG_STATE;
3297 GST_DEBUG_OBJECT (basesink,
3298 "we are EOS, dropping object, return UNEXPECTED");
3299 GST_PAD_PREROLL_UNLOCK (pad);
3300 gst_mini_object_unref (obj);
3301 return GST_FLOW_UNEXPECTED;
3306 gst_base_sink_flush_start (GstBaseSink * basesink, GstPad * pad)
3308 /* make sure we are not blocked on the clock also clear any pending
3310 gst_base_sink_set_flushing (basesink, pad, TRUE);
3312 /* we grab the stream lock but that is not needed since setting the
3313 * sink to flushing would make sure no state commit is being done
3315 GST_PAD_STREAM_LOCK (pad);
3316 gst_base_sink_reset_qos (basesink);
3317 /* and we need to commit our state again on the next
3318 * prerolled buffer */
3319 basesink->playing_async = TRUE;
3320 if (basesink->priv->async_enabled) {
3321 gst_element_lost_state (GST_ELEMENT_CAST (basesink));
3323 basesink->priv->have_latency = TRUE;
3325 gst_base_sink_set_last_buffer (basesink, NULL);
3326 GST_PAD_STREAM_UNLOCK (pad);
3330 gst_base_sink_flush_stop (GstBaseSink * basesink, GstPad * pad)
3332 /* unset flushing so we can accept new data, this also flushes out any EOS
3334 gst_base_sink_set_flushing (basesink, pad, FALSE);
3336 /* for position reporting */
3337 GST_OBJECT_LOCK (basesink);
3338 basesink->priv->current_sstart = GST_CLOCK_TIME_NONE;
3339 basesink->priv->current_sstop = GST_CLOCK_TIME_NONE;
3340 basesink->priv->eos_rtime = GST_CLOCK_TIME_NONE;
3341 basesink->priv->call_preroll = TRUE;
3342 basesink->priv->current_step.valid = FALSE;
3343 basesink->priv->pending_step.valid = FALSE;
3344 if (basesink->pad_mode == GST_ACTIVATE_PUSH) {
3345 /* we need new segment info after the flush. */
3346 basesink->have_newsegment = FALSE;
3347 gst_segment_init (&basesink->segment, GST_FORMAT_UNDEFINED);
3348 gst_segment_init (basesink->abidata.ABI.clip_segment, GST_FORMAT_UNDEFINED);
3350 GST_OBJECT_UNLOCK (basesink);
3354 gst_base_sink_event (GstPad * pad, GstEvent * event)
3356 GstBaseSink *basesink;
3357 gboolean result = TRUE;
3358 GstBaseSinkClass *bclass;
3360 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
3362 bclass = GST_BASE_SINK_GET_CLASS (basesink);
3364 GST_DEBUG_OBJECT (basesink, "received event %p %" GST_PTR_FORMAT, event,
3367 switch (GST_EVENT_TYPE (event)) {
3372 GST_PAD_PREROLL_LOCK (pad);
3373 if (G_UNLIKELY (basesink->flushing))
3376 if (G_UNLIKELY (basesink->priv->received_eos)) {
3377 /* we can't accept anything when we are EOS */
3379 gst_event_unref (event);
3381 /* we set the received EOS flag here so that we can use it when testing if
3382 * we are prerolled and to refuse more buffers. */
3383 basesink->priv->received_eos = TRUE;
3385 /* EOS is a prerollable object, we call the unlocked version because it
3386 * does not check the received_eos flag. */
3387 ret = gst_base_sink_queue_object_unlocked (basesink, pad,
3388 _PR_IS_EVENT, GST_MINI_OBJECT_CAST (event), TRUE);
3389 if (G_UNLIKELY (ret != GST_FLOW_OK))
3392 GST_PAD_PREROLL_UNLOCK (pad);
3395 case GST_EVENT_NEWSEGMENT:
3400 GST_DEBUG_OBJECT (basesink, "newsegment %p", event);
3402 GST_PAD_PREROLL_LOCK (pad);
3403 if (G_UNLIKELY (basesink->flushing))
3406 gst_event_parse_new_segment_full (event, &update, NULL, NULL, NULL, NULL,
3409 if (G_UNLIKELY (basesink->priv->received_eos && !update)) {
3410 /* we can't accept anything when we are EOS */
3412 gst_event_unref (event);
3414 /* the new segment is a non prerollable item and does not block anything,
3415 * we need to configure the current clipping segment and insert the event
3416 * in the queue to serialize it with the buffers for rendering. */
3417 gst_base_sink_configure_segment (basesink, pad, event,
3418 basesink->abidata.ABI.clip_segment);
3421 gst_base_sink_queue_object_unlocked (basesink, pad,
3422 _PR_IS_EVENT, GST_MINI_OBJECT_CAST (event), FALSE);
3423 if (G_UNLIKELY (ret != GST_FLOW_OK))
3426 GST_OBJECT_LOCK (basesink);
3427 basesink->have_newsegment = TRUE;
3428 GST_OBJECT_UNLOCK (basesink);
3431 GST_PAD_PREROLL_UNLOCK (pad);
3434 case GST_EVENT_FLUSH_START:
3436 bclass->event (basesink, event);
3438 GST_DEBUG_OBJECT (basesink, "flush-start %p", event);
3440 gst_base_sink_flush_start (basesink, pad);
3442 gst_event_unref (event);
3444 case GST_EVENT_FLUSH_STOP:
3446 bclass->event (basesink, event);
3448 GST_DEBUG_OBJECT (basesink, "flush-stop %p", event);
3450 gst_base_sink_flush_stop (basesink, pad);
3452 gst_event_unref (event);
3455 /* other events are sent to queue or subclass depending on if they
3456 * are serialized. */
3457 if (GST_EVENT_IS_SERIALIZED (event)) {
3458 gst_base_sink_queue_object (basesink, pad,
3459 GST_MINI_OBJECT_CAST (event), FALSE);
3462 bclass->event (basesink, event);
3463 gst_event_unref (event);
3468 gst_object_unref (basesink);
3475 GST_DEBUG_OBJECT (basesink, "we are flushing");
3476 GST_PAD_PREROLL_UNLOCK (pad);
3478 gst_event_unref (event);
3483 /* default implementation to calculate the start and end
3484 * timestamps on a buffer, subclasses can override
3487 gst_base_sink_get_times (GstBaseSink * basesink, GstBuffer * buffer,
3488 GstClockTime * start, GstClockTime * end)
3490 GstClockTime timestamp, duration;
3492 timestamp = GST_BUFFER_TIMESTAMP (buffer);
3493 if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
3495 /* get duration to calculate end time */
3496 duration = GST_BUFFER_DURATION (buffer);
3497 if (GST_CLOCK_TIME_IS_VALID (duration)) {
3498 *end = timestamp + duration;
3504 /* must be called with PREROLL_LOCK */
3506 gst_base_sink_needs_preroll (GstBaseSink * basesink)
3508 gboolean is_prerolled, res;
3510 /* we have 2 cases where the PREROLL_LOCK is released:
3511 * 1) we are blocking in the PREROLL_LOCK and thus are prerolled.
3512 * 2) we are syncing on the clock
3514 is_prerolled = basesink->have_preroll || basesink->priv->received_eos;
3515 res = !is_prerolled;
3517 GST_DEBUG_OBJECT (basesink, "have_preroll: %d, EOS: %d => needs preroll: %d",
3518 basesink->have_preroll, basesink->priv->received_eos, res);
3523 /* with STREAM_LOCK, PREROLL_LOCK
3525 * Takes a buffer and compare the timestamps with the last segment.
3526 * If the buffer falls outside of the segment boundaries, drop it.
3527 * Else queue the buffer for preroll and rendering.
3529 * This function takes ownership of the buffer.
3531 static GstFlowReturn
3532 gst_base_sink_chain_unlocked (GstBaseSink * basesink, GstPad * pad,
3533 guint8 obj_type, gpointer obj)
3535 GstBaseSinkClass *bclass;
3536 GstFlowReturn result;
3537 GstClockTime start = GST_CLOCK_TIME_NONE, end = GST_CLOCK_TIME_NONE;
3538 GstSegment *clip_segment;
3539 GstBuffer *time_buf;
3541 if (G_UNLIKELY (basesink->flushing))
3544 if (G_UNLIKELY (basesink->priv->received_eos))
3547 if (OBJ_IS_BUFFERLIST (obj_type)) {
3548 time_buf = gst_buffer_list_get (GST_BUFFER_LIST_CAST (obj), 0, 0);
3549 g_assert (NULL != time_buf);
3551 time_buf = GST_BUFFER_CAST (obj);
3554 /* for code clarity */
3555 clip_segment = basesink->abidata.ABI.clip_segment;
3557 if (G_UNLIKELY (!basesink->have_newsegment)) {
3560 sync = gst_base_sink_get_sync (basesink);
3562 GST_ELEMENT_WARNING (basesink, STREAM, FAILED,
3563 (_("Internal data flow problem.")),
3564 ("Received buffer without a new-segment. Assuming timestamps start from 0."));
3567 /* this means this sink will assume timestamps start from 0 */
3568 GST_OBJECT_LOCK (basesink);
3569 clip_segment->start = 0;
3570 clip_segment->stop = -1;
3571 basesink->segment.start = 0;
3572 basesink->segment.stop = -1;
3573 basesink->have_newsegment = TRUE;
3574 GST_OBJECT_UNLOCK (basesink);
3577 bclass = GST_BASE_SINK_GET_CLASS (basesink);
3579 /* check if the buffer needs to be dropped, we first ask the subclass for the
3581 if (bclass->get_times)
3582 bclass->get_times (basesink, time_buf, &start, &end);
3584 if (!GST_CLOCK_TIME_IS_VALID (start)) {
3585 /* if the subclass does not want sync, we use our own values so that we at
3586 * least clip the buffer to the segment */
3587 gst_base_sink_get_times (basesink, time_buf, &start, &end);
3590 GST_DEBUG_OBJECT (basesink, "got times start: %" GST_TIME_FORMAT
3591 ", end: %" GST_TIME_FORMAT, GST_TIME_ARGS (start), GST_TIME_ARGS (end));
3593 /* a dropped buffer does not participate in anything */
3594 if (GST_CLOCK_TIME_IS_VALID (start) &&
3595 (clip_segment->format == GST_FORMAT_TIME)) {
3596 if (G_UNLIKELY (!gst_segment_clip (clip_segment,
3597 GST_FORMAT_TIME, (gint64) start, (gint64) end, NULL, NULL)))
3598 goto out_of_segment;
3601 /* now we can process the buffer in the queue, this function takes ownership
3603 result = gst_base_sink_queue_object_unlocked (basesink, pad,
3604 obj_type, obj, TRUE);
3610 GST_DEBUG_OBJECT (basesink, "sink is flushing");
3611 gst_mini_object_unref (GST_MINI_OBJECT_CAST (obj));
3612 return GST_FLOW_WRONG_STATE;
3616 GST_DEBUG_OBJECT (basesink,
3617 "we are EOS, dropping object, return UNEXPECTED");
3618 gst_mini_object_unref (GST_MINI_OBJECT_CAST (obj));
3619 return GST_FLOW_UNEXPECTED;
3623 GST_DEBUG_OBJECT (basesink, "dropping buffer, out of clipping segment");
3624 gst_mini_object_unref (GST_MINI_OBJECT_CAST (obj));
3631 static GstFlowReturn
3632 gst_base_sink_chain_main (GstBaseSink * basesink, GstPad * pad,
3633 guint8 obj_type, gpointer obj)
3635 GstFlowReturn result;
3637 if (G_UNLIKELY (basesink->pad_mode != GST_ACTIVATE_PUSH))
3640 GST_PAD_PREROLL_LOCK (pad);
3641 result = gst_base_sink_chain_unlocked (basesink, pad, obj_type, obj);
3642 GST_PAD_PREROLL_UNLOCK (pad);
3650 GST_OBJECT_LOCK (pad);
3651 GST_WARNING_OBJECT (basesink,
3652 "Push on pad %s:%s, but it was not activated in push mode",
3653 GST_DEBUG_PAD_NAME (pad));
3654 GST_OBJECT_UNLOCK (pad);
3655 gst_mini_object_unref (GST_MINI_OBJECT_CAST (obj));
3656 /* we don't post an error message this will signal to the peer
3657 * pushing that EOS is reached. */
3658 result = GST_FLOW_UNEXPECTED;
3663 static GstFlowReturn
3664 gst_base_sink_chain (GstPad * pad, GstBuffer * buf)
3666 GstBaseSink *basesink;
3668 basesink = GST_BASE_SINK (GST_OBJECT_PARENT (pad));
3670 return gst_base_sink_chain_main (basesink, pad, _PR_IS_BUFFER, buf);
3673 static GstFlowReturn
3674 gst_base_sink_chain_list (GstPad * pad, GstBufferList * list)
3676 GstBaseSink *basesink;
3677 GstBaseSinkClass *bclass;
3678 GstFlowReturn result;
3680 basesink = GST_BASE_SINK (GST_OBJECT_PARENT (pad));
3681 bclass = GST_BASE_SINK_GET_CLASS (basesink);
3683 if (G_LIKELY (bclass->render_list)) {
3684 result = gst_base_sink_chain_main (basesink, pad, _PR_IS_BUFFERLIST, list);
3686 GstBufferListIterator *it;
3689 GST_INFO_OBJECT (pad, "chaining each group in list as a merged buffer");
3691 it = gst_buffer_list_iterate (list);
3693 if (gst_buffer_list_iterator_next_group (it)) {
3695 group = gst_buffer_list_iterator_merge_group (it);
3696 if (group == NULL) {
3697 group = gst_buffer_new ();
3698 GST_CAT_INFO_OBJECT (GST_CAT_SCHEDULING, pad, "chaining empty group");
3700 GST_CAT_INFO_OBJECT (GST_CAT_SCHEDULING, pad, "chaining group");
3702 result = gst_base_sink_chain_main (basesink, pad, _PR_IS_BUFFER, group);
3703 } while (result == GST_FLOW_OK
3704 && gst_buffer_list_iterator_next_group (it));
3706 GST_CAT_INFO_OBJECT (GST_CAT_SCHEDULING, pad, "chaining empty group");
3708 gst_base_sink_chain_main (basesink, pad, _PR_IS_BUFFER,
3711 gst_buffer_list_iterator_free (it);
3712 gst_buffer_list_unref (list);
3719 gst_base_sink_default_do_seek (GstBaseSink * sink, GstSegment * segment)
3721 gboolean res = TRUE;
3723 /* update our offset if the start/stop position was updated */
3724 if (segment->format == GST_FORMAT_BYTES) {
3725 segment->time = segment->start;
3726 } else if (segment->start == 0) {
3727 /* seek to start, we can implement a default for this. */
3731 GST_INFO_OBJECT (sink, "Can't do a default seek");
3737 #define SEEK_TYPE_IS_RELATIVE(t) (((t) != GST_SEEK_TYPE_NONE) && ((t) != GST_SEEK_TYPE_SET))
3740 gst_base_sink_default_prepare_seek_segment (GstBaseSink * sink,
3741 GstEvent * event, GstSegment * segment)
3743 /* By default, we try one of 2 things:
3744 * - For absolute seek positions, convert the requested position to our
3745 * configured processing format and place it in the output segment \
3746 * - For relative seek positions, convert our current (input) values to the
3747 * seek format, adjust by the relative seek offset and then convert back to
3748 * the processing format
3750 GstSeekType cur_type, stop_type;
3753 GstFormat seek_format, dest_format;
3756 gboolean res = TRUE;
3758 gst_event_parse_seek (event, &rate, &seek_format, &flags,
3759 &cur_type, &cur, &stop_type, &stop);
3760 dest_format = segment->format;
3762 if (seek_format == dest_format) {
3763 gst_segment_set_seek (segment, rate, seek_format, flags,
3764 cur_type, cur, stop_type, stop, &update);
3768 if (cur_type != GST_SEEK_TYPE_NONE) {
3769 /* FIXME: Handle seek_cur & seek_end by converting the input segment vals */
3771 gst_pad_query_convert (sink->sinkpad, seek_format, cur, &dest_format,
3773 cur_type = GST_SEEK_TYPE_SET;
3776 if (res && stop_type != GST_SEEK_TYPE_NONE) {
3777 /* FIXME: Handle seek_cur & seek_end by converting the input segment vals */
3779 gst_pad_query_convert (sink->sinkpad, seek_format, stop, &dest_format,
3781 stop_type = GST_SEEK_TYPE_SET;
3784 /* And finally, configure our output segment in the desired format */
3785 gst_segment_set_seek (segment, rate, dest_format, flags, cur_type, cur,
3786 stop_type, stop, &update);
3795 GST_DEBUG_OBJECT (sink, "undefined format given, seek aborted.");
3800 /* perform a seek, only executed in pull mode */
3802 gst_base_sink_perform_seek (GstBaseSink * sink, GstPad * pad, GstEvent * event)
3806 GstFormat seek_format, dest_format;
3808 GstSeekType cur_type, stop_type;
3809 gboolean seekseg_configured = FALSE;
3811 gboolean update, res = TRUE;
3812 GstSegment seeksegment;
3814 dest_format = sink->segment.format;
3817 GST_DEBUG_OBJECT (sink, "performing seek with event %p", event);
3818 gst_event_parse_seek (event, &rate, &seek_format, &flags,
3819 &cur_type, &cur, &stop_type, &stop);
3821 flush = flags & GST_SEEK_FLAG_FLUSH;
3823 GST_DEBUG_OBJECT (sink, "performing seek without event");
3828 GST_DEBUG_OBJECT (sink, "flushing upstream");
3829 gst_pad_push_event (pad, gst_event_new_flush_start ());
3830 gst_base_sink_flush_start (sink, pad);
3832 GST_DEBUG_OBJECT (sink, "pausing pulling thread");
3835 GST_PAD_STREAM_LOCK (pad);
3837 /* If we configured the seeksegment above, don't overwrite it now. Otherwise
3838 * copy the current segment info into the temp segment that we can actually
3839 * attempt the seek with. We only update the real segment if the seek suceeds. */
3840 if (!seekseg_configured) {
3841 memcpy (&seeksegment, &sink->segment, sizeof (GstSegment));
3843 /* now configure the final seek segment */
3845 if (sink->segment.format != seek_format) {
3846 /* OK, here's where we give the subclass a chance to convert the relative
3847 * seek into an absolute one in the processing format. We set up any
3848 * absolute seek above, before taking the stream lock. */
3849 if (!gst_base_sink_default_prepare_seek_segment (sink, event,
3851 GST_DEBUG_OBJECT (sink,
3852 "Preparing the seek failed after flushing. " "Aborting seek");
3856 /* The seek format matches our processing format, no need to ask the
3857 * the subclass to configure the segment. */
3858 gst_segment_set_seek (&seeksegment, rate, seek_format, flags,
3859 cur_type, cur, stop_type, stop, &update);
3862 /* Else, no seek event passed, so we're just (re)starting the
3867 GST_DEBUG_OBJECT (sink, "segment configured from %" G_GINT64_FORMAT
3868 " to %" G_GINT64_FORMAT ", position %" G_GINT64_FORMAT,
3869 seeksegment.start, seeksegment.stop, seeksegment.last_stop);
3871 /* do the seek, segment.last_stop contains the new position. */
3872 res = gst_base_sink_default_do_seek (sink, &seeksegment);
3877 GST_DEBUG_OBJECT (sink, "stop flushing upstream");
3878 gst_pad_push_event (pad, gst_event_new_flush_stop ());
3879 gst_base_sink_flush_stop (sink, pad);
3880 } else if (res && sink->abidata.ABI.running) {
3881 /* we are running the current segment and doing a non-flushing seek,
3882 * close the segment first based on the last_stop. */
3883 GST_DEBUG_OBJECT (sink, "closing running segment %" G_GINT64_FORMAT
3884 " to %" G_GINT64_FORMAT, sink->segment.start, sink->segment.last_stop);
3887 /* The subclass must have converted the segment to the processing format
3889 if (res && seeksegment.format != dest_format) {
3890 GST_DEBUG_OBJECT (sink, "Subclass failed to prepare a seek segment "
3891 "in the correct format. Aborting seek.");
3895 /* if successfull seek, we update our real segment and push
3896 * out the new segment. */
3898 memcpy (&sink->segment, &seeksegment, sizeof (GstSegment));
3900 if (sink->segment.flags & GST_SEEK_FLAG_SEGMENT) {
3901 gst_element_post_message (GST_ELEMENT (sink),
3902 gst_message_new_segment_start (GST_OBJECT (sink),
3903 sink->segment.format, sink->segment.last_stop));
3907 sink->priv->discont = TRUE;
3908 sink->abidata.ABI.running = TRUE;
3910 GST_PAD_STREAM_UNLOCK (pad);
3916 set_step_info (GstBaseSink * sink, GstStepInfo * current, GstStepInfo * pending,
3917 guint seqnum, GstFormat format, guint64 amount, gdouble rate,
3918 gboolean flush, gboolean intermediate)
3920 GST_OBJECT_LOCK (sink);
3921 pending->seqnum = seqnum;
3922 pending->format = format;
3923 pending->amount = amount;
3924 pending->position = 0;
3925 pending->rate = rate;
3926 pending->flush = flush;
3927 pending->intermediate = intermediate;
3928 pending->valid = TRUE;
3929 /* flush invalidates the current stepping segment */
3931 current->valid = FALSE;
3932 GST_OBJECT_UNLOCK (sink);
3936 gst_base_sink_perform_step (GstBaseSink * sink, GstPad * pad, GstEvent * event)
3938 GstBaseSinkPrivate *priv;
3939 GstBaseSinkClass *bclass;
3940 gboolean flush, intermediate;
3945 GstStepInfo *pending, *current;
3946 GstMessage *message;
3948 bclass = GST_BASE_SINK_GET_CLASS (sink);
3951 GST_DEBUG_OBJECT (sink, "performing step with event %p", event);
3953 gst_event_parse_step (event, &format, &amount, &rate, &flush, &intermediate);
3954 seqnum = gst_event_get_seqnum (event);
3956 pending = &priv->pending_step;
3957 current = &priv->current_step;
3959 /* post message first */
3960 message = gst_message_new_step_start (GST_OBJECT (sink), FALSE, format,
3961 amount, rate, flush, intermediate);
3962 gst_message_set_seqnum (message, seqnum);
3963 gst_element_post_message (GST_ELEMENT (sink), message);
3966 /* we need to call ::unlock before locking PREROLL_LOCK
3967 * since we lock it before going into ::render */
3969 bclass->unlock (sink);
3971 GST_PAD_PREROLL_LOCK (sink->sinkpad);
3972 /* now that we have the PREROLL lock, clear our unlock request */
3973 if (bclass->unlock_stop)
3974 bclass->unlock_stop (sink);
3976 /* update the stepinfo and make it valid */
3977 set_step_info (sink, current, pending, seqnum, format, amount, rate, flush,
3980 if (sink->priv->async_enabled) {
3981 /* and we need to commit our state again on the next
3982 * prerolled buffer */
3983 sink->playing_async = TRUE;
3984 priv->pending_step.need_preroll = TRUE;
3985 sink->need_preroll = FALSE;
3986 gst_element_lost_state_full (GST_ELEMENT_CAST (sink), FALSE);
3988 sink->priv->have_latency = TRUE;
3989 sink->need_preroll = FALSE;
3991 priv->current_sstart = GST_CLOCK_TIME_NONE;
3992 priv->current_sstop = GST_CLOCK_TIME_NONE;
3993 priv->eos_rtime = GST_CLOCK_TIME_NONE;
3994 priv->call_preroll = TRUE;
3995 gst_base_sink_set_last_buffer (sink, NULL);
3996 gst_base_sink_reset_qos (sink);
3998 if (sink->clock_id) {
3999 gst_clock_id_unschedule (sink->clock_id);
4002 if (sink->have_preroll) {
4003 GST_DEBUG_OBJECT (sink, "signal waiter");
4004 priv->step_unlock = TRUE;
4005 GST_PAD_PREROLL_SIGNAL (sink->sinkpad);
4007 GST_PAD_PREROLL_UNLOCK (sink->sinkpad);
4009 /* update the stepinfo and make it valid */
4010 set_step_info (sink, current, pending, seqnum, format, amount, rate, flush,
4020 gst_base_sink_loop (GstPad * pad)
4022 GstBaseSink *basesink;
4023 GstBuffer *buf = NULL;
4024 GstFlowReturn result;
4028 basesink = GST_BASE_SINK (GST_OBJECT_PARENT (pad));
4030 g_assert (basesink->pad_mode == GST_ACTIVATE_PULL);
4032 if ((blocksize = basesink->priv->blocksize) == 0)
4035 offset = basesink->segment.last_stop;
4037 GST_DEBUG_OBJECT (basesink, "pulling %" G_GUINT64_FORMAT ", %u",
4040 result = gst_pad_pull_range (pad, offset, blocksize, &buf);
4041 if (G_UNLIKELY (result != GST_FLOW_OK))
4044 if (G_UNLIKELY (buf == NULL))
4047 offset += GST_BUFFER_SIZE (buf);
4049 gst_segment_set_last_stop (&basesink->segment, GST_FORMAT_BYTES, offset);
4051 GST_PAD_PREROLL_LOCK (pad);
4052 result = gst_base_sink_chain_unlocked (basesink, pad, _PR_IS_BUFFER, buf);
4053 GST_PAD_PREROLL_UNLOCK (pad);
4054 if (G_UNLIKELY (result != GST_FLOW_OK))
4062 GST_LOG_OBJECT (basesink, "pausing task, reason %s",
4063 gst_flow_get_name (result));
4064 gst_pad_pause_task (pad);
4065 if (result == GST_FLOW_UNEXPECTED) {
4066 /* perform EOS logic */
4067 if (basesink->segment.flags & GST_SEEK_FLAG_SEGMENT) {
4068 gst_element_post_message (GST_ELEMENT_CAST (basesink),
4069 gst_message_new_segment_done (GST_OBJECT_CAST (basesink),
4070 basesink->segment.format, basesink->segment.last_stop));
4072 gst_base_sink_event (pad, gst_event_new_eos ());
4074 } else if (result == GST_FLOW_NOT_LINKED || result <= GST_FLOW_UNEXPECTED) {
4075 /* for fatal errors we post an error message, post the error
4076 * first so the app knows about the error first.
4077 * wrong-state is not a fatal error because it happens due to
4078 * flushing and posting an error message in that case is the
4079 * wrong thing to do, e.g. when basesrc is doing a flushing
4081 GST_ELEMENT_ERROR (basesink, STREAM, FAILED,
4082 (_("Internal data stream error.")),
4083 ("stream stopped, reason %s", gst_flow_get_name (result)));
4084 gst_base_sink_event (pad, gst_event_new_eos ());
4090 GST_LOG_OBJECT (basesink, "no buffer, pausing");
4091 GST_ELEMENT_ERROR (basesink, STREAM, FAILED,
4092 (_("Internal data flow error.")), ("element returned NULL buffer"));
4093 result = GST_FLOW_ERROR;
4099 gst_base_sink_set_flushing (GstBaseSink * basesink, GstPad * pad,
4102 GstBaseSinkClass *bclass;
4104 bclass = GST_BASE_SINK_GET_CLASS (basesink);
4107 /* unlock any subclasses, we need to do this before grabbing the
4108 * PREROLL_LOCK since we hold this lock before going into ::render. */
4110 bclass->unlock (basesink);
4113 GST_PAD_PREROLL_LOCK (pad);
4114 basesink->flushing = flushing;
4116 /* step 1, now that we have the PREROLL lock, clear our unlock request */
4117 if (bclass->unlock_stop)
4118 bclass->unlock_stop (basesink);
4120 /* set need_preroll before we unblock the clock. If the clock is unblocked
4121 * before timing out, we can reuse the buffer for preroll. */
4122 basesink->need_preroll = TRUE;
4124 /* step 2, unblock clock sync (if any) or any other blocking thing */
4125 if (basesink->clock_id) {
4126 gst_clock_id_unschedule (basesink->clock_id);
4129 /* flush out the data thread if it's locked in finish_preroll, this will
4130 * also flush out the EOS state */
4131 GST_DEBUG_OBJECT (basesink,
4132 "flushing out data thread, need preroll to TRUE");
4133 gst_base_sink_preroll_queue_flush (basesink, pad);
4135 GST_PAD_PREROLL_UNLOCK (pad);
4141 gst_base_sink_default_activate_pull (GstBaseSink * basesink, gboolean active)
4147 result = gst_pad_start_task (basesink->sinkpad,
4148 (GstTaskFunction) gst_base_sink_loop, basesink->sinkpad);
4150 /* step 2, make sure streaming finishes */
4151 result = gst_pad_stop_task (basesink->sinkpad);
4158 gst_base_sink_pad_activate (GstPad * pad)
4160 gboolean result = FALSE;
4161 GstBaseSink *basesink;
4163 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
4165 GST_DEBUG_OBJECT (basesink, "Trying pull mode first");
4167 gst_base_sink_set_flushing (basesink, pad, FALSE);
4169 /* we need to have the pull mode enabled */
4170 if (!basesink->can_activate_pull) {
4171 GST_DEBUG_OBJECT (basesink, "pull mode disabled");
4175 /* check if downstreams supports pull mode at all */
4176 if (!gst_pad_check_pull_range (pad)) {
4177 GST_DEBUG_OBJECT (basesink, "pull mode not supported");
4181 /* set the pad mode before starting the task so that it's in the
4182 * correct state for the new thread. also the sink set_caps and get_caps
4183 * function checks this */
4184 basesink->pad_mode = GST_ACTIVATE_PULL;
4186 /* we first try to negotiate a format so that when we try to activate
4187 * downstream, it knows about our format */
4188 if (!gst_base_sink_negotiate_pull (basesink)) {
4189 GST_DEBUG_OBJECT (basesink, "failed to negotiate in pull mode");
4193 /* ok activate now */
4194 if (!gst_pad_activate_pull (pad, TRUE)) {
4195 /* clear any pending caps */
4196 GST_OBJECT_LOCK (basesink);
4197 gst_caps_replace (&basesink->priv->pull_caps, NULL);
4198 GST_OBJECT_UNLOCK (basesink);
4199 GST_DEBUG_OBJECT (basesink, "failed to activate in pull mode");
4203 GST_DEBUG_OBJECT (basesink, "Success activating pull mode");
4207 /* push mode fallback */
4209 GST_DEBUG_OBJECT (basesink, "Falling back to push mode");
4210 if ((result = gst_pad_activate_push (pad, TRUE))) {
4211 GST_DEBUG_OBJECT (basesink, "Success activating push mode");
4216 GST_WARNING_OBJECT (basesink, "Could not activate pad in either mode");
4217 gst_base_sink_set_flushing (basesink, pad, TRUE);
4220 gst_object_unref (basesink);
4226 gst_base_sink_pad_activate_push (GstPad * pad, gboolean active)
4229 GstBaseSink *basesink;
4231 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
4234 if (!basesink->can_activate_push) {
4236 basesink->pad_mode = GST_ACTIVATE_NONE;
4239 basesink->pad_mode = GST_ACTIVATE_PUSH;
4242 if (G_UNLIKELY (basesink->pad_mode != GST_ACTIVATE_PUSH)) {
4243 g_warning ("Internal GStreamer activation error!!!");
4246 gst_base_sink_set_flushing (basesink, pad, TRUE);
4248 basesink->pad_mode = GST_ACTIVATE_NONE;
4252 gst_object_unref (basesink);
4258 gst_base_sink_negotiate_pull (GstBaseSink * basesink)
4265 /* this returns the intersection between our caps and the peer caps. If there
4266 * is no peer, it returns NULL and we can't operate in pull mode so we can
4267 * fail the negotiation. */
4268 caps = gst_pad_get_allowed_caps (GST_BASE_SINK_PAD (basesink));
4269 if (caps == NULL || gst_caps_is_empty (caps))
4270 goto no_caps_possible;
4272 GST_DEBUG_OBJECT (basesink, "allowed caps: %" GST_PTR_FORMAT, caps);
4274 caps = gst_caps_make_writable (caps);
4275 /* get the first (prefered) format */
4276 gst_caps_truncate (caps);
4278 gst_pad_fixate_caps (GST_BASE_SINK_PAD (basesink), caps);
4280 GST_DEBUG_OBJECT (basesink, "fixated to: %" GST_PTR_FORMAT, caps);
4282 if (gst_caps_is_any (caps)) {
4283 GST_DEBUG_OBJECT (basesink, "caps were ANY after fixating, "
4285 /* neither side has template caps in this case, so they are prepared for
4286 pull() without setcaps() */
4288 } else if (gst_caps_is_fixed (caps)) {
4289 if (!gst_pad_set_caps (GST_BASE_SINK_PAD (basesink), caps))
4290 goto could_not_set_caps;
4292 GST_OBJECT_LOCK (basesink);
4293 gst_caps_replace (&basesink->priv->pull_caps, caps);
4294 GST_OBJECT_UNLOCK (basesink);
4299 gst_caps_unref (caps);
4305 GST_INFO_OBJECT (basesink, "Pipeline could not agree on caps");
4306 GST_DEBUG_OBJECT (basesink, "get_allowed_caps() returned EMPTY");
4308 gst_caps_unref (caps);
4313 GST_INFO_OBJECT (basesink, "Could not set caps: %" GST_PTR_FORMAT, caps);
4314 gst_caps_unref (caps);
4319 /* this won't get called until we implement an activate function */
4321 gst_base_sink_pad_activate_pull (GstPad * pad, gboolean active)
4323 gboolean result = FALSE;
4324 GstBaseSink *basesink;
4325 GstBaseSinkClass *bclass;
4327 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
4328 bclass = GST_BASE_SINK_GET_CLASS (basesink);
4334 /* we mark we have a newsegment here because pull based
4335 * mode works just fine without having a newsegment before the
4337 format = GST_FORMAT_BYTES;
4339 gst_segment_init (&basesink->segment, format);
4340 gst_segment_init (basesink->abidata.ABI.clip_segment, format);
4341 GST_OBJECT_LOCK (basesink);
4342 basesink->have_newsegment = TRUE;
4343 GST_OBJECT_UNLOCK (basesink);
4345 /* get the peer duration in bytes */
4346 result = gst_pad_query_peer_duration (pad, &format, &duration);
4348 GST_DEBUG_OBJECT (basesink,
4349 "setting duration in bytes to %" G_GINT64_FORMAT, duration);
4350 gst_segment_set_duration (basesink->abidata.ABI.clip_segment, format,
4352 gst_segment_set_duration (&basesink->segment, format, duration);
4354 GST_DEBUG_OBJECT (basesink, "unknown duration");
4357 if (bclass->activate_pull)
4358 result = bclass->activate_pull (basesink, TRUE);
4363 goto activate_failed;
4366 if (G_UNLIKELY (basesink->pad_mode != GST_ACTIVATE_PULL)) {
4367 g_warning ("Internal GStreamer activation error!!!");
4370 result = gst_base_sink_set_flushing (basesink, pad, TRUE);
4371 if (bclass->activate_pull)
4372 result &= bclass->activate_pull (basesink, FALSE);
4373 basesink->pad_mode = GST_ACTIVATE_NONE;
4374 /* clear any pending caps */
4375 GST_OBJECT_LOCK (basesink);
4376 gst_caps_replace (&basesink->priv->pull_caps, NULL);
4377 GST_OBJECT_UNLOCK (basesink);
4380 gst_object_unref (basesink);
4387 /* reset, as starting the thread failed */
4388 basesink->pad_mode = GST_ACTIVATE_NONE;
4390 GST_ERROR_OBJECT (basesink, "subclass failed to activate in pull mode");
4395 /* send an event to our sinkpad peer. */
4397 gst_base_sink_send_event (GstElement * element, GstEvent * event)
4400 GstBaseSink *basesink = GST_BASE_SINK (element);
4401 gboolean forward, result = TRUE;
4402 GstActivateMode mode;
4404 GST_OBJECT_LOCK (element);
4405 /* get the pad and the scheduling mode */
4406 pad = gst_object_ref (basesink->sinkpad);
4407 mode = basesink->pad_mode;
4408 GST_OBJECT_UNLOCK (element);
4410 /* only push UPSTREAM events upstream */
4411 forward = GST_EVENT_IS_UPSTREAM (event);
4413 GST_DEBUG_OBJECT (basesink, "handling event %p %" GST_PTR_FORMAT, event,
4416 switch (GST_EVENT_TYPE (event)) {
4417 case GST_EVENT_LATENCY:
4419 GstClockTime latency;
4421 gst_event_parse_latency (event, &latency);
4423 /* store the latency. We use this to adjust the running_time before syncing
4424 * it to the clock. */
4425 GST_OBJECT_LOCK (element);
4426 basesink->priv->latency = latency;
4427 if (!basesink->priv->have_latency)
4429 GST_OBJECT_UNLOCK (element);
4430 GST_DEBUG_OBJECT (basesink, "latency set to %" GST_TIME_FORMAT,
4431 GST_TIME_ARGS (latency));
4433 /* We forward this event so that all elements know about the global pipeline
4434 * latency. This is interesting for an element when it wants to figure out
4435 * when a particular piece of data will be rendered. */
4438 case GST_EVENT_SEEK:
4439 /* in pull mode we will execute the seek */
4440 if (mode == GST_ACTIVATE_PULL)
4441 result = gst_base_sink_perform_seek (basesink, pad, event);
4443 case GST_EVENT_STEP:
4444 result = gst_base_sink_perform_step (basesink, pad, event);
4452 result = gst_pad_push_event (pad, event);
4454 /* not forwarded, unref the event */
4455 gst_event_unref (event);
4458 gst_object_unref (pad);
4463 gst_base_sink_get_position (GstBaseSink * basesink, GstFormat format,
4464 gint64 * cur, gboolean * upstream)
4466 GstClock *clock = NULL;
4467 gboolean res = FALSE;
4468 GstFormat oformat, tformat;
4469 GstSegment *segment;
4470 GstClockTime now, latency;
4471 GstClockTimeDiff base;
4472 gint64 time, accum, duration;
4475 gboolean last_seen, with_clock, in_paused;
4477 GST_OBJECT_LOCK (basesink);
4478 /* we can only get the segment when we are not NULL or READY */
4479 if (!basesink->have_newsegment)
4483 /* when not in PLAYING or when we're busy with a state change, we
4484 * cannot read from the clock so we report time based on the
4485 * last seen timestamp. */
4486 if (GST_STATE (basesink) != GST_STATE_PLAYING ||
4487 GST_STATE_PENDING (basesink) != GST_STATE_VOID_PENDING) {
4491 /* we don't use the clip segment in pull mode, when seeking we update the
4492 * main segment directly with the new segment values without it having to be
4493 * activated by the rendering after preroll */
4494 if (basesink->pad_mode == GST_ACTIVATE_PUSH)
4495 segment = basesink->abidata.ABI.clip_segment;
4497 segment = &basesink->segment;
4499 /* our intermediate time format */
4500 tformat = GST_FORMAT_TIME;
4501 /* get the format in the segment */
4502 oformat = segment->format;
4504 /* report with last seen position when EOS */
4505 last_seen = basesink->eos;
4507 /* assume we will use the clock for getting the current position */
4509 if (basesink->sync == FALSE)
4512 /* and we need a clock */
4513 if (G_UNLIKELY ((clock = GST_ELEMENT_CLOCK (basesink)) == NULL))
4516 gst_object_ref (clock);
4518 /* collect all data we need holding the lock */
4519 if (GST_CLOCK_TIME_IS_VALID (segment->time))
4520 time = segment->time;
4524 if (GST_CLOCK_TIME_IS_VALID (segment->stop))
4525 duration = segment->stop - segment->start;
4529 accum = segment->accum;
4530 rate = segment->rate * segment->applied_rate;
4531 latency = basesink->priv->latency;
4533 if (oformat == GST_FORMAT_TIME) {
4536 start = basesink->priv->current_sstart;
4537 stop = basesink->priv->current_sstop;
4540 /* in paused we use the last position as a lower bound */
4541 if (stop == -1 || segment->rate > 0.0)
4546 /* in playing, use last stop time as upper bound */
4547 if (start == -1 || segment->rate > 0.0)
4553 /* convert last stop to stream time */
4554 last = gst_segment_to_stream_time (segment, oformat, segment->last_stop);
4558 /* in paused, use start_time */
4559 base = GST_ELEMENT_START_TIME (basesink);
4560 GST_DEBUG_OBJECT (basesink, "in paused, using start time %" GST_TIME_FORMAT,
4561 GST_TIME_ARGS (base));
4562 } else if (with_clock) {
4563 /* else use clock when needed */
4564 base = GST_ELEMENT_CAST (basesink)->base_time;
4565 GST_DEBUG_OBJECT (basesink, "using clock and base time %" GST_TIME_FORMAT,
4566 GST_TIME_ARGS (base));
4568 /* else, no sync or clock -> no base time */
4569 GST_DEBUG_OBJECT (basesink, "no sync or no clock");
4573 /* no base, we can't calculate running_time, use last seem timestamp to report
4578 /* need to release the object lock before we can get the time,
4579 * a clock might take the LOCK of the provider, which could be
4580 * a basesink subclass. */
4581 GST_OBJECT_UNLOCK (basesink);
4584 /* in EOS or when no valid stream_time, report the value of last seen
4587 /* no timestamp, we need to ask upstream */
4588 GST_DEBUG_OBJECT (basesink, "no last seen timestamp, asking upstream");
4593 GST_DEBUG_OBJECT (basesink, "using last seen timestamp %" GST_TIME_FORMAT,
4594 GST_TIME_ARGS (last));
4597 if (oformat != tformat) {
4598 /* convert accum, time and duration to time */
4599 if (!gst_pad_query_convert (basesink->sinkpad, oformat, accum, &tformat,
4601 goto convert_failed;
4602 if (!gst_pad_query_convert (basesink->sinkpad, oformat, duration,
4603 &tformat, &duration))
4604 goto convert_failed;
4605 if (!gst_pad_query_convert (basesink->sinkpad, oformat, time, &tformat,
4607 goto convert_failed;
4608 if (!gst_pad_query_convert (basesink->sinkpad, oformat, last, &tformat,
4610 goto convert_failed;
4612 /* assume time format from now on */
4616 if (!in_paused && with_clock) {
4617 now = gst_clock_get_time (clock);
4623 /* subtract base time and accumulated time from the clock time.
4624 * Make sure we don't go negative. This is the current time in
4625 * the segment which we need to scale with the combined
4626 * rate and applied rate. */
4629 if (GST_CLOCK_DIFF (base, now) < 0)
4632 /* for negative rates we need to count back from the segment
4637 *cur = time + gst_guint64_to_gdouble (now - base) * rate;
4640 /* never report less than segment values in paused */
4642 *cur = MAX (last, *cur);
4644 /* never report more than last seen position in playing */
4646 *cur = MIN (last, *cur);
4649 GST_DEBUG_OBJECT (basesink,
4650 "now %" GST_TIME_FORMAT " - base %" GST_TIME_FORMAT " - accum %"
4651 GST_TIME_FORMAT " + time %" GST_TIME_FORMAT " last %" GST_TIME_FORMAT,
4652 GST_TIME_ARGS (now), GST_TIME_ARGS (base), GST_TIME_ARGS (accum),
4653 GST_TIME_ARGS (time), GST_TIME_ARGS (last));
4656 if (oformat != format) {
4657 /* convert to final format */
4658 if (!gst_pad_query_convert (basesink->sinkpad, oformat, *cur, &format, cur))
4659 goto convert_failed;
4665 GST_DEBUG_OBJECT (basesink, "res: %d, POSITION: %" GST_TIME_FORMAT,
4666 res, GST_TIME_ARGS (*cur));
4669 gst_object_unref (clock);
4676 /* in NULL or READY we always return FALSE and -1 */
4677 GST_DEBUG_OBJECT (basesink, "position in wrong state, return -1");
4680 GST_OBJECT_UNLOCK (basesink);
4685 GST_DEBUG_OBJECT (basesink, "convert failed, try upstream");
4693 gst_base_sink_get_duration (GstBaseSink * basesink, GstFormat format,
4694 gint64 * dur, gboolean * upstream)
4696 gboolean res = FALSE;
4698 if (basesink->pad_mode == GST_ACTIVATE_PULL) {
4699 GstFormat uformat = GST_FORMAT_BYTES;
4702 /* get the duration in bytes, in pull mode that's all we are sure to
4703 * know. We have to explicitly get this value from upstream instead of
4704 * using our cached value because it might change. Duration caching
4705 * should be done at a higher level. */
4706 res = gst_pad_query_peer_duration (basesink->sinkpad, &uformat, &uduration);
4708 gst_segment_set_duration (&basesink->segment, uformat, uduration);
4709 if (format != uformat) {
4710 /* convert to the requested format */
4711 res = gst_pad_query_convert (basesink->sinkpad, uformat, uduration,
4725 static const GstQueryType *
4726 gst_base_sink_get_query_types (GstElement * element)
4728 static const GstQueryType query_types[] = {
4740 gst_base_sink_query (GstElement * element, GstQuery * query)
4742 gboolean res = FALSE;
4744 GstBaseSink *basesink = GST_BASE_SINK (element);
4746 switch (GST_QUERY_TYPE (query)) {
4747 case GST_QUERY_POSITION:
4751 gboolean upstream = FALSE;
4753 gst_query_parse_position (query, &format, NULL);
4755 GST_DEBUG_OBJECT (basesink, "position query in format %s",
4756 gst_format_get_name (format));
4758 /* first try to get the position based on the clock */
4760 gst_base_sink_get_position (basesink, format, &cur, &upstream))) {
4761 gst_query_set_position (query, format, cur);
4762 } else if (upstream) {
4763 /* fallback to peer query */
4764 res = gst_pad_peer_query (basesink->sinkpad, query);
4767 /* we can handle a few things if upstream failed */
4768 if (format == GST_FORMAT_PERCENT) {
4770 GstFormat uformat = GST_FORMAT_TIME;
4772 res = gst_base_sink_get_position (basesink, GST_FORMAT_TIME, &cur,
4774 if (!res && upstream) {
4775 res = gst_pad_query_peer_position (basesink->sinkpad, &uformat,
4779 res = gst_base_sink_get_duration (basesink, GST_FORMAT_TIME, &dur,
4781 if (!res && upstream) {
4782 res = gst_pad_query_peer_duration (basesink->sinkpad, &uformat,
4789 pos = gst_util_uint64_scale (100 * GST_FORMAT_PERCENT_SCALE, cur,
4791 gst_query_set_position (query, GST_FORMAT_PERCENT, pos);
4797 case GST_QUERY_DURATION:
4801 gboolean upstream = FALSE;
4803 gst_query_parse_duration (query, &format, NULL);
4805 GST_DEBUG_OBJECT (basesink, "duration query in format %s",
4806 gst_format_get_name (format));
4809 gst_base_sink_get_duration (basesink, format, &dur, &upstream))) {
4810 gst_query_set_duration (query, format, dur);
4811 } else if (upstream) {
4812 /* fallback to peer query */
4813 res = gst_pad_peer_query (basesink->sinkpad, query);
4816 /* we can handle a few things if upstream failed */
4817 if (format == GST_FORMAT_PERCENT) {
4818 gst_query_set_duration (query, GST_FORMAT_PERCENT,
4819 GST_FORMAT_PERCENT_MAX);
4825 case GST_QUERY_LATENCY:
4827 gboolean live, us_live;
4828 GstClockTime min, max;
4830 if ((res = gst_base_sink_query_latency (basesink, &live, &us_live, &min,
4832 gst_query_set_latency (query, live, min, max);
4836 case GST_QUERY_JITTER:
4838 case GST_QUERY_RATE:
4839 /* gst_query_set_rate (query, basesink->segment_rate); */
4842 case GST_QUERY_SEGMENT:
4844 if (basesink->pad_mode == GST_ACTIVATE_PULL) {
4845 gst_query_set_segment (query, basesink->segment.rate,
4846 GST_FORMAT_TIME, basesink->segment.start, basesink->segment.stop);
4849 res = gst_pad_peer_query (basesink->sinkpad, query);
4853 case GST_QUERY_SEEKING:
4854 case GST_QUERY_CONVERT:
4855 case GST_QUERY_FORMATS:
4857 res = gst_pad_peer_query (basesink->sinkpad, query);
4860 GST_DEBUG_OBJECT (basesink, "query %s returns %d",
4861 GST_QUERY_TYPE_NAME (query), res);
4865 static GstStateChangeReturn
4866 gst_base_sink_change_state (GstElement * element, GstStateChange transition)
4868 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
4869 GstBaseSink *basesink = GST_BASE_SINK (element);
4870 GstBaseSinkClass *bclass;
4871 GstBaseSinkPrivate *priv;
4873 priv = basesink->priv;
4875 bclass = GST_BASE_SINK_GET_CLASS (basesink);
4877 switch (transition) {
4878 case GST_STATE_CHANGE_NULL_TO_READY:
4880 if (!bclass->start (basesink))
4883 case GST_STATE_CHANGE_READY_TO_PAUSED:
4884 /* need to complete preroll before this state change completes, there
4885 * is no data flow in READY so we can safely assume we need to preroll. */
4886 GST_PAD_PREROLL_LOCK (basesink->sinkpad);
4887 GST_DEBUG_OBJECT (basesink, "READY to PAUSED");
4888 basesink->have_newsegment = FALSE;
4889 gst_segment_init (&basesink->segment, GST_FORMAT_UNDEFINED);
4890 gst_segment_init (basesink->abidata.ABI.clip_segment,
4891 GST_FORMAT_UNDEFINED);
4892 basesink->offset = 0;
4893 basesink->have_preroll = FALSE;
4894 priv->step_unlock = FALSE;
4895 basesink->need_preroll = TRUE;
4896 basesink->playing_async = TRUE;
4897 priv->current_sstart = GST_CLOCK_TIME_NONE;
4898 priv->current_sstop = GST_CLOCK_TIME_NONE;
4899 priv->eos_rtime = GST_CLOCK_TIME_NONE;
4901 basesink->eos = FALSE;
4902 priv->received_eos = FALSE;
4903 gst_base_sink_reset_qos (basesink);
4904 priv->commited = FALSE;
4905 priv->call_preroll = TRUE;
4906 priv->current_step.valid = FALSE;
4907 priv->pending_step.valid = FALSE;
4908 if (priv->async_enabled) {
4909 GST_DEBUG_OBJECT (basesink, "doing async state change");
4910 /* when async enabled, post async-start message and return ASYNC from
4911 * the state change function */
4912 ret = GST_STATE_CHANGE_ASYNC;
4913 gst_element_post_message (GST_ELEMENT_CAST (basesink),
4914 gst_message_new_async_start (GST_OBJECT_CAST (basesink), FALSE));
4916 priv->have_latency = TRUE;
4918 GST_PAD_PREROLL_UNLOCK (basesink->sinkpad);
4920 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
4921 GST_PAD_PREROLL_LOCK (basesink->sinkpad);
4922 if (!gst_base_sink_needs_preroll (basesink)) {
4923 GST_DEBUG_OBJECT (basesink, "PAUSED to PLAYING, don't need preroll");
4924 /* no preroll needed anymore now. */
4925 basesink->playing_async = FALSE;
4926 basesink->need_preroll = FALSE;
4927 if (basesink->eos) {
4928 GstMessage *message;
4930 /* need to post EOS message here */
4931 GST_DEBUG_OBJECT (basesink, "Now posting EOS");
4932 message = gst_message_new_eos (GST_OBJECT_CAST (basesink));
4933 gst_message_set_seqnum (message, basesink->priv->seqnum);
4934 gst_element_post_message (GST_ELEMENT_CAST (basesink), message);
4936 GST_DEBUG_OBJECT (basesink, "signal preroll");
4937 GST_PAD_PREROLL_SIGNAL (basesink->sinkpad);
4940 GST_DEBUG_OBJECT (basesink, "PAUSED to PLAYING, we are not prerolled");
4941 basesink->need_preroll = TRUE;
4942 basesink->playing_async = TRUE;
4943 priv->call_preroll = TRUE;
4944 priv->commited = FALSE;
4945 if (priv->async_enabled) {
4946 GST_DEBUG_OBJECT (basesink, "doing async state change");
4947 ret = GST_STATE_CHANGE_ASYNC;
4948 gst_element_post_message (GST_ELEMENT_CAST (basesink),
4949 gst_message_new_async_start (GST_OBJECT_CAST (basesink), FALSE));
4952 GST_PAD_PREROLL_UNLOCK (basesink->sinkpad);
4959 GstStateChangeReturn bret;
4961 bret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
4962 if (G_UNLIKELY (bret == GST_STATE_CHANGE_FAILURE))
4963 goto activate_failed;
4966 switch (transition) {
4967 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
4968 GST_DEBUG_OBJECT (basesink, "PLAYING to PAUSED");
4969 /* FIXME, make sure we cannot enter _render first */
4971 /* we need to call ::unlock before locking PREROLL_LOCK
4972 * since we lock it before going into ::render */
4974 bclass->unlock (basesink);
4976 GST_PAD_PREROLL_LOCK (basesink->sinkpad);
4977 GST_DEBUG_OBJECT (basesink, "got preroll lock");
4978 /* now that we have the PREROLL lock, clear our unlock request */
4979 if (bclass->unlock_stop)
4980 bclass->unlock_stop (basesink);
4982 /* we need preroll again and we set the flag before unlocking the clockid
4983 * because if the clockid is unlocked before a current buffer expired, we
4984 * can use that buffer to preroll with */
4985 basesink->need_preroll = TRUE;
4987 if (basesink->clock_id) {
4988 GST_DEBUG_OBJECT (basesink, "unschedule clock");
4989 gst_clock_id_unschedule (basesink->clock_id);
4992 /* if we don't have a preroll buffer we need to wait for a preroll and
4994 if (!gst_base_sink_needs_preroll (basesink)) {
4995 GST_DEBUG_OBJECT (basesink, "PLAYING to PAUSED, we are prerolled");
4996 basesink->playing_async = FALSE;
4998 if (GST_STATE_TARGET (GST_ELEMENT (basesink)) <= GST_STATE_READY) {
4999 GST_DEBUG_OBJECT (basesink, "element is <= READY");
5000 ret = GST_STATE_CHANGE_SUCCESS;
5002 GST_DEBUG_OBJECT (basesink,
5003 "PLAYING to PAUSED, we are not prerolled");
5004 basesink->playing_async = TRUE;
5005 priv->commited = FALSE;
5006 priv->call_preroll = TRUE;
5007 if (priv->async_enabled) {
5008 GST_DEBUG_OBJECT (basesink, "doing async state change");
5009 ret = GST_STATE_CHANGE_ASYNC;
5010 gst_element_post_message (GST_ELEMENT_CAST (basesink),
5011 gst_message_new_async_start (GST_OBJECT_CAST (basesink),
5016 GST_DEBUG_OBJECT (basesink, "rendered: %" G_GUINT64_FORMAT
5017 ", dropped: %" G_GUINT64_FORMAT, priv->rendered, priv->dropped);
5019 gst_base_sink_reset_qos (basesink);
5020 GST_PAD_PREROLL_UNLOCK (basesink->sinkpad);
5022 case GST_STATE_CHANGE_PAUSED_TO_READY:
5023 GST_PAD_PREROLL_LOCK (basesink->sinkpad);
5024 /* start by reseting our position state with the object lock so that the
5025 * position query gets the right idea. We do this before we post the
5026 * messages so that the message handlers pick this up. */
5027 GST_OBJECT_LOCK (basesink);
5028 basesink->have_newsegment = FALSE;
5029 priv->current_sstart = GST_CLOCK_TIME_NONE;
5030 priv->current_sstop = GST_CLOCK_TIME_NONE;
5031 priv->have_latency = FALSE;
5032 if (priv->cached_clock_id) {
5033 gst_clock_id_unref (priv->cached_clock_id);
5034 priv->cached_clock_id = NULL;
5036 GST_OBJECT_UNLOCK (basesink);
5038 gst_base_sink_set_last_buffer (basesink, NULL);
5039 priv->call_preroll = FALSE;
5041 if (!priv->commited) {
5042 if (priv->async_enabled) {
5043 GST_DEBUG_OBJECT (basesink, "PAUSED to READY, posting async-done");
5045 gst_element_post_message (GST_ELEMENT_CAST (basesink),
5046 gst_message_new_state_changed (GST_OBJECT_CAST (basesink),
5047 GST_STATE_PLAYING, GST_STATE_PAUSED, GST_STATE_READY));
5049 gst_element_post_message (GST_ELEMENT_CAST (basesink),
5050 gst_message_new_async_done (GST_OBJECT_CAST (basesink)));
5052 priv->commited = TRUE;
5054 GST_DEBUG_OBJECT (basesink, "PAUSED to READY, don't need_preroll");
5056 GST_PAD_PREROLL_UNLOCK (basesink->sinkpad);
5058 case GST_STATE_CHANGE_READY_TO_NULL:
5060 if (!bclass->stop (basesink)) {
5061 GST_WARNING_OBJECT (basesink, "failed to stop");
5064 gst_base_sink_set_last_buffer (basesink, NULL);
5065 priv->call_preroll = FALSE;
5076 GST_DEBUG_OBJECT (basesink, "failed to start");
5077 return GST_STATE_CHANGE_FAILURE;
5081 GST_DEBUG_OBJECT (basesink,
5082 "element failed to change states -- activation problem?");
5083 return GST_STATE_CHANGE_FAILURE;