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, GstQOSType type,
2594 gdouble proportion, GstClockTime time, GstClockTimeDiff diff)
2599 /* generate Quality-of-Service event */
2600 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, basesink,
2601 "qos: type %d, proportion: %lf, diff %" G_GINT64_FORMAT ", timestamp %"
2602 GST_TIME_FORMAT, type, proportion, diff, GST_TIME_ARGS (time));
2604 event = gst_event_new_qos_full (type, 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) {
2719 GstClockTimeDiff diff;
2721 /* if we have a valid rate, start sending QoS messages */
2722 if (priv->current_jitter < 0) {
2723 /* make sure we never go below 0 when adding the jitter to the
2725 if (priv->current_rstart < -priv->current_jitter)
2726 priv->current_jitter = -priv->current_rstart;
2729 if (priv->throttle_time > 0) {
2730 diff = priv->throttle_time;
2731 type = GST_QOS_TYPE_THROTTLE;
2733 diff = priv->current_jitter;
2735 type = GST_QOS_TYPE_OVERFLOW;
2737 type = GST_QOS_TYPE_UNDERFLOW;
2740 gst_base_sink_send_qos (sink, type, priv->avg_rate, priv->current_rstart,
2744 /* record when this buffer will leave us */
2745 priv->last_left = left;
2748 /* reset all qos measuring */
2750 gst_base_sink_reset_qos (GstBaseSink * sink)
2752 GstBaseSinkPrivate *priv;
2756 priv->last_in_time = GST_CLOCK_TIME_NONE;
2757 priv->last_left = GST_CLOCK_TIME_NONE;
2758 priv->avg_duration = GST_CLOCK_TIME_NONE;
2759 priv->avg_pt = GST_CLOCK_TIME_NONE;
2760 priv->avg_rate = -1.0;
2761 priv->avg_render = GST_CLOCK_TIME_NONE;
2767 /* Checks if the object was scheduled too late.
2769 * start/stop contain the raw timestamp start and stop values
2772 * status and jitter contain the return values from the clock wait.
2774 * returns TRUE if the buffer was too late.
2777 gst_base_sink_is_too_late (GstBaseSink * basesink, GstMiniObject * obj,
2778 GstClockTime start, GstClockTime stop,
2779 GstClockReturn status, GstClockTimeDiff jitter)
2782 gint64 max_lateness;
2783 GstBaseSinkPrivate *priv;
2785 priv = basesink->priv;
2789 /* only for objects that were too late */
2790 if (G_LIKELY (status != GST_CLOCK_EARLY))
2793 max_lateness = basesink->abidata.ABI.max_lateness;
2795 /* check if frame dropping is enabled */
2796 if (max_lateness == -1)
2799 /* only check for buffers */
2800 if (G_UNLIKELY (!GST_IS_BUFFER (obj)))
2803 /* can't do check if we don't have a timestamp */
2804 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (start)))
2807 /* we can add a valid stop time */
2808 if (GST_CLOCK_TIME_IS_VALID (stop))
2809 max_lateness += stop;
2811 max_lateness += start;
2813 /* if the jitter bigger than duration and lateness we are too late */
2814 if ((late = start + jitter > max_lateness)) {
2815 GST_CAT_DEBUG_OBJECT (GST_CAT_PERFORMANCE, basesink,
2816 "buffer is too late %" GST_TIME_FORMAT
2817 " > %" GST_TIME_FORMAT, GST_TIME_ARGS (start + jitter),
2818 GST_TIME_ARGS (max_lateness));
2819 /* !!emergency!!, if we did not receive anything valid for more than a
2820 * second, render it anyway so the user sees something */
2821 if (GST_CLOCK_TIME_IS_VALID (priv->last_in_time) &&
2822 start - priv->last_in_time > GST_SECOND) {
2824 GST_ELEMENT_WARNING (basesink, CORE, CLOCK,
2825 (_("A lot of buffers are being dropped.")),
2826 ("There may be a timestamping problem, or this computer is too slow."));
2827 GST_CAT_DEBUG_OBJECT (GST_CAT_PERFORMANCE, basesink,
2828 "**emergency** last buffer at %" GST_TIME_FORMAT " > GST_SECOND",
2829 GST_TIME_ARGS (priv->last_in_time));
2834 if (!late || !GST_CLOCK_TIME_IS_VALID (priv->last_in_time)) {
2835 priv->last_in_time = start;
2842 GST_DEBUG_OBJECT (basesink, "object was scheduled in time");
2847 GST_DEBUG_OBJECT (basesink, "frame dropping disabled");
2852 GST_DEBUG_OBJECT (basesink, "object is not a buffer");
2857 GST_DEBUG_OBJECT (basesink, "buffer has no timestamp");
2862 /* called before and after calling the render vmethod. It keeps track of how
2863 * much time was spent in the render method and is used to check if we are
2866 gst_base_sink_do_render_stats (GstBaseSink * basesink, gboolean start)
2868 GstBaseSinkPrivate *priv;
2870 priv = basesink->priv;
2873 priv->start = gst_util_get_timestamp ();
2875 GstClockTime elapsed;
2877 priv->stop = gst_util_get_timestamp ();
2879 elapsed = GST_CLOCK_DIFF (priv->start, priv->stop);
2881 if (!GST_CLOCK_TIME_IS_VALID (priv->avg_render))
2882 priv->avg_render = elapsed;
2884 priv->avg_render = UPDATE_RUNNING_AVG (priv->avg_render, elapsed);
2886 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, basesink,
2887 "avg_render: %" GST_TIME_FORMAT, GST_TIME_ARGS (priv->avg_render));
2891 /* with STREAM_LOCK, PREROLL_LOCK,
2893 * Synchronize the object on the clock and then render it.
2895 * takes ownership of obj.
2897 static GstFlowReturn
2898 gst_base_sink_render_object (GstBaseSink * basesink, GstPad * pad,
2899 guint8 obj_type, gpointer obj)
2902 GstBaseSinkClass *bclass;
2903 gboolean late, step_end;
2905 GstBaseSinkPrivate *priv;
2907 priv = basesink->priv;
2909 if (OBJ_IS_BUFFERLIST (obj_type)) {
2911 * If buffer list, use the first group buffer within the list
2914 sync_obj = gst_buffer_list_get (GST_BUFFER_LIST_CAST (obj), 0, 0);
2915 g_assert (NULL != sync_obj);
2924 /* synchronize this object, non syncable objects return OK
2927 gst_base_sink_do_sync (basesink, pad, sync_obj, &late, &step_end,
2929 if (G_UNLIKELY (ret != GST_FLOW_OK))
2932 /* and now render, event or buffer/buffer list. */
2933 if (G_LIKELY (OBJ_IS_BUFFERFULL (obj_type))) {
2934 /* drop late buffers unconditionally, let's hope it's unlikely */
2935 if (G_UNLIKELY (late))
2938 bclass = GST_BASE_SINK_GET_CLASS (basesink);
2940 if (G_LIKELY ((OBJ_IS_BUFFERLIST (obj_type) && bclass->render_list) ||
2941 (!OBJ_IS_BUFFERLIST (obj_type) && bclass->render))) {
2944 /* read once, to get same value before and after */
2945 do_qos = g_atomic_int_get (&priv->qos_enabled);
2947 GST_DEBUG_OBJECT (basesink, "rendering object %p", obj);
2949 /* record rendering time for QoS and stats */
2951 gst_base_sink_do_render_stats (basesink, TRUE);
2953 if (!OBJ_IS_BUFFERLIST (obj_type)) {
2956 /* For buffer lists do not set last buffer. Creating buffer
2957 * with meaningful data can be done only with memcpy which will
2958 * significantly affect performance */
2959 buf = GST_BUFFER_CAST (obj);
2960 gst_base_sink_set_last_buffer (basesink, buf);
2962 ret = bclass->render (basesink, buf);
2964 GstBufferList *buflist;
2966 buflist = GST_BUFFER_LIST_CAST (obj);
2968 ret = bclass->render_list (basesink, buflist);
2972 gst_base_sink_do_render_stats (basesink, FALSE);
2974 if (ret == GST_FLOW_STEP)
2977 if (G_UNLIKELY (basesink->flushing))
2982 } else if (G_LIKELY (OBJ_IS_EVENT (obj_type))) {
2983 GstEvent *event = GST_EVENT_CAST (obj);
2984 gboolean event_res = TRUE;
2987 bclass = GST_BASE_SINK_GET_CLASS (basesink);
2989 type = GST_EVENT_TYPE (event);
2991 GST_DEBUG_OBJECT (basesink, "rendering event %p, type %s", obj,
2992 gst_event_type_get_name (type));
2995 event_res = bclass->event (basesink, event);
2997 /* when we get here we could be flushing again when the event handler calls
2998 * _wait_eos(). We have to ignore this object in that case. */
2999 if (G_UNLIKELY (basesink->flushing))
3002 if (G_LIKELY (event_res)) {
3005 seqnum = basesink->priv->seqnum = gst_event_get_seqnum (event);
3006 GST_DEBUG_OBJECT (basesink, "Got seqnum #%" G_GUINT32_FORMAT, seqnum);
3011 GstMessage *message;
3013 /* the EOS event is completely handled so we mark
3014 * ourselves as being in the EOS state. eos is also
3015 * protected by the object lock so we can read it when
3016 * answering the POSITION query. */
3017 GST_OBJECT_LOCK (basesink);
3018 basesink->eos = TRUE;
3019 GST_OBJECT_UNLOCK (basesink);
3021 /* ok, now we can post the message */
3022 GST_DEBUG_OBJECT (basesink, "Now posting EOS");
3024 message = gst_message_new_eos (GST_OBJECT_CAST (basesink));
3025 gst_message_set_seqnum (message, seqnum);
3026 gst_element_post_message (GST_ELEMENT_CAST (basesink), message);
3029 case GST_EVENT_NEWSEGMENT:
3030 /* configure the segment */
3031 gst_base_sink_configure_segment (basesink, pad, event,
3032 &basesink->segment);
3034 case GST_EVENT_SINK_MESSAGE:{
3035 GstMessage *msg = NULL;
3037 gst_event_parse_sink_message (event, &msg);
3040 gst_element_post_message (GST_ELEMENT_CAST (basesink), msg);
3047 g_return_val_if_reached (GST_FLOW_ERROR);
3052 /* the step ended, check if we need to activate a new step */
3053 GST_DEBUG_OBJECT (basesink, "step ended");
3054 stop_stepping (basesink, &basesink->segment, &priv->current_step,
3055 priv->current_rstart, priv->current_rstop, basesink->eos);
3059 gst_base_sink_perform_qos (basesink, late);
3061 GST_DEBUG_OBJECT (basesink, "object unref after render %p", obj);
3062 gst_mini_object_unref (GST_MINI_OBJECT_CAST (obj));
3068 GST_DEBUG_OBJECT (basesink, "do_sync returned %s", gst_flow_get_name (ret));
3074 GST_DEBUG_OBJECT (basesink, "buffer late, dropping");
3076 if (g_atomic_int_get (&priv->qos_enabled)) {
3077 GstMessage *qos_msg;
3078 GstClockTime timestamp, duration;
3080 timestamp = GST_BUFFER_TIMESTAMP (GST_BUFFER_CAST (sync_obj));
3081 duration = GST_BUFFER_DURATION (GST_BUFFER_CAST (sync_obj));
3083 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, basesink,
3084 "qos: dropped buffer rt %" GST_TIME_FORMAT ", st %" GST_TIME_FORMAT
3085 ", ts %" GST_TIME_FORMAT ", dur %" GST_TIME_FORMAT,
3086 GST_TIME_ARGS (priv->current_rstart),
3087 GST_TIME_ARGS (priv->current_sstart), GST_TIME_ARGS (timestamp),
3088 GST_TIME_ARGS (duration));
3089 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, basesink,
3090 "qos: rendered %" G_GUINT64_FORMAT ", dropped %" G_GUINT64_FORMAT,
3091 priv->rendered, priv->dropped);
3094 gst_message_new_qos (GST_OBJECT_CAST (basesink), basesink->sync,
3095 priv->current_rstart, priv->current_sstart, timestamp, duration);
3096 gst_message_set_qos_values (qos_msg, priv->current_jitter, priv->avg_rate,
3098 gst_message_set_qos_stats (qos_msg, GST_FORMAT_BUFFERS, priv->rendered,
3100 gst_element_post_message (GST_ELEMENT_CAST (basesink), qos_msg);
3106 GST_DEBUG_OBJECT (basesink, "we are flushing, ignore object");
3107 gst_mini_object_unref (obj);
3108 return GST_FLOW_WRONG_STATE;
3112 /* with STREAM_LOCK, PREROLL_LOCK
3114 * Perform preroll on the given object. For buffers this means
3115 * calling the preroll subclass method.
3116 * If that succeeds, the state will be commited.
3118 * function does not take ownership of obj.
3120 static GstFlowReturn
3121 gst_base_sink_preroll_object (GstBaseSink * basesink, guint8 obj_type,
3122 GstMiniObject * obj)
3126 GST_DEBUG_OBJECT (basesink, "prerolling object %p", obj);
3128 /* if it's a buffer, we need to call the preroll method */
3129 if (G_LIKELY (OBJ_IS_BUFFERFULL (obj_type) && basesink->priv->call_preroll)) {
3130 GstBaseSinkClass *bclass;
3132 GstClockTime timestamp;
3134 if (OBJ_IS_BUFFERLIST (obj_type)) {
3135 buf = gst_buffer_list_get (GST_BUFFER_LIST_CAST (obj), 0, 0);
3136 g_assert (NULL != buf);
3138 buf = GST_BUFFER_CAST (obj);
3141 timestamp = GST_BUFFER_TIMESTAMP (buf);
3143 GST_DEBUG_OBJECT (basesink, "preroll buffer %" GST_TIME_FORMAT,
3144 GST_TIME_ARGS (timestamp));
3147 * For buffer lists do not set last buffer. Creating buffer
3148 * with meaningful data can be done only with memcpy which will
3149 * significantly affect performance
3151 if (!OBJ_IS_BUFFERLIST (obj_type)) {
3152 gst_base_sink_set_last_buffer (basesink, buf);
3155 bclass = GST_BASE_SINK_GET_CLASS (basesink);
3156 if (bclass->preroll)
3157 if ((ret = bclass->preroll (basesink, buf)) != GST_FLOW_OK)
3158 goto preroll_failed;
3160 basesink->priv->call_preroll = FALSE;
3164 if (G_LIKELY (basesink->playing_async)) {
3165 if (G_UNLIKELY (!gst_base_sink_commit_state (basesink)))
3174 GST_DEBUG_OBJECT (basesink, "preroll failed, abort state");
3175 gst_element_abort_state (GST_ELEMENT_CAST (basesink));
3180 GST_DEBUG_OBJECT (basesink, "stopping while commiting state");
3181 return GST_FLOW_WRONG_STATE;
3185 /* with STREAM_LOCK, PREROLL_LOCK
3187 * Queue an object for rendering.
3188 * The first prerollable object queued will complete the preroll. If the
3189 * preroll queue if filled, we render all the objects in the queue.
3191 * This function takes ownership of the object.
3193 static GstFlowReturn
3194 gst_base_sink_queue_object_unlocked (GstBaseSink * basesink, GstPad * pad,
3195 guint8 obj_type, gpointer obj, gboolean prerollable)
3197 GstFlowReturn ret = GST_FLOW_OK;
3201 if (G_UNLIKELY (basesink->need_preroll)) {
3202 if (G_LIKELY (prerollable))
3203 basesink->preroll_queued++;
3205 length = basesink->preroll_queued;
3207 GST_DEBUG_OBJECT (basesink, "now %d prerolled items", length);
3209 /* first prerollable item needs to finish the preroll */
3211 ret = gst_base_sink_preroll_object (basesink, obj_type, obj);
3212 if (G_UNLIKELY (ret != GST_FLOW_OK))
3213 goto preroll_failed;
3215 /* need to recheck if we need preroll, commmit state during preroll
3216 * could have made us not need more preroll. */
3217 if (G_UNLIKELY (basesink->need_preroll)) {
3218 /* see if we can render now, if we can't add the object to the preroll
3220 if (G_UNLIKELY (length <= basesink->preroll_queue_max_len))
3224 /* we can start rendering (or blocking) the queued object
3226 q = basesink->preroll_queue;
3227 while (G_UNLIKELY (!g_queue_is_empty (q))) {
3231 o = g_queue_pop_head (q);
3232 GST_DEBUG_OBJECT (basesink, "rendering queued object %p", o);
3234 ot = get_object_type (o);
3236 /* do something with the return value */
3237 ret = gst_base_sink_render_object (basesink, pad, ot, o);
3238 if (ret != GST_FLOW_OK)
3239 goto dequeue_failed;
3242 /* now render the object */
3243 ret = gst_base_sink_render_object (basesink, pad, obj_type, obj);
3244 basesink->preroll_queued = 0;
3251 GST_DEBUG_OBJECT (basesink, "preroll failed, reason %s",
3252 gst_flow_get_name (ret));
3253 gst_mini_object_unref (GST_MINI_OBJECT_CAST (obj));
3258 /* add object to the queue and return */
3259 GST_DEBUG_OBJECT (basesink, "need more preroll data %d <= %d",
3260 length, basesink->preroll_queue_max_len);
3261 g_queue_push_tail (basesink->preroll_queue, obj);
3266 GST_DEBUG_OBJECT (basesink, "rendering queued objects failed, reason %s",
3267 gst_flow_get_name (ret));
3268 gst_mini_object_unref (GST_MINI_OBJECT_CAST (obj));
3275 * This function grabs the PREROLL_LOCK and adds the object to
3278 * This function takes ownership of obj.
3280 * Note: Only GstEvent seem to be passed to this private method
3282 static GstFlowReturn
3283 gst_base_sink_queue_object (GstBaseSink * basesink, GstPad * pad,
3284 GstMiniObject * obj, gboolean prerollable)
3288 GST_PAD_PREROLL_LOCK (pad);
3289 if (G_UNLIKELY (basesink->flushing))
3292 if (G_UNLIKELY (basesink->priv->received_eos))
3296 gst_base_sink_queue_object_unlocked (basesink, pad, _PR_IS_EVENT, obj,
3298 GST_PAD_PREROLL_UNLOCK (pad);
3305 GST_DEBUG_OBJECT (basesink, "sink is flushing");
3306 GST_PAD_PREROLL_UNLOCK (pad);
3307 gst_mini_object_unref (obj);
3308 return GST_FLOW_WRONG_STATE;
3312 GST_DEBUG_OBJECT (basesink,
3313 "we are EOS, dropping object, return UNEXPECTED");
3314 GST_PAD_PREROLL_UNLOCK (pad);
3315 gst_mini_object_unref (obj);
3316 return GST_FLOW_UNEXPECTED;
3321 gst_base_sink_flush_start (GstBaseSink * basesink, GstPad * pad)
3323 /* make sure we are not blocked on the clock also clear any pending
3325 gst_base_sink_set_flushing (basesink, pad, TRUE);
3327 /* we grab the stream lock but that is not needed since setting the
3328 * sink to flushing would make sure no state commit is being done
3330 GST_PAD_STREAM_LOCK (pad);
3331 gst_base_sink_reset_qos (basesink);
3332 /* and we need to commit our state again on the next
3333 * prerolled buffer */
3334 basesink->playing_async = TRUE;
3335 if (basesink->priv->async_enabled) {
3336 gst_element_lost_state (GST_ELEMENT_CAST (basesink));
3338 basesink->priv->have_latency = TRUE;
3340 gst_base_sink_set_last_buffer (basesink, NULL);
3341 GST_PAD_STREAM_UNLOCK (pad);
3345 gst_base_sink_flush_stop (GstBaseSink * basesink, GstPad * pad)
3347 /* unset flushing so we can accept new data, this also flushes out any EOS
3349 gst_base_sink_set_flushing (basesink, pad, FALSE);
3351 /* for position reporting */
3352 GST_OBJECT_LOCK (basesink);
3353 basesink->priv->current_sstart = GST_CLOCK_TIME_NONE;
3354 basesink->priv->current_sstop = GST_CLOCK_TIME_NONE;
3355 basesink->priv->eos_rtime = GST_CLOCK_TIME_NONE;
3356 basesink->priv->call_preroll = TRUE;
3357 basesink->priv->current_step.valid = FALSE;
3358 basesink->priv->pending_step.valid = FALSE;
3359 if (basesink->pad_mode == GST_ACTIVATE_PUSH) {
3360 /* we need new segment info after the flush. */
3361 basesink->have_newsegment = FALSE;
3362 gst_segment_init (&basesink->segment, GST_FORMAT_UNDEFINED);
3363 gst_segment_init (basesink->abidata.ABI.clip_segment, GST_FORMAT_UNDEFINED);
3365 GST_OBJECT_UNLOCK (basesink);
3369 gst_base_sink_event (GstPad * pad, GstEvent * event)
3371 GstBaseSink *basesink;
3372 gboolean result = TRUE;
3373 GstBaseSinkClass *bclass;
3375 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
3377 bclass = GST_BASE_SINK_GET_CLASS (basesink);
3379 GST_DEBUG_OBJECT (basesink, "received event %p %" GST_PTR_FORMAT, event,
3382 switch (GST_EVENT_TYPE (event)) {
3387 GST_PAD_PREROLL_LOCK (pad);
3388 if (G_UNLIKELY (basesink->flushing))
3391 if (G_UNLIKELY (basesink->priv->received_eos)) {
3392 /* we can't accept anything when we are EOS */
3394 gst_event_unref (event);
3396 /* we set the received EOS flag here so that we can use it when testing if
3397 * we are prerolled and to refuse more buffers. */
3398 basesink->priv->received_eos = TRUE;
3400 /* EOS is a prerollable object, we call the unlocked version because it
3401 * does not check the received_eos flag. */
3402 ret = gst_base_sink_queue_object_unlocked (basesink, pad,
3403 _PR_IS_EVENT, GST_MINI_OBJECT_CAST (event), TRUE);
3404 if (G_UNLIKELY (ret != GST_FLOW_OK))
3407 GST_PAD_PREROLL_UNLOCK (pad);
3410 case GST_EVENT_NEWSEGMENT:
3415 GST_DEBUG_OBJECT (basesink, "newsegment %p", event);
3417 GST_PAD_PREROLL_LOCK (pad);
3418 if (G_UNLIKELY (basesink->flushing))
3421 gst_event_parse_new_segment_full (event, &update, NULL, NULL, NULL, NULL,
3424 if (G_UNLIKELY (basesink->priv->received_eos && !update)) {
3425 /* we can't accept anything when we are EOS */
3427 gst_event_unref (event);
3429 /* the new segment is a non prerollable item and does not block anything,
3430 * we need to configure the current clipping segment and insert the event
3431 * in the queue to serialize it with the buffers for rendering. */
3432 gst_base_sink_configure_segment (basesink, pad, event,
3433 basesink->abidata.ABI.clip_segment);
3436 gst_base_sink_queue_object_unlocked (basesink, pad,
3437 _PR_IS_EVENT, GST_MINI_OBJECT_CAST (event), FALSE);
3438 if (G_UNLIKELY (ret != GST_FLOW_OK))
3441 GST_OBJECT_LOCK (basesink);
3442 basesink->have_newsegment = TRUE;
3443 GST_OBJECT_UNLOCK (basesink);
3446 GST_PAD_PREROLL_UNLOCK (pad);
3449 case GST_EVENT_FLUSH_START:
3451 bclass->event (basesink, event);
3453 GST_DEBUG_OBJECT (basesink, "flush-start %p", event);
3455 gst_base_sink_flush_start (basesink, pad);
3457 gst_event_unref (event);
3459 case GST_EVENT_FLUSH_STOP:
3461 bclass->event (basesink, event);
3463 GST_DEBUG_OBJECT (basesink, "flush-stop %p", event);
3465 gst_base_sink_flush_stop (basesink, pad);
3467 gst_event_unref (event);
3470 /* other events are sent to queue or subclass depending on if they
3471 * are serialized. */
3472 if (GST_EVENT_IS_SERIALIZED (event)) {
3473 gst_base_sink_queue_object (basesink, pad,
3474 GST_MINI_OBJECT_CAST (event), FALSE);
3477 bclass->event (basesink, event);
3478 gst_event_unref (event);
3483 gst_object_unref (basesink);
3490 GST_DEBUG_OBJECT (basesink, "we are flushing");
3491 GST_PAD_PREROLL_UNLOCK (pad);
3493 gst_event_unref (event);
3498 /* default implementation to calculate the start and end
3499 * timestamps on a buffer, subclasses can override
3502 gst_base_sink_get_times (GstBaseSink * basesink, GstBuffer * buffer,
3503 GstClockTime * start, GstClockTime * end)
3505 GstClockTime timestamp, duration;
3507 timestamp = GST_BUFFER_TIMESTAMP (buffer);
3508 if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
3510 /* get duration to calculate end time */
3511 duration = GST_BUFFER_DURATION (buffer);
3512 if (GST_CLOCK_TIME_IS_VALID (duration)) {
3513 *end = timestamp + duration;
3519 /* must be called with PREROLL_LOCK */
3521 gst_base_sink_needs_preroll (GstBaseSink * basesink)
3523 gboolean is_prerolled, res;
3525 /* we have 2 cases where the PREROLL_LOCK is released:
3526 * 1) we are blocking in the PREROLL_LOCK and thus are prerolled.
3527 * 2) we are syncing on the clock
3529 is_prerolled = basesink->have_preroll || basesink->priv->received_eos;
3530 res = !is_prerolled;
3532 GST_DEBUG_OBJECT (basesink, "have_preroll: %d, EOS: %d => needs preroll: %d",
3533 basesink->have_preroll, basesink->priv->received_eos, res);
3538 /* with STREAM_LOCK, PREROLL_LOCK
3540 * Takes a buffer and compare the timestamps with the last segment.
3541 * If the buffer falls outside of the segment boundaries, drop it.
3542 * Else queue the buffer for preroll and rendering.
3544 * This function takes ownership of the buffer.
3546 static GstFlowReturn
3547 gst_base_sink_chain_unlocked (GstBaseSink * basesink, GstPad * pad,
3548 guint8 obj_type, gpointer obj)
3550 GstBaseSinkClass *bclass;
3551 GstFlowReturn result;
3552 GstClockTime start = GST_CLOCK_TIME_NONE, end = GST_CLOCK_TIME_NONE;
3553 GstSegment *clip_segment;
3554 GstBuffer *time_buf;
3556 if (G_UNLIKELY (basesink->flushing))
3559 if (G_UNLIKELY (basesink->priv->received_eos))
3562 if (OBJ_IS_BUFFERLIST (obj_type)) {
3563 time_buf = gst_buffer_list_get (GST_BUFFER_LIST_CAST (obj), 0, 0);
3564 g_assert (NULL != time_buf);
3566 time_buf = GST_BUFFER_CAST (obj);
3569 /* for code clarity */
3570 clip_segment = basesink->abidata.ABI.clip_segment;
3572 if (G_UNLIKELY (!basesink->have_newsegment)) {
3575 sync = gst_base_sink_get_sync (basesink);
3577 GST_ELEMENT_WARNING (basesink, STREAM, FAILED,
3578 (_("Internal data flow problem.")),
3579 ("Received buffer without a new-segment. Assuming timestamps start from 0."));
3582 /* this means this sink will assume timestamps start from 0 */
3583 GST_OBJECT_LOCK (basesink);
3584 clip_segment->start = 0;
3585 clip_segment->stop = -1;
3586 basesink->segment.start = 0;
3587 basesink->segment.stop = -1;
3588 basesink->have_newsegment = TRUE;
3589 GST_OBJECT_UNLOCK (basesink);
3592 bclass = GST_BASE_SINK_GET_CLASS (basesink);
3594 /* check if the buffer needs to be dropped, we first ask the subclass for the
3596 if (bclass->get_times)
3597 bclass->get_times (basesink, time_buf, &start, &end);
3599 if (!GST_CLOCK_TIME_IS_VALID (start)) {
3600 /* if the subclass does not want sync, we use our own values so that we at
3601 * least clip the buffer to the segment */
3602 gst_base_sink_get_times (basesink, time_buf, &start, &end);
3605 GST_DEBUG_OBJECT (basesink, "got times start: %" GST_TIME_FORMAT
3606 ", end: %" GST_TIME_FORMAT, GST_TIME_ARGS (start), GST_TIME_ARGS (end));
3608 /* a dropped buffer does not participate in anything */
3609 if (GST_CLOCK_TIME_IS_VALID (start) &&
3610 (clip_segment->format == GST_FORMAT_TIME)) {
3611 if (G_UNLIKELY (!gst_segment_clip (clip_segment,
3612 GST_FORMAT_TIME, (gint64) start, (gint64) end, NULL, NULL)))
3613 goto out_of_segment;
3616 /* now we can process the buffer in the queue, this function takes ownership
3618 result = gst_base_sink_queue_object_unlocked (basesink, pad,
3619 obj_type, obj, TRUE);
3625 GST_DEBUG_OBJECT (basesink, "sink is flushing");
3626 gst_mini_object_unref (GST_MINI_OBJECT_CAST (obj));
3627 return GST_FLOW_WRONG_STATE;
3631 GST_DEBUG_OBJECT (basesink,
3632 "we are EOS, dropping object, return UNEXPECTED");
3633 gst_mini_object_unref (GST_MINI_OBJECT_CAST (obj));
3634 return GST_FLOW_UNEXPECTED;
3638 GST_DEBUG_OBJECT (basesink, "dropping buffer, out of clipping segment");
3639 gst_mini_object_unref (GST_MINI_OBJECT_CAST (obj));
3646 static GstFlowReturn
3647 gst_base_sink_chain_main (GstBaseSink * basesink, GstPad * pad,
3648 guint8 obj_type, gpointer obj)
3650 GstFlowReturn result;
3652 if (G_UNLIKELY (basesink->pad_mode != GST_ACTIVATE_PUSH))
3655 GST_PAD_PREROLL_LOCK (pad);
3656 result = gst_base_sink_chain_unlocked (basesink, pad, obj_type, obj);
3657 GST_PAD_PREROLL_UNLOCK (pad);
3665 GST_OBJECT_LOCK (pad);
3666 GST_WARNING_OBJECT (basesink,
3667 "Push on pad %s:%s, but it was not activated in push mode",
3668 GST_DEBUG_PAD_NAME (pad));
3669 GST_OBJECT_UNLOCK (pad);
3670 gst_mini_object_unref (GST_MINI_OBJECT_CAST (obj));
3671 /* we don't post an error message this will signal to the peer
3672 * pushing that EOS is reached. */
3673 result = GST_FLOW_UNEXPECTED;
3678 static GstFlowReturn
3679 gst_base_sink_chain (GstPad * pad, GstBuffer * buf)
3681 GstBaseSink *basesink;
3683 basesink = GST_BASE_SINK (GST_OBJECT_PARENT (pad));
3685 return gst_base_sink_chain_main (basesink, pad, _PR_IS_BUFFER, buf);
3688 static GstFlowReturn
3689 gst_base_sink_chain_list (GstPad * pad, GstBufferList * list)
3691 GstBaseSink *basesink;
3692 GstBaseSinkClass *bclass;
3693 GstFlowReturn result;
3695 basesink = GST_BASE_SINK (GST_OBJECT_PARENT (pad));
3696 bclass = GST_BASE_SINK_GET_CLASS (basesink);
3698 if (G_LIKELY (bclass->render_list)) {
3699 result = gst_base_sink_chain_main (basesink, pad, _PR_IS_BUFFERLIST, list);
3701 GstBufferListIterator *it;
3704 GST_INFO_OBJECT (pad, "chaining each group in list as a merged buffer");
3706 it = gst_buffer_list_iterate (list);
3708 if (gst_buffer_list_iterator_next_group (it)) {
3710 group = gst_buffer_list_iterator_merge_group (it);
3711 if (group == NULL) {
3712 group = gst_buffer_new ();
3713 GST_CAT_INFO_OBJECT (GST_CAT_SCHEDULING, pad, "chaining empty group");
3715 GST_CAT_INFO_OBJECT (GST_CAT_SCHEDULING, pad, "chaining group");
3717 result = gst_base_sink_chain_main (basesink, pad, _PR_IS_BUFFER, group);
3718 } while (result == GST_FLOW_OK
3719 && gst_buffer_list_iterator_next_group (it));
3721 GST_CAT_INFO_OBJECT (GST_CAT_SCHEDULING, pad, "chaining empty group");
3723 gst_base_sink_chain_main (basesink, pad, _PR_IS_BUFFER,
3726 gst_buffer_list_iterator_free (it);
3727 gst_buffer_list_unref (list);
3734 gst_base_sink_default_do_seek (GstBaseSink * sink, GstSegment * segment)
3736 gboolean res = TRUE;
3738 /* update our offset if the start/stop position was updated */
3739 if (segment->format == GST_FORMAT_BYTES) {
3740 segment->time = segment->start;
3741 } else if (segment->start == 0) {
3742 /* seek to start, we can implement a default for this. */
3746 GST_INFO_OBJECT (sink, "Can't do a default seek");
3752 #define SEEK_TYPE_IS_RELATIVE(t) (((t) != GST_SEEK_TYPE_NONE) && ((t) != GST_SEEK_TYPE_SET))
3755 gst_base_sink_default_prepare_seek_segment (GstBaseSink * sink,
3756 GstEvent * event, GstSegment * segment)
3758 /* By default, we try one of 2 things:
3759 * - For absolute seek positions, convert the requested position to our
3760 * configured processing format and place it in the output segment \
3761 * - For relative seek positions, convert our current (input) values to the
3762 * seek format, adjust by the relative seek offset and then convert back to
3763 * the processing format
3765 GstSeekType cur_type, stop_type;
3768 GstFormat seek_format, dest_format;
3771 gboolean res = TRUE;
3773 gst_event_parse_seek (event, &rate, &seek_format, &flags,
3774 &cur_type, &cur, &stop_type, &stop);
3775 dest_format = segment->format;
3777 if (seek_format == dest_format) {
3778 gst_segment_set_seek (segment, rate, seek_format, flags,
3779 cur_type, cur, stop_type, stop, &update);
3783 if (cur_type != GST_SEEK_TYPE_NONE) {
3784 /* FIXME: Handle seek_cur & seek_end by converting the input segment vals */
3786 gst_pad_query_convert (sink->sinkpad, seek_format, cur, &dest_format,
3788 cur_type = GST_SEEK_TYPE_SET;
3791 if (res && stop_type != GST_SEEK_TYPE_NONE) {
3792 /* FIXME: Handle seek_cur & seek_end by converting the input segment vals */
3794 gst_pad_query_convert (sink->sinkpad, seek_format, stop, &dest_format,
3796 stop_type = GST_SEEK_TYPE_SET;
3799 /* And finally, configure our output segment in the desired format */
3800 gst_segment_set_seek (segment, rate, dest_format, flags, cur_type, cur,
3801 stop_type, stop, &update);
3810 GST_DEBUG_OBJECT (sink, "undefined format given, seek aborted.");
3815 /* perform a seek, only executed in pull mode */
3817 gst_base_sink_perform_seek (GstBaseSink * sink, GstPad * pad, GstEvent * event)
3821 GstFormat seek_format, dest_format;
3823 GstSeekType cur_type, stop_type;
3824 gboolean seekseg_configured = FALSE;
3826 gboolean update, res = TRUE;
3827 GstSegment seeksegment;
3829 dest_format = sink->segment.format;
3832 GST_DEBUG_OBJECT (sink, "performing seek with event %p", event);
3833 gst_event_parse_seek (event, &rate, &seek_format, &flags,
3834 &cur_type, &cur, &stop_type, &stop);
3836 flush = flags & GST_SEEK_FLAG_FLUSH;
3838 GST_DEBUG_OBJECT (sink, "performing seek without event");
3843 GST_DEBUG_OBJECT (sink, "flushing upstream");
3844 gst_pad_push_event (pad, gst_event_new_flush_start ());
3845 gst_base_sink_flush_start (sink, pad);
3847 GST_DEBUG_OBJECT (sink, "pausing pulling thread");
3850 GST_PAD_STREAM_LOCK (pad);
3852 /* If we configured the seeksegment above, don't overwrite it now. Otherwise
3853 * copy the current segment info into the temp segment that we can actually
3854 * attempt the seek with. We only update the real segment if the seek suceeds. */
3855 if (!seekseg_configured) {
3856 memcpy (&seeksegment, &sink->segment, sizeof (GstSegment));
3858 /* now configure the final seek segment */
3860 if (sink->segment.format != seek_format) {
3861 /* OK, here's where we give the subclass a chance to convert the relative
3862 * seek into an absolute one in the processing format. We set up any
3863 * absolute seek above, before taking the stream lock. */
3864 if (!gst_base_sink_default_prepare_seek_segment (sink, event,
3866 GST_DEBUG_OBJECT (sink,
3867 "Preparing the seek failed after flushing. " "Aborting seek");
3871 /* The seek format matches our processing format, no need to ask the
3872 * the subclass to configure the segment. */
3873 gst_segment_set_seek (&seeksegment, rate, seek_format, flags,
3874 cur_type, cur, stop_type, stop, &update);
3877 /* Else, no seek event passed, so we're just (re)starting the
3882 GST_DEBUG_OBJECT (sink, "segment configured from %" G_GINT64_FORMAT
3883 " to %" G_GINT64_FORMAT ", position %" G_GINT64_FORMAT,
3884 seeksegment.start, seeksegment.stop, seeksegment.last_stop);
3886 /* do the seek, segment.last_stop contains the new position. */
3887 res = gst_base_sink_default_do_seek (sink, &seeksegment);
3892 GST_DEBUG_OBJECT (sink, "stop flushing upstream");
3893 gst_pad_push_event (pad, gst_event_new_flush_stop ());
3894 gst_base_sink_flush_stop (sink, pad);
3895 } else if (res && sink->abidata.ABI.running) {
3896 /* we are running the current segment and doing a non-flushing seek,
3897 * close the segment first based on the last_stop. */
3898 GST_DEBUG_OBJECT (sink, "closing running segment %" G_GINT64_FORMAT
3899 " to %" G_GINT64_FORMAT, sink->segment.start, sink->segment.last_stop);
3902 /* The subclass must have converted the segment to the processing format
3904 if (res && seeksegment.format != dest_format) {
3905 GST_DEBUG_OBJECT (sink, "Subclass failed to prepare a seek segment "
3906 "in the correct format. Aborting seek.");
3910 /* if successfull seek, we update our real segment and push
3911 * out the new segment. */
3913 memcpy (&sink->segment, &seeksegment, sizeof (GstSegment));
3915 if (sink->segment.flags & GST_SEEK_FLAG_SEGMENT) {
3916 gst_element_post_message (GST_ELEMENT (sink),
3917 gst_message_new_segment_start (GST_OBJECT (sink),
3918 sink->segment.format, sink->segment.last_stop));
3922 sink->priv->discont = TRUE;
3923 sink->abidata.ABI.running = TRUE;
3925 GST_PAD_STREAM_UNLOCK (pad);
3931 set_step_info (GstBaseSink * sink, GstStepInfo * current, GstStepInfo * pending,
3932 guint seqnum, GstFormat format, guint64 amount, gdouble rate,
3933 gboolean flush, gboolean intermediate)
3935 GST_OBJECT_LOCK (sink);
3936 pending->seqnum = seqnum;
3937 pending->format = format;
3938 pending->amount = amount;
3939 pending->position = 0;
3940 pending->rate = rate;
3941 pending->flush = flush;
3942 pending->intermediate = intermediate;
3943 pending->valid = TRUE;
3944 /* flush invalidates the current stepping segment */
3946 current->valid = FALSE;
3947 GST_OBJECT_UNLOCK (sink);
3951 gst_base_sink_perform_step (GstBaseSink * sink, GstPad * pad, GstEvent * event)
3953 GstBaseSinkPrivate *priv;
3954 GstBaseSinkClass *bclass;
3955 gboolean flush, intermediate;
3960 GstStepInfo *pending, *current;
3961 GstMessage *message;
3963 bclass = GST_BASE_SINK_GET_CLASS (sink);
3966 GST_DEBUG_OBJECT (sink, "performing step with event %p", event);
3968 gst_event_parse_step (event, &format, &amount, &rate, &flush, &intermediate);
3969 seqnum = gst_event_get_seqnum (event);
3971 pending = &priv->pending_step;
3972 current = &priv->current_step;
3974 /* post message first */
3975 message = gst_message_new_step_start (GST_OBJECT (sink), FALSE, format,
3976 amount, rate, flush, intermediate);
3977 gst_message_set_seqnum (message, seqnum);
3978 gst_element_post_message (GST_ELEMENT (sink), message);
3981 /* we need to call ::unlock before locking PREROLL_LOCK
3982 * since we lock it before going into ::render */
3984 bclass->unlock (sink);
3986 GST_PAD_PREROLL_LOCK (sink->sinkpad);
3987 /* now that we have the PREROLL lock, clear our unlock request */
3988 if (bclass->unlock_stop)
3989 bclass->unlock_stop (sink);
3991 /* update the stepinfo and make it valid */
3992 set_step_info (sink, current, pending, seqnum, format, amount, rate, flush,
3995 if (sink->priv->async_enabled) {
3996 /* and we need to commit our state again on the next
3997 * prerolled buffer */
3998 sink->playing_async = TRUE;
3999 priv->pending_step.need_preroll = TRUE;
4000 sink->need_preroll = FALSE;
4001 gst_element_lost_state_full (GST_ELEMENT_CAST (sink), FALSE);
4003 sink->priv->have_latency = TRUE;
4004 sink->need_preroll = FALSE;
4006 priv->current_sstart = GST_CLOCK_TIME_NONE;
4007 priv->current_sstop = GST_CLOCK_TIME_NONE;
4008 priv->eos_rtime = GST_CLOCK_TIME_NONE;
4009 priv->call_preroll = TRUE;
4010 gst_base_sink_set_last_buffer (sink, NULL);
4011 gst_base_sink_reset_qos (sink);
4013 if (sink->clock_id) {
4014 gst_clock_id_unschedule (sink->clock_id);
4017 if (sink->have_preroll) {
4018 GST_DEBUG_OBJECT (sink, "signal waiter");
4019 priv->step_unlock = TRUE;
4020 GST_PAD_PREROLL_SIGNAL (sink->sinkpad);
4022 GST_PAD_PREROLL_UNLOCK (sink->sinkpad);
4024 /* update the stepinfo and make it valid */
4025 set_step_info (sink, current, pending, seqnum, format, amount, rate, flush,
4035 gst_base_sink_loop (GstPad * pad)
4037 GstBaseSink *basesink;
4038 GstBuffer *buf = NULL;
4039 GstFlowReturn result;
4043 basesink = GST_BASE_SINK (GST_OBJECT_PARENT (pad));
4045 g_assert (basesink->pad_mode == GST_ACTIVATE_PULL);
4047 if ((blocksize = basesink->priv->blocksize) == 0)
4050 offset = basesink->segment.last_stop;
4052 GST_DEBUG_OBJECT (basesink, "pulling %" G_GUINT64_FORMAT ", %u",
4055 result = gst_pad_pull_range (pad, offset, blocksize, &buf);
4056 if (G_UNLIKELY (result != GST_FLOW_OK))
4059 if (G_UNLIKELY (buf == NULL))
4062 offset += GST_BUFFER_SIZE (buf);
4064 gst_segment_set_last_stop (&basesink->segment, GST_FORMAT_BYTES, offset);
4066 GST_PAD_PREROLL_LOCK (pad);
4067 result = gst_base_sink_chain_unlocked (basesink, pad, _PR_IS_BUFFER, buf);
4068 GST_PAD_PREROLL_UNLOCK (pad);
4069 if (G_UNLIKELY (result != GST_FLOW_OK))
4077 GST_LOG_OBJECT (basesink, "pausing task, reason %s",
4078 gst_flow_get_name (result));
4079 gst_pad_pause_task (pad);
4080 if (result == GST_FLOW_UNEXPECTED) {
4081 /* perform EOS logic */
4082 if (basesink->segment.flags & GST_SEEK_FLAG_SEGMENT) {
4083 gst_element_post_message (GST_ELEMENT_CAST (basesink),
4084 gst_message_new_segment_done (GST_OBJECT_CAST (basesink),
4085 basesink->segment.format, basesink->segment.last_stop));
4087 gst_base_sink_event (pad, gst_event_new_eos ());
4089 } else if (result == GST_FLOW_NOT_LINKED || result <= GST_FLOW_UNEXPECTED) {
4090 /* for fatal errors we post an error message, post the error
4091 * first so the app knows about the error first.
4092 * wrong-state is not a fatal error because it happens due to
4093 * flushing and posting an error message in that case is the
4094 * wrong thing to do, e.g. when basesrc is doing a flushing
4096 GST_ELEMENT_ERROR (basesink, STREAM, FAILED,
4097 (_("Internal data stream error.")),
4098 ("stream stopped, reason %s", gst_flow_get_name (result)));
4099 gst_base_sink_event (pad, gst_event_new_eos ());
4105 GST_LOG_OBJECT (basesink, "no buffer, pausing");
4106 GST_ELEMENT_ERROR (basesink, STREAM, FAILED,
4107 (_("Internal data flow error.")), ("element returned NULL buffer"));
4108 result = GST_FLOW_ERROR;
4114 gst_base_sink_set_flushing (GstBaseSink * basesink, GstPad * pad,
4117 GstBaseSinkClass *bclass;
4119 bclass = GST_BASE_SINK_GET_CLASS (basesink);
4122 /* unlock any subclasses, we need to do this before grabbing the
4123 * PREROLL_LOCK since we hold this lock before going into ::render. */
4125 bclass->unlock (basesink);
4128 GST_PAD_PREROLL_LOCK (pad);
4129 basesink->flushing = flushing;
4131 /* step 1, now that we have the PREROLL lock, clear our unlock request */
4132 if (bclass->unlock_stop)
4133 bclass->unlock_stop (basesink);
4135 /* set need_preroll before we unblock the clock. If the clock is unblocked
4136 * before timing out, we can reuse the buffer for preroll. */
4137 basesink->need_preroll = TRUE;
4139 /* step 2, unblock clock sync (if any) or any other blocking thing */
4140 if (basesink->clock_id) {
4141 gst_clock_id_unschedule (basesink->clock_id);
4144 /* flush out the data thread if it's locked in finish_preroll, this will
4145 * also flush out the EOS state */
4146 GST_DEBUG_OBJECT (basesink,
4147 "flushing out data thread, need preroll to TRUE");
4148 gst_base_sink_preroll_queue_flush (basesink, pad);
4150 GST_PAD_PREROLL_UNLOCK (pad);
4156 gst_base_sink_default_activate_pull (GstBaseSink * basesink, gboolean active)
4162 result = gst_pad_start_task (basesink->sinkpad,
4163 (GstTaskFunction) gst_base_sink_loop, basesink->sinkpad);
4165 /* step 2, make sure streaming finishes */
4166 result = gst_pad_stop_task (basesink->sinkpad);
4173 gst_base_sink_pad_activate (GstPad * pad)
4175 gboolean result = FALSE;
4176 GstBaseSink *basesink;
4178 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
4180 GST_DEBUG_OBJECT (basesink, "Trying pull mode first");
4182 gst_base_sink_set_flushing (basesink, pad, FALSE);
4184 /* we need to have the pull mode enabled */
4185 if (!basesink->can_activate_pull) {
4186 GST_DEBUG_OBJECT (basesink, "pull mode disabled");
4190 /* check if downstreams supports pull mode at all */
4191 if (!gst_pad_check_pull_range (pad)) {
4192 GST_DEBUG_OBJECT (basesink, "pull mode not supported");
4196 /* set the pad mode before starting the task so that it's in the
4197 * correct state for the new thread. also the sink set_caps and get_caps
4198 * function checks this */
4199 basesink->pad_mode = GST_ACTIVATE_PULL;
4201 /* we first try to negotiate a format so that when we try to activate
4202 * downstream, it knows about our format */
4203 if (!gst_base_sink_negotiate_pull (basesink)) {
4204 GST_DEBUG_OBJECT (basesink, "failed to negotiate in pull mode");
4208 /* ok activate now */
4209 if (!gst_pad_activate_pull (pad, TRUE)) {
4210 /* clear any pending caps */
4211 GST_OBJECT_LOCK (basesink);
4212 gst_caps_replace (&basesink->priv->pull_caps, NULL);
4213 GST_OBJECT_UNLOCK (basesink);
4214 GST_DEBUG_OBJECT (basesink, "failed to activate in pull mode");
4218 GST_DEBUG_OBJECT (basesink, "Success activating pull mode");
4222 /* push mode fallback */
4224 GST_DEBUG_OBJECT (basesink, "Falling back to push mode");
4225 if ((result = gst_pad_activate_push (pad, TRUE))) {
4226 GST_DEBUG_OBJECT (basesink, "Success activating push mode");
4231 GST_WARNING_OBJECT (basesink, "Could not activate pad in either mode");
4232 gst_base_sink_set_flushing (basesink, pad, TRUE);
4235 gst_object_unref (basesink);
4241 gst_base_sink_pad_activate_push (GstPad * pad, gboolean active)
4244 GstBaseSink *basesink;
4246 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
4249 if (!basesink->can_activate_push) {
4251 basesink->pad_mode = GST_ACTIVATE_NONE;
4254 basesink->pad_mode = GST_ACTIVATE_PUSH;
4257 if (G_UNLIKELY (basesink->pad_mode != GST_ACTIVATE_PUSH)) {
4258 g_warning ("Internal GStreamer activation error!!!");
4261 gst_base_sink_set_flushing (basesink, pad, TRUE);
4263 basesink->pad_mode = GST_ACTIVATE_NONE;
4267 gst_object_unref (basesink);
4273 gst_base_sink_negotiate_pull (GstBaseSink * basesink)
4280 /* this returns the intersection between our caps and the peer caps. If there
4281 * is no peer, it returns NULL and we can't operate in pull mode so we can
4282 * fail the negotiation. */
4283 caps = gst_pad_get_allowed_caps (GST_BASE_SINK_PAD (basesink));
4284 if (caps == NULL || gst_caps_is_empty (caps))
4285 goto no_caps_possible;
4287 GST_DEBUG_OBJECT (basesink, "allowed caps: %" GST_PTR_FORMAT, caps);
4289 caps = gst_caps_make_writable (caps);
4290 /* get the first (prefered) format */
4291 gst_caps_truncate (caps);
4293 gst_pad_fixate_caps (GST_BASE_SINK_PAD (basesink), caps);
4295 GST_DEBUG_OBJECT (basesink, "fixated to: %" GST_PTR_FORMAT, caps);
4297 if (gst_caps_is_any (caps)) {
4298 GST_DEBUG_OBJECT (basesink, "caps were ANY after fixating, "
4300 /* neither side has template caps in this case, so they are prepared for
4301 pull() without setcaps() */
4303 } else if (gst_caps_is_fixed (caps)) {
4304 if (!gst_pad_set_caps (GST_BASE_SINK_PAD (basesink), caps))
4305 goto could_not_set_caps;
4307 GST_OBJECT_LOCK (basesink);
4308 gst_caps_replace (&basesink->priv->pull_caps, caps);
4309 GST_OBJECT_UNLOCK (basesink);
4314 gst_caps_unref (caps);
4320 GST_INFO_OBJECT (basesink, "Pipeline could not agree on caps");
4321 GST_DEBUG_OBJECT (basesink, "get_allowed_caps() returned EMPTY");
4323 gst_caps_unref (caps);
4328 GST_INFO_OBJECT (basesink, "Could not set caps: %" GST_PTR_FORMAT, caps);
4329 gst_caps_unref (caps);
4334 /* this won't get called until we implement an activate function */
4336 gst_base_sink_pad_activate_pull (GstPad * pad, gboolean active)
4338 gboolean result = FALSE;
4339 GstBaseSink *basesink;
4340 GstBaseSinkClass *bclass;
4342 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
4343 bclass = GST_BASE_SINK_GET_CLASS (basesink);
4349 /* we mark we have a newsegment here because pull based
4350 * mode works just fine without having a newsegment before the
4352 format = GST_FORMAT_BYTES;
4354 gst_segment_init (&basesink->segment, format);
4355 gst_segment_init (basesink->abidata.ABI.clip_segment, format);
4356 GST_OBJECT_LOCK (basesink);
4357 basesink->have_newsegment = TRUE;
4358 GST_OBJECT_UNLOCK (basesink);
4360 /* get the peer duration in bytes */
4361 result = gst_pad_query_peer_duration (pad, &format, &duration);
4363 GST_DEBUG_OBJECT (basesink,
4364 "setting duration in bytes to %" G_GINT64_FORMAT, duration);
4365 gst_segment_set_duration (basesink->abidata.ABI.clip_segment, format,
4367 gst_segment_set_duration (&basesink->segment, format, duration);
4369 GST_DEBUG_OBJECT (basesink, "unknown duration");
4372 if (bclass->activate_pull)
4373 result = bclass->activate_pull (basesink, TRUE);
4378 goto activate_failed;
4381 if (G_UNLIKELY (basesink->pad_mode != GST_ACTIVATE_PULL)) {
4382 g_warning ("Internal GStreamer activation error!!!");
4385 result = gst_base_sink_set_flushing (basesink, pad, TRUE);
4386 if (bclass->activate_pull)
4387 result &= bclass->activate_pull (basesink, FALSE);
4388 basesink->pad_mode = GST_ACTIVATE_NONE;
4389 /* clear any pending caps */
4390 GST_OBJECT_LOCK (basesink);
4391 gst_caps_replace (&basesink->priv->pull_caps, NULL);
4392 GST_OBJECT_UNLOCK (basesink);
4395 gst_object_unref (basesink);
4402 /* reset, as starting the thread failed */
4403 basesink->pad_mode = GST_ACTIVATE_NONE;
4405 GST_ERROR_OBJECT (basesink, "subclass failed to activate in pull mode");
4410 /* send an event to our sinkpad peer. */
4412 gst_base_sink_send_event (GstElement * element, GstEvent * event)
4415 GstBaseSink *basesink = GST_BASE_SINK (element);
4416 gboolean forward, result = TRUE;
4417 GstActivateMode mode;
4419 GST_OBJECT_LOCK (element);
4420 /* get the pad and the scheduling mode */
4421 pad = gst_object_ref (basesink->sinkpad);
4422 mode = basesink->pad_mode;
4423 GST_OBJECT_UNLOCK (element);
4425 /* only push UPSTREAM events upstream */
4426 forward = GST_EVENT_IS_UPSTREAM (event);
4428 GST_DEBUG_OBJECT (basesink, "handling event %p %" GST_PTR_FORMAT, event,
4431 switch (GST_EVENT_TYPE (event)) {
4432 case GST_EVENT_LATENCY:
4434 GstClockTime latency;
4436 gst_event_parse_latency (event, &latency);
4438 /* store the latency. We use this to adjust the running_time before syncing
4439 * it to the clock. */
4440 GST_OBJECT_LOCK (element);
4441 basesink->priv->latency = latency;
4442 if (!basesink->priv->have_latency)
4444 GST_OBJECT_UNLOCK (element);
4445 GST_DEBUG_OBJECT (basesink, "latency set to %" GST_TIME_FORMAT,
4446 GST_TIME_ARGS (latency));
4448 /* We forward this event so that all elements know about the global pipeline
4449 * latency. This is interesting for an element when it wants to figure out
4450 * when a particular piece of data will be rendered. */
4453 case GST_EVENT_SEEK:
4454 /* in pull mode we will execute the seek */
4455 if (mode == GST_ACTIVATE_PULL)
4456 result = gst_base_sink_perform_seek (basesink, pad, event);
4458 case GST_EVENT_STEP:
4459 result = gst_base_sink_perform_step (basesink, pad, event);
4467 result = gst_pad_push_event (pad, event);
4469 /* not forwarded, unref the event */
4470 gst_event_unref (event);
4473 gst_object_unref (pad);
4478 gst_base_sink_get_position (GstBaseSink * basesink, GstFormat format,
4479 gint64 * cur, gboolean * upstream)
4481 GstClock *clock = NULL;
4482 gboolean res = FALSE;
4483 GstFormat oformat, tformat;
4484 GstSegment *segment;
4485 GstClockTime now, latency;
4486 GstClockTimeDiff base;
4487 gint64 time, accum, duration;
4490 gboolean last_seen, with_clock, in_paused;
4492 GST_OBJECT_LOCK (basesink);
4493 /* we can only get the segment when we are not NULL or READY */
4494 if (!basesink->have_newsegment)
4498 /* when not in PLAYING or when we're busy with a state change, we
4499 * cannot read from the clock so we report time based on the
4500 * last seen timestamp. */
4501 if (GST_STATE (basesink) != GST_STATE_PLAYING ||
4502 GST_STATE_PENDING (basesink) != GST_STATE_VOID_PENDING) {
4506 /* we don't use the clip segment in pull mode, when seeking we update the
4507 * main segment directly with the new segment values without it having to be
4508 * activated by the rendering after preroll */
4509 if (basesink->pad_mode == GST_ACTIVATE_PUSH)
4510 segment = basesink->abidata.ABI.clip_segment;
4512 segment = &basesink->segment;
4514 /* our intermediate time format */
4515 tformat = GST_FORMAT_TIME;
4516 /* get the format in the segment */
4517 oformat = segment->format;
4519 /* report with last seen position when EOS */
4520 last_seen = basesink->eos;
4522 /* assume we will use the clock for getting the current position */
4524 if (basesink->sync == FALSE)
4527 /* and we need a clock */
4528 if (G_UNLIKELY ((clock = GST_ELEMENT_CLOCK (basesink)) == NULL))
4531 gst_object_ref (clock);
4533 /* collect all data we need holding the lock */
4534 if (GST_CLOCK_TIME_IS_VALID (segment->time))
4535 time = segment->time;
4539 if (GST_CLOCK_TIME_IS_VALID (segment->stop))
4540 duration = segment->stop - segment->start;
4544 accum = segment->accum;
4545 rate = segment->rate * segment->applied_rate;
4546 latency = basesink->priv->latency;
4548 if (oformat == GST_FORMAT_TIME) {
4551 start = basesink->priv->current_sstart;
4552 stop = basesink->priv->current_sstop;
4555 /* in paused we use the last position as a lower bound */
4556 if (stop == -1 || segment->rate > 0.0)
4561 /* in playing, use last stop time as upper bound */
4562 if (start == -1 || segment->rate > 0.0)
4568 /* convert last stop to stream time */
4569 last = gst_segment_to_stream_time (segment, oformat, segment->last_stop);
4573 /* in paused, use start_time */
4574 base = GST_ELEMENT_START_TIME (basesink);
4575 GST_DEBUG_OBJECT (basesink, "in paused, using start time %" GST_TIME_FORMAT,
4576 GST_TIME_ARGS (base));
4577 } else if (with_clock) {
4578 /* else use clock when needed */
4579 base = GST_ELEMENT_CAST (basesink)->base_time;
4580 GST_DEBUG_OBJECT (basesink, "using clock and base time %" GST_TIME_FORMAT,
4581 GST_TIME_ARGS (base));
4583 /* else, no sync or clock -> no base time */
4584 GST_DEBUG_OBJECT (basesink, "no sync or no clock");
4588 /* no base, we can't calculate running_time, use last seem timestamp to report
4593 /* need to release the object lock before we can get the time,
4594 * a clock might take the LOCK of the provider, which could be
4595 * a basesink subclass. */
4596 GST_OBJECT_UNLOCK (basesink);
4599 /* in EOS or when no valid stream_time, report the value of last seen
4602 /* no timestamp, we need to ask upstream */
4603 GST_DEBUG_OBJECT (basesink, "no last seen timestamp, asking upstream");
4608 GST_DEBUG_OBJECT (basesink, "using last seen timestamp %" GST_TIME_FORMAT,
4609 GST_TIME_ARGS (last));
4612 if (oformat != tformat) {
4613 /* convert accum, time and duration to time */
4614 if (!gst_pad_query_convert (basesink->sinkpad, oformat, accum, &tformat,
4616 goto convert_failed;
4617 if (!gst_pad_query_convert (basesink->sinkpad, oformat, duration,
4618 &tformat, &duration))
4619 goto convert_failed;
4620 if (!gst_pad_query_convert (basesink->sinkpad, oformat, time, &tformat,
4622 goto convert_failed;
4623 if (!gst_pad_query_convert (basesink->sinkpad, oformat, last, &tformat,
4625 goto convert_failed;
4627 /* assume time format from now on */
4631 if (!in_paused && with_clock) {
4632 now = gst_clock_get_time (clock);
4638 /* subtract base time and accumulated time from the clock time.
4639 * Make sure we don't go negative. This is the current time in
4640 * the segment which we need to scale with the combined
4641 * rate and applied rate. */
4644 if (GST_CLOCK_DIFF (base, now) < 0)
4647 /* for negative rates we need to count back from the segment
4652 *cur = time + gst_guint64_to_gdouble (now - base) * rate;
4655 /* never report less than segment values in paused */
4657 *cur = MAX (last, *cur);
4659 /* never report more than last seen position in playing */
4661 *cur = MIN (last, *cur);
4664 GST_DEBUG_OBJECT (basesink,
4665 "now %" GST_TIME_FORMAT " - base %" GST_TIME_FORMAT " - accum %"
4666 GST_TIME_FORMAT " + time %" GST_TIME_FORMAT " last %" GST_TIME_FORMAT,
4667 GST_TIME_ARGS (now), GST_TIME_ARGS (base), GST_TIME_ARGS (accum),
4668 GST_TIME_ARGS (time), GST_TIME_ARGS (last));
4671 if (oformat != format) {
4672 /* convert to final format */
4673 if (!gst_pad_query_convert (basesink->sinkpad, oformat, *cur, &format, cur))
4674 goto convert_failed;
4680 GST_DEBUG_OBJECT (basesink, "res: %d, POSITION: %" GST_TIME_FORMAT,
4681 res, GST_TIME_ARGS (*cur));
4684 gst_object_unref (clock);
4691 /* in NULL or READY we always return FALSE and -1 */
4692 GST_DEBUG_OBJECT (basesink, "position in wrong state, return -1");
4695 GST_OBJECT_UNLOCK (basesink);
4700 GST_DEBUG_OBJECT (basesink, "convert failed, try upstream");
4708 gst_base_sink_get_duration (GstBaseSink * basesink, GstFormat format,
4709 gint64 * dur, gboolean * upstream)
4711 gboolean res = FALSE;
4713 if (basesink->pad_mode == GST_ACTIVATE_PULL) {
4714 GstFormat uformat = GST_FORMAT_BYTES;
4717 /* get the duration in bytes, in pull mode that's all we are sure to
4718 * know. We have to explicitly get this value from upstream instead of
4719 * using our cached value because it might change. Duration caching
4720 * should be done at a higher level. */
4721 res = gst_pad_query_peer_duration (basesink->sinkpad, &uformat, &uduration);
4723 gst_segment_set_duration (&basesink->segment, uformat, uduration);
4724 if (format != uformat) {
4725 /* convert to the requested format */
4726 res = gst_pad_query_convert (basesink->sinkpad, uformat, uduration,
4740 static const GstQueryType *
4741 gst_base_sink_get_query_types (GstElement * element)
4743 static const GstQueryType query_types[] = {
4755 gst_base_sink_query (GstElement * element, GstQuery * query)
4757 gboolean res = FALSE;
4759 GstBaseSink *basesink = GST_BASE_SINK (element);
4761 switch (GST_QUERY_TYPE (query)) {
4762 case GST_QUERY_POSITION:
4766 gboolean upstream = FALSE;
4768 gst_query_parse_position (query, &format, NULL);
4770 GST_DEBUG_OBJECT (basesink, "position query in format %s",
4771 gst_format_get_name (format));
4773 /* first try to get the position based on the clock */
4775 gst_base_sink_get_position (basesink, format, &cur, &upstream))) {
4776 gst_query_set_position (query, format, cur);
4777 } else if (upstream) {
4778 /* fallback to peer query */
4779 res = gst_pad_peer_query (basesink->sinkpad, query);
4782 /* we can handle a few things if upstream failed */
4783 if (format == GST_FORMAT_PERCENT) {
4785 GstFormat uformat = GST_FORMAT_TIME;
4787 res = gst_base_sink_get_position (basesink, GST_FORMAT_TIME, &cur,
4789 if (!res && upstream) {
4790 res = gst_pad_query_peer_position (basesink->sinkpad, &uformat,
4794 res = gst_base_sink_get_duration (basesink, GST_FORMAT_TIME, &dur,
4796 if (!res && upstream) {
4797 res = gst_pad_query_peer_duration (basesink->sinkpad, &uformat,
4804 pos = gst_util_uint64_scale (100 * GST_FORMAT_PERCENT_SCALE, cur,
4806 gst_query_set_position (query, GST_FORMAT_PERCENT, pos);
4812 case GST_QUERY_DURATION:
4816 gboolean upstream = FALSE;
4818 gst_query_parse_duration (query, &format, NULL);
4820 GST_DEBUG_OBJECT (basesink, "duration query in format %s",
4821 gst_format_get_name (format));
4824 gst_base_sink_get_duration (basesink, format, &dur, &upstream))) {
4825 gst_query_set_duration (query, format, dur);
4826 } else if (upstream) {
4827 /* fallback to peer query */
4828 res = gst_pad_peer_query (basesink->sinkpad, query);
4831 /* we can handle a few things if upstream failed */
4832 if (format == GST_FORMAT_PERCENT) {
4833 gst_query_set_duration (query, GST_FORMAT_PERCENT,
4834 GST_FORMAT_PERCENT_MAX);
4840 case GST_QUERY_LATENCY:
4842 gboolean live, us_live;
4843 GstClockTime min, max;
4845 if ((res = gst_base_sink_query_latency (basesink, &live, &us_live, &min,
4847 gst_query_set_latency (query, live, min, max);
4851 case GST_QUERY_JITTER:
4853 case GST_QUERY_RATE:
4854 /* gst_query_set_rate (query, basesink->segment_rate); */
4857 case GST_QUERY_SEGMENT:
4859 if (basesink->pad_mode == GST_ACTIVATE_PULL) {
4860 gst_query_set_segment (query, basesink->segment.rate,
4861 GST_FORMAT_TIME, basesink->segment.start, basesink->segment.stop);
4864 res = gst_pad_peer_query (basesink->sinkpad, query);
4868 case GST_QUERY_SEEKING:
4869 case GST_QUERY_CONVERT:
4870 case GST_QUERY_FORMATS:
4872 res = gst_pad_peer_query (basesink->sinkpad, query);
4875 GST_DEBUG_OBJECT (basesink, "query %s returns %d",
4876 GST_QUERY_TYPE_NAME (query), res);
4880 static GstStateChangeReturn
4881 gst_base_sink_change_state (GstElement * element, GstStateChange transition)
4883 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
4884 GstBaseSink *basesink = GST_BASE_SINK (element);
4885 GstBaseSinkClass *bclass;
4886 GstBaseSinkPrivate *priv;
4888 priv = basesink->priv;
4890 bclass = GST_BASE_SINK_GET_CLASS (basesink);
4892 switch (transition) {
4893 case GST_STATE_CHANGE_NULL_TO_READY:
4895 if (!bclass->start (basesink))
4898 case GST_STATE_CHANGE_READY_TO_PAUSED:
4899 /* need to complete preroll before this state change completes, there
4900 * is no data flow in READY so we can safely assume we need to preroll. */
4901 GST_PAD_PREROLL_LOCK (basesink->sinkpad);
4902 GST_DEBUG_OBJECT (basesink, "READY to PAUSED");
4903 basesink->have_newsegment = FALSE;
4904 gst_segment_init (&basesink->segment, GST_FORMAT_UNDEFINED);
4905 gst_segment_init (basesink->abidata.ABI.clip_segment,
4906 GST_FORMAT_UNDEFINED);
4907 basesink->offset = 0;
4908 basesink->have_preroll = FALSE;
4909 priv->step_unlock = FALSE;
4910 basesink->need_preroll = TRUE;
4911 basesink->playing_async = TRUE;
4912 priv->current_sstart = GST_CLOCK_TIME_NONE;
4913 priv->current_sstop = GST_CLOCK_TIME_NONE;
4914 priv->eos_rtime = GST_CLOCK_TIME_NONE;
4916 basesink->eos = FALSE;
4917 priv->received_eos = FALSE;
4918 gst_base_sink_reset_qos (basesink);
4919 priv->commited = FALSE;
4920 priv->call_preroll = TRUE;
4921 priv->current_step.valid = FALSE;
4922 priv->pending_step.valid = FALSE;
4923 if (priv->async_enabled) {
4924 GST_DEBUG_OBJECT (basesink, "doing async state change");
4925 /* when async enabled, post async-start message and return ASYNC from
4926 * the state change function */
4927 ret = GST_STATE_CHANGE_ASYNC;
4928 gst_element_post_message (GST_ELEMENT_CAST (basesink),
4929 gst_message_new_async_start (GST_OBJECT_CAST (basesink), FALSE));
4931 priv->have_latency = TRUE;
4933 GST_PAD_PREROLL_UNLOCK (basesink->sinkpad);
4935 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
4936 GST_PAD_PREROLL_LOCK (basesink->sinkpad);
4937 if (!gst_base_sink_needs_preroll (basesink)) {
4938 GST_DEBUG_OBJECT (basesink, "PAUSED to PLAYING, don't need preroll");
4939 /* no preroll needed anymore now. */
4940 basesink->playing_async = FALSE;
4941 basesink->need_preroll = FALSE;
4942 if (basesink->eos) {
4943 GstMessage *message;
4945 /* need to post EOS message here */
4946 GST_DEBUG_OBJECT (basesink, "Now posting EOS");
4947 message = gst_message_new_eos (GST_OBJECT_CAST (basesink));
4948 gst_message_set_seqnum (message, basesink->priv->seqnum);
4949 gst_element_post_message (GST_ELEMENT_CAST (basesink), message);
4951 GST_DEBUG_OBJECT (basesink, "signal preroll");
4952 GST_PAD_PREROLL_SIGNAL (basesink->sinkpad);
4955 GST_DEBUG_OBJECT (basesink, "PAUSED to PLAYING, we are not prerolled");
4956 basesink->need_preroll = TRUE;
4957 basesink->playing_async = TRUE;
4958 priv->call_preroll = TRUE;
4959 priv->commited = FALSE;
4960 if (priv->async_enabled) {
4961 GST_DEBUG_OBJECT (basesink, "doing async state change");
4962 ret = GST_STATE_CHANGE_ASYNC;
4963 gst_element_post_message (GST_ELEMENT_CAST (basesink),
4964 gst_message_new_async_start (GST_OBJECT_CAST (basesink), FALSE));
4967 GST_PAD_PREROLL_UNLOCK (basesink->sinkpad);
4974 GstStateChangeReturn bret;
4976 bret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
4977 if (G_UNLIKELY (bret == GST_STATE_CHANGE_FAILURE))
4978 goto activate_failed;
4981 switch (transition) {
4982 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
4983 GST_DEBUG_OBJECT (basesink, "PLAYING to PAUSED");
4984 /* FIXME, make sure we cannot enter _render first */
4986 /* we need to call ::unlock before locking PREROLL_LOCK
4987 * since we lock it before going into ::render */
4989 bclass->unlock (basesink);
4991 GST_PAD_PREROLL_LOCK (basesink->sinkpad);
4992 GST_DEBUG_OBJECT (basesink, "got preroll lock");
4993 /* now that we have the PREROLL lock, clear our unlock request */
4994 if (bclass->unlock_stop)
4995 bclass->unlock_stop (basesink);
4997 /* we need preroll again and we set the flag before unlocking the clockid
4998 * because if the clockid is unlocked before a current buffer expired, we
4999 * can use that buffer to preroll with */
5000 basesink->need_preroll = TRUE;
5002 if (basesink->clock_id) {
5003 GST_DEBUG_OBJECT (basesink, "unschedule clock");
5004 gst_clock_id_unschedule (basesink->clock_id);
5007 /* if we don't have a preroll buffer we need to wait for a preroll and
5009 if (!gst_base_sink_needs_preroll (basesink)) {
5010 GST_DEBUG_OBJECT (basesink, "PLAYING to PAUSED, we are prerolled");
5011 basesink->playing_async = FALSE;
5013 if (GST_STATE_TARGET (GST_ELEMENT (basesink)) <= GST_STATE_READY) {
5014 GST_DEBUG_OBJECT (basesink, "element is <= READY");
5015 ret = GST_STATE_CHANGE_SUCCESS;
5017 GST_DEBUG_OBJECT (basesink,
5018 "PLAYING to PAUSED, we are not prerolled");
5019 basesink->playing_async = TRUE;
5020 priv->commited = FALSE;
5021 priv->call_preroll = TRUE;
5022 if (priv->async_enabled) {
5023 GST_DEBUG_OBJECT (basesink, "doing async state change");
5024 ret = GST_STATE_CHANGE_ASYNC;
5025 gst_element_post_message (GST_ELEMENT_CAST (basesink),
5026 gst_message_new_async_start (GST_OBJECT_CAST (basesink),
5031 GST_DEBUG_OBJECT (basesink, "rendered: %" G_GUINT64_FORMAT
5032 ", dropped: %" G_GUINT64_FORMAT, priv->rendered, priv->dropped);
5034 gst_base_sink_reset_qos (basesink);
5035 GST_PAD_PREROLL_UNLOCK (basesink->sinkpad);
5037 case GST_STATE_CHANGE_PAUSED_TO_READY:
5038 GST_PAD_PREROLL_LOCK (basesink->sinkpad);
5039 /* start by reseting our position state with the object lock so that the
5040 * position query gets the right idea. We do this before we post the
5041 * messages so that the message handlers pick this up. */
5042 GST_OBJECT_LOCK (basesink);
5043 basesink->have_newsegment = FALSE;
5044 priv->current_sstart = GST_CLOCK_TIME_NONE;
5045 priv->current_sstop = GST_CLOCK_TIME_NONE;
5046 priv->have_latency = FALSE;
5047 if (priv->cached_clock_id) {
5048 gst_clock_id_unref (priv->cached_clock_id);
5049 priv->cached_clock_id = NULL;
5051 GST_OBJECT_UNLOCK (basesink);
5053 gst_base_sink_set_last_buffer (basesink, NULL);
5054 priv->call_preroll = FALSE;
5056 if (!priv->commited) {
5057 if (priv->async_enabled) {
5058 GST_DEBUG_OBJECT (basesink, "PAUSED to READY, posting async-done");
5060 gst_element_post_message (GST_ELEMENT_CAST (basesink),
5061 gst_message_new_state_changed (GST_OBJECT_CAST (basesink),
5062 GST_STATE_PLAYING, GST_STATE_PAUSED, GST_STATE_READY));
5064 gst_element_post_message (GST_ELEMENT_CAST (basesink),
5065 gst_message_new_async_done (GST_OBJECT_CAST (basesink)));
5067 priv->commited = TRUE;
5069 GST_DEBUG_OBJECT (basesink, "PAUSED to READY, don't need_preroll");
5071 GST_PAD_PREROLL_UNLOCK (basesink->sinkpad);
5073 case GST_STATE_CHANGE_READY_TO_NULL:
5075 if (!bclass->stop (basesink)) {
5076 GST_WARNING_OBJECT (basesink, "failed to stop");
5079 gst_base_sink_set_last_buffer (basesink, NULL);
5080 priv->call_preroll = FALSE;
5091 GST_DEBUG_OBJECT (basesink, "failed to start");
5092 return GST_STATE_CHANGE_FAILURE;
5096 GST_DEBUG_OBJECT (basesink,
5097 "element failed to change states -- activation problem?");
5098 return GST_STATE_CHANGE_FAILURE;