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 gboolean enable_last_buffer;
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;
261 #define DO_RUNNING_AVG(avg,val,size) (((val) + ((size)-1) * (avg)) / (size))
263 /* generic running average, this has a neutral window size */
264 #define UPDATE_RUNNING_AVG(avg,val) DO_RUNNING_AVG(avg,val,8)
266 /* the windows for these running averages are experimentally obtained.
267 * possitive values get averaged more while negative values use a small
268 * window so we can react faster to badness. */
269 #define UPDATE_RUNNING_AVG_P(avg,val) DO_RUNNING_AVG(avg,val,16)
270 #define UPDATE_RUNNING_AVG_N(avg,val) DO_RUNNING_AVG(avg,val,4)
272 /* BaseSink properties */
274 #define DEFAULT_CAN_ACTIVATE_PULL FALSE /* fixme: enable me */
275 #define DEFAULT_CAN_ACTIVATE_PUSH TRUE
277 #define DEFAULT_PREROLL_QUEUE_LEN 0
278 #define DEFAULT_SYNC TRUE
279 #define DEFAULT_MAX_LATENESS -1
280 #define DEFAULT_QOS FALSE
281 #define DEFAULT_ASYNC TRUE
282 #define DEFAULT_TS_OFFSET 0
283 #define DEFAULT_BLOCKSIZE 4096
284 #define DEFAULT_RENDER_DELAY 0
285 #define DEFAULT_ENABLE_LAST_BUFFER TRUE
290 PROP_PREROLL_QUEUE_LEN,
296 PROP_ENABLE_LAST_BUFFER,
303 static GstElementClass *parent_class = NULL;
305 static void gst_base_sink_class_init (GstBaseSinkClass * klass);
306 static void gst_base_sink_init (GstBaseSink * trans, gpointer g_class);
307 static void gst_base_sink_finalize (GObject * object);
310 gst_base_sink_get_type (void)
312 static volatile gsize base_sink_type = 0;
314 if (g_once_init_enter (&base_sink_type)) {
316 static const GTypeInfo base_sink_info = {
317 sizeof (GstBaseSinkClass),
320 (GClassInitFunc) gst_base_sink_class_init,
323 sizeof (GstBaseSink),
325 (GInstanceInitFunc) gst_base_sink_init,
328 _type = g_type_register_static (GST_TYPE_ELEMENT,
329 "GstBaseSink", &base_sink_info, G_TYPE_FLAG_ABSTRACT);
330 g_once_init_leave (&base_sink_type, _type);
332 return base_sink_type;
335 static void gst_base_sink_set_property (GObject * object, guint prop_id,
336 const GValue * value, GParamSpec * pspec);
337 static void gst_base_sink_get_property (GObject * object, guint prop_id,
338 GValue * value, GParamSpec * pspec);
340 static gboolean gst_base_sink_send_event (GstElement * element,
342 static gboolean gst_base_sink_query (GstElement * element, GstQuery * query);
343 static const GstQueryType *gst_base_sink_get_query_types (GstElement * element);
345 static GstCaps *gst_base_sink_get_caps (GstBaseSink * sink);
346 static gboolean gst_base_sink_set_caps (GstBaseSink * sink, GstCaps * caps);
347 static GstFlowReturn gst_base_sink_buffer_alloc (GstBaseSink * sink,
348 guint64 offset, guint size, GstCaps * caps, GstBuffer ** buf);
349 static void gst_base_sink_get_times (GstBaseSink * basesink, GstBuffer * buffer,
350 GstClockTime * start, GstClockTime * end);
351 static gboolean gst_base_sink_set_flushing (GstBaseSink * basesink,
352 GstPad * pad, gboolean flushing);
353 static gboolean gst_base_sink_default_activate_pull (GstBaseSink * basesink,
355 static gboolean gst_base_sink_default_do_seek (GstBaseSink * sink,
356 GstSegment * segment);
357 static gboolean gst_base_sink_default_prepare_seek_segment (GstBaseSink * sink,
358 GstEvent * event, GstSegment * segment);
360 static GstStateChangeReturn gst_base_sink_change_state (GstElement * element,
361 GstStateChange transition);
363 static GstFlowReturn gst_base_sink_chain (GstPad * pad, GstBuffer * buffer);
364 static GstFlowReturn gst_base_sink_chain_list (GstPad * pad,
365 GstBufferList * list);
367 static void gst_base_sink_loop (GstPad * pad);
368 static gboolean gst_base_sink_pad_activate (GstPad * pad);
369 static gboolean gst_base_sink_pad_activate_push (GstPad * pad, gboolean active);
370 static gboolean gst_base_sink_pad_activate_pull (GstPad * pad, gboolean active);
371 static gboolean gst_base_sink_event (GstPad * pad, GstEvent * event);
373 static gboolean gst_base_sink_negotiate_pull (GstBaseSink * basesink);
374 static GstCaps *gst_base_sink_pad_getcaps (GstPad * pad);
375 static gboolean gst_base_sink_pad_setcaps (GstPad * pad, GstCaps * caps);
376 static void gst_base_sink_pad_fixate (GstPad * pad, GstCaps * caps);
377 static GstFlowReturn gst_base_sink_pad_buffer_alloc (GstPad * pad,
378 guint64 offset, guint size, GstCaps * caps, GstBuffer ** buf);
381 /* check if an object was too late */
382 static gboolean gst_base_sink_is_too_late (GstBaseSink * basesink,
383 GstMiniObject * obj, GstClockTime start, GstClockTime stop,
384 GstClockReturn status, GstClockTimeDiff jitter);
385 static GstFlowReturn gst_base_sink_preroll_object (GstBaseSink * basesink,
386 gboolean is_list, GstMiniObject * obj);
389 gst_base_sink_class_init (GstBaseSinkClass * klass)
391 GObjectClass *gobject_class;
392 GstElementClass *gstelement_class;
394 gobject_class = G_OBJECT_CLASS (klass);
395 gstelement_class = GST_ELEMENT_CLASS (klass);
397 GST_DEBUG_CATEGORY_INIT (gst_base_sink_debug, "basesink", 0,
400 g_type_class_add_private (klass, sizeof (GstBaseSinkPrivate));
402 parent_class = g_type_class_peek_parent (klass);
404 gobject_class->finalize = gst_base_sink_finalize;
405 gobject_class->set_property = gst_base_sink_set_property;
406 gobject_class->get_property = gst_base_sink_get_property;
408 /* FIXME, this next value should be configured using an event from the
409 * upstream element, ie, the BUFFER_SIZE event. */
410 g_object_class_install_property (gobject_class, PROP_PREROLL_QUEUE_LEN,
411 g_param_spec_uint ("preroll-queue-len", "Preroll queue length",
412 "Number of buffers to queue during preroll", 0, G_MAXUINT,
413 DEFAULT_PREROLL_QUEUE_LEN,
414 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
416 g_object_class_install_property (gobject_class, PROP_SYNC,
417 g_param_spec_boolean ("sync", "Sync", "Sync on the clock", DEFAULT_SYNC,
418 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
420 g_object_class_install_property (gobject_class, PROP_MAX_LATENESS,
421 g_param_spec_int64 ("max-lateness", "Max Lateness",
422 "Maximum number of nanoseconds that a buffer can be late before it "
423 "is dropped (-1 unlimited)", -1, G_MAXINT64, DEFAULT_MAX_LATENESS,
424 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
426 g_object_class_install_property (gobject_class, PROP_QOS,
427 g_param_spec_boolean ("qos", "Qos",
428 "Generate Quality-of-Service events upstream", DEFAULT_QOS,
429 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
433 * If set to #TRUE, the basesink will perform asynchronous state changes.
434 * When set to #FALSE, the sink will not signal the parent when it prerolls.
435 * Use this option when dealing with sparse streams or when synchronisation is
440 g_object_class_install_property (gobject_class, PROP_ASYNC,
441 g_param_spec_boolean ("async", "Async",
442 "Go asynchronously to PAUSED", DEFAULT_ASYNC,
443 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
445 * GstBaseSink:ts-offset
447 * Controls the final synchronisation, a negative value will render the buffer
448 * earlier while a positive value delays playback. This property can be
449 * used to fix synchronisation in bad files.
453 g_object_class_install_property (gobject_class, PROP_TS_OFFSET,
454 g_param_spec_int64 ("ts-offset", "TS Offset",
455 "Timestamp offset in nanoseconds", G_MININT64, G_MAXINT64,
456 DEFAULT_TS_OFFSET, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
459 * GstBaseSink:enable-last-buffer
461 * Enable the last-buffer property. If FALSE, basesink doesn't keep a
462 * reference to the last buffer arrived and the last-buffer property is always
463 * set to NULL. This can be useful if you need buffers to be released as soon
464 * as possible, eg. if you're using a buffer pool.
468 g_object_class_install_property (gobject_class, PROP_ENABLE_LAST_BUFFER,
469 g_param_spec_boolean ("enable-last-buffer", "Enable Last Buffer",
470 "Enable the last-buffer property", DEFAULT_ENABLE_LAST_BUFFER,
471 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
474 * GstBaseSink:last-buffer
476 * The last buffer that arrived in the sink and was used for preroll or for
477 * rendering. This property can be used to generate thumbnails. This property
478 * can be NULL when the sink has not yet received a bufer.
482 g_object_class_install_property (gobject_class, PROP_LAST_BUFFER,
483 gst_param_spec_mini_object ("last-buffer", "Last Buffer",
484 "The last buffer received in the sink", GST_TYPE_BUFFER,
485 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
487 * GstBaseSink:blocksize
489 * The amount of bytes to pull when operating in pull mode.
493 g_object_class_install_property (gobject_class, PROP_BLOCKSIZE,
494 g_param_spec_uint ("blocksize", "Block size",
495 "Size in bytes to pull per buffer (0 = default)", 0, G_MAXUINT,
496 DEFAULT_BLOCKSIZE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
498 * GstBaseSink:render-delay
500 * The additional delay between synchronisation and actual rendering of the
501 * media. This property will add additional latency to the device in order to
502 * make other sinks compensate for the delay.
506 g_object_class_install_property (gobject_class, PROP_RENDER_DELAY,
507 g_param_spec_uint64 ("render-delay", "Render Delay",
508 "Additional render delay of the sink in nanoseconds", 0, G_MAXUINT64,
509 DEFAULT_RENDER_DELAY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
511 gstelement_class->change_state =
512 GST_DEBUG_FUNCPTR (gst_base_sink_change_state);
513 gstelement_class->send_event = GST_DEBUG_FUNCPTR (gst_base_sink_send_event);
514 gstelement_class->query = GST_DEBUG_FUNCPTR (gst_base_sink_query);
515 gstelement_class->get_query_types =
516 GST_DEBUG_FUNCPTR (gst_base_sink_get_query_types);
518 klass->get_caps = GST_DEBUG_FUNCPTR (gst_base_sink_get_caps);
519 klass->set_caps = GST_DEBUG_FUNCPTR (gst_base_sink_set_caps);
520 klass->buffer_alloc = GST_DEBUG_FUNCPTR (gst_base_sink_buffer_alloc);
521 klass->get_times = GST_DEBUG_FUNCPTR (gst_base_sink_get_times);
522 klass->activate_pull =
523 GST_DEBUG_FUNCPTR (gst_base_sink_default_activate_pull);
525 /* Registering debug symbols for function pointers */
526 GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_pad_getcaps);
527 GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_pad_setcaps);
528 GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_pad_fixate);
529 GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_pad_buffer_alloc);
530 GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_pad_activate);
531 GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_pad_activate_push);
532 GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_pad_activate_pull);
533 GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_event);
534 GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_chain);
535 GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_chain_list);
539 gst_base_sink_pad_getcaps (GstPad * pad)
541 GstBaseSinkClass *bclass;
543 GstCaps *caps = NULL;
545 bsink = GST_BASE_SINK (gst_pad_get_parent (pad));
546 bclass = GST_BASE_SINK_GET_CLASS (bsink);
548 if (bsink->pad_mode == GST_ACTIVATE_PULL) {
549 /* if we are operating in pull mode we only accept the negotiated caps */
550 GST_OBJECT_LOCK (pad);
551 if ((caps = GST_PAD_CAPS (pad)))
553 GST_OBJECT_UNLOCK (pad);
556 if (bclass->get_caps)
557 caps = bclass->get_caps (bsink);
560 GstPadTemplate *pad_template;
563 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (bclass),
565 if (pad_template != NULL) {
566 caps = gst_caps_ref (gst_pad_template_get_caps (pad_template));
570 gst_object_unref (bsink);
576 gst_base_sink_pad_setcaps (GstPad * pad, GstCaps * caps)
578 GstBaseSinkClass *bclass;
582 bsink = GST_BASE_SINK (gst_pad_get_parent (pad));
583 bclass = GST_BASE_SINK_GET_CLASS (bsink);
585 if (res && bclass->set_caps)
586 res = bclass->set_caps (bsink, caps);
588 gst_object_unref (bsink);
594 gst_base_sink_pad_fixate (GstPad * pad, GstCaps * caps)
596 GstBaseSinkClass *bclass;
599 bsink = GST_BASE_SINK (gst_pad_get_parent (pad));
600 bclass = GST_BASE_SINK_GET_CLASS (bsink);
603 bclass->fixate (bsink, caps);
605 gst_object_unref (bsink);
609 gst_base_sink_pad_buffer_alloc (GstPad * pad, guint64 offset, guint size,
610 GstCaps * caps, GstBuffer ** buf)
612 GstBaseSinkClass *bclass;
614 GstFlowReturn result = GST_FLOW_OK;
616 bsink = GST_BASE_SINK (gst_pad_get_parent (pad));
617 bclass = GST_BASE_SINK_GET_CLASS (bsink);
619 if (bclass->buffer_alloc)
620 result = bclass->buffer_alloc (bsink, offset, size, caps, buf);
622 *buf = NULL; /* fallback in gstpad.c will allocate generic buffer */
624 gst_object_unref (bsink);
630 gst_base_sink_init (GstBaseSink * basesink, gpointer g_class)
632 GstPadTemplate *pad_template;
633 GstBaseSinkPrivate *priv;
635 basesink->priv = priv = GST_BASE_SINK_GET_PRIVATE (basesink);
638 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (g_class), "sink");
639 g_return_if_fail (pad_template != NULL);
641 basesink->sinkpad = gst_pad_new_from_template (pad_template, "sink");
643 gst_pad_set_getcaps_function (basesink->sinkpad, gst_base_sink_pad_getcaps);
644 gst_pad_set_setcaps_function (basesink->sinkpad, gst_base_sink_pad_setcaps);
645 gst_pad_set_fixatecaps_function (basesink->sinkpad, gst_base_sink_pad_fixate);
646 gst_pad_set_bufferalloc_function (basesink->sinkpad,
647 gst_base_sink_pad_buffer_alloc);
648 gst_pad_set_activate_function (basesink->sinkpad, gst_base_sink_pad_activate);
649 gst_pad_set_activatepush_function (basesink->sinkpad,
650 gst_base_sink_pad_activate_push);
651 gst_pad_set_activatepull_function (basesink->sinkpad,
652 gst_base_sink_pad_activate_pull);
653 gst_pad_set_event_function (basesink->sinkpad, gst_base_sink_event);
654 gst_pad_set_chain_function (basesink->sinkpad, gst_base_sink_chain);
655 gst_pad_set_chain_list_function (basesink->sinkpad, gst_base_sink_chain_list);
656 gst_element_add_pad (GST_ELEMENT_CAST (basesink), basesink->sinkpad);
658 basesink->pad_mode = GST_ACTIVATE_NONE;
659 basesink->preroll_queue = g_queue_new ();
660 basesink->abidata.ABI.clip_segment = gst_segment_new ();
661 priv->have_latency = FALSE;
663 basesink->can_activate_push = DEFAULT_CAN_ACTIVATE_PUSH;
664 basesink->can_activate_pull = DEFAULT_CAN_ACTIVATE_PULL;
666 basesink->sync = DEFAULT_SYNC;
667 basesink->abidata.ABI.max_lateness = DEFAULT_MAX_LATENESS;
668 g_atomic_int_set (&priv->qos_enabled, DEFAULT_QOS);
669 priv->async_enabled = DEFAULT_ASYNC;
670 priv->ts_offset = DEFAULT_TS_OFFSET;
671 priv->render_delay = DEFAULT_RENDER_DELAY;
672 priv->blocksize = DEFAULT_BLOCKSIZE;
673 priv->enable_last_buffer = DEFAULT_ENABLE_LAST_BUFFER;
675 GST_OBJECT_FLAG_SET (basesink, GST_ELEMENT_IS_SINK);
679 gst_base_sink_finalize (GObject * object)
681 GstBaseSink *basesink;
683 basesink = GST_BASE_SINK (object);
685 g_queue_free (basesink->preroll_queue);
686 gst_segment_free (basesink->abidata.ABI.clip_segment);
688 G_OBJECT_CLASS (parent_class)->finalize (object);
692 * gst_base_sink_set_sync:
694 * @sync: the new sync value.
696 * Configures @sink to synchronize on the clock or not. When
697 * @sync is FALSE, incomming samples will be played as fast as
698 * possible. If @sync is TRUE, the timestamps of the incomming
699 * buffers will be used to schedule the exact render time of its
705 gst_base_sink_set_sync (GstBaseSink * sink, gboolean sync)
707 g_return_if_fail (GST_IS_BASE_SINK (sink));
709 GST_OBJECT_LOCK (sink);
711 GST_OBJECT_UNLOCK (sink);
715 * gst_base_sink_get_sync:
718 * Checks if @sink is currently configured to synchronize against the
721 * Returns: TRUE if the sink is configured to synchronize against the clock.
726 gst_base_sink_get_sync (GstBaseSink * sink)
730 g_return_val_if_fail (GST_IS_BASE_SINK (sink), FALSE);
732 GST_OBJECT_LOCK (sink);
734 GST_OBJECT_UNLOCK (sink);
740 * gst_base_sink_set_max_lateness:
742 * @max_lateness: the new max lateness value.
744 * Sets the new max lateness value to @max_lateness. This value is
745 * used to decide if a buffer should be dropped or not based on the
746 * buffer timestamp and the current clock time. A value of -1 means
752 gst_base_sink_set_max_lateness (GstBaseSink * sink, gint64 max_lateness)
754 g_return_if_fail (GST_IS_BASE_SINK (sink));
756 GST_OBJECT_LOCK (sink);
757 sink->abidata.ABI.max_lateness = max_lateness;
758 GST_OBJECT_UNLOCK (sink);
762 * gst_base_sink_get_max_lateness:
765 * Gets the max lateness value. See gst_base_sink_set_max_lateness for
768 * Returns: The maximum time in nanoseconds that a buffer can be late
769 * before it is dropped and not rendered. A value of -1 means an
775 gst_base_sink_get_max_lateness (GstBaseSink * sink)
779 g_return_val_if_fail (GST_IS_BASE_SINK (sink), -1);
781 GST_OBJECT_LOCK (sink);
782 res = sink->abidata.ABI.max_lateness;
783 GST_OBJECT_UNLOCK (sink);
789 * gst_base_sink_set_qos_enabled:
791 * @enabled: the new qos value.
793 * Configures @sink to send Quality-of-Service events upstream.
798 gst_base_sink_set_qos_enabled (GstBaseSink * sink, gboolean enabled)
800 g_return_if_fail (GST_IS_BASE_SINK (sink));
802 g_atomic_int_set (&sink->priv->qos_enabled, enabled);
806 * gst_base_sink_is_qos_enabled:
809 * Checks if @sink is currently configured to send Quality-of-Service events
812 * Returns: TRUE if the sink is configured to perform Quality-of-Service.
817 gst_base_sink_is_qos_enabled (GstBaseSink * sink)
821 g_return_val_if_fail (GST_IS_BASE_SINK (sink), FALSE);
823 res = g_atomic_int_get (&sink->priv->qos_enabled);
829 * gst_base_sink_set_async_enabled:
831 * @enabled: the new async value.
833 * Configures @sink to perform all state changes asynchronusly. When async is
834 * disabled, the sink will immediatly go to PAUSED instead of waiting for a
835 * preroll buffer. This feature is usefull if the sink does not synchronize
836 * against the clock or when it is dealing with sparse streams.
841 gst_base_sink_set_async_enabled (GstBaseSink * sink, gboolean enabled)
843 g_return_if_fail (GST_IS_BASE_SINK (sink));
845 GST_PAD_PREROLL_LOCK (sink->sinkpad);
846 g_atomic_int_set (&sink->priv->async_enabled, enabled);
847 GST_LOG_OBJECT (sink, "set async enabled to %d", enabled);
848 GST_PAD_PREROLL_UNLOCK (sink->sinkpad);
852 * gst_base_sink_is_async_enabled:
855 * Checks if @sink is currently configured to perform asynchronous state
858 * Returns: TRUE if the sink is configured to perform asynchronous state
864 gst_base_sink_is_async_enabled (GstBaseSink * sink)
868 g_return_val_if_fail (GST_IS_BASE_SINK (sink), FALSE);
870 res = g_atomic_int_get (&sink->priv->async_enabled);
876 * gst_base_sink_set_ts_offset:
878 * @offset: the new offset
880 * Adjust the synchronisation of @sink with @offset. A negative value will
881 * render buffers earlier than their timestamp. A positive value will delay
882 * rendering. This function can be used to fix playback of badly timestamped
888 gst_base_sink_set_ts_offset (GstBaseSink * sink, GstClockTimeDiff offset)
890 g_return_if_fail (GST_IS_BASE_SINK (sink));
892 GST_OBJECT_LOCK (sink);
893 sink->priv->ts_offset = offset;
894 GST_LOG_OBJECT (sink, "set time offset to %" G_GINT64_FORMAT, offset);
895 GST_OBJECT_UNLOCK (sink);
899 * gst_base_sink_get_ts_offset:
902 * Get the synchronisation offset of @sink.
904 * Returns: The synchronisation offset.
909 gst_base_sink_get_ts_offset (GstBaseSink * sink)
911 GstClockTimeDiff res;
913 g_return_val_if_fail (GST_IS_BASE_SINK (sink), 0);
915 GST_OBJECT_LOCK (sink);
916 res = sink->priv->ts_offset;
917 GST_OBJECT_UNLOCK (sink);
923 * gst_base_sink_get_last_buffer:
926 * Get the last buffer that arrived in the sink and was used for preroll or for
927 * rendering. This property can be used to generate thumbnails.
929 * The #GstCaps on the buffer can be used to determine the type of the buffer.
931 * Returns: a #GstBuffer. gst_buffer_unref() after usage. This function returns
932 * NULL when no buffer has arrived in the sink yet or when the sink is not in
938 gst_base_sink_get_last_buffer (GstBaseSink * sink)
942 g_return_val_if_fail (GST_IS_BASE_SINK (sink), NULL);
944 GST_OBJECT_LOCK (sink);
945 if ((res = sink->priv->last_buffer))
946 gst_buffer_ref (res);
947 GST_OBJECT_UNLOCK (sink);
952 /* with OBJECT_LOCK */
954 gst_base_sink_set_last_buffer_unlocked (GstBaseSink * sink, GstBuffer * buffer)
958 old = sink->priv->last_buffer;
959 if (G_LIKELY (old != buffer)) {
960 GST_DEBUG_OBJECT (sink, "setting last buffer to %p", buffer);
961 if (G_LIKELY (buffer))
962 gst_buffer_ref (buffer);
963 sink->priv->last_buffer = buffer;
967 /* avoid unreffing with the lock because cleanup code might want to take the
969 if (G_LIKELY (old)) {
970 GST_OBJECT_UNLOCK (sink);
971 gst_buffer_unref (old);
972 GST_OBJECT_LOCK (sink);
977 gst_base_sink_set_last_buffer (GstBaseSink * sink, GstBuffer * buffer)
979 GST_OBJECT_LOCK (sink);
980 if (sink->priv->enable_last_buffer == FALSE)
983 gst_base_sink_set_last_buffer_unlocked (sink, buffer);
986 GST_OBJECT_UNLOCK (sink);
990 * gst_base_sink_set_last_buffer_enabled:
992 * @enabled: the new enable-last-buffer value.
994 * Configures @sink to store the last received buffer in the last-buffer
1000 gst_base_sink_set_last_buffer_enabled (GstBaseSink * sink, gboolean enabled)
1002 g_return_if_fail (GST_IS_BASE_SINK (sink));
1004 GST_OBJECT_LOCK (sink);
1005 if (enabled != sink->priv->enable_last_buffer) {
1006 sink->priv->enable_last_buffer = enabled;
1008 gst_base_sink_set_last_buffer_unlocked (sink, NULL);
1010 GST_OBJECT_UNLOCK (sink);
1014 * gst_base_sink_is_last_buffer_enabled:
1017 * Checks if @sink is currently configured to store the last received buffer in
1018 * the last-buffer property.
1020 * Returns: TRUE if the sink is configured to store the last received buffer.
1025 gst_base_sink_is_last_buffer_enabled (GstBaseSink * sink)
1029 g_return_val_if_fail (GST_IS_BASE_SINK (sink), FALSE);
1031 GST_OBJECT_LOCK (sink);
1032 res = sink->priv->enable_last_buffer;
1033 GST_OBJECT_UNLOCK (sink);
1039 * gst_base_sink_get_latency:
1042 * Get the currently configured latency.
1044 * Returns: The configured latency.
1049 gst_base_sink_get_latency (GstBaseSink * sink)
1053 GST_OBJECT_LOCK (sink);
1054 res = sink->priv->latency;
1055 GST_OBJECT_UNLOCK (sink);
1061 * gst_base_sink_query_latency:
1063 * @live: if the sink is live
1064 * @upstream_live: if an upstream element is live
1065 * @min_latency: the min latency of the upstream elements
1066 * @max_latency: the max latency of the upstream elements
1068 * Query the sink for the latency parameters. The latency will be queried from
1069 * the upstream elements. @live will be TRUE if @sink is configured to
1070 * synchronize against the clock. @upstream_live will be TRUE if an upstream
1073 * If both @live and @upstream_live are TRUE, the sink will want to compensate
1074 * for the latency introduced by the upstream elements by setting the
1075 * @min_latency to a strictly possitive value.
1077 * This function is mostly used by subclasses.
1079 * Returns: TRUE if the query succeeded.
1084 gst_base_sink_query_latency (GstBaseSink * sink, gboolean * live,
1085 gboolean * upstream_live, GstClockTime * min_latency,
1086 GstClockTime * max_latency)
1088 gboolean l, us_live, res, have_latency;
1089 GstClockTime min, max, render_delay;
1091 GstClockTime us_min, us_max;
1093 /* we are live when we sync to the clock */
1094 GST_OBJECT_LOCK (sink);
1096 have_latency = sink->priv->have_latency;
1097 render_delay = sink->priv->render_delay;
1098 GST_OBJECT_UNLOCK (sink);
1100 /* assume no latency */
1106 GST_DEBUG_OBJECT (sink, "we are ready for LATENCY query");
1107 /* we are ready for a latency query this is when we preroll or when we are
1109 query = gst_query_new_latency ();
1111 /* ask the peer for the latency */
1112 if ((res = gst_pad_peer_query (sink->sinkpad, query))) {
1113 /* get upstream min and max latency */
1114 gst_query_parse_latency (query, &us_live, &us_min, &us_max);
1117 /* upstream live, use its latency, subclasses should use these
1118 * values to create the complete latency. */
1123 /* we need to add the render delay if we are live */
1125 min += render_delay;
1127 max += render_delay;
1130 gst_query_unref (query);
1132 GST_DEBUG_OBJECT (sink, "we are not yet ready for LATENCY query");
1136 /* not live, we tried to do the query, if it failed we return TRUE anyway */
1140 GST_DEBUG_OBJECT (sink, "latency query failed but we are not live");
1142 GST_DEBUG_OBJECT (sink, "latency query failed and we are live");
1147 GST_DEBUG_OBJECT (sink, "latency query: live: %d, have_latency %d,"
1148 " upstream: %d, min %" GST_TIME_FORMAT ", max %" GST_TIME_FORMAT, l,
1149 have_latency, us_live, GST_TIME_ARGS (min), GST_TIME_ARGS (max));
1154 *upstream_live = us_live;
1164 * gst_base_sink_set_render_delay:
1165 * @sink: a #GstBaseSink
1166 * @delay: the new delay
1168 * Set the render delay in @sink to @delay. The render delay is the time
1169 * between actual rendering of a buffer and its synchronisation time. Some
1170 * devices might delay media rendering which can be compensated for with this
1173 * After calling this function, this sink will report additional latency and
1174 * other sinks will adjust their latency to delay the rendering of their media.
1176 * This function is usually called by subclasses.
1181 gst_base_sink_set_render_delay (GstBaseSink * sink, GstClockTime delay)
1183 GstClockTime old_render_delay;
1185 g_return_if_fail (GST_IS_BASE_SINK (sink));
1187 GST_OBJECT_LOCK (sink);
1188 old_render_delay = sink->priv->render_delay;
1189 sink->priv->render_delay = delay;
1190 GST_LOG_OBJECT (sink, "set render delay to %" GST_TIME_FORMAT,
1191 GST_TIME_ARGS (delay));
1192 GST_OBJECT_UNLOCK (sink);
1194 if (delay != old_render_delay) {
1195 GST_DEBUG_OBJECT (sink, "posting latency changed");
1196 gst_element_post_message (GST_ELEMENT_CAST (sink),
1197 gst_message_new_latency (GST_OBJECT_CAST (sink)));
1202 * gst_base_sink_get_render_delay:
1203 * @sink: a #GstBaseSink
1205 * Get the render delay of @sink. see gst_base_sink_set_render_delay() for more
1206 * information about the render delay.
1208 * Returns: the render delay of @sink.
1213 gst_base_sink_get_render_delay (GstBaseSink * sink)
1215 GstClockTimeDiff res;
1217 g_return_val_if_fail (GST_IS_BASE_SINK (sink), 0);
1219 GST_OBJECT_LOCK (sink);
1220 res = sink->priv->render_delay;
1221 GST_OBJECT_UNLOCK (sink);
1227 * gst_base_sink_set_blocksize:
1228 * @sink: a #GstBaseSink
1229 * @blocksize: the blocksize in bytes
1231 * Set the number of bytes that the sink will pull when it is operating in pull
1237 gst_base_sink_set_blocksize (GstBaseSink * sink, guint blocksize)
1239 g_return_if_fail (GST_IS_BASE_SINK (sink));
1241 GST_OBJECT_LOCK (sink);
1242 sink->priv->blocksize = blocksize;
1243 GST_LOG_OBJECT (sink, "set blocksize to %u", blocksize);
1244 GST_OBJECT_UNLOCK (sink);
1248 * gst_base_sink_get_blocksize:
1249 * @sink: a #GstBaseSink
1251 * Get the number of bytes that the sink will pull when it is operating in pull
1254 * Returns: the number of bytes @sink will pull in pull mode.
1259 gst_base_sink_get_blocksize (GstBaseSink * sink)
1263 g_return_val_if_fail (GST_IS_BASE_SINK (sink), 0);
1265 GST_OBJECT_LOCK (sink);
1266 res = sink->priv->blocksize;
1267 GST_OBJECT_UNLOCK (sink);
1273 gst_base_sink_set_property (GObject * object, guint prop_id,
1274 const GValue * value, GParamSpec * pspec)
1276 GstBaseSink *sink = GST_BASE_SINK (object);
1279 case PROP_PREROLL_QUEUE_LEN:
1280 /* preroll lock necessary to serialize with finish_preroll */
1281 GST_PAD_PREROLL_LOCK (sink->sinkpad);
1282 g_atomic_int_set (&sink->preroll_queue_max_len, g_value_get_uint (value));
1283 GST_PAD_PREROLL_UNLOCK (sink->sinkpad);
1286 gst_base_sink_set_sync (sink, g_value_get_boolean (value));
1288 case PROP_MAX_LATENESS:
1289 gst_base_sink_set_max_lateness (sink, g_value_get_int64 (value));
1292 gst_base_sink_set_qos_enabled (sink, g_value_get_boolean (value));
1295 gst_base_sink_set_async_enabled (sink, g_value_get_boolean (value));
1297 case PROP_TS_OFFSET:
1298 gst_base_sink_set_ts_offset (sink, g_value_get_int64 (value));
1300 case PROP_BLOCKSIZE:
1301 gst_base_sink_set_blocksize (sink, g_value_get_uint (value));
1303 case PROP_RENDER_DELAY:
1304 gst_base_sink_set_render_delay (sink, g_value_get_uint64 (value));
1306 case PROP_ENABLE_LAST_BUFFER:
1307 gst_base_sink_set_last_buffer_enabled (sink, g_value_get_boolean (value));
1310 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1316 gst_base_sink_get_property (GObject * object, guint prop_id, GValue * value,
1319 GstBaseSink *sink = GST_BASE_SINK (object);
1322 case PROP_PREROLL_QUEUE_LEN:
1323 g_value_set_uint (value, g_atomic_int_get (&sink->preroll_queue_max_len));
1326 g_value_set_boolean (value, gst_base_sink_get_sync (sink));
1328 case PROP_MAX_LATENESS:
1329 g_value_set_int64 (value, gst_base_sink_get_max_lateness (sink));
1332 g_value_set_boolean (value, gst_base_sink_is_qos_enabled (sink));
1335 g_value_set_boolean (value, gst_base_sink_is_async_enabled (sink));
1337 case PROP_TS_OFFSET:
1338 g_value_set_int64 (value, gst_base_sink_get_ts_offset (sink));
1340 case PROP_LAST_BUFFER:
1341 gst_value_take_buffer (value, gst_base_sink_get_last_buffer (sink));
1343 case PROP_ENABLE_LAST_BUFFER:
1344 g_value_set_boolean (value, gst_base_sink_is_last_buffer_enabled (sink));
1346 case PROP_BLOCKSIZE:
1347 g_value_set_uint (value, gst_base_sink_get_blocksize (sink));
1349 case PROP_RENDER_DELAY:
1350 g_value_set_uint64 (value, gst_base_sink_get_render_delay (sink));
1353 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1360 gst_base_sink_get_caps (GstBaseSink * sink)
1366 gst_base_sink_set_caps (GstBaseSink * sink, GstCaps * caps)
1371 static GstFlowReturn
1372 gst_base_sink_buffer_alloc (GstBaseSink * sink, guint64 offset, guint size,
1373 GstCaps * caps, GstBuffer ** buf)
1379 /* with PREROLL_LOCK, STREAM_LOCK */
1381 gst_base_sink_preroll_queue_flush (GstBaseSink * basesink, GstPad * pad)
1385 GST_DEBUG_OBJECT (basesink, "flushing queue %p", basesink);
1386 while ((obj = g_queue_pop_head (basesink->preroll_queue))) {
1387 GST_DEBUG_OBJECT (basesink, "popped %p", obj);
1388 gst_mini_object_unref (obj);
1390 /* we can't have EOS anymore now */
1391 basesink->eos = FALSE;
1392 basesink->priv->received_eos = FALSE;
1393 basesink->have_preroll = FALSE;
1394 basesink->priv->step_unlock = FALSE;
1395 basesink->eos_queued = FALSE;
1396 basesink->preroll_queued = 0;
1397 basesink->buffers_queued = 0;
1398 basesink->events_queued = 0;
1399 /* can't report latency anymore until we preroll again */
1400 if (basesink->priv->async_enabled) {
1401 GST_OBJECT_LOCK (basesink);
1402 basesink->priv->have_latency = FALSE;
1403 GST_OBJECT_UNLOCK (basesink);
1405 /* and signal any waiters now */
1406 GST_PAD_PREROLL_SIGNAL (pad);
1409 /* with STREAM_LOCK, configures given segment with the event information. */
1411 gst_base_sink_configure_segment (GstBaseSink * basesink, GstPad * pad,
1412 GstEvent * event, GstSegment * segment)
1415 gdouble rate, arate;
1421 /* the newsegment event is needed to bring the buffer timestamps to the
1422 * stream time and to drop samples outside of the playback segment. */
1423 gst_event_parse_new_segment_full (event, &update, &rate, &arate, &format,
1424 &start, &stop, &time);
1426 /* The segment is protected with both the STREAM_LOCK and the OBJECT_LOCK.
1427 * We protect with the OBJECT_LOCK so that we can use the values to
1428 * safely answer a POSITION query. */
1429 GST_OBJECT_LOCK (basesink);
1430 gst_segment_set_newsegment_full (segment, update, rate, arate, format, start,
1433 if (format == GST_FORMAT_TIME) {
1434 GST_DEBUG_OBJECT (basesink,
1435 "configured NEWSEGMENT update %d, rate %lf, applied rate %lf, "
1436 "format GST_FORMAT_TIME, "
1437 "%" GST_TIME_FORMAT " -- %" GST_TIME_FORMAT
1438 ", time %" GST_TIME_FORMAT ", accum %" GST_TIME_FORMAT,
1439 update, rate, arate, GST_TIME_ARGS (segment->start),
1440 GST_TIME_ARGS (segment->stop), GST_TIME_ARGS (segment->time),
1441 GST_TIME_ARGS (segment->accum));
1443 GST_DEBUG_OBJECT (basesink,
1444 "configured NEWSEGMENT update %d, rate %lf, applied rate %lf, "
1446 "%" G_GINT64_FORMAT " -- %" G_GINT64_FORMAT ", time %"
1447 G_GINT64_FORMAT ", accum %" G_GINT64_FORMAT, update, rate, arate,
1448 segment->format, segment->start, segment->stop, segment->time,
1451 GST_OBJECT_UNLOCK (basesink);
1454 /* with PREROLL_LOCK, STREAM_LOCK */
1456 gst_base_sink_commit_state (GstBaseSink * basesink)
1458 /* commit state and proceed to next pending state */
1459 GstState current, next, pending, post_pending;
1460 gboolean post_paused = FALSE;
1461 gboolean post_async_done = FALSE;
1462 gboolean post_playing = FALSE;
1464 /* we are certainly not playing async anymore now */
1465 basesink->playing_async = FALSE;
1467 GST_OBJECT_LOCK (basesink);
1468 current = GST_STATE (basesink);
1469 next = GST_STATE_NEXT (basesink);
1470 pending = GST_STATE_PENDING (basesink);
1471 post_pending = pending;
1474 case GST_STATE_PLAYING:
1476 GstBaseSinkClass *bclass;
1477 GstStateChangeReturn ret;
1479 bclass = GST_BASE_SINK_GET_CLASS (basesink);
1481 GST_DEBUG_OBJECT (basesink, "commiting state to PLAYING");
1483 basesink->need_preroll = FALSE;
1484 post_async_done = TRUE;
1485 basesink->priv->commited = TRUE;
1486 post_playing = TRUE;
1487 /* post PAUSED too when we were READY */
1488 if (current == GST_STATE_READY) {
1492 /* make sure we notify the subclass of async playing */
1493 if (bclass->async_play) {
1494 GST_WARNING_OBJECT (basesink, "deprecated async_play");
1495 ret = bclass->async_play (basesink);
1496 if (ret == GST_STATE_CHANGE_FAILURE)
1501 case GST_STATE_PAUSED:
1502 GST_DEBUG_OBJECT (basesink, "commiting state to PAUSED");
1504 post_async_done = TRUE;
1505 basesink->priv->commited = TRUE;
1506 post_pending = GST_STATE_VOID_PENDING;
1508 case GST_STATE_READY:
1509 case GST_STATE_NULL:
1511 case GST_STATE_VOID_PENDING:
1512 goto nothing_pending;
1517 /* we can report latency queries now */
1518 basesink->priv->have_latency = TRUE;
1520 GST_STATE (basesink) = pending;
1521 GST_STATE_NEXT (basesink) = GST_STATE_VOID_PENDING;
1522 GST_STATE_PENDING (basesink) = GST_STATE_VOID_PENDING;
1523 GST_STATE_RETURN (basesink) = GST_STATE_CHANGE_SUCCESS;
1524 GST_OBJECT_UNLOCK (basesink);
1527 GST_DEBUG_OBJECT (basesink, "posting PAUSED state change message");
1528 gst_element_post_message (GST_ELEMENT_CAST (basesink),
1529 gst_message_new_state_changed (GST_OBJECT_CAST (basesink),
1530 current, next, post_pending));
1532 if (post_async_done) {
1533 GST_DEBUG_OBJECT (basesink, "posting async-done message");
1534 gst_element_post_message (GST_ELEMENT_CAST (basesink),
1535 gst_message_new_async_done (GST_OBJECT_CAST (basesink)));
1538 GST_DEBUG_OBJECT (basesink, "posting PLAYING state change message");
1539 gst_element_post_message (GST_ELEMENT_CAST (basesink),
1540 gst_message_new_state_changed (GST_OBJECT_CAST (basesink),
1541 next, pending, GST_STATE_VOID_PENDING));
1544 GST_STATE_BROADCAST (basesink);
1550 /* Depending on the state, set our vars. We get in this situation when the
1551 * state change function got a change to update the state vars before the
1552 * streaming thread did. This is fine but we need to make sure that we
1553 * update the need_preroll var since it was TRUE when we got here and might
1554 * become FALSE if we got to PLAYING. */
1555 GST_DEBUG_OBJECT (basesink, "nothing to commit, now in %s",
1556 gst_element_state_get_name (current));
1558 case GST_STATE_PLAYING:
1559 basesink->need_preroll = FALSE;
1561 case GST_STATE_PAUSED:
1562 basesink->need_preroll = TRUE;
1565 basesink->need_preroll = FALSE;
1566 basesink->flushing = TRUE;
1569 /* we can report latency queries now */
1570 basesink->priv->have_latency = TRUE;
1571 GST_OBJECT_UNLOCK (basesink);
1576 /* app is going to READY */
1577 GST_DEBUG_OBJECT (basesink, "stopping");
1578 basesink->need_preroll = FALSE;
1579 basesink->flushing = TRUE;
1580 GST_OBJECT_UNLOCK (basesink);
1585 GST_DEBUG_OBJECT (basesink, "async commit failed");
1586 GST_STATE_RETURN (basesink) = GST_STATE_CHANGE_FAILURE;
1587 GST_OBJECT_UNLOCK (basesink);
1593 start_stepping (GstBaseSink * sink, GstSegment * segment,
1594 GstStepInfo * pending, GstStepInfo * current)
1597 GstMessage *message;
1599 GST_DEBUG_OBJECT (sink, "update pending step");
1601 GST_OBJECT_LOCK (sink);
1602 memcpy (current, pending, sizeof (GstStepInfo));
1603 pending->valid = FALSE;
1604 GST_OBJECT_UNLOCK (sink);
1606 /* post message first */
1608 gst_message_new_step_start (GST_OBJECT (sink), TRUE, current->format,
1609 current->amount, current->rate, current->flush, current->intermediate);
1610 gst_message_set_seqnum (message, current->seqnum);
1611 gst_element_post_message (GST_ELEMENT (sink), message);
1613 /* get the running time of where we paused and remember it */
1614 current->start = gst_element_get_start_time (GST_ELEMENT_CAST (sink));
1615 gst_segment_set_running_time (segment, GST_FORMAT_TIME, current->start);
1617 /* set the new rate for the remainder of the segment */
1618 current->start_rate = segment->rate;
1619 segment->rate *= current->rate;
1620 segment->abs_rate = ABS (segment->rate);
1623 if (segment->rate > 0.0)
1624 current->start_stop = segment->stop;
1626 current->start_start = segment->start;
1628 if (current->format == GST_FORMAT_TIME) {
1629 end = current->start + current->amount;
1630 if (!current->flush) {
1631 /* update the segment clipping regions for non-flushing seeks */
1632 if (segment->rate > 0.0) {
1633 segment->stop = gst_segment_to_position (segment, GST_FORMAT_TIME, end);
1634 segment->last_stop = segment->stop;
1638 position = gst_segment_to_position (segment, GST_FORMAT_TIME, end);
1639 segment->time = position;
1640 segment->start = position;
1641 segment->last_stop = position;
1646 GST_DEBUG_OBJECT (sink,
1647 "segment now rate %lf, applied rate %lf, "
1648 "format GST_FORMAT_TIME, "
1649 "%" GST_TIME_FORMAT " -- %" GST_TIME_FORMAT
1650 ", time %" GST_TIME_FORMAT ", accum %" GST_TIME_FORMAT,
1651 segment->rate, segment->applied_rate, GST_TIME_ARGS (segment->start),
1652 GST_TIME_ARGS (segment->stop), GST_TIME_ARGS (segment->time),
1653 GST_TIME_ARGS (segment->accum));
1655 GST_DEBUG_OBJECT (sink, "step started at running_time %" GST_TIME_FORMAT,
1656 GST_TIME_ARGS (current->start));
1658 if (current->amount == -1) {
1659 GST_DEBUG_OBJECT (sink, "step amount == -1, stop stepping");
1660 current->valid = FALSE;
1662 GST_DEBUG_OBJECT (sink, "step amount: %" G_GUINT64_FORMAT ", format: %s, "
1663 "rate: %f", current->amount, gst_format_get_name (current->format),
1669 stop_stepping (GstBaseSink * sink, GstSegment * segment,
1670 GstStepInfo * current, gint64 rstart, gint64 rstop, gboolean eos)
1672 gint64 stop, position;
1673 GstMessage *message;
1675 GST_DEBUG_OBJECT (sink, "step complete");
1677 if (segment->rate > 0.0)
1682 GST_DEBUG_OBJECT (sink,
1683 "step stop at running_time %" GST_TIME_FORMAT, GST_TIME_ARGS (stop));
1686 current->duration = current->position;
1688 current->duration = stop - current->start;
1690 GST_DEBUG_OBJECT (sink, "step elapsed running_time %" GST_TIME_FORMAT,
1691 GST_TIME_ARGS (current->duration));
1693 position = current->start + current->duration;
1695 /* now move the segment to the new running time */
1696 gst_segment_set_running_time (segment, GST_FORMAT_TIME, position);
1698 if (current->flush) {
1699 /* and remove the accumulated time we flushed, start time did not change */
1700 segment->accum = current->start;
1702 /* start time is now the stepped position */
1703 gst_element_set_start_time (GST_ELEMENT_CAST (sink), position);
1706 /* restore the previous rate */
1707 segment->rate = current->start_rate;
1708 segment->abs_rate = ABS (segment->rate);
1710 if (segment->rate > 0.0)
1711 segment->stop = current->start_stop;
1713 segment->start = current->start_start;
1715 /* the clip segment is used for position report in paused... */
1716 memcpy (sink->abidata.ABI.clip_segment, segment, sizeof (GstSegment));
1718 /* post the step done when we know the stepped duration in TIME */
1720 gst_message_new_step_done (GST_OBJECT_CAST (sink), current->format,
1721 current->amount, current->rate, current->flush, current->intermediate,
1722 current->duration, eos);
1723 gst_message_set_seqnum (message, current->seqnum);
1724 gst_element_post_message (GST_ELEMENT_CAST (sink), message);
1726 if (!current->intermediate)
1727 sink->need_preroll = current->need_preroll;
1729 /* and the current step info finished and becomes invalid */
1730 current->valid = FALSE;
1734 handle_stepping (GstBaseSink * sink, GstSegment * segment,
1735 GstStepInfo * current, gint64 * cstart, gint64 * cstop, gint64 * rstart,
1738 gboolean step_end = FALSE;
1740 /* see if we need to skip this buffer because of stepping */
1741 switch (current->format) {
1742 case GST_FORMAT_TIME:
1747 if (segment->rate > 0.0) {
1748 if (segment->stop == *cstop)
1749 *rstop = *rstart + current->amount;
1754 if (segment->start == *cstart)
1755 *rstart = *rstop + current->amount;
1761 end = current->start + current->amount;
1762 current->position = first - current->start;
1764 if (G_UNLIKELY (segment->abs_rate != 1.0))
1765 current->position /= segment->abs_rate;
1767 GST_DEBUG_OBJECT (sink,
1768 "buffer: %" GST_TIME_FORMAT "-%" GST_TIME_FORMAT,
1769 GST_TIME_ARGS (first), GST_TIME_ARGS (last));
1770 GST_DEBUG_OBJECT (sink,
1771 "got time step %" GST_TIME_FORMAT "-%" GST_TIME_FORMAT "/%"
1772 GST_TIME_FORMAT, GST_TIME_ARGS (current->position),
1773 GST_TIME_ARGS (last - current->start),
1774 GST_TIME_ARGS (current->amount));
1776 if ((current->flush && current->position >= current->amount)
1778 GST_DEBUG_OBJECT (sink, "step ended, we need clipping");
1780 if (segment->rate > 0.0) {
1782 *cstart = gst_segment_to_position (segment, GST_FORMAT_TIME, end);
1785 *cstop = gst_segment_to_position (segment, GST_FORMAT_TIME, end);
1788 GST_DEBUG_OBJECT (sink,
1789 "cstart %" GST_TIME_FORMAT ", rstart %" GST_TIME_FORMAT,
1790 GST_TIME_ARGS (*cstart), GST_TIME_ARGS (*rstart));
1791 GST_DEBUG_OBJECT (sink,
1792 "cstop %" GST_TIME_FORMAT ", rstop %" GST_TIME_FORMAT,
1793 GST_TIME_ARGS (*cstop), GST_TIME_ARGS (*rstop));
1796 case GST_FORMAT_BUFFERS:
1797 GST_DEBUG_OBJECT (sink,
1798 "got default step %" G_GUINT64_FORMAT "/%" G_GUINT64_FORMAT,
1799 current->position, current->amount);
1801 if (current->position < current->amount) {
1802 current->position++;
1807 case GST_FORMAT_DEFAULT:
1809 GST_DEBUG_OBJECT (sink,
1810 "got unknown step %" G_GUINT64_FORMAT "/%" G_GUINT64_FORMAT,
1811 current->position, current->amount);
1817 /* with STREAM_LOCK, PREROLL_LOCK
1819 * Returns TRUE if the object needs synchronisation and takes therefore
1820 * part in prerolling.
1822 * rsstart/rsstop contain the start/stop in stream time.
1823 * rrstart/rrstop contain the start/stop in running time.
1826 gst_base_sink_get_sync_times (GstBaseSink * basesink, GstMiniObject * obj,
1827 GstClockTime * rsstart, GstClockTime * rsstop,
1828 GstClockTime * rrstart, GstClockTime * rrstop, gboolean * do_sync,
1829 gboolean * stepped, GstSegment * segment, GstStepInfo * step,
1830 gboolean * step_end)
1832 GstBaseSinkClass *bclass;
1834 GstClockTime start, stop; /* raw start/stop timestamps */
1835 gint64 cstart, cstop; /* clipped raw timestamps */
1836 gint64 rstart, rstop; /* clipped timestamps converted to running time */
1837 GstClockTime sstart, sstop; /* clipped timestamps converted to stream time */
1839 GstBaseSinkPrivate *priv;
1842 priv = basesink->priv;
1844 /* start with nothing */
1845 start = stop = GST_CLOCK_TIME_NONE;
1847 if (G_UNLIKELY (GST_IS_EVENT (obj))) {
1848 GstEvent *event = GST_EVENT_CAST (obj);
1850 switch (GST_EVENT_TYPE (event)) {
1851 /* EOS event needs syncing */
1854 if (basesink->segment.rate >= 0.0) {
1855 sstart = sstop = priv->current_sstop;
1856 if (!GST_CLOCK_TIME_IS_VALID (sstart)) {
1857 /* we have not seen a buffer yet, use the segment values */
1858 sstart = sstop = gst_segment_to_stream_time (&basesink->segment,
1859 basesink->segment.format, basesink->segment.stop);
1862 sstart = sstop = priv->current_sstart;
1863 if (!GST_CLOCK_TIME_IS_VALID (sstart)) {
1864 /* we have not seen a buffer yet, use the segment values */
1865 sstart = sstop = gst_segment_to_stream_time (&basesink->segment,
1866 basesink->segment.format, basesink->segment.start);
1870 rstart = rstop = priv->eos_rtime;
1871 *do_sync = rstart != -1;
1872 GST_DEBUG_OBJECT (basesink, "sync times for EOS %" GST_TIME_FORMAT,
1873 GST_TIME_ARGS (rstart));
1874 /* if we are stepping, we end now */
1875 *step_end = step->valid;
1880 /* other events do not need syncing */
1881 /* FIXME, maybe NEWSEGMENT might need synchronisation
1882 * since the POSITION query depends on accumulated times and
1883 * we cannot accumulate the current segment before the previous
1892 /* else do buffer sync code */
1893 buffer = GST_BUFFER_CAST (obj);
1895 bclass = GST_BASE_SINK_GET_CLASS (basesink);
1897 /* just get the times to see if we need syncing, if the start returns -1 we
1899 if (bclass->get_times)
1900 bclass->get_times (basesink, buffer, &start, &stop);
1902 if (!GST_CLOCK_TIME_IS_VALID (start)) {
1903 /* we don't need to sync but we still want to get the timestamps for
1904 * tracking the position */
1905 gst_base_sink_get_times (basesink, buffer, &start, &stop);
1911 GST_DEBUG_OBJECT (basesink, "got times start: %" GST_TIME_FORMAT
1912 ", stop: %" GST_TIME_FORMAT ", do_sync %d", GST_TIME_ARGS (start),
1913 GST_TIME_ARGS (stop), *do_sync);
1915 /* collect segment and format for code clarity */
1916 format = segment->format;
1918 /* no timestamp clipping if we did not get a TIME segment format */
1919 if (G_UNLIKELY (format != GST_FORMAT_TIME)) {
1922 /* do running and stream time in TIME format */
1923 format = GST_FORMAT_TIME;
1924 GST_LOG_OBJECT (basesink, "not time format, don't clip");
1928 /* clip, only when we know about time */
1929 if (G_UNLIKELY (!gst_segment_clip (segment, GST_FORMAT_TIME,
1930 (gint64) start, (gint64) stop, &cstart, &cstop))) {
1932 GST_DEBUG_OBJECT (basesink, "step out of segment");
1933 /* when we are stepping, pretend we're at the end of the segment */
1934 if (segment->rate > 0.0) {
1935 cstart = segment->stop;
1936 cstop = segment->stop;
1938 cstart = segment->start;
1939 cstop = segment->start;
1943 goto out_of_segment;
1946 if (G_UNLIKELY (start != cstart || stop != cstop)) {
1947 GST_DEBUG_OBJECT (basesink, "clipped to: start %" GST_TIME_FORMAT
1948 ", stop: %" GST_TIME_FORMAT, GST_TIME_ARGS (cstart),
1949 GST_TIME_ARGS (cstop));
1952 /* set last stop position */
1953 if (G_LIKELY (cstop != GST_CLOCK_TIME_NONE))
1954 gst_segment_set_last_stop (segment, GST_FORMAT_TIME, cstop);
1956 gst_segment_set_last_stop (segment, GST_FORMAT_TIME, cstart);
1959 rstart = gst_segment_to_running_time (segment, format, cstart);
1960 rstop = gst_segment_to_running_time (segment, format, cstop);
1962 if (G_UNLIKELY (step->valid)) {
1963 if (!(*step_end = handle_stepping (basesink, segment, step, &cstart, &cstop,
1964 &rstart, &rstop))) {
1965 /* step is still busy, we discard data when we are flushing */
1966 *stepped = step->flush;
1967 GST_DEBUG_OBJECT (basesink, "stepping busy");
1970 /* this can produce wrong values if we accumulated non-TIME segments. If this happens,
1971 * upstream is behaving very badly */
1972 sstart = gst_segment_to_stream_time (segment, format, cstart);
1973 sstop = gst_segment_to_stream_time (segment, format, cstop);
1976 /* eos_done label only called when doing EOS, we also stop stepping then */
1977 if (*step_end && step->flush) {
1978 GST_DEBUG_OBJECT (basesink, "flushing step ended");
1979 stop_stepping (basesink, segment, step, rstart, rstop, eos);
1989 /* buffers and EOS always need syncing and preroll */
1995 /* we usually clip in the chain function already but stepping could cause
1996 * the segment to be updated later. we return FALSE so that we don't try
1998 GST_LOG_OBJECT (basesink, "buffer skipped, not in segment");
2003 /* with STREAM_LOCK, PREROLL_LOCK, LOCK
2004 * adjust a timestamp with the latency and timestamp offset. This function does
2005 * not adjust for the render delay. */
2007 gst_base_sink_adjust_time (GstBaseSink * basesink, GstClockTime time)
2009 GstClockTimeDiff ts_offset;
2011 /* don't do anything funny with invalid timestamps */
2012 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (time)))
2015 time += basesink->priv->latency;
2017 /* apply offset, be carefull for underflows */
2018 ts_offset = basesink->priv->ts_offset;
2019 if (ts_offset < 0) {
2020 ts_offset = -ts_offset;
2021 if (ts_offset < time)
2028 /* subtract the render delay again, which was included in the latency */
2029 if (time > basesink->priv->render_delay)
2030 time -= basesink->priv->render_delay;
2038 * gst_base_sink_wait_clock:
2040 * @time: the running_time to be reached
2041 * @jitter: the jitter to be filled with time diff (can be NULL)
2043 * This function will block until @time is reached. It is usually called by
2044 * subclasses that use their own internal synchronisation.
2046 * If @time is not valid, no sycnhronisation is done and #GST_CLOCK_BADTIME is
2047 * returned. Likewise, if synchronisation is disabled in the element or there
2048 * is no clock, no synchronisation is done and #GST_CLOCK_BADTIME is returned.
2050 * This function should only be called with the PREROLL_LOCK held, like when
2051 * receiving an EOS event in the #GstBaseSinkClass.event() vmethod or when
2052 * receiving a buffer in
2053 * the #GstBaseSinkClass.render() vmethod.
2055 * The @time argument should be the running_time of when this method should
2056 * return and is not adjusted with any latency or offset configured in the
2061 * Returns: #GstClockReturn
2064 gst_base_sink_wait_clock (GstBaseSink * sink, GstClockTime time,
2065 GstClockTimeDiff * jitter)
2070 GstClockTime base_time;
2072 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (time)))
2075 GST_OBJECT_LOCK (sink);
2076 if (G_UNLIKELY (!sink->sync))
2079 if (G_UNLIKELY ((clock = GST_ELEMENT_CLOCK (sink)) == NULL))
2082 base_time = GST_ELEMENT_CAST (sink)->base_time;
2083 GST_LOG_OBJECT (sink,
2084 "time %" GST_TIME_FORMAT ", base_time %" GST_TIME_FORMAT,
2085 GST_TIME_ARGS (time), GST_TIME_ARGS (base_time));
2087 /* add base_time to running_time to get the time against the clock */
2090 id = gst_clock_new_single_shot_id (clock, time);
2091 GST_OBJECT_UNLOCK (sink);
2093 /* A blocking wait is performed on the clock. We save the ClockID
2094 * so we can unlock the entry at any time. While we are blocking, we
2095 * release the PREROLL_LOCK so that other threads can interrupt the
2097 sink->clock_id = id;
2098 /* release the preroll lock while waiting */
2099 GST_PAD_PREROLL_UNLOCK (sink->sinkpad);
2101 ret = gst_clock_id_wait (id, jitter);
2103 GST_PAD_PREROLL_LOCK (sink->sinkpad);
2104 gst_clock_id_unref (id);
2105 sink->clock_id = NULL;
2109 /* no syncing needed */
2112 GST_DEBUG_OBJECT (sink, "time not valid, no sync needed");
2113 return GST_CLOCK_BADTIME;
2117 GST_DEBUG_OBJECT (sink, "sync disabled");
2118 GST_OBJECT_UNLOCK (sink);
2119 return GST_CLOCK_BADTIME;
2123 GST_DEBUG_OBJECT (sink, "no clock, can't sync");
2124 GST_OBJECT_UNLOCK (sink);
2125 return GST_CLOCK_BADTIME;
2130 * gst_base_sink_wait_preroll:
2133 * If the #GstBaseSinkClass.render() method performs its own synchronisation
2134 * against the clock it must unblock when going from PLAYING to the PAUSED state
2135 * and call this method before continuing to render the remaining data.
2137 * This function will block until a state change to PLAYING happens (in which
2138 * case this function returns #GST_FLOW_OK) or the processing must be stopped due
2139 * to a state change to READY or a FLUSH event (in which case this function
2140 * returns #GST_FLOW_WRONG_STATE).
2142 * This function should only be called with the PREROLL_LOCK held, like in the
2147 * Returns: #GST_FLOW_OK if the preroll completed and processing can
2148 * continue. Any other return value should be returned from the render vmethod.
2151 gst_base_sink_wait_preroll (GstBaseSink * sink)
2153 sink->have_preroll = TRUE;
2154 GST_DEBUG_OBJECT (sink, "waiting in preroll for flush or PLAYING");
2155 /* block until the state changes, or we get a flush, or something */
2156 GST_PAD_PREROLL_WAIT (sink->sinkpad);
2157 sink->have_preroll = FALSE;
2158 if (G_UNLIKELY (sink->flushing))
2160 if (G_UNLIKELY (sink->priv->step_unlock))
2162 GST_DEBUG_OBJECT (sink, "continue after preroll");
2169 GST_DEBUG_OBJECT (sink, "preroll interrupted because of flush");
2170 return GST_FLOW_WRONG_STATE;
2174 sink->priv->step_unlock = FALSE;
2175 GST_DEBUG_OBJECT (sink, "preroll interrupted because of step");
2176 return GST_FLOW_STEP;
2181 * gst_base_sink_do_preroll:
2183 * @obj: the object that caused the preroll
2185 * If the @sink spawns its own thread for pulling buffers from upstream it
2186 * should call this method after it has pulled a buffer. If the element needed
2187 * to preroll, this function will perform the preroll and will then block
2188 * until the element state is changed.
2190 * This function should be called with the PREROLL_LOCK held.
2194 * Returns: #GST_FLOW_OK if the preroll completed and processing can
2195 * continue. Any other return value should be returned from the render vmethod.
2198 gst_base_sink_do_preroll (GstBaseSink * sink, GstMiniObject * obj)
2202 while (G_UNLIKELY (sink->need_preroll)) {
2203 GST_DEBUG_OBJECT (sink, "prerolling object %p", obj);
2205 ret = gst_base_sink_preroll_object (sink, FALSE, obj);
2206 if (ret != GST_FLOW_OK)
2207 goto preroll_failed;
2209 /* need to recheck here because the commit state could have
2210 * made us not need the preroll anymore */
2211 if (G_LIKELY (sink->need_preroll)) {
2212 /* block until the state changes, or we get a flush, or something */
2213 ret = gst_base_sink_wait_preroll (sink);
2214 if ((ret != GST_FLOW_OK) && (ret != GST_FLOW_STEP))
2215 goto preroll_failed;
2223 GST_DEBUG_OBJECT (sink, "preroll failed %d", ret);
2229 * gst_base_sink_wait_eos:
2231 * @time: the running_time to be reached
2232 * @jitter: the jitter to be filled with time diff (can be NULL)
2234 * This function will block until @time is reached. It is usually called by
2235 * subclasses that use their own internal synchronisation but want to let the
2236 * EOS be handled by the base class.
2238 * This function should only be called with the PREROLL_LOCK held, like when
2239 * receiving an EOS event in the ::event vmethod.
2241 * The @time argument should be the running_time of when the EOS should happen
2242 * and will be adjusted with any latency and offset configured in the sink.
2246 * Returns: #GstFlowReturn
2249 gst_base_sink_wait_eos (GstBaseSink * sink, GstClockTime time,
2250 GstClockTimeDiff * jitter)
2252 GstClockReturn status;
2258 GST_DEBUG_OBJECT (sink, "checking preroll");
2260 /* first wait for the playing state before we can continue */
2261 if (G_UNLIKELY (sink->need_preroll)) {
2262 ret = gst_base_sink_wait_preroll (sink);
2263 if ((ret != GST_FLOW_OK) && (ret != GST_FLOW_STEP))
2267 /* preroll done, we can sync since we are in PLAYING now. */
2268 GST_DEBUG_OBJECT (sink, "possibly waiting for clock to reach %"
2269 GST_TIME_FORMAT, GST_TIME_ARGS (time));
2271 /* compensate for latency and ts_offset. We don't adjust for render delay
2272 * because we don't interact with the device on EOS normally. */
2273 stime = gst_base_sink_adjust_time (sink, time);
2275 /* wait for the clock, this can be interrupted because we got shut down or
2277 status = gst_base_sink_wait_clock (sink, stime, jitter);
2279 GST_DEBUG_OBJECT (sink, "clock returned %d", status);
2281 /* invalid time, no clock or sync disabled, just continue then */
2282 if (status == GST_CLOCK_BADTIME)
2285 /* waiting could have been interrupted and we can be flushing now */
2286 if (G_UNLIKELY (sink->flushing))
2289 /* retry if we got unscheduled, which means we did not reach the timeout
2290 * yet. if some other error occures, we continue. */
2291 } while (status == GST_CLOCK_UNSCHEDULED);
2293 GST_DEBUG_OBJECT (sink, "end of stream");
2300 GST_DEBUG_OBJECT (sink, "we are flushing");
2301 return GST_FLOW_WRONG_STATE;
2305 /* with STREAM_LOCK, PREROLL_LOCK
2307 * Make sure we are in PLAYING and synchronize an object to the clock.
2309 * If we need preroll, we are not in PLAYING. We try to commit the state
2310 * if needed and then block if we still are not PLAYING.
2312 * We start waiting on the clock in PLAYING. If we got interrupted, we
2313 * immediatly try to re-preroll.
2315 * Some objects do not need synchronisation (most events) and so this function
2316 * immediatly returns GST_FLOW_OK.
2318 * for objects that arrive later than max-lateness to be synchronized to the
2319 * clock have the @late boolean set to TRUE.
2321 * This function keeps a running average of the jitter (the diff between the
2322 * clock time and the requested sync time). The jitter is negative for
2323 * objects that arrive in time and positive for late buffers.
2325 * does not take ownership of obj.
2327 static GstFlowReturn
2328 gst_base_sink_do_sync (GstBaseSink * basesink, GstPad * pad,
2329 GstMiniObject * obj, gboolean * late, gboolean * step_end)
2331 GstClockTimeDiff jitter = 0;
2333 GstClockReturn status = GST_CLOCK_OK;
2334 GstClockTime rstart, rstop, sstart, sstop, stime;
2336 GstBaseSinkPrivate *priv;
2338 GstStepInfo *current, *pending;
2341 priv = basesink->priv;
2344 sstart = sstop = rstart = rstop = GST_CLOCK_TIME_NONE;
2348 priv->current_rstart = GST_CLOCK_TIME_NONE;
2350 /* get stepping info */
2351 current = &priv->current_step;
2352 pending = &priv->pending_step;
2354 /* get timing information for this object against the render segment */
2355 syncable = gst_base_sink_get_sync_times (basesink, obj,
2356 &sstart, &sstop, &rstart, &rstop, &do_sync, &stepped, &basesink->segment,
2359 if (G_UNLIKELY (stepped))
2362 /* a syncable object needs to participate in preroll and
2363 * clocking. All buffers and EOS are syncable. */
2364 if (G_UNLIKELY (!syncable))
2367 /* store timing info for current object */
2368 priv->current_rstart = rstart;
2369 priv->current_rstop = (GST_CLOCK_TIME_IS_VALID (rstop) ? rstop : rstart);
2371 /* save sync time for eos when the previous object needed sync */
2372 priv->eos_rtime = (do_sync ? priv->current_rstop : GST_CLOCK_TIME_NONE);
2375 /* first do preroll, this makes sure we commit our state
2376 * to PAUSED and can continue to PLAYING. We cannot perform
2377 * any clock sync in PAUSED because there is no clock. */
2378 ret = gst_base_sink_do_preroll (basesink, obj);
2379 if (G_UNLIKELY (ret != GST_FLOW_OK))
2380 goto preroll_failed;
2382 /* update the segment with a pending step if the current one is invalid and we
2383 * have a new pending one. We only accept new step updates after a preroll */
2384 if (G_UNLIKELY (pending->valid && !current->valid)) {
2385 start_stepping (basesink, &basesink->segment, pending, current);
2389 /* After rendering we store the position of the last buffer so that we can use
2390 * it to report the position. We need to take the lock here. */
2391 GST_OBJECT_LOCK (basesink);
2392 priv->current_sstart = sstart;
2393 priv->current_sstop = (GST_CLOCK_TIME_IS_VALID (sstop) ? sstop : sstart);
2394 GST_OBJECT_UNLOCK (basesink);
2399 /* adjust for latency */
2400 stime = gst_base_sink_adjust_time (basesink, rstart);
2402 /* adjust for render-delay, avoid underflows */
2403 if (GST_CLOCK_TIME_IS_VALID (stime)) {
2404 if (stime > priv->render_delay)
2405 stime -= priv->render_delay;
2410 /* preroll done, we can sync since we are in PLAYING now. */
2411 GST_DEBUG_OBJECT (basesink, "possibly waiting for clock to reach %"
2412 GST_TIME_FORMAT ", adjusted %" GST_TIME_FORMAT,
2413 GST_TIME_ARGS (rstart), GST_TIME_ARGS (stime));
2415 /* This function will return immediatly if start == -1, no clock
2416 * or sync is disabled with GST_CLOCK_BADTIME. */
2417 status = gst_base_sink_wait_clock (basesink, stime, &jitter);
2419 GST_DEBUG_OBJECT (basesink, "clock returned %d, jitter %c%" GST_TIME_FORMAT,
2420 status, (jitter < 0 ? '-' : ' '), GST_TIME_ARGS (ABS (jitter)));
2422 /* invalid time, no clock or sync disabled, just render */
2423 if (status == GST_CLOCK_BADTIME)
2426 /* waiting could have been interrupted and we can be flushing now */
2427 if (G_UNLIKELY (basesink->flushing))
2430 /* check for unlocked by a state change, we are not flushing so
2431 * we can try to preroll on the current buffer. */
2432 if (G_UNLIKELY (status == GST_CLOCK_UNSCHEDULED)) {
2433 GST_DEBUG_OBJECT (basesink, "unscheduled, waiting some more");
2434 priv->call_preroll = TRUE;
2438 /* successful syncing done, record observation */
2439 priv->current_jitter = jitter;
2441 /* check if the object should be dropped */
2442 *late = gst_base_sink_is_too_late (basesink, obj, rstart, rstop,
2451 GST_DEBUG_OBJECT (basesink, "skipped stepped object %p", obj);
2457 GST_DEBUG_OBJECT (basesink, "non syncable object %p", obj);
2462 GST_DEBUG_OBJECT (basesink, "we are flushing");
2463 return GST_FLOW_WRONG_STATE;
2467 GST_DEBUG_OBJECT (basesink, "preroll failed");
2474 gst_base_sink_send_qos (GstBaseSink * basesink,
2475 gdouble proportion, GstClockTime time, GstClockTimeDiff diff)
2480 /* generate Quality-of-Service event */
2481 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, basesink,
2482 "qos: proportion: %lf, diff %" G_GINT64_FORMAT ", timestamp %"
2483 GST_TIME_FORMAT, proportion, diff, GST_TIME_ARGS (time));
2485 event = gst_event_new_qos (proportion, diff, time);
2488 res = gst_pad_push_event (basesink->sinkpad, event);
2494 gst_base_sink_perform_qos (GstBaseSink * sink, gboolean dropped)
2496 GstBaseSinkPrivate *priv;
2497 GstClockTime start, stop;
2498 GstClockTimeDiff jitter;
2499 GstClockTime pt, entered, left;
2500 GstClockTime duration;
2505 start = priv->current_rstart;
2507 if (priv->current_step.valid)
2510 /* if Quality-of-Service disabled, do nothing */
2511 if (!g_atomic_int_get (&priv->qos_enabled) ||
2512 !GST_CLOCK_TIME_IS_VALID (start))
2515 stop = priv->current_rstop;
2516 jitter = priv->current_jitter;
2519 /* this is the time the buffer entered the sink */
2520 if (start < -jitter)
2523 entered = start + jitter;
2526 /* this is the time the buffer entered the sink */
2527 entered = start + jitter;
2528 /* this is the time the buffer left the sink */
2529 left = start + jitter;
2532 /* calculate duration of the buffer */
2533 if (GST_CLOCK_TIME_IS_VALID (stop))
2534 duration = stop - start;
2536 duration = GST_CLOCK_TIME_NONE;
2538 /* if we have the time when the last buffer left us, calculate
2539 * processing time */
2540 if (GST_CLOCK_TIME_IS_VALID (priv->last_left)) {
2541 if (entered > priv->last_left) {
2542 pt = entered - priv->last_left;
2550 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, sink, "start: %" GST_TIME_FORMAT
2551 ", entered %" GST_TIME_FORMAT ", left %" GST_TIME_FORMAT ", pt: %"
2552 GST_TIME_FORMAT ", duration %" GST_TIME_FORMAT ",jitter %"
2553 G_GINT64_FORMAT, GST_TIME_ARGS (start), GST_TIME_ARGS (entered),
2554 GST_TIME_ARGS (left), GST_TIME_ARGS (pt), GST_TIME_ARGS (duration),
2557 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, sink, "avg_duration: %" GST_TIME_FORMAT
2558 ", avg_pt: %" GST_TIME_FORMAT ", avg_rate: %g",
2559 GST_TIME_ARGS (priv->avg_duration), GST_TIME_ARGS (priv->avg_pt),
2562 /* collect running averages. for first observations, we copy the
2564 if (!GST_CLOCK_TIME_IS_VALID (priv->avg_duration))
2565 priv->avg_duration = duration;
2567 priv->avg_duration = UPDATE_RUNNING_AVG (priv->avg_duration, duration);
2569 if (!GST_CLOCK_TIME_IS_VALID (priv->avg_pt))
2572 priv->avg_pt = UPDATE_RUNNING_AVG (priv->avg_pt, pt);
2574 if (priv->avg_duration != 0)
2576 gst_guint64_to_gdouble (priv->avg_pt) /
2577 gst_guint64_to_gdouble (priv->avg_duration);
2581 if (GST_CLOCK_TIME_IS_VALID (priv->last_left)) {
2582 if (dropped || priv->avg_rate < 0.0) {
2583 priv->avg_rate = rate;
2586 priv->avg_rate = UPDATE_RUNNING_AVG_N (priv->avg_rate, rate);
2588 priv->avg_rate = UPDATE_RUNNING_AVG_P (priv->avg_rate, rate);
2592 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, sink,
2593 "updated: avg_duration: %" GST_TIME_FORMAT ", avg_pt: %" GST_TIME_FORMAT
2594 ", avg_rate: %g", GST_TIME_ARGS (priv->avg_duration),
2595 GST_TIME_ARGS (priv->avg_pt), priv->avg_rate);
2598 if (priv->avg_rate >= 0.0) {
2599 /* if we have a valid rate, start sending QoS messages */
2600 if (priv->current_jitter < 0) {
2601 /* make sure we never go below 0 when adding the jitter to the
2603 if (priv->current_rstart < -priv->current_jitter)
2604 priv->current_jitter = -priv->current_rstart;
2606 gst_base_sink_send_qos (sink, priv->avg_rate, priv->current_rstart,
2607 priv->current_jitter);
2610 /* record when this buffer will leave us */
2611 priv->last_left = left;
2614 /* reset all qos measuring */
2616 gst_base_sink_reset_qos (GstBaseSink * sink)
2618 GstBaseSinkPrivate *priv;
2622 priv->last_in_time = GST_CLOCK_TIME_NONE;
2623 priv->last_left = GST_CLOCK_TIME_NONE;
2624 priv->avg_duration = GST_CLOCK_TIME_NONE;
2625 priv->avg_pt = GST_CLOCK_TIME_NONE;
2626 priv->avg_rate = -1.0;
2627 priv->avg_render = GST_CLOCK_TIME_NONE;
2633 /* Checks if the object was scheduled too late.
2635 * start/stop contain the raw timestamp start and stop values
2638 * status and jitter contain the return values from the clock wait.
2640 * returns TRUE if the buffer was too late.
2643 gst_base_sink_is_too_late (GstBaseSink * basesink, GstMiniObject * obj,
2644 GstClockTime start, GstClockTime stop,
2645 GstClockReturn status, GstClockTimeDiff jitter)
2648 gint64 max_lateness;
2649 GstBaseSinkPrivate *priv;
2651 priv = basesink->priv;
2655 /* only for objects that were too late */
2656 if (G_LIKELY (status != GST_CLOCK_EARLY))
2659 max_lateness = basesink->abidata.ABI.max_lateness;
2661 /* check if frame dropping is enabled */
2662 if (max_lateness == -1)
2665 /* only check for buffers */
2666 if (G_UNLIKELY (!GST_IS_BUFFER (obj)))
2669 /* can't do check if we don't have a timestamp */
2670 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (start)))
2673 /* we can add a valid stop time */
2674 if (GST_CLOCK_TIME_IS_VALID (stop))
2675 max_lateness += stop;
2677 max_lateness += start;
2679 /* if the jitter bigger than duration and lateness we are too late */
2680 if ((late = start + jitter > max_lateness)) {
2681 GST_CAT_DEBUG_OBJECT (GST_CAT_PERFORMANCE, basesink,
2682 "buffer is too late %" GST_TIME_FORMAT
2683 " > %" GST_TIME_FORMAT, GST_TIME_ARGS (start + jitter),
2684 GST_TIME_ARGS (max_lateness));
2685 /* !!emergency!!, if we did not receive anything valid for more than a
2686 * second, render it anyway so the user sees something */
2687 if (GST_CLOCK_TIME_IS_VALID (priv->last_in_time) &&
2688 start - priv->last_in_time > GST_SECOND) {
2690 GST_ELEMENT_WARNING (basesink, CORE, CLOCK,
2691 (_("A lot of buffers are being dropped.")),
2692 ("There may be a timestamping problem, or this computer is too slow."));
2693 GST_CAT_DEBUG_OBJECT (GST_CAT_PERFORMANCE, basesink,
2694 "**emergency** last buffer at %" GST_TIME_FORMAT " > GST_SECOND",
2695 GST_TIME_ARGS (priv->last_in_time));
2700 if (!late || !GST_CLOCK_TIME_IS_VALID (priv->last_in_time)) {
2701 priv->last_in_time = start;
2708 GST_DEBUG_OBJECT (basesink, "object was scheduled in time");
2713 GST_DEBUG_OBJECT (basesink, "frame dropping disabled");
2718 GST_DEBUG_OBJECT (basesink, "object is not a buffer");
2723 GST_DEBUG_OBJECT (basesink, "buffer has no timestamp");
2728 /* called before and after calling the render vmethod. It keeps track of how
2729 * much time was spent in the render method and is used to check if we are
2732 gst_base_sink_do_render_stats (GstBaseSink * basesink, gboolean start)
2734 GstBaseSinkPrivate *priv;
2736 priv = basesink->priv;
2739 priv->start = gst_util_get_timestamp ();
2741 GstClockTime elapsed;
2743 priv->stop = gst_util_get_timestamp ();
2745 elapsed = GST_CLOCK_DIFF (priv->start, priv->stop);
2747 if (!GST_CLOCK_TIME_IS_VALID (priv->avg_render))
2748 priv->avg_render = elapsed;
2750 priv->avg_render = UPDATE_RUNNING_AVG (priv->avg_render, elapsed);
2752 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, basesink,
2753 "avg_render: %" GST_TIME_FORMAT, GST_TIME_ARGS (priv->avg_render));
2757 /* with STREAM_LOCK, PREROLL_LOCK,
2759 * Synchronize the object on the clock and then render it.
2761 * takes ownership of obj.
2763 static GstFlowReturn
2764 gst_base_sink_render_object (GstBaseSink * basesink, GstPad * pad,
2765 gboolean is_list, gpointer obj)
2768 GstBaseSinkClass *bclass;
2769 gboolean late, step_end;
2771 GstBaseSinkPrivate *priv;
2773 priv = basesink->priv;
2777 * If buffer list, use the first group buffer within the list
2780 sync_obj = gst_buffer_list_get (GST_BUFFER_LIST_CAST (obj), 0, 0);
2781 g_assert (NULL != sync_obj);
2790 /* synchronize this object, non syncable objects return OK
2792 ret = gst_base_sink_do_sync (basesink, pad, sync_obj, &late, &step_end);
2793 if (G_UNLIKELY (ret != GST_FLOW_OK))
2796 /* and now render, event or buffer/buffer list. */
2797 if (G_LIKELY (is_list || GST_IS_BUFFER (obj))) {
2798 /* drop late buffers unconditionally, let's hope it's unlikely */
2799 if (G_UNLIKELY (late))
2802 bclass = GST_BASE_SINK_GET_CLASS (basesink);
2804 if (G_LIKELY ((is_list && bclass->render_list) ||
2805 (!is_list && bclass->render))) {
2808 /* read once, to get same value before and after */
2809 do_qos = g_atomic_int_get (&priv->qos_enabled);
2811 GST_DEBUG_OBJECT (basesink, "rendering object %p", obj);
2813 /* record rendering time for QoS and stats */
2815 gst_base_sink_do_render_stats (basesink, TRUE);
2820 /* For buffer lists do not set last buffer. Creating buffer
2821 * with meaningful data can be done only with memcpy which will
2822 * significantly affect performance */
2823 buf = GST_BUFFER_CAST (obj);
2824 gst_base_sink_set_last_buffer (basesink, buf);
2826 ret = bclass->render (basesink, buf);
2828 GstBufferList *buflist;
2830 buflist = GST_BUFFER_LIST_CAST (obj);
2832 ret = bclass->render_list (basesink, buflist);
2836 gst_base_sink_do_render_stats (basesink, FALSE);
2838 if (ret == GST_FLOW_STEP)
2841 if (G_UNLIKELY (basesink->flushing))
2846 } else if (G_LIKELY (GST_IS_EVENT (obj))) {
2847 GstEvent *event = GST_EVENT_CAST (obj);
2848 gboolean event_res = TRUE;
2851 bclass = GST_BASE_SINK_GET_CLASS (basesink);
2853 type = GST_EVENT_TYPE (event);
2855 GST_DEBUG_OBJECT (basesink, "rendering event %p, type %s", obj,
2856 gst_event_type_get_name (type));
2859 event_res = bclass->event (basesink, event);
2861 /* when we get here we could be flushing again when the event handler calls
2862 * _wait_eos(). We have to ignore this object in that case. */
2863 if (G_UNLIKELY (basesink->flushing))
2866 if (G_LIKELY (event_res)) {
2869 seqnum = basesink->priv->seqnum = gst_event_get_seqnum (event);
2870 GST_DEBUG_OBJECT (basesink, "Got seqnum #%" G_GUINT32_FORMAT, seqnum);
2875 GstMessage *message;
2877 /* the EOS event is completely handled so we mark
2878 * ourselves as being in the EOS state. eos is also
2879 * protected by the object lock so we can read it when
2880 * answering the POSITION query. */
2881 GST_OBJECT_LOCK (basesink);
2882 basesink->eos = TRUE;
2883 GST_OBJECT_UNLOCK (basesink);
2885 /* ok, now we can post the message */
2886 GST_DEBUG_OBJECT (basesink, "Now posting EOS");
2888 message = gst_message_new_eos (GST_OBJECT_CAST (basesink));
2889 gst_message_set_seqnum (message, seqnum);
2890 gst_element_post_message (GST_ELEMENT_CAST (basesink), message);
2893 case GST_EVENT_NEWSEGMENT:
2894 /* configure the segment */
2895 gst_base_sink_configure_segment (basesink, pad, event,
2896 &basesink->segment);
2898 case GST_EVENT_SINK_MESSAGE:{
2899 GstMessage *msg = NULL;
2901 gst_event_parse_sink_message (event, &msg);
2904 gst_element_post_message (GST_ELEMENT_CAST (basesink), msg);
2911 g_return_val_if_reached (GST_FLOW_ERROR);
2916 /* the step ended, check if we need to activate a new step */
2917 GST_DEBUG_OBJECT (basesink, "step ended");
2918 stop_stepping (basesink, &basesink->segment, &priv->current_step,
2919 priv->current_rstart, priv->current_rstop, basesink->eos);
2923 gst_base_sink_perform_qos (basesink, late);
2925 GST_DEBUG_OBJECT (basesink, "object unref after render %p", obj);
2926 gst_mini_object_unref (GST_MINI_OBJECT_CAST (obj));
2932 GST_DEBUG_OBJECT (basesink, "do_sync returned %s", gst_flow_get_name (ret));
2938 GST_DEBUG_OBJECT (basesink, "buffer late, dropping");
2940 if (g_atomic_int_get (&priv->qos_enabled)) {
2941 GstMessage *qos_msg;
2942 GstClockTime timestamp, duration;
2944 timestamp = GST_BUFFER_TIMESTAMP (GST_BUFFER_CAST (sync_obj));
2945 duration = GST_BUFFER_DURATION (GST_BUFFER_CAST (sync_obj));
2947 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, basesink,
2948 "qos: dropped buffer rt %" GST_TIME_FORMAT ", st %" GST_TIME_FORMAT
2949 ", ts %" GST_TIME_FORMAT ", dur %" GST_TIME_FORMAT,
2950 GST_TIME_ARGS (priv->current_rstart),
2951 GST_TIME_ARGS (priv->current_sstart), GST_TIME_ARGS (timestamp),
2952 GST_TIME_ARGS (duration));
2953 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, basesink,
2954 "qos: rendered %" G_GUINT64_FORMAT ", dropped %" G_GUINT64_FORMAT,
2955 priv->rendered, priv->dropped);
2958 gst_message_new_qos (GST_OBJECT_CAST (basesink), basesink->sync,
2959 priv->current_rstart, priv->current_sstart, timestamp, duration);
2960 gst_message_set_qos_values (qos_msg, priv->current_jitter, priv->avg_rate,
2962 gst_message_set_qos_stats (qos_msg, GST_FORMAT_BUFFERS, priv->rendered,
2964 gst_element_post_message (GST_ELEMENT_CAST (basesink), qos_msg);
2970 GST_DEBUG_OBJECT (basesink, "we are flushing, ignore object");
2971 gst_mini_object_unref (obj);
2972 return GST_FLOW_WRONG_STATE;
2976 /* with STREAM_LOCK, PREROLL_LOCK
2978 * Perform preroll on the given object. For buffers this means
2979 * calling the preroll subclass method.
2980 * If that succeeds, the state will be commited.
2982 * function does not take ownership of obj.
2984 static GstFlowReturn
2985 gst_base_sink_preroll_object (GstBaseSink * basesink, gboolean is_list,
2986 GstMiniObject * obj)
2990 GST_DEBUG_OBJECT (basesink, "prerolling object %p", obj);
2992 /* if it's a buffer, we need to call the preroll method */
2993 if (G_LIKELY (is_list || GST_IS_BUFFER (obj)) && basesink->priv->call_preroll) {
2994 GstBaseSinkClass *bclass;
2996 GstClockTime timestamp;
2999 buf = gst_buffer_list_get (GST_BUFFER_LIST_CAST (obj), 0, 0);
3000 g_assert (NULL != buf);
3002 buf = GST_BUFFER_CAST (obj);
3005 timestamp = GST_BUFFER_TIMESTAMP (buf);
3007 GST_DEBUG_OBJECT (basesink, "preroll buffer %" GST_TIME_FORMAT,
3008 GST_TIME_ARGS (timestamp));
3011 * For buffer lists do not set last buffer. Creating buffer
3012 * with meaningful data can be done only with memcpy which will
3013 * significantly affect performance
3016 gst_base_sink_set_last_buffer (basesink, buf);
3019 bclass = GST_BASE_SINK_GET_CLASS (basesink);
3020 if (bclass->preroll)
3021 if ((ret = bclass->preroll (basesink, buf)) != GST_FLOW_OK)
3022 goto preroll_failed;
3024 basesink->priv->call_preroll = FALSE;
3028 if (G_LIKELY (basesink->playing_async)) {
3029 if (G_UNLIKELY (!gst_base_sink_commit_state (basesink)))
3038 GST_DEBUG_OBJECT (basesink, "preroll failed, abort state");
3039 gst_element_abort_state (GST_ELEMENT_CAST (basesink));
3044 GST_DEBUG_OBJECT (basesink, "stopping while commiting state");
3045 return GST_FLOW_WRONG_STATE;
3049 /* with STREAM_LOCK, PREROLL_LOCK
3051 * Queue an object for rendering.
3052 * The first prerollable object queued will complete the preroll. If the
3053 * preroll queue if filled, we render all the objects in the queue.
3055 * This function takes ownership of the object.
3057 static GstFlowReturn
3058 gst_base_sink_queue_object_unlocked (GstBaseSink * basesink, GstPad * pad,
3059 gboolean is_list, gpointer obj, gboolean prerollable)
3061 GstFlowReturn ret = GST_FLOW_OK;
3065 if (G_UNLIKELY (basesink->need_preroll)) {
3066 if (G_LIKELY (prerollable))
3067 basesink->preroll_queued++;
3069 length = basesink->preroll_queued;
3071 GST_DEBUG_OBJECT (basesink, "now %d prerolled items", length);
3073 /* first prerollable item needs to finish the preroll */
3075 ret = gst_base_sink_preroll_object (basesink, is_list, obj);
3076 if (G_UNLIKELY (ret != GST_FLOW_OK))
3077 goto preroll_failed;
3079 /* need to recheck if we need preroll, commmit state during preroll
3080 * could have made us not need more preroll. */
3081 if (G_UNLIKELY (basesink->need_preroll)) {
3082 /* see if we can render now, if we can't add the object to the preroll
3084 if (G_UNLIKELY (length <= basesink->preroll_queue_max_len))
3088 /* we can start rendering (or blocking) the queued object
3090 q = basesink->preroll_queue;
3091 while (G_UNLIKELY (!g_queue_is_empty (q))) {
3094 o = g_queue_pop_head (q);
3095 GST_DEBUG_OBJECT (basesink, "rendering queued object %p", o);
3097 /* do something with the return value */
3098 ret = gst_base_sink_render_object (basesink, pad, FALSE, o);
3099 if (ret != GST_FLOW_OK)
3100 goto dequeue_failed;
3103 /* now render the object */
3104 ret = gst_base_sink_render_object (basesink, pad, is_list, obj);
3105 basesink->preroll_queued = 0;
3112 GST_DEBUG_OBJECT (basesink, "preroll failed, reason %s",
3113 gst_flow_get_name (ret));
3114 gst_mini_object_unref (GST_MINI_OBJECT_CAST (obj));
3119 /* add object to the queue and return */
3120 GST_DEBUG_OBJECT (basesink, "need more preroll data %d <= %d",
3121 length, basesink->preroll_queue_max_len);
3122 g_queue_push_tail (basesink->preroll_queue, obj);
3127 GST_DEBUG_OBJECT (basesink, "rendering queued objects failed, reason %s",
3128 gst_flow_get_name (ret));
3129 gst_mini_object_unref (GST_MINI_OBJECT_CAST (obj));
3136 * This function grabs the PREROLL_LOCK and adds the object to
3139 * This function takes ownership of obj.
3141 static GstFlowReturn
3142 gst_base_sink_queue_object (GstBaseSink * basesink, GstPad * pad,
3143 GstMiniObject * obj, gboolean prerollable)
3147 GST_PAD_PREROLL_LOCK (pad);
3148 if (G_UNLIKELY (basesink->flushing))
3151 if (G_UNLIKELY (basesink->priv->received_eos))
3155 gst_base_sink_queue_object_unlocked (basesink, pad, FALSE, obj,
3157 GST_PAD_PREROLL_UNLOCK (pad);
3164 GST_DEBUG_OBJECT (basesink, "sink is flushing");
3165 GST_PAD_PREROLL_UNLOCK (pad);
3166 gst_mini_object_unref (obj);
3167 return GST_FLOW_WRONG_STATE;
3171 GST_DEBUG_OBJECT (basesink,
3172 "we are EOS, dropping object, return UNEXPECTED");
3173 GST_PAD_PREROLL_UNLOCK (pad);
3174 gst_mini_object_unref (obj);
3175 return GST_FLOW_UNEXPECTED;
3180 gst_base_sink_flush_start (GstBaseSink * basesink, GstPad * pad)
3182 /* make sure we are not blocked on the clock also clear any pending
3184 gst_base_sink_set_flushing (basesink, pad, TRUE);
3186 /* we grab the stream lock but that is not needed since setting the
3187 * sink to flushing would make sure no state commit is being done
3189 GST_PAD_STREAM_LOCK (pad);
3190 gst_base_sink_reset_qos (basesink);
3191 if (basesink->priv->async_enabled) {
3192 /* and we need to commit our state again on the next
3193 * prerolled buffer */
3194 basesink->playing_async = TRUE;
3195 gst_element_lost_state (GST_ELEMENT_CAST (basesink));
3197 basesink->priv->have_latency = TRUE;
3198 basesink->need_preroll = FALSE;
3200 gst_base_sink_set_last_buffer (basesink, NULL);
3201 GST_PAD_STREAM_UNLOCK (pad);
3205 gst_base_sink_flush_stop (GstBaseSink * basesink, GstPad * pad)
3207 /* unset flushing so we can accept new data, this also flushes out any EOS
3209 gst_base_sink_set_flushing (basesink, pad, FALSE);
3211 /* for position reporting */
3212 GST_OBJECT_LOCK (basesink);
3213 basesink->priv->current_sstart = GST_CLOCK_TIME_NONE;
3214 basesink->priv->current_sstop = GST_CLOCK_TIME_NONE;
3215 basesink->priv->eos_rtime = GST_CLOCK_TIME_NONE;
3216 basesink->priv->call_preroll = TRUE;
3217 basesink->priv->current_step.valid = FALSE;
3218 basesink->priv->pending_step.valid = FALSE;
3219 if (basesink->pad_mode == GST_ACTIVATE_PUSH) {
3220 /* we need new segment info after the flush. */
3221 basesink->have_newsegment = FALSE;
3222 gst_segment_init (&basesink->segment, GST_FORMAT_UNDEFINED);
3223 gst_segment_init (basesink->abidata.ABI.clip_segment, GST_FORMAT_UNDEFINED);
3225 GST_OBJECT_UNLOCK (basesink);
3229 gst_base_sink_event (GstPad * pad, GstEvent * event)
3231 GstBaseSink *basesink;
3232 gboolean result = TRUE;
3233 GstBaseSinkClass *bclass;
3235 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
3237 bclass = GST_BASE_SINK_GET_CLASS (basesink);
3239 GST_DEBUG_OBJECT (basesink, "reveived event %p %" GST_PTR_FORMAT, event,
3242 switch (GST_EVENT_TYPE (event)) {
3247 GST_PAD_PREROLL_LOCK (pad);
3248 if (G_UNLIKELY (basesink->flushing))
3251 if (G_UNLIKELY (basesink->priv->received_eos)) {
3252 /* we can't accept anything when we are EOS */
3254 gst_event_unref (event);
3256 /* we set the received EOS flag here so that we can use it when testing if
3257 * we are prerolled and to refuse more buffers. */
3258 basesink->priv->received_eos = TRUE;
3260 /* EOS is a prerollable object, we call the unlocked version because it
3261 * does not check the received_eos flag. */
3262 ret = gst_base_sink_queue_object_unlocked (basesink, pad,
3263 FALSE, GST_MINI_OBJECT_CAST (event), TRUE);
3264 if (G_UNLIKELY (ret != GST_FLOW_OK))
3267 GST_PAD_PREROLL_UNLOCK (pad);
3270 case GST_EVENT_NEWSEGMENT:
3275 GST_DEBUG_OBJECT (basesink, "newsegment %p", event);
3277 GST_PAD_PREROLL_LOCK (pad);
3278 if (G_UNLIKELY (basesink->flushing))
3281 gst_event_parse_new_segment_full (event, &update, NULL, NULL, NULL, NULL,
3284 if (G_UNLIKELY (basesink->priv->received_eos && !update)) {
3285 /* we can't accept anything when we are EOS */
3287 gst_event_unref (event);
3289 /* the new segment is a non prerollable item and does not block anything,
3290 * we need to configure the current clipping segment and insert the event
3291 * in the queue to serialize it with the buffers for rendering. */
3292 gst_base_sink_configure_segment (basesink, pad, event,
3293 basesink->abidata.ABI.clip_segment);
3296 gst_base_sink_queue_object_unlocked (basesink, pad,
3297 FALSE, GST_MINI_OBJECT_CAST (event), FALSE);
3298 if (G_UNLIKELY (ret != GST_FLOW_OK))
3301 GST_OBJECT_LOCK (basesink);
3302 basesink->have_newsegment = TRUE;
3303 GST_OBJECT_UNLOCK (basesink);
3306 GST_PAD_PREROLL_UNLOCK (pad);
3309 case GST_EVENT_FLUSH_START:
3311 bclass->event (basesink, event);
3313 GST_DEBUG_OBJECT (basesink, "flush-start %p", event);
3315 gst_base_sink_flush_start (basesink, pad);
3317 gst_event_unref (event);
3319 case GST_EVENT_FLUSH_STOP:
3321 bclass->event (basesink, event);
3323 GST_DEBUG_OBJECT (basesink, "flush-stop %p", event);
3325 gst_base_sink_flush_stop (basesink, pad);
3327 gst_event_unref (event);
3330 /* other events are sent to queue or subclass depending on if they
3331 * are serialized. */
3332 if (GST_EVENT_IS_SERIALIZED (event)) {
3333 gst_base_sink_queue_object (basesink, pad,
3334 GST_MINI_OBJECT_CAST (event), FALSE);
3337 bclass->event (basesink, event);
3338 gst_event_unref (event);
3343 gst_object_unref (basesink);
3350 GST_DEBUG_OBJECT (basesink, "we are flushing");
3351 GST_PAD_PREROLL_UNLOCK (pad);
3353 gst_event_unref (event);
3358 /* default implementation to calculate the start and end
3359 * timestamps on a buffer, subclasses can override
3362 gst_base_sink_get_times (GstBaseSink * basesink, GstBuffer * buffer,
3363 GstClockTime * start, GstClockTime * end)
3365 GstClockTime timestamp, duration;
3367 timestamp = GST_BUFFER_TIMESTAMP (buffer);
3368 if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
3370 /* get duration to calculate end time */
3371 duration = GST_BUFFER_DURATION (buffer);
3372 if (GST_CLOCK_TIME_IS_VALID (duration)) {
3373 *end = timestamp + duration;
3379 /* must be called with PREROLL_LOCK */
3381 gst_base_sink_needs_preroll (GstBaseSink * basesink)
3383 gboolean is_prerolled, res;
3385 /* we have 2 cases where the PREROLL_LOCK is released:
3386 * 1) we are blocking in the PREROLL_LOCK and thus are prerolled.
3387 * 2) we are syncing on the clock
3389 is_prerolled = basesink->have_preroll || basesink->priv->received_eos;
3390 res = !is_prerolled;
3392 GST_DEBUG_OBJECT (basesink, "have_preroll: %d, EOS: %d => needs preroll: %d",
3393 basesink->have_preroll, basesink->priv->received_eos, res);
3398 /* with STREAM_LOCK, PREROLL_LOCK
3400 * Takes a buffer and compare the timestamps with the last segment.
3401 * If the buffer falls outside of the segment boundaries, drop it.
3402 * Else queue the buffer for preroll and rendering.
3404 * This function takes ownership of the buffer.
3406 static GstFlowReturn
3407 gst_base_sink_chain_unlocked (GstBaseSink * basesink, GstPad * pad,
3408 gboolean is_list, gpointer obj)
3410 GstBaseSinkClass *bclass;
3411 GstFlowReturn result;
3412 GstClockTime start = GST_CLOCK_TIME_NONE, end = GST_CLOCK_TIME_NONE;
3413 GstSegment *clip_segment;
3414 GstBuffer *time_buf;
3416 if (G_UNLIKELY (basesink->flushing))
3419 if (G_UNLIKELY (basesink->priv->received_eos))
3423 time_buf = gst_buffer_list_get (GST_BUFFER_LIST_CAST (obj), 0, 0);
3424 g_assert (NULL != time_buf);
3426 time_buf = GST_BUFFER_CAST (obj);
3429 /* for code clarity */
3430 clip_segment = basesink->abidata.ABI.clip_segment;
3432 if (G_UNLIKELY (!basesink->have_newsegment)) {
3435 sync = gst_base_sink_get_sync (basesink);
3437 GST_ELEMENT_WARNING (basesink, STREAM, FAILED,
3438 (_("Internal data flow problem.")),
3439 ("Received buffer without a new-segment. Assuming timestamps start from 0."));
3442 /* this means this sink will assume timestamps start from 0 */
3443 GST_OBJECT_LOCK (basesink);
3444 clip_segment->start = 0;
3445 clip_segment->stop = -1;
3446 basesink->segment.start = 0;
3447 basesink->segment.stop = -1;
3448 basesink->have_newsegment = TRUE;
3449 GST_OBJECT_UNLOCK (basesink);
3452 bclass = GST_BASE_SINK_GET_CLASS (basesink);
3454 /* check if the buffer needs to be dropped, we first ask the subclass for the
3456 if (bclass->get_times)
3457 bclass->get_times (basesink, time_buf, &start, &end);
3459 if (!GST_CLOCK_TIME_IS_VALID (start)) {
3460 /* if the subclass does not want sync, we use our own values so that we at
3461 * least clip the buffer to the segment */
3462 gst_base_sink_get_times (basesink, time_buf, &start, &end);
3465 GST_DEBUG_OBJECT (basesink, "got times start: %" GST_TIME_FORMAT
3466 ", end: %" GST_TIME_FORMAT, GST_TIME_ARGS (start), GST_TIME_ARGS (end));
3468 /* a dropped buffer does not participate in anything */
3469 if (GST_CLOCK_TIME_IS_VALID (start) &&
3470 (clip_segment->format == GST_FORMAT_TIME)) {
3471 if (G_UNLIKELY (!gst_segment_clip (clip_segment,
3472 GST_FORMAT_TIME, (gint64) start, (gint64) end, NULL, NULL)))
3473 goto out_of_segment;
3476 /* now we can process the buffer in the queue, this function takes ownership
3478 result = gst_base_sink_queue_object_unlocked (basesink, pad,
3479 is_list, obj, TRUE);
3485 GST_DEBUG_OBJECT (basesink, "sink is flushing");
3486 gst_mini_object_unref (GST_MINI_OBJECT_CAST (obj));
3487 return GST_FLOW_WRONG_STATE;
3491 GST_DEBUG_OBJECT (basesink,
3492 "we are EOS, dropping object, return UNEXPECTED");
3493 gst_mini_object_unref (GST_MINI_OBJECT_CAST (obj));
3494 return GST_FLOW_UNEXPECTED;
3498 GST_DEBUG_OBJECT (basesink, "dropping buffer, out of clipping segment");
3499 gst_mini_object_unref (GST_MINI_OBJECT_CAST (obj));
3506 static GstFlowReturn
3507 gst_base_sink_chain_main (GstBaseSink * basesink, GstPad * pad,
3508 gboolean is_list, gpointer obj)
3510 GstFlowReturn result;
3512 if (G_UNLIKELY (basesink->pad_mode != GST_ACTIVATE_PUSH))
3515 GST_PAD_PREROLL_LOCK (pad);
3516 result = gst_base_sink_chain_unlocked (basesink, pad, is_list, obj);
3517 GST_PAD_PREROLL_UNLOCK (pad);
3525 GST_OBJECT_LOCK (pad);
3526 GST_WARNING_OBJECT (basesink,
3527 "Push on pad %s:%s, but it was not activated in push mode",
3528 GST_DEBUG_PAD_NAME (pad));
3529 GST_OBJECT_UNLOCK (pad);
3530 gst_mini_object_unref (GST_MINI_OBJECT_CAST (obj));
3531 /* we don't post an error message this will signal to the peer
3532 * pushing that EOS is reached. */
3533 result = GST_FLOW_UNEXPECTED;
3538 static GstFlowReturn
3539 gst_base_sink_chain (GstPad * pad, GstBuffer * buf)
3541 GstBaseSink *basesink;
3543 basesink = GST_BASE_SINK (GST_OBJECT_PARENT (pad));
3545 return gst_base_sink_chain_main (basesink, pad, FALSE, buf);
3548 static GstFlowReturn
3549 gst_base_sink_chain_list (GstPad * pad, GstBufferList * list)
3551 GstBaseSink *basesink;
3552 GstBaseSinkClass *bclass;
3553 GstFlowReturn result;
3555 basesink = GST_BASE_SINK (GST_OBJECT_PARENT (pad));
3556 bclass = GST_BASE_SINK_GET_CLASS (basesink);
3558 if (G_LIKELY (bclass->render_list)) {
3559 result = gst_base_sink_chain_main (basesink, pad, TRUE, list);
3561 GstBufferListIterator *it;
3564 GST_INFO_OBJECT (pad, "chaining each group in list as a merged buffer");
3566 it = gst_buffer_list_iterate (list);
3568 if (gst_buffer_list_iterator_next_group (it)) {
3570 group = gst_buffer_list_iterator_merge_group (it);
3571 if (group == NULL) {
3572 group = gst_buffer_new ();
3573 GST_CAT_INFO_OBJECT (GST_CAT_SCHEDULING, pad, "chaining empty group");
3575 GST_CAT_INFO_OBJECT (GST_CAT_SCHEDULING, pad, "chaining group");
3577 result = gst_base_sink_chain_main (basesink, pad, FALSE, group);
3578 } while (result == GST_FLOW_OK
3579 && gst_buffer_list_iterator_next_group (it));
3581 GST_CAT_INFO_OBJECT (GST_CAT_SCHEDULING, pad, "chaining empty group");
3583 gst_base_sink_chain_main (basesink, pad, FALSE, gst_buffer_new ());
3585 gst_buffer_list_iterator_free (it);
3586 gst_buffer_list_unref (list);
3593 gst_base_sink_default_do_seek (GstBaseSink * sink, GstSegment * segment)
3595 gboolean res = TRUE;
3597 /* update our offset if the start/stop position was updated */
3598 if (segment->format == GST_FORMAT_BYTES) {
3599 segment->time = segment->start;
3600 } else if (segment->start == 0) {
3601 /* seek to start, we can implement a default for this. */
3605 GST_INFO_OBJECT (sink, "Can't do a default seek");
3611 #define SEEK_TYPE_IS_RELATIVE(t) (((t) != GST_SEEK_TYPE_NONE) && ((t) != GST_SEEK_TYPE_SET))
3614 gst_base_sink_default_prepare_seek_segment (GstBaseSink * sink,
3615 GstEvent * event, GstSegment * segment)
3617 /* By default, we try one of 2 things:
3618 * - For absolute seek positions, convert the requested position to our
3619 * configured processing format and place it in the output segment \
3620 * - For relative seek positions, convert our current (input) values to the
3621 * seek format, adjust by the relative seek offset and then convert back to
3622 * the processing format
3624 GstSeekType cur_type, stop_type;
3627 GstFormat seek_format, dest_format;
3630 gboolean res = TRUE;
3632 gst_event_parse_seek (event, &rate, &seek_format, &flags,
3633 &cur_type, &cur, &stop_type, &stop);
3634 dest_format = segment->format;
3636 if (seek_format == dest_format) {
3637 gst_segment_set_seek (segment, rate, seek_format, flags,
3638 cur_type, cur, stop_type, stop, &update);
3642 if (cur_type != GST_SEEK_TYPE_NONE) {
3643 /* FIXME: Handle seek_cur & seek_end by converting the input segment vals */
3645 gst_pad_query_convert (sink->sinkpad, seek_format, cur, &dest_format,
3647 cur_type = GST_SEEK_TYPE_SET;
3650 if (res && stop_type != GST_SEEK_TYPE_NONE) {
3651 /* FIXME: Handle seek_cur & seek_end by converting the input segment vals */
3653 gst_pad_query_convert (sink->sinkpad, seek_format, stop, &dest_format,
3655 stop_type = GST_SEEK_TYPE_SET;
3658 /* And finally, configure our output segment in the desired format */
3659 gst_segment_set_seek (segment, rate, dest_format, flags, cur_type, cur,
3660 stop_type, stop, &update);
3669 GST_DEBUG_OBJECT (sink, "undefined format given, seek aborted.");
3674 /* perform a seek, only executed in pull mode */
3676 gst_base_sink_perform_seek (GstBaseSink * sink, GstPad * pad, GstEvent * event)
3680 GstFormat seek_format, dest_format;
3682 GstSeekType cur_type, stop_type;
3683 gboolean seekseg_configured = FALSE;
3685 gboolean update, res = TRUE;
3686 GstSegment seeksegment;
3688 dest_format = sink->segment.format;
3691 GST_DEBUG_OBJECT (sink, "performing seek with event %p", event);
3692 gst_event_parse_seek (event, &rate, &seek_format, &flags,
3693 &cur_type, &cur, &stop_type, &stop);
3695 flush = flags & GST_SEEK_FLAG_FLUSH;
3697 GST_DEBUG_OBJECT (sink, "performing seek without event");
3702 GST_DEBUG_OBJECT (sink, "flushing upstream");
3703 gst_pad_push_event (pad, gst_event_new_flush_start ());
3704 gst_base_sink_flush_start (sink, pad);
3706 GST_DEBUG_OBJECT (sink, "pausing pulling thread");
3709 GST_PAD_STREAM_LOCK (pad);
3711 /* If we configured the seeksegment above, don't overwrite it now. Otherwise
3712 * copy the current segment info into the temp segment that we can actually
3713 * attempt the seek with. We only update the real segment if the seek suceeds. */
3714 if (!seekseg_configured) {
3715 memcpy (&seeksegment, &sink->segment, sizeof (GstSegment));
3717 /* now configure the final seek segment */
3719 if (sink->segment.format != seek_format) {
3720 /* OK, here's where we give the subclass a chance to convert the relative
3721 * seek into an absolute one in the processing format. We set up any
3722 * absolute seek above, before taking the stream lock. */
3723 if (!gst_base_sink_default_prepare_seek_segment (sink, event,
3725 GST_DEBUG_OBJECT (sink,
3726 "Preparing the seek failed after flushing. " "Aborting seek");
3730 /* The seek format matches our processing format, no need to ask the
3731 * the subclass to configure the segment. */
3732 gst_segment_set_seek (&seeksegment, rate, seek_format, flags,
3733 cur_type, cur, stop_type, stop, &update);
3736 /* Else, no seek event passed, so we're just (re)starting the
3741 GST_DEBUG_OBJECT (sink, "segment configured from %" G_GINT64_FORMAT
3742 " to %" G_GINT64_FORMAT ", position %" G_GINT64_FORMAT,
3743 seeksegment.start, seeksegment.stop, seeksegment.last_stop);
3745 /* do the seek, segment.last_stop contains the new position. */
3746 res = gst_base_sink_default_do_seek (sink, &seeksegment);
3751 GST_DEBUG_OBJECT (sink, "stop flushing upstream");
3752 gst_pad_push_event (pad, gst_event_new_flush_stop ());
3753 gst_base_sink_flush_stop (sink, pad);
3754 } else if (res && sink->abidata.ABI.running) {
3755 /* we are running the current segment and doing a non-flushing seek,
3756 * close the segment first based on the last_stop. */
3757 GST_DEBUG_OBJECT (sink, "closing running segment %" G_GINT64_FORMAT
3758 " to %" G_GINT64_FORMAT, sink->segment.start, sink->segment.last_stop);
3761 /* The subclass must have converted the segment to the processing format
3763 if (res && seeksegment.format != dest_format) {
3764 GST_DEBUG_OBJECT (sink, "Subclass failed to prepare a seek segment "
3765 "in the correct format. Aborting seek.");
3769 /* if successfull seek, we update our real segment and push
3770 * out the new segment. */
3772 memcpy (&sink->segment, &seeksegment, sizeof (GstSegment));
3774 if (sink->segment.flags & GST_SEEK_FLAG_SEGMENT) {
3775 gst_element_post_message (GST_ELEMENT (sink),
3776 gst_message_new_segment_start (GST_OBJECT (sink),
3777 sink->segment.format, sink->segment.last_stop));
3781 sink->priv->discont = TRUE;
3782 sink->abidata.ABI.running = TRUE;
3784 GST_PAD_STREAM_UNLOCK (pad);
3790 set_step_info (GstBaseSink * sink, GstStepInfo * current, GstStepInfo * pending,
3791 guint seqnum, GstFormat format, guint64 amount, gdouble rate,
3792 gboolean flush, gboolean intermediate)
3794 GST_OBJECT_LOCK (sink);
3795 pending->seqnum = seqnum;
3796 pending->format = format;
3797 pending->amount = amount;
3798 pending->position = 0;
3799 pending->rate = rate;
3800 pending->flush = flush;
3801 pending->intermediate = intermediate;
3802 pending->valid = TRUE;
3803 /* flush invalidates the current stepping segment */
3805 current->valid = FALSE;
3806 GST_OBJECT_UNLOCK (sink);
3810 gst_base_sink_perform_step (GstBaseSink * sink, GstPad * pad, GstEvent * event)
3812 GstBaseSinkPrivate *priv;
3813 GstBaseSinkClass *bclass;
3814 gboolean flush, intermediate;
3819 GstStepInfo *pending, *current;
3820 GstMessage *message;
3822 bclass = GST_BASE_SINK_GET_CLASS (sink);
3825 GST_DEBUG_OBJECT (sink, "performing step with event %p", event);
3827 gst_event_parse_step (event, &format, &amount, &rate, &flush, &intermediate);
3828 seqnum = gst_event_get_seqnum (event);
3830 pending = &priv->pending_step;
3831 current = &priv->current_step;
3833 /* post message first */
3834 message = gst_message_new_step_start (GST_OBJECT (sink), FALSE, format,
3835 amount, rate, flush, intermediate);
3836 gst_message_set_seqnum (message, seqnum);
3837 gst_element_post_message (GST_ELEMENT (sink), message);
3840 /* we need to call ::unlock before locking PREROLL_LOCK
3841 * since we lock it before going into ::render */
3843 bclass->unlock (sink);
3845 GST_PAD_PREROLL_LOCK (sink->sinkpad);
3846 /* now that we have the PREROLL lock, clear our unlock request */
3847 if (bclass->unlock_stop)
3848 bclass->unlock_stop (sink);
3850 /* update the stepinfo and make it valid */
3851 set_step_info (sink, current, pending, seqnum, format, amount, rate, flush,
3854 if (sink->priv->async_enabled) {
3855 /* and we need to commit our state again on the next
3856 * prerolled buffer */
3857 sink->playing_async = TRUE;
3858 priv->pending_step.need_preroll = TRUE;
3859 sink->need_preroll = FALSE;
3860 gst_element_lost_state_full (GST_ELEMENT_CAST (sink), FALSE);
3862 sink->priv->have_latency = TRUE;
3863 sink->need_preroll = FALSE;
3865 priv->current_sstart = GST_CLOCK_TIME_NONE;
3866 priv->current_sstop = GST_CLOCK_TIME_NONE;
3867 priv->eos_rtime = GST_CLOCK_TIME_NONE;
3868 priv->call_preroll = TRUE;
3869 gst_base_sink_set_last_buffer (sink, NULL);
3870 gst_base_sink_reset_qos (sink);
3872 if (sink->clock_id) {
3873 gst_clock_id_unschedule (sink->clock_id);
3876 if (sink->have_preroll) {
3877 GST_DEBUG_OBJECT (sink, "signal waiter");
3878 priv->step_unlock = TRUE;
3879 GST_PAD_PREROLL_SIGNAL (sink->sinkpad);
3881 GST_PAD_PREROLL_UNLOCK (sink->sinkpad);
3883 /* update the stepinfo and make it valid */
3884 set_step_info (sink, current, pending, seqnum, format, amount, rate, flush,
3894 gst_base_sink_loop (GstPad * pad)
3896 GstBaseSink *basesink;
3897 GstBuffer *buf = NULL;
3898 GstFlowReturn result;
3902 basesink = GST_BASE_SINK (GST_OBJECT_PARENT (pad));
3904 g_assert (basesink->pad_mode == GST_ACTIVATE_PULL);
3906 if ((blocksize = basesink->priv->blocksize) == 0)
3909 offset = basesink->segment.last_stop;
3911 GST_DEBUG_OBJECT (basesink, "pulling %" G_GUINT64_FORMAT ", %u",
3914 result = gst_pad_pull_range (pad, offset, blocksize, &buf);
3915 if (G_UNLIKELY (result != GST_FLOW_OK))
3918 if (G_UNLIKELY (buf == NULL))
3921 offset += GST_BUFFER_SIZE (buf);
3923 gst_segment_set_last_stop (&basesink->segment, GST_FORMAT_BYTES, offset);
3925 GST_PAD_PREROLL_LOCK (pad);
3926 result = gst_base_sink_chain_unlocked (basesink, pad, FALSE, buf);
3927 GST_PAD_PREROLL_UNLOCK (pad);
3928 if (G_UNLIKELY (result != GST_FLOW_OK))
3936 GST_LOG_OBJECT (basesink, "pausing task, reason %s",
3937 gst_flow_get_name (result));
3938 gst_pad_pause_task (pad);
3939 if (result == GST_FLOW_UNEXPECTED) {
3940 /* perform EOS logic */
3941 if (basesink->segment.flags & GST_SEEK_FLAG_SEGMENT) {
3942 gst_element_post_message (GST_ELEMENT_CAST (basesink),
3943 gst_message_new_segment_done (GST_OBJECT_CAST (basesink),
3944 basesink->segment.format, basesink->segment.last_stop));
3946 gst_base_sink_event (pad, gst_event_new_eos ());
3948 } else if (result == GST_FLOW_NOT_LINKED || result <= GST_FLOW_UNEXPECTED) {
3949 /* for fatal errors we post an error message, post the error
3950 * first so the app knows about the error first.
3951 * wrong-state is not a fatal error because it happens due to
3952 * flushing and posting an error message in that case is the
3953 * wrong thing to do, e.g. when basesrc is doing a flushing
3955 GST_ELEMENT_ERROR (basesink, STREAM, FAILED,
3956 (_("Internal data stream error.")),
3957 ("stream stopped, reason %s", gst_flow_get_name (result)));
3958 gst_base_sink_event (pad, gst_event_new_eos ());
3964 GST_LOG_OBJECT (basesink, "no buffer, pausing");
3965 GST_ELEMENT_ERROR (basesink, STREAM, FAILED,
3966 (_("Internal data flow error.")), ("element returned NULL buffer"));
3967 result = GST_FLOW_ERROR;
3973 gst_base_sink_set_flushing (GstBaseSink * basesink, GstPad * pad,
3976 GstBaseSinkClass *bclass;
3978 bclass = GST_BASE_SINK_GET_CLASS (basesink);
3981 /* unlock any subclasses, we need to do this before grabbing the
3982 * PREROLL_LOCK since we hold this lock before going into ::render. */
3984 bclass->unlock (basesink);
3987 GST_PAD_PREROLL_LOCK (pad);
3988 basesink->flushing = flushing;
3990 /* step 1, now that we have the PREROLL lock, clear our unlock request */
3991 if (bclass->unlock_stop)
3992 bclass->unlock_stop (basesink);
3994 /* set need_preroll before we unblock the clock. If the clock is unblocked
3995 * before timing out, we can reuse the buffer for preroll. */
3996 basesink->need_preroll = TRUE;
3998 /* step 2, unblock clock sync (if any) or any other blocking thing */
3999 if (basesink->clock_id) {
4000 gst_clock_id_unschedule (basesink->clock_id);
4003 /* flush out the data thread if it's locked in finish_preroll, this will
4004 * also flush out the EOS state */
4005 GST_DEBUG_OBJECT (basesink,
4006 "flushing out data thread, need preroll to TRUE");
4007 gst_base_sink_preroll_queue_flush (basesink, pad);
4009 GST_PAD_PREROLL_UNLOCK (pad);
4015 gst_base_sink_default_activate_pull (GstBaseSink * basesink, gboolean active)
4021 result = gst_pad_start_task (basesink->sinkpad,
4022 (GstTaskFunction) gst_base_sink_loop, basesink->sinkpad);
4024 /* step 2, make sure streaming finishes */
4025 result = gst_pad_stop_task (basesink->sinkpad);
4032 gst_base_sink_pad_activate (GstPad * pad)
4034 gboolean result = FALSE;
4035 GstBaseSink *basesink;
4037 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
4039 GST_DEBUG_OBJECT (basesink, "Trying pull mode first");
4041 gst_base_sink_set_flushing (basesink, pad, FALSE);
4043 /* we need to have the pull mode enabled */
4044 if (!basesink->can_activate_pull) {
4045 GST_DEBUG_OBJECT (basesink, "pull mode disabled");
4049 /* check if downstreams supports pull mode at all */
4050 if (!gst_pad_check_pull_range (pad)) {
4051 GST_DEBUG_OBJECT (basesink, "pull mode not supported");
4055 /* set the pad mode before starting the task so that it's in the
4056 * correct state for the new thread. also the sink set_caps and get_caps
4057 * function checks this */
4058 basesink->pad_mode = GST_ACTIVATE_PULL;
4060 /* we first try to negotiate a format so that when we try to activate
4061 * downstream, it knows about our format */
4062 if (!gst_base_sink_negotiate_pull (basesink)) {
4063 GST_DEBUG_OBJECT (basesink, "failed to negotiate in pull mode");
4067 /* ok activate now */
4068 if (!gst_pad_activate_pull (pad, TRUE)) {
4069 /* clear any pending caps */
4070 GST_OBJECT_LOCK (basesink);
4071 gst_caps_replace (&basesink->priv->pull_caps, NULL);
4072 GST_OBJECT_UNLOCK (basesink);
4073 GST_DEBUG_OBJECT (basesink, "failed to activate in pull mode");
4077 GST_DEBUG_OBJECT (basesink, "Success activating pull mode");
4081 /* push mode fallback */
4083 GST_DEBUG_OBJECT (basesink, "Falling back to push mode");
4084 if ((result = gst_pad_activate_push (pad, TRUE))) {
4085 GST_DEBUG_OBJECT (basesink, "Success activating push mode");
4090 GST_WARNING_OBJECT (basesink, "Could not activate pad in either mode");
4091 gst_base_sink_set_flushing (basesink, pad, TRUE);
4094 gst_object_unref (basesink);
4100 gst_base_sink_pad_activate_push (GstPad * pad, gboolean active)
4103 GstBaseSink *basesink;
4105 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
4108 if (!basesink->can_activate_push) {
4110 basesink->pad_mode = GST_ACTIVATE_NONE;
4113 basesink->pad_mode = GST_ACTIVATE_PUSH;
4116 if (G_UNLIKELY (basesink->pad_mode != GST_ACTIVATE_PUSH)) {
4117 g_warning ("Internal GStreamer activation error!!!");
4120 gst_base_sink_set_flushing (basesink, pad, TRUE);
4122 basesink->pad_mode = GST_ACTIVATE_NONE;
4126 gst_object_unref (basesink);
4132 gst_base_sink_negotiate_pull (GstBaseSink * basesink)
4139 /* this returns the intersection between our caps and the peer caps. If there
4140 * is no peer, it returns NULL and we can't operate in pull mode so we can
4141 * fail the negotiation. */
4142 caps = gst_pad_get_allowed_caps (GST_BASE_SINK_PAD (basesink));
4143 if (caps == NULL || gst_caps_is_empty (caps))
4144 goto no_caps_possible;
4146 GST_DEBUG_OBJECT (basesink, "allowed caps: %" GST_PTR_FORMAT, caps);
4148 caps = gst_caps_make_writable (caps);
4149 /* get the first (prefered) format */
4150 gst_caps_truncate (caps);
4152 gst_pad_fixate_caps (GST_BASE_SINK_PAD (basesink), caps);
4154 GST_DEBUG_OBJECT (basesink, "fixated to: %" GST_PTR_FORMAT, caps);
4156 if (gst_caps_is_any (caps)) {
4157 GST_DEBUG_OBJECT (basesink, "caps were ANY after fixating, "
4159 /* neither side has template caps in this case, so they are prepared for
4160 pull() without setcaps() */
4162 } else if (gst_caps_is_fixed (caps)) {
4163 if (!gst_pad_set_caps (GST_BASE_SINK_PAD (basesink), caps))
4164 goto could_not_set_caps;
4166 GST_OBJECT_LOCK (basesink);
4167 gst_caps_replace (&basesink->priv->pull_caps, caps);
4168 GST_OBJECT_UNLOCK (basesink);
4173 gst_caps_unref (caps);
4179 GST_INFO_OBJECT (basesink, "Pipeline could not agree on caps");
4180 GST_DEBUG_OBJECT (basesink, "get_allowed_caps() returned EMPTY");
4182 gst_caps_unref (caps);
4187 GST_INFO_OBJECT (basesink, "Could not set caps: %" GST_PTR_FORMAT, caps);
4188 gst_caps_unref (caps);
4193 /* this won't get called until we implement an activate function */
4195 gst_base_sink_pad_activate_pull (GstPad * pad, gboolean active)
4197 gboolean result = FALSE;
4198 GstBaseSink *basesink;
4199 GstBaseSinkClass *bclass;
4201 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
4202 bclass = GST_BASE_SINK_GET_CLASS (basesink);
4208 /* we mark we have a newsegment here because pull based
4209 * mode works just fine without having a newsegment before the
4211 format = GST_FORMAT_BYTES;
4213 gst_segment_init (&basesink->segment, format);
4214 gst_segment_init (basesink->abidata.ABI.clip_segment, format);
4215 GST_OBJECT_LOCK (basesink);
4216 basesink->have_newsegment = TRUE;
4217 GST_OBJECT_UNLOCK (basesink);
4219 /* get the peer duration in bytes */
4220 result = gst_pad_query_peer_duration (pad, &format, &duration);
4222 GST_DEBUG_OBJECT (basesink,
4223 "setting duration in bytes to %" G_GINT64_FORMAT, duration);
4224 gst_segment_set_duration (basesink->abidata.ABI.clip_segment, format,
4226 gst_segment_set_duration (&basesink->segment, format, duration);
4228 GST_DEBUG_OBJECT (basesink, "unknown duration");
4231 if (bclass->activate_pull)
4232 result = bclass->activate_pull (basesink, TRUE);
4237 goto activate_failed;
4240 if (G_UNLIKELY (basesink->pad_mode != GST_ACTIVATE_PULL)) {
4241 g_warning ("Internal GStreamer activation error!!!");
4244 result = gst_base_sink_set_flushing (basesink, pad, TRUE);
4245 if (bclass->activate_pull)
4246 result &= bclass->activate_pull (basesink, FALSE);
4247 basesink->pad_mode = GST_ACTIVATE_NONE;
4248 /* clear any pending caps */
4249 GST_OBJECT_LOCK (basesink);
4250 gst_caps_replace (&basesink->priv->pull_caps, NULL);
4251 GST_OBJECT_UNLOCK (basesink);
4254 gst_object_unref (basesink);
4261 /* reset, as starting the thread failed */
4262 basesink->pad_mode = GST_ACTIVATE_NONE;
4264 GST_ERROR_OBJECT (basesink, "subclass failed to activate in pull mode");
4269 /* send an event to our sinkpad peer. */
4271 gst_base_sink_send_event (GstElement * element, GstEvent * event)
4274 GstBaseSink *basesink = GST_BASE_SINK (element);
4275 gboolean forward, result = TRUE;
4276 GstActivateMode mode;
4278 GST_OBJECT_LOCK (element);
4279 /* get the pad and the scheduling mode */
4280 pad = gst_object_ref (basesink->sinkpad);
4281 mode = basesink->pad_mode;
4282 GST_OBJECT_UNLOCK (element);
4284 /* only push UPSTREAM events upstream */
4285 forward = GST_EVENT_IS_UPSTREAM (event);
4287 GST_DEBUG_OBJECT (basesink, "handling event %p %" GST_PTR_FORMAT, event,
4290 switch (GST_EVENT_TYPE (event)) {
4291 case GST_EVENT_LATENCY:
4293 GstClockTime latency;
4295 gst_event_parse_latency (event, &latency);
4297 /* store the latency. We use this to adjust the running_time before syncing
4298 * it to the clock. */
4299 GST_OBJECT_LOCK (element);
4300 basesink->priv->latency = latency;
4301 if (!basesink->priv->have_latency)
4303 GST_OBJECT_UNLOCK (element);
4304 GST_DEBUG_OBJECT (basesink, "latency set to %" GST_TIME_FORMAT,
4305 GST_TIME_ARGS (latency));
4307 /* We forward this event so that all elements know about the global pipeline
4308 * latency. This is interesting for an element when it wants to figure out
4309 * when a particular piece of data will be rendered. */
4312 case GST_EVENT_SEEK:
4313 /* in pull mode we will execute the seek */
4314 if (mode == GST_ACTIVATE_PULL)
4315 result = gst_base_sink_perform_seek (basesink, pad, event);
4317 case GST_EVENT_STEP:
4318 result = gst_base_sink_perform_step (basesink, pad, event);
4326 result = gst_pad_push_event (pad, event);
4328 /* not forwarded, unref the event */
4329 gst_event_unref (event);
4332 gst_object_unref (pad);
4336 /* get the end position of the last seen object, this is used
4337 * for EOS and for making sure that we don't report a position we
4338 * have not reached yet. With LOCK. */
4340 gst_base_sink_get_position_last (GstBaseSink * basesink, GstFormat format,
4341 gint64 * cur, gboolean * upstream)
4344 GstSegment *segment;
4345 gboolean ret = TRUE;
4347 segment = &basesink->segment;
4348 oformat = segment->format;
4350 if (oformat == GST_FORMAT_TIME) {
4351 /* return last observed stream time, we keep the stream time around in the
4353 *cur = basesink->priv->current_sstop;
4355 /* convert last stop to stream time */
4356 *cur = gst_segment_to_stream_time (segment, oformat, segment->last_stop);
4359 if (*cur != -1 && oformat != format) {
4360 GST_OBJECT_UNLOCK (basesink);
4361 /* convert to the target format if we need to, release lock first */
4363 gst_pad_query_convert (basesink->sinkpad, oformat, *cur, &format, cur);
4368 GST_OBJECT_LOCK (basesink);
4371 GST_DEBUG_OBJECT (basesink, "POSITION: %" GST_TIME_FORMAT,
4372 GST_TIME_ARGS (*cur));
4377 /* get the position when we are PAUSED, this is the stream time of the buffer
4378 * that prerolled. If no buffer is prerolled (we are still flushing), this
4379 * value will be -1. With LOCK. */
4381 gst_base_sink_get_position_paused (GstBaseSink * basesink, GstFormat format,
4382 gint64 * cur, gboolean * upstream)
4386 GstSegment *segment;
4389 /* we don't use the clip segment in pull mode, when seeking we update the
4390 * main segment directly with the new segment values without it having to be
4391 * activated by the rendering after preroll */
4392 if (basesink->pad_mode == GST_ACTIVATE_PUSH)
4393 segment = basesink->abidata.ABI.clip_segment;
4395 segment = &basesink->segment;
4396 oformat = segment->format;
4398 if (oformat == GST_FORMAT_TIME) {
4399 *cur = basesink->priv->current_sstart;
4400 if (segment->rate < 0.0 &&
4401 GST_CLOCK_TIME_IS_VALID (basesink->priv->current_sstop)) {
4402 /* for reverse playback we prefer the stream time stop position if we have
4404 *cur = basesink->priv->current_sstop;
4407 *cur = gst_segment_to_stream_time (segment, oformat, segment->last_stop);
4410 time = segment->time;
4413 *cur = MAX (*cur, time);
4414 GST_DEBUG_OBJECT (basesink, "POSITION as max: %" GST_TIME_FORMAT
4415 ", time %" GST_TIME_FORMAT, GST_TIME_ARGS (*cur), GST_TIME_ARGS (time));
4417 /* we have no buffer, use the segment times. */
4418 if (segment->rate >= 0.0) {
4419 /* forward, next position is always the time of the segment */
4421 GST_DEBUG_OBJECT (basesink, "POSITION as time: %" GST_TIME_FORMAT,
4422 GST_TIME_ARGS (*cur));
4424 /* reverse, next expected timestamp is segment->stop. We use the function
4425 * to get things right for negative applied_rates. */
4426 *cur = gst_segment_to_stream_time (segment, oformat, segment->stop);
4427 GST_DEBUG_OBJECT (basesink, "reverse POSITION: %" GST_TIME_FORMAT,
4428 GST_TIME_ARGS (*cur));
4433 if (res && oformat != format) {
4434 GST_OBJECT_UNLOCK (basesink);
4436 gst_pad_query_convert (basesink->sinkpad, oformat, *cur, &format, cur);
4441 GST_OBJECT_LOCK (basesink);
4448 gst_base_sink_get_position (GstBaseSink * basesink, GstFormat format,
4449 gint64 * cur, gboolean * upstream)
4452 gboolean res = FALSE;
4453 GstFormat oformat, tformat;
4454 GstClockTime now, latency;
4455 GstClockTimeDiff base;
4456 gint64 time, accum, duration;
4460 GST_OBJECT_LOCK (basesink);
4461 /* our intermediate time format */
4462 tformat = GST_FORMAT_TIME;
4463 /* get the format in the segment */
4464 oformat = basesink->segment.format;
4466 /* can only give answer based on the clock if not EOS */
4467 if (G_UNLIKELY (basesink->eos))
4470 /* we can only get the segment when we are not NULL or READY */
4471 if (!basesink->have_newsegment)
4474 /* when not in PLAYING or when we're busy with a state change, we
4475 * cannot read from the clock so we report time based on the
4476 * last seen timestamp. */
4477 if (GST_STATE (basesink) != GST_STATE_PLAYING ||
4478 GST_STATE_PENDING (basesink) != GST_STATE_VOID_PENDING)
4481 /* we need to sync on the clock. */
4482 if (basesink->sync == FALSE)
4485 /* and we need a clock */
4486 if (G_UNLIKELY ((clock = GST_ELEMENT_CLOCK (basesink)) == NULL))
4489 /* collect all data we need holding the lock */
4490 if (GST_CLOCK_TIME_IS_VALID (basesink->segment.time))
4491 time = basesink->segment.time;
4495 if (GST_CLOCK_TIME_IS_VALID (basesink->segment.stop))
4496 duration = basesink->segment.stop - basesink->segment.start;
4500 base = GST_ELEMENT_CAST (basesink)->base_time;
4501 accum = basesink->segment.accum;
4502 rate = basesink->segment.rate * basesink->segment.applied_rate;
4503 latency = basesink->priv->latency;
4505 gst_object_ref (clock);
4507 /* this function might release the LOCK */
4508 gst_base_sink_get_position_last (basesink, format, &last, upstream);
4510 /* need to release the object lock before we can get the time,
4511 * a clock might take the LOCK of the provider, which could be
4512 * a basesink subclass. */
4513 GST_OBJECT_UNLOCK (basesink);
4515 now = gst_clock_get_time (clock);
4517 if (oformat != tformat) {
4518 /* convert accum, time and duration to time */
4519 if (!gst_pad_query_convert (basesink->sinkpad, oformat, accum, &tformat,
4521 goto convert_failed;
4522 if (!gst_pad_query_convert (basesink->sinkpad, oformat, duration, &tformat,
4524 goto convert_failed;
4525 if (!gst_pad_query_convert (basesink->sinkpad, oformat, time, &tformat,
4527 goto convert_failed;
4530 /* subtract base time and accumulated time from the clock time.
4531 * Make sure we don't go negative. This is the current time in
4532 * the segment which we need to scale with the combined
4533 * rate and applied rate. */
4536 if (GST_CLOCK_DIFF (base, now) < 0)
4539 /* for negative rates we need to count back from the segment
4544 *cur = time + gst_guint64_to_gdouble (now - base) * rate;
4546 /* never report more than last seen position */
4548 *cur = MIN (last, *cur);
4550 gst_object_unref (clock);
4552 GST_DEBUG_OBJECT (basesink,
4553 "now %" GST_TIME_FORMAT " - base %" GST_TIME_FORMAT " - accum %"
4554 GST_TIME_FORMAT " + time %" GST_TIME_FORMAT,
4555 GST_TIME_ARGS (now), GST_TIME_ARGS (base),
4556 GST_TIME_ARGS (accum), GST_TIME_ARGS (time));
4558 if (oformat != format) {
4559 /* convert time to final format */
4560 if (!gst_pad_query_convert (basesink->sinkpad, tformat, *cur, &format, cur))
4561 goto convert_failed;
4567 GST_DEBUG_OBJECT (basesink, "res: %d, POSITION: %" GST_TIME_FORMAT,
4568 res, GST_TIME_ARGS (*cur));
4574 GST_DEBUG_OBJECT (basesink, "position in EOS");
4575 res = gst_base_sink_get_position_last (basesink, format, cur, upstream);
4576 GST_OBJECT_UNLOCK (basesink);
4581 GST_DEBUG_OBJECT (basesink, "position in PAUSED");
4582 res = gst_base_sink_get_position_paused (basesink, format, cur, upstream);
4583 GST_OBJECT_UNLOCK (basesink);
4588 /* in NULL or READY we always return FALSE and -1 */
4589 GST_DEBUG_OBJECT (basesink, "position in wrong state, return -1");
4592 GST_OBJECT_UNLOCK (basesink);
4597 /* report last seen timestamp if any, else ask upstream to answer */
4598 if ((*cur = basesink->priv->current_sstart) != -1)
4603 GST_DEBUG_OBJECT (basesink, "no sync, res %d, POSITION %" GST_TIME_FORMAT,
4604 res, GST_TIME_ARGS (*cur));
4605 GST_OBJECT_UNLOCK (basesink);
4610 GST_DEBUG_OBJECT (basesink, "convert failed, try upstream");
4617 gst_base_sink_get_duration (GstBaseSink * basesink, GstFormat format,
4618 gint64 * dur, gboolean * upstream)
4620 gboolean res = FALSE;
4622 if (basesink->pad_mode == GST_ACTIVATE_PULL) {
4623 GstFormat uformat = GST_FORMAT_BYTES;
4626 /* get the duration in bytes, in pull mode that's all we are sure to
4627 * know. We have to explicitly get this value from upstream instead of
4628 * using our cached value because it might change. Duration caching
4629 * should be done at a higher level. */
4630 res = gst_pad_query_peer_duration (basesink->sinkpad, &uformat, &uduration);
4632 gst_segment_set_duration (&basesink->segment, uformat, uduration);
4633 if (format != uformat) {
4634 /* convert to the requested format */
4635 res = gst_pad_query_convert (basesink->sinkpad, uformat, uduration,
4649 static const GstQueryType *
4650 gst_base_sink_get_query_types (GstElement * element)
4652 static const GstQueryType query_types[] = {
4664 gst_base_sink_query (GstElement * element, GstQuery * query)
4666 gboolean res = FALSE;
4668 GstBaseSink *basesink = GST_BASE_SINK (element);
4670 switch (GST_QUERY_TYPE (query)) {
4671 case GST_QUERY_POSITION:
4675 gboolean upstream = FALSE;
4677 gst_query_parse_position (query, &format, NULL);
4679 GST_DEBUG_OBJECT (basesink, "position query in format %s",
4680 gst_format_get_name (format));
4682 /* first try to get the position based on the clock */
4684 gst_base_sink_get_position (basesink, format, &cur, &upstream))) {
4685 gst_query_set_position (query, format, cur);
4686 } else if (upstream) {
4687 /* fallback to peer query */
4688 res = gst_pad_peer_query (basesink->sinkpad, query);
4691 /* we can handle a few things if upstream failed */
4692 if (format == GST_FORMAT_PERCENT) {
4694 GstFormat uformat = GST_FORMAT_TIME;
4696 res = gst_base_sink_get_position (basesink, GST_FORMAT_TIME, &cur,
4698 if (!res && upstream) {
4699 res = gst_pad_query_peer_position (basesink->sinkpad, &uformat,
4703 res = gst_base_sink_get_duration (basesink, GST_FORMAT_TIME, &dur,
4705 if (!res && upstream) {
4706 res = gst_pad_query_peer_duration (basesink->sinkpad, &uformat,
4713 pos = gst_util_uint64_scale (100 * GST_FORMAT_PERCENT_SCALE, cur,
4715 gst_query_set_position (query, GST_FORMAT_PERCENT, pos);
4721 case GST_QUERY_DURATION:
4725 gboolean upstream = FALSE;
4727 gst_query_parse_duration (query, &format, NULL);
4729 GST_DEBUG_OBJECT (basesink, "duration query in format %s",
4730 gst_format_get_name (format));
4733 gst_base_sink_get_duration (basesink, format, &dur, &upstream))) {
4734 gst_query_set_duration (query, format, dur);
4735 } else if (upstream) {
4736 /* fallback to peer query */
4737 res = gst_pad_peer_query (basesink->sinkpad, query);
4740 /* we can handle a few things if upstream failed */
4741 if (format == GST_FORMAT_PERCENT) {
4742 gst_query_set_duration (query, GST_FORMAT_PERCENT,
4743 GST_FORMAT_PERCENT_MAX);
4749 case GST_QUERY_LATENCY:
4751 gboolean live, us_live;
4752 GstClockTime min, max;
4754 if ((res = gst_base_sink_query_latency (basesink, &live, &us_live, &min,
4756 gst_query_set_latency (query, live, min, max);
4760 case GST_QUERY_JITTER:
4762 case GST_QUERY_RATE:
4763 /* gst_query_set_rate (query, basesink->segment_rate); */
4766 case GST_QUERY_SEGMENT:
4768 if (basesink->pad_mode == GST_ACTIVATE_PULL) {
4769 gst_query_set_segment (query, basesink->segment.rate,
4770 GST_FORMAT_TIME, basesink->segment.start, basesink->segment.stop);
4773 res = gst_pad_peer_query (basesink->sinkpad, query);
4777 case GST_QUERY_SEEKING:
4778 case GST_QUERY_CONVERT:
4779 case GST_QUERY_FORMATS:
4781 res = gst_pad_peer_query (basesink->sinkpad, query);
4784 GST_DEBUG_OBJECT (basesink, "query %s returns %d",
4785 GST_QUERY_TYPE_NAME (query), res);
4789 static GstStateChangeReturn
4790 gst_base_sink_change_state (GstElement * element, GstStateChange transition)
4792 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
4793 GstBaseSink *basesink = GST_BASE_SINK (element);
4794 GstBaseSinkClass *bclass;
4795 GstBaseSinkPrivate *priv;
4797 priv = basesink->priv;
4799 bclass = GST_BASE_SINK_GET_CLASS (basesink);
4801 switch (transition) {
4802 case GST_STATE_CHANGE_NULL_TO_READY:
4804 if (!bclass->start (basesink))
4807 case GST_STATE_CHANGE_READY_TO_PAUSED:
4808 /* need to complete preroll before this state change completes, there
4809 * is no data flow in READY so we can safely assume we need to preroll. */
4810 GST_PAD_PREROLL_LOCK (basesink->sinkpad);
4811 GST_DEBUG_OBJECT (basesink, "READY to PAUSED");
4812 basesink->have_newsegment = FALSE;
4813 gst_segment_init (&basesink->segment, GST_FORMAT_UNDEFINED);
4814 gst_segment_init (basesink->abidata.ABI.clip_segment,
4815 GST_FORMAT_UNDEFINED);
4816 basesink->offset = 0;
4817 basesink->have_preroll = FALSE;
4818 priv->step_unlock = FALSE;
4819 basesink->need_preroll = TRUE;
4820 basesink->playing_async = TRUE;
4821 priv->current_sstart = GST_CLOCK_TIME_NONE;
4822 priv->current_sstop = GST_CLOCK_TIME_NONE;
4823 priv->eos_rtime = GST_CLOCK_TIME_NONE;
4825 basesink->eos = FALSE;
4826 priv->received_eos = FALSE;
4827 gst_base_sink_reset_qos (basesink);
4828 priv->commited = FALSE;
4829 priv->call_preroll = TRUE;
4830 priv->current_step.valid = FALSE;
4831 priv->pending_step.valid = FALSE;
4832 if (priv->async_enabled) {
4833 GST_DEBUG_OBJECT (basesink, "doing async state change");
4834 /* when async enabled, post async-start message and return ASYNC from
4835 * the state change function */
4836 ret = GST_STATE_CHANGE_ASYNC;
4837 gst_element_post_message (GST_ELEMENT_CAST (basesink),
4838 gst_message_new_async_start (GST_OBJECT_CAST (basesink), FALSE));
4840 priv->have_latency = TRUE;
4842 GST_PAD_PREROLL_UNLOCK (basesink->sinkpad);
4844 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
4845 GST_PAD_PREROLL_LOCK (basesink->sinkpad);
4846 if (!gst_base_sink_needs_preroll (basesink)) {
4847 GST_DEBUG_OBJECT (basesink, "PAUSED to PLAYING, don't need preroll");
4848 /* no preroll needed anymore now. */
4849 basesink->playing_async = FALSE;
4850 basesink->need_preroll = FALSE;
4851 if (basesink->eos) {
4852 GstMessage *message;
4854 /* need to post EOS message here */
4855 GST_DEBUG_OBJECT (basesink, "Now posting EOS");
4856 message = gst_message_new_eos (GST_OBJECT_CAST (basesink));
4857 gst_message_set_seqnum (message, basesink->priv->seqnum);
4858 gst_element_post_message (GST_ELEMENT_CAST (basesink), message);
4860 GST_DEBUG_OBJECT (basesink, "signal preroll");
4861 GST_PAD_PREROLL_SIGNAL (basesink->sinkpad);
4864 GST_DEBUG_OBJECT (basesink, "PAUSED to PLAYING, we are not prerolled");
4865 basesink->need_preroll = TRUE;
4866 basesink->playing_async = TRUE;
4867 priv->call_preroll = TRUE;
4868 priv->commited = FALSE;
4869 if (priv->async_enabled) {
4870 GST_DEBUG_OBJECT (basesink, "doing async state change");
4871 ret = GST_STATE_CHANGE_ASYNC;
4872 gst_element_post_message (GST_ELEMENT_CAST (basesink),
4873 gst_message_new_async_start (GST_OBJECT_CAST (basesink), FALSE));
4876 GST_PAD_PREROLL_UNLOCK (basesink->sinkpad);
4883 GstStateChangeReturn bret;
4885 bret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
4886 if (G_UNLIKELY (bret == GST_STATE_CHANGE_FAILURE))
4887 goto activate_failed;
4890 switch (transition) {
4891 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
4892 GST_DEBUG_OBJECT (basesink, "PLAYING to PAUSED");
4893 /* FIXME, make sure we cannot enter _render first */
4895 /* we need to call ::unlock before locking PREROLL_LOCK
4896 * since we lock it before going into ::render */
4898 bclass->unlock (basesink);
4900 GST_PAD_PREROLL_LOCK (basesink->sinkpad);
4901 GST_DEBUG_OBJECT (basesink, "got preroll lock");
4902 /* now that we have the PREROLL lock, clear our unlock request */
4903 if (bclass->unlock_stop)
4904 bclass->unlock_stop (basesink);
4906 /* we need preroll again and we set the flag before unlocking the clockid
4907 * because if the clockid is unlocked before a current buffer expired, we
4908 * can use that buffer to preroll with */
4909 basesink->need_preroll = TRUE;
4911 if (basesink->clock_id) {
4912 GST_DEBUG_OBJECT (basesink, "unschedule clock");
4913 gst_clock_id_unschedule (basesink->clock_id);
4916 /* if we don't have a preroll buffer we need to wait for a preroll and
4918 if (!gst_base_sink_needs_preroll (basesink)) {
4919 GST_DEBUG_OBJECT (basesink, "PLAYING to PAUSED, we are prerolled");
4920 basesink->playing_async = FALSE;
4922 if (GST_STATE_TARGET (GST_ELEMENT (basesink)) <= GST_STATE_READY) {
4923 GST_DEBUG_OBJECT (basesink, "element is <= READY");
4924 ret = GST_STATE_CHANGE_SUCCESS;
4926 GST_DEBUG_OBJECT (basesink,
4927 "PLAYING to PAUSED, we are not prerolled");
4928 basesink->playing_async = TRUE;
4929 priv->commited = FALSE;
4930 priv->call_preroll = TRUE;
4931 if (priv->async_enabled) {
4932 GST_DEBUG_OBJECT (basesink, "doing async state change");
4933 ret = GST_STATE_CHANGE_ASYNC;
4934 gst_element_post_message (GST_ELEMENT_CAST (basesink),
4935 gst_message_new_async_start (GST_OBJECT_CAST (basesink),
4940 GST_DEBUG_OBJECT (basesink, "rendered: %" G_GUINT64_FORMAT
4941 ", dropped: %" G_GUINT64_FORMAT, priv->rendered, priv->dropped);
4943 gst_base_sink_reset_qos (basesink);
4944 GST_PAD_PREROLL_UNLOCK (basesink->sinkpad);
4946 case GST_STATE_CHANGE_PAUSED_TO_READY:
4947 GST_PAD_PREROLL_LOCK (basesink->sinkpad);
4948 /* start by reseting our position state with the object lock so that the
4949 * position query gets the right idea. We do this before we post the
4950 * messages so that the message handlers pick this up. */
4951 GST_OBJECT_LOCK (basesink);
4952 basesink->have_newsegment = FALSE;
4953 priv->current_sstart = GST_CLOCK_TIME_NONE;
4954 priv->current_sstop = GST_CLOCK_TIME_NONE;
4955 priv->have_latency = FALSE;
4956 GST_OBJECT_UNLOCK (basesink);
4958 gst_base_sink_set_last_buffer (basesink, NULL);
4959 priv->call_preroll = FALSE;
4961 if (!priv->commited) {
4962 if (priv->async_enabled) {
4963 GST_DEBUG_OBJECT (basesink, "PAUSED to READY, posting async-done");
4965 gst_element_post_message (GST_ELEMENT_CAST (basesink),
4966 gst_message_new_state_changed (GST_OBJECT_CAST (basesink),
4967 GST_STATE_PLAYING, GST_STATE_PAUSED, GST_STATE_READY));
4969 gst_element_post_message (GST_ELEMENT_CAST (basesink),
4970 gst_message_new_async_done (GST_OBJECT_CAST (basesink)));
4972 priv->commited = TRUE;
4974 GST_DEBUG_OBJECT (basesink, "PAUSED to READY, don't need_preroll");
4976 GST_PAD_PREROLL_UNLOCK (basesink->sinkpad);
4978 case GST_STATE_CHANGE_READY_TO_NULL:
4980 if (!bclass->stop (basesink)) {
4981 GST_WARNING_OBJECT (basesink, "failed to stop");
4984 gst_base_sink_set_last_buffer (basesink, NULL);
4985 priv->call_preroll = FALSE;
4996 GST_DEBUG_OBJECT (basesink, "failed to start");
4997 return GST_STATE_CHANGE_FAILURE;
5001 GST_DEBUG_OBJECT (basesink,
5002 "element failed to change states -- activation problem?");
5003 return GST_STATE_CHANGE_FAILURE;