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 class_init function, like so:
42 * my_element_class_init (GstMyElementClass *klass)
44 * GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
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 * The #GstBaseSinkClass.unlock() method is called when the elements should
109 * unblock any blocking operations they perform in the
110 * #GstBaseSinkClass.render() method. This is mostly useful when the
111 * #GstBaseSinkClass.render() method performs a blocking write on a file
112 * descriptor, for example.
114 * The #GstBaseSink:max-lateness property affects how the sink deals with
115 * buffers that arrive too late in the sink. A buffer arrives too late in the
116 * sink when the presentation time (as a combination of the last segment, buffer
117 * timestamp and element base_time) plus the duration is before the current
119 * If the frame is later than max-lateness, the sink will drop the buffer
120 * without calling the render method.
121 * This feature is disabled if sync is disabled, the
122 * #GstBaseSinkClass.get_times() method does not return a valid start time or
123 * max-lateness is set to -1 (the default).
124 * Subclasses can use gst_base_sink_set_max_lateness() to configure the
125 * max-lateness value.
127 * The #GstBaseSink:qos property will enable the quality-of-service features of
128 * the basesink which gather statistics about the real-time performance of the
129 * clock synchronisation. For each buffer received in the sink, statistics are
130 * gathered and a QOS event is sent upstream with these numbers. This
131 * information can then be used by upstream elements to reduce their processing
134 * Since 0.10.15 the #GstBaseSink:async property can be used to instruct the
135 * sink to never perform an ASYNC state change. This feature is mostly usable
136 * when dealing with non-synchronized streams or sparse streams.
138 * Last reviewed on 2007-08-29 (0.10.15)
145 #include <gst/gst_private.h>
147 #include "gstbasesink.h"
148 #include <gst/gstmarshal.h>
149 #include <gst/gst-i18n-lib.h>
151 GST_DEBUG_CATEGORY_STATIC (gst_base_sink_debug);
152 #define GST_CAT_DEFAULT gst_base_sink_debug
154 #define GST_BASE_SINK_GET_PRIVATE(obj) \
155 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_BASE_SINK, GstBaseSinkPrivate))
157 #define GST_FLOW_STEP GST_FLOW_CUSTOM_ERROR
161 gboolean valid; /* if this info is valid */
162 guint32 seqnum; /* the seqnum of the STEP event */
163 GstFormat format; /* the format of the amount */
164 guint64 amount; /* the total amount of data to skip */
165 guint64 position; /* the position in the stepped data */
166 guint64 duration; /* the duration in time of the skipped data */
167 guint64 start; /* running_time of the start */
168 gdouble rate; /* rate of skipping */
169 gdouble start_rate; /* rate before skipping */
170 guint64 start_start; /* start position skipping */
171 guint64 start_stop; /* stop position skipping */
172 gboolean flush; /* if this was a flushing step */
173 gboolean intermediate; /* if this is an intermediate step */
174 gboolean need_preroll; /* if we need preroll after this step */
177 /* FIXME, some stuff in ABI.data and other in Private...
178 * Make up your mind please.
180 struct _GstBaseSinkPrivate
182 gint qos_enabled; /* ATOMIC */
183 gboolean async_enabled;
184 GstClockTimeDiff ts_offset;
185 GstClockTime render_delay;
187 /* start, stop of current buffer, stream time, used to report position */
188 GstClockTime current_sstart;
189 GstClockTime current_sstop;
191 /* start, stop and jitter of current buffer, running time */
192 GstClockTime current_rstart;
193 GstClockTime current_rstop;
194 GstClockTimeDiff current_jitter;
195 /* the running time of the previous buffer */
196 GstClockTime prev_rstart;
198 /* EOS sync time in running time */
199 GstClockTime eos_rtime;
201 /* last buffer that arrived in time, running time */
202 GstClockTime last_render_time;
203 /* when the last buffer left the sink, running time */
204 GstClockTime last_left;
206 /* running averages go here these are done on running time */
208 GstClockTime avg_duration;
210 GstClockTime avg_in_diff;
212 /* these are done on system time. avg_jitter and avg_render are
213 * compared to eachother to see if the rendering time takes a
214 * huge amount of the processing, If so we are flooded with
216 GstClockTime last_left_systime;
217 GstClockTime avg_jitter;
218 GstClockTime start, stop;
219 GstClockTime avg_render;
221 /* number of rendered and dropped frames */
226 GstClockTime latency;
228 /* if we already commited the state */
231 /* when we received EOS */
232 gboolean received_eos;
234 /* when we are prerolled and able to report latency */
235 gboolean have_latency;
237 /* the last buffer we prerolled or rendered. Useful for making snapshots */
238 gint enable_last_buffer; /* atomic */
239 GstBuffer *last_buffer;
241 /* caps for pull based scheduling */
244 /* blocksize for pulling */
249 /* seqnum of the stream */
252 gboolean call_preroll;
253 gboolean step_unlock;
255 /* we have a pending and a current step operation */
256 GstStepInfo current_step;
257 GstStepInfo pending_step;
259 /* Cached GstClockID */
260 GstClockID cached_clock_id;
262 /* for throttling and QoS */
263 GstClockTime earliest_in_time;
264 GstClockTime throttle_time;
269 #define DO_RUNNING_AVG(avg,val,size) (((val) + ((size)-1) * (avg)) / (size))
271 /* generic running average, this has a neutral window size */
272 #define UPDATE_RUNNING_AVG(avg,val) DO_RUNNING_AVG(avg,val,8)
274 /* the windows for these running averages are experimentally obtained.
275 * possitive values get averaged more while negative values use a small
276 * window so we can react faster to badness. */
277 #define UPDATE_RUNNING_AVG_P(avg,val) DO_RUNNING_AVG(avg,val,16)
278 #define UPDATE_RUNNING_AVG_N(avg,val) DO_RUNNING_AVG(avg,val,4)
282 _PR_IS_NOTHING = 1 << 0,
283 _PR_IS_BUFFER = 1 << 1,
284 _PR_IS_BUFFERLIST = 1 << 2,
285 _PR_IS_EVENT = 1 << 3
288 #define OBJ_IS_BUFFER(a) ((a) & _PR_IS_BUFFER)
289 #define OBJ_IS_BUFFERLIST(a) ((a) & _PR_IS_BUFFERLIST)
290 #define OBJ_IS_EVENT(a) ((a) & _PR_IS_EVENT)
291 #define OBJ_IS_BUFFERFULL(a) ((a) & (_PR_IS_BUFFER | _PR_IS_BUFFERLIST))
293 /* BaseSink properties */
295 #define DEFAULT_CAN_ACTIVATE_PULL FALSE /* fixme: enable me */
296 #define DEFAULT_CAN_ACTIVATE_PUSH TRUE
298 #define DEFAULT_PREROLL_QUEUE_LEN 0
299 #define DEFAULT_SYNC TRUE
300 #define DEFAULT_MAX_LATENESS -1
301 #define DEFAULT_QOS FALSE
302 #define DEFAULT_ASYNC TRUE
303 #define DEFAULT_TS_OFFSET 0
304 #define DEFAULT_BLOCKSIZE 4096
305 #define DEFAULT_RENDER_DELAY 0
306 #define DEFAULT_ENABLE_LAST_BUFFER TRUE
307 #define DEFAULT_THROTTLE_TIME 0
312 PROP_PREROLL_QUEUE_LEN,
318 PROP_ENABLE_LAST_BUFFER,
326 static GstElementClass *parent_class = NULL;
328 static void gst_base_sink_class_init (GstBaseSinkClass * klass);
329 static void gst_base_sink_init (GstBaseSink * trans, gpointer g_class);
330 static void gst_base_sink_finalize (GObject * object);
333 gst_base_sink_get_type (void)
335 static volatile gsize base_sink_type = 0;
337 if (g_once_init_enter (&base_sink_type)) {
339 static const GTypeInfo base_sink_info = {
340 sizeof (GstBaseSinkClass),
343 (GClassInitFunc) gst_base_sink_class_init,
346 sizeof (GstBaseSink),
348 (GInstanceInitFunc) gst_base_sink_init,
351 _type = g_type_register_static (GST_TYPE_ELEMENT,
352 "GstBaseSink", &base_sink_info, G_TYPE_FLAG_ABSTRACT);
353 g_once_init_leave (&base_sink_type, _type);
355 return base_sink_type;
358 static void gst_base_sink_set_property (GObject * object, guint prop_id,
359 const GValue * value, GParamSpec * pspec);
360 static void gst_base_sink_get_property (GObject * object, guint prop_id,
361 GValue * value, GParamSpec * pspec);
363 static gboolean gst_base_sink_send_event (GstElement * element,
365 static gboolean gst_base_sink_query (GstElement * element, GstQuery * query);
366 static const GstQueryType *gst_base_sink_get_query_types (GstElement * element);
368 static GstCaps *gst_base_sink_get_caps (GstBaseSink * sink, GstCaps * caps);
369 static gboolean gst_base_sink_set_caps (GstBaseSink * sink, GstCaps * caps);
370 static void gst_base_sink_get_times (GstBaseSink * basesink, GstBuffer * buffer,
371 GstClockTime * start, GstClockTime * end);
372 static gboolean gst_base_sink_set_flushing (GstBaseSink * basesink,
373 GstPad * pad, gboolean flushing);
374 static gboolean gst_base_sink_default_activate_pull (GstBaseSink * basesink,
376 static gboolean gst_base_sink_default_do_seek (GstBaseSink * sink,
377 GstSegment * segment);
378 static gboolean gst_base_sink_default_prepare_seek_segment (GstBaseSink * sink,
379 GstEvent * event, GstSegment * segment);
381 static GstStateChangeReturn gst_base_sink_change_state (GstElement * element,
382 GstStateChange transition);
384 static GstFlowReturn gst_base_sink_chain (GstPad * pad, GstBuffer * buffer);
385 static GstFlowReturn gst_base_sink_chain_list (GstPad * pad,
386 GstBufferList * list);
388 static void gst_base_sink_loop (GstPad * pad);
389 static gboolean gst_base_sink_pad_activate (GstPad * pad);
390 static gboolean gst_base_sink_pad_activate_push (GstPad * pad, gboolean active);
391 static gboolean gst_base_sink_pad_activate_pull (GstPad * pad, gboolean active);
392 static gboolean gst_base_sink_event (GstPad * pad, GstEvent * event);
394 static gboolean gst_base_sink_negotiate_pull (GstBaseSink * basesink);
395 static GstCaps *gst_base_sink_pad_getcaps (GstPad * pad, GstCaps * filter);
396 static void gst_base_sink_pad_fixate (GstPad * pad, GstCaps * caps);
398 /* check if an object was too late */
399 static gboolean gst_base_sink_is_too_late (GstBaseSink * basesink,
400 GstMiniObject * obj, GstClockTime rstart, GstClockTime rstop,
401 GstClockReturn status, GstClockTimeDiff jitter);
402 static GstFlowReturn gst_base_sink_preroll_object (GstBaseSink * basesink,
403 guint8 obj_type, GstMiniObject * obj);
406 gst_base_sink_class_init (GstBaseSinkClass * klass)
408 GObjectClass *gobject_class;
409 GstElementClass *gstelement_class;
411 gobject_class = G_OBJECT_CLASS (klass);
412 gstelement_class = GST_ELEMENT_CLASS (klass);
414 GST_DEBUG_CATEGORY_INIT (gst_base_sink_debug, "basesink", 0,
417 g_type_class_add_private (klass, sizeof (GstBaseSinkPrivate));
419 parent_class = g_type_class_peek_parent (klass);
421 gobject_class->finalize = gst_base_sink_finalize;
422 gobject_class->set_property = gst_base_sink_set_property;
423 gobject_class->get_property = gst_base_sink_get_property;
425 /* FIXME, this next value should be configured using an event from the
426 * upstream element, ie, the BUFFER_SIZE event. */
427 g_object_class_install_property (gobject_class, PROP_PREROLL_QUEUE_LEN,
428 g_param_spec_uint ("preroll-queue-len", "Preroll queue length",
429 "Number of buffers to queue during preroll", 0, G_MAXUINT,
430 DEFAULT_PREROLL_QUEUE_LEN,
431 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
433 g_object_class_install_property (gobject_class, PROP_SYNC,
434 g_param_spec_boolean ("sync", "Sync", "Sync on the clock", DEFAULT_SYNC,
435 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
437 g_object_class_install_property (gobject_class, PROP_MAX_LATENESS,
438 g_param_spec_int64 ("max-lateness", "Max Lateness",
439 "Maximum number of nanoseconds that a buffer can be late before it "
440 "is dropped (-1 unlimited)", -1, G_MAXINT64, DEFAULT_MAX_LATENESS,
441 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
443 g_object_class_install_property (gobject_class, PROP_QOS,
444 g_param_spec_boolean ("qos", "Qos",
445 "Generate Quality-of-Service events upstream", DEFAULT_QOS,
446 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
450 * If set to #TRUE, the basesink will perform asynchronous state changes.
451 * When set to #FALSE, the sink will not signal the parent when it prerolls.
452 * Use this option when dealing with sparse streams or when synchronisation is
457 g_object_class_install_property (gobject_class, PROP_ASYNC,
458 g_param_spec_boolean ("async", "Async",
459 "Go asynchronously to PAUSED", DEFAULT_ASYNC,
460 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
462 * GstBaseSink:ts-offset
464 * Controls the final synchronisation, a negative value will render the buffer
465 * earlier while a positive value delays playback. This property can be
466 * used to fix synchronisation in bad files.
470 g_object_class_install_property (gobject_class, PROP_TS_OFFSET,
471 g_param_spec_int64 ("ts-offset", "TS Offset",
472 "Timestamp offset in nanoseconds", G_MININT64, G_MAXINT64,
473 DEFAULT_TS_OFFSET, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
476 * GstBaseSink:enable-last-buffer
478 * Enable the last-buffer property. If FALSE, basesink doesn't keep a
479 * reference to the last buffer arrived and the last-buffer property is always
480 * set to NULL. This can be useful if you need buffers to be released as soon
481 * as possible, eg. if you're using a buffer pool.
485 g_object_class_install_property (gobject_class, PROP_ENABLE_LAST_BUFFER,
486 g_param_spec_boolean ("enable-last-buffer", "Enable Last Buffer",
487 "Enable the last-buffer property", DEFAULT_ENABLE_LAST_BUFFER,
488 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
491 * GstBaseSink:last-buffer
493 * The last buffer that arrived in the sink and was used for preroll or for
494 * rendering. This property can be used to generate thumbnails. This property
495 * can be NULL when the sink has not yet received a bufer.
499 g_object_class_install_property (gobject_class, PROP_LAST_BUFFER,
500 g_param_spec_boxed ("last-buffer", "Last Buffer",
501 "The last buffer received in the sink", GST_TYPE_BUFFER,
502 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
504 * GstBaseSink:blocksize
506 * The amount of bytes to pull when operating in pull mode.
510 /* FIXME 0.11: blocksize property should be int, otherwise min>max.. */
511 g_object_class_install_property (gobject_class, PROP_BLOCKSIZE,
512 g_param_spec_uint ("blocksize", "Block size",
513 "Size in bytes to pull per buffer (0 = default)", 0, G_MAXUINT,
514 DEFAULT_BLOCKSIZE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
516 * GstBaseSink:render-delay
518 * The additional delay between synchronisation and actual rendering of the
519 * media. This property will add additional latency to the device in order to
520 * make other sinks compensate for the delay.
524 g_object_class_install_property (gobject_class, PROP_RENDER_DELAY,
525 g_param_spec_uint64 ("render-delay", "Render Delay",
526 "Additional render delay of the sink in nanoseconds", 0, G_MAXUINT64,
527 DEFAULT_RENDER_DELAY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
529 * GstBaseSink:throttle-time
531 * The time to insert between buffers. This property can be used to control
532 * the maximum amount of buffers per second to render. Setting this property
533 * to a value bigger than 0 will make the sink create THROTTLE QoS events.
537 g_object_class_install_property (gobject_class, PROP_THROTTLE_TIME,
538 g_param_spec_uint64 ("throttle-time", "Throttle time",
539 "The time to keep between rendered buffers (unused)", 0, G_MAXUINT64,
540 DEFAULT_THROTTLE_TIME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
542 gstelement_class->change_state =
543 GST_DEBUG_FUNCPTR (gst_base_sink_change_state);
544 gstelement_class->send_event = GST_DEBUG_FUNCPTR (gst_base_sink_send_event);
545 gstelement_class->query = GST_DEBUG_FUNCPTR (gst_base_sink_query);
546 gstelement_class->get_query_types =
547 GST_DEBUG_FUNCPTR (gst_base_sink_get_query_types);
549 klass->get_caps = GST_DEBUG_FUNCPTR (gst_base_sink_get_caps);
550 klass->set_caps = GST_DEBUG_FUNCPTR (gst_base_sink_set_caps);
551 klass->get_times = GST_DEBUG_FUNCPTR (gst_base_sink_get_times);
552 klass->activate_pull =
553 GST_DEBUG_FUNCPTR (gst_base_sink_default_activate_pull);
555 /* Registering debug symbols for function pointers */
556 GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_pad_getcaps);
557 GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_pad_fixate);
558 GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_pad_activate);
559 GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_pad_activate_push);
560 GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_pad_activate_pull);
561 GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_event);
562 GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_chain);
563 GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_chain_list);
567 gst_base_sink_pad_getcaps (GstPad * pad, GstCaps * filter)
569 GstBaseSinkClass *bclass;
571 GstCaps *caps = NULL;
573 bsink = GST_BASE_SINK (gst_pad_get_parent (pad));
574 bclass = GST_BASE_SINK_GET_CLASS (bsink);
576 if (bsink->pad_mode == GST_ACTIVATE_PULL) {
577 /* if we are operating in pull mode we only accept the negotiated caps */
578 caps = gst_pad_get_current_caps (pad);
581 if (bclass->get_caps)
582 caps = bclass->get_caps (bsink, filter);
585 GstPadTemplate *pad_template;
588 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (bclass),
590 if (pad_template != NULL) {
591 caps = gst_pad_template_get_caps (pad_template);
594 GstCaps *intersection;
597 gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
598 gst_caps_unref (caps);
604 gst_object_unref (bsink);
610 gst_base_sink_pad_fixate (GstPad * pad, GstCaps * caps)
612 GstBaseSinkClass *bclass;
615 bsink = GST_BASE_SINK (gst_pad_get_parent (pad));
616 bclass = GST_BASE_SINK_GET_CLASS (bsink);
619 bclass->fixate (bsink, caps);
621 gst_object_unref (bsink);
625 gst_base_sink_init (GstBaseSink * basesink, gpointer g_class)
627 GstPadTemplate *pad_template;
628 GstBaseSinkPrivate *priv;
630 basesink->priv = priv = GST_BASE_SINK_GET_PRIVATE (basesink);
633 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (g_class), "sink");
634 g_return_if_fail (pad_template != NULL);
636 basesink->sinkpad = gst_pad_new_from_template (pad_template, "sink");
638 gst_pad_set_getcaps_function (basesink->sinkpad, gst_base_sink_pad_getcaps);
639 gst_pad_set_fixatecaps_function (basesink->sinkpad, gst_base_sink_pad_fixate);
640 gst_pad_set_activate_function (basesink->sinkpad, gst_base_sink_pad_activate);
641 gst_pad_set_activatepush_function (basesink->sinkpad,
642 gst_base_sink_pad_activate_push);
643 gst_pad_set_activatepull_function (basesink->sinkpad,
644 gst_base_sink_pad_activate_pull);
645 gst_pad_set_event_function (basesink->sinkpad, gst_base_sink_event);
646 gst_pad_set_chain_function (basesink->sinkpad, gst_base_sink_chain);
647 gst_pad_set_chain_list_function (basesink->sinkpad, gst_base_sink_chain_list);
648 gst_element_add_pad (GST_ELEMENT_CAST (basesink), basesink->sinkpad);
650 basesink->pad_mode = GST_ACTIVATE_NONE;
651 basesink->preroll_lock = g_mutex_new ();
652 basesink->preroll_cond = g_cond_new ();
653 basesink->preroll_queue = g_queue_new ();
654 basesink->clip_segment = gst_segment_new ();
655 priv->have_latency = FALSE;
657 basesink->can_activate_push = DEFAULT_CAN_ACTIVATE_PUSH;
658 basesink->can_activate_pull = DEFAULT_CAN_ACTIVATE_PULL;
660 basesink->sync = DEFAULT_SYNC;
661 basesink->max_lateness = DEFAULT_MAX_LATENESS;
662 g_atomic_int_set (&priv->qos_enabled, DEFAULT_QOS);
663 priv->async_enabled = DEFAULT_ASYNC;
664 priv->ts_offset = DEFAULT_TS_OFFSET;
665 priv->render_delay = DEFAULT_RENDER_DELAY;
666 priv->blocksize = DEFAULT_BLOCKSIZE;
667 priv->cached_clock_id = NULL;
668 g_atomic_int_set (&priv->enable_last_buffer, DEFAULT_ENABLE_LAST_BUFFER);
669 priv->throttle_time = DEFAULT_THROTTLE_TIME;
671 GST_OBJECT_FLAG_SET (basesink, GST_ELEMENT_IS_SINK);
675 gst_base_sink_finalize (GObject * object)
677 GstBaseSink *basesink;
679 basesink = GST_BASE_SINK (object);
681 g_mutex_free (basesink->preroll_lock);
682 g_cond_free (basesink->preroll_cond);
683 g_queue_free (basesink->preroll_queue);
684 gst_segment_free (basesink->clip_segment);
686 G_OBJECT_CLASS (parent_class)->finalize (object);
690 * gst_base_sink_set_sync:
692 * @sync: the new sync value.
694 * Configures @sink to synchronize on the clock or not. When
695 * @sync is FALSE, incomming samples will be played as fast as
696 * possible. If @sync is TRUE, the timestamps of the incomming
697 * buffers will be used to schedule the exact render time of its
703 gst_base_sink_set_sync (GstBaseSink * sink, gboolean sync)
705 g_return_if_fail (GST_IS_BASE_SINK (sink));
707 GST_OBJECT_LOCK (sink);
709 GST_OBJECT_UNLOCK (sink);
713 * gst_base_sink_get_sync:
716 * Checks if @sink is currently configured to synchronize against the
719 * Returns: TRUE if the sink is configured to synchronize against the clock.
724 gst_base_sink_get_sync (GstBaseSink * sink)
728 g_return_val_if_fail (GST_IS_BASE_SINK (sink), FALSE);
730 GST_OBJECT_LOCK (sink);
732 GST_OBJECT_UNLOCK (sink);
738 * gst_base_sink_set_max_lateness:
740 * @max_lateness: the new max lateness value.
742 * Sets the new max lateness value to @max_lateness. This value is
743 * used to decide if a buffer should be dropped or not based on the
744 * buffer timestamp and the current clock time. A value of -1 means
750 gst_base_sink_set_max_lateness (GstBaseSink * sink, gint64 max_lateness)
752 g_return_if_fail (GST_IS_BASE_SINK (sink));
754 GST_OBJECT_LOCK (sink);
755 sink->max_lateness = max_lateness;
756 GST_OBJECT_UNLOCK (sink);
760 * gst_base_sink_get_max_lateness:
763 * Gets the max lateness value. See gst_base_sink_set_max_lateness for
766 * Returns: The maximum time in nanoseconds that a buffer can be late
767 * before it is dropped and not rendered. A value of -1 means an
773 gst_base_sink_get_max_lateness (GstBaseSink * sink)
777 g_return_val_if_fail (GST_IS_BASE_SINK (sink), -1);
779 GST_OBJECT_LOCK (sink);
780 res = sink->max_lateness;
781 GST_OBJECT_UNLOCK (sink);
787 * gst_base_sink_set_qos_enabled:
789 * @enabled: the new qos value.
791 * Configures @sink to send Quality-of-Service events upstream.
796 gst_base_sink_set_qos_enabled (GstBaseSink * sink, gboolean enabled)
798 g_return_if_fail (GST_IS_BASE_SINK (sink));
800 g_atomic_int_set (&sink->priv->qos_enabled, enabled);
804 * gst_base_sink_is_qos_enabled:
807 * Checks if @sink is currently configured to send Quality-of-Service events
810 * Returns: TRUE if the sink is configured to perform Quality-of-Service.
815 gst_base_sink_is_qos_enabled (GstBaseSink * sink)
819 g_return_val_if_fail (GST_IS_BASE_SINK (sink), FALSE);
821 res = g_atomic_int_get (&sink->priv->qos_enabled);
827 * gst_base_sink_set_async_enabled:
829 * @enabled: the new async value.
831 * Configures @sink to perform all state changes asynchronusly. When async is
832 * disabled, the sink will immediatly go to PAUSED instead of waiting for a
833 * preroll buffer. This feature is usefull if the sink does not synchronize
834 * against the clock or when it is dealing with sparse streams.
839 gst_base_sink_set_async_enabled (GstBaseSink * sink, gboolean enabled)
841 g_return_if_fail (GST_IS_BASE_SINK (sink));
843 GST_BASE_SINK_PREROLL_LOCK (sink);
844 g_atomic_int_set (&sink->priv->async_enabled, enabled);
845 GST_LOG_OBJECT (sink, "set async enabled to %d", enabled);
846 GST_BASE_SINK_PREROLL_UNLOCK (sink);
850 * gst_base_sink_is_async_enabled:
853 * Checks if @sink is currently configured to perform asynchronous state
856 * Returns: TRUE if the sink is configured to perform asynchronous state
862 gst_base_sink_is_async_enabled (GstBaseSink * sink)
866 g_return_val_if_fail (GST_IS_BASE_SINK (sink), FALSE);
868 res = g_atomic_int_get (&sink->priv->async_enabled);
874 * gst_base_sink_set_ts_offset:
876 * @offset: the new offset
878 * Adjust the synchronisation of @sink with @offset. A negative value will
879 * render buffers earlier than their timestamp. A positive value will delay
880 * rendering. This function can be used to fix playback of badly timestamped
886 gst_base_sink_set_ts_offset (GstBaseSink * sink, GstClockTimeDiff offset)
888 g_return_if_fail (GST_IS_BASE_SINK (sink));
890 GST_OBJECT_LOCK (sink);
891 sink->priv->ts_offset = offset;
892 GST_LOG_OBJECT (sink, "set time offset to %" G_GINT64_FORMAT, offset);
893 GST_OBJECT_UNLOCK (sink);
897 * gst_base_sink_get_ts_offset:
900 * Get the synchronisation offset of @sink.
902 * Returns: The synchronisation offset.
907 gst_base_sink_get_ts_offset (GstBaseSink * sink)
909 GstClockTimeDiff res;
911 g_return_val_if_fail (GST_IS_BASE_SINK (sink), 0);
913 GST_OBJECT_LOCK (sink);
914 res = sink->priv->ts_offset;
915 GST_OBJECT_UNLOCK (sink);
921 * gst_base_sink_get_last_buffer:
924 * Get the last buffer that arrived in the sink and was used for preroll or for
925 * rendering. This property can be used to generate thumbnails.
927 * The #GstCaps on the buffer can be used to determine the type of the buffer.
929 * Free-function: gst_buffer_unref
931 * Returns: (transfer full): a #GstBuffer. gst_buffer_unref() after usage.
932 * This function returns NULL when no buffer has arrived in the sink yet
933 * or when the sink is not in PAUSED or PLAYING.
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 if (!g_atomic_int_get (&sink->priv->enable_last_buffer))
982 GST_OBJECT_LOCK (sink);
983 gst_base_sink_set_last_buffer_unlocked (sink, buffer);
984 GST_OBJECT_UNLOCK (sink);
988 * gst_base_sink_set_last_buffer_enabled:
990 * @enabled: the new enable-last-buffer value.
992 * Configures @sink to store the last received buffer in the last-buffer
998 gst_base_sink_set_last_buffer_enabled (GstBaseSink * sink, gboolean enabled)
1000 g_return_if_fail (GST_IS_BASE_SINK (sink));
1002 /* Only take lock if we change the value */
1003 if (g_atomic_int_compare_and_exchange (&sink->priv->enable_last_buffer,
1004 !enabled, enabled) && !enabled) {
1005 GST_OBJECT_LOCK (sink);
1006 gst_base_sink_set_last_buffer_unlocked (sink, NULL);
1007 GST_OBJECT_UNLOCK (sink);
1012 * gst_base_sink_is_last_buffer_enabled:
1015 * Checks if @sink is currently configured to store the last received buffer in
1016 * the last-buffer property.
1018 * Returns: TRUE if the sink is configured to store the last received buffer.
1023 gst_base_sink_is_last_buffer_enabled (GstBaseSink * sink)
1025 g_return_val_if_fail (GST_IS_BASE_SINK (sink), FALSE);
1027 return g_atomic_int_get (&sink->priv->enable_last_buffer);
1031 * gst_base_sink_get_latency:
1034 * Get the currently configured latency.
1036 * Returns: The configured latency.
1041 gst_base_sink_get_latency (GstBaseSink * sink)
1045 GST_OBJECT_LOCK (sink);
1046 res = sink->priv->latency;
1047 GST_OBJECT_UNLOCK (sink);
1053 * gst_base_sink_query_latency:
1055 * @live: (out) (allow-none): if the sink is live
1056 * @upstream_live: (out) (allow-none): if an upstream element is live
1057 * @min_latency: (out) (allow-none): the min latency of the upstream elements
1058 * @max_latency: (out) (allow-none): the max latency of the upstream elements
1060 * Query the sink for the latency parameters. The latency will be queried from
1061 * the upstream elements. @live will be TRUE if @sink is configured to
1062 * synchronize against the clock. @upstream_live will be TRUE if an upstream
1065 * If both @live and @upstream_live are TRUE, the sink will want to compensate
1066 * for the latency introduced by the upstream elements by setting the
1067 * @min_latency to a strictly possitive value.
1069 * This function is mostly used by subclasses.
1071 * Returns: TRUE if the query succeeded.
1076 gst_base_sink_query_latency (GstBaseSink * sink, gboolean * live,
1077 gboolean * upstream_live, GstClockTime * min_latency,
1078 GstClockTime * max_latency)
1080 gboolean l, us_live, res, have_latency;
1081 GstClockTime min, max, render_delay;
1083 GstClockTime us_min, us_max;
1085 /* we are live when we sync to the clock */
1086 GST_OBJECT_LOCK (sink);
1088 have_latency = sink->priv->have_latency;
1089 render_delay = sink->priv->render_delay;
1090 GST_OBJECT_UNLOCK (sink);
1092 /* assume no latency */
1098 GST_DEBUG_OBJECT (sink, "we are ready for LATENCY query");
1099 /* we are ready for a latency query this is when we preroll or when we are
1101 query = gst_query_new_latency ();
1103 /* ask the peer for the latency */
1104 if ((res = gst_pad_peer_query (sink->sinkpad, query))) {
1105 /* get upstream min and max latency */
1106 gst_query_parse_latency (query, &us_live, &us_min, &us_max);
1109 /* upstream live, use its latency, subclasses should use these
1110 * values to create the complete latency. */
1115 /* we need to add the render delay if we are live */
1117 min += render_delay;
1119 max += render_delay;
1122 gst_query_unref (query);
1124 GST_DEBUG_OBJECT (sink, "we are not yet ready for LATENCY query");
1128 /* not live, we tried to do the query, if it failed we return TRUE anyway */
1132 GST_DEBUG_OBJECT (sink, "latency query failed but we are not live");
1134 GST_DEBUG_OBJECT (sink, "latency query failed and we are live");
1139 GST_DEBUG_OBJECT (sink, "latency query: live: %d, have_latency %d,"
1140 " upstream: %d, min %" GST_TIME_FORMAT ", max %" GST_TIME_FORMAT, l,
1141 have_latency, us_live, GST_TIME_ARGS (min), GST_TIME_ARGS (max));
1146 *upstream_live = us_live;
1156 * gst_base_sink_set_render_delay:
1157 * @sink: a #GstBaseSink
1158 * @delay: the new delay
1160 * Set the render delay in @sink to @delay. The render delay is the time
1161 * between actual rendering of a buffer and its synchronisation time. Some
1162 * devices might delay media rendering which can be compensated for with this
1165 * After calling this function, this sink will report additional latency and
1166 * other sinks will adjust their latency to delay the rendering of their media.
1168 * This function is usually called by subclasses.
1173 gst_base_sink_set_render_delay (GstBaseSink * sink, GstClockTime delay)
1175 GstClockTime old_render_delay;
1177 g_return_if_fail (GST_IS_BASE_SINK (sink));
1179 GST_OBJECT_LOCK (sink);
1180 old_render_delay = sink->priv->render_delay;
1181 sink->priv->render_delay = delay;
1182 GST_LOG_OBJECT (sink, "set render delay to %" GST_TIME_FORMAT,
1183 GST_TIME_ARGS (delay));
1184 GST_OBJECT_UNLOCK (sink);
1186 if (delay != old_render_delay) {
1187 GST_DEBUG_OBJECT (sink, "posting latency changed");
1188 gst_element_post_message (GST_ELEMENT_CAST (sink),
1189 gst_message_new_latency (GST_OBJECT_CAST (sink)));
1194 * gst_base_sink_get_render_delay:
1195 * @sink: a #GstBaseSink
1197 * Get the render delay of @sink. see gst_base_sink_set_render_delay() for more
1198 * information about the render delay.
1200 * Returns: the render delay of @sink.
1205 gst_base_sink_get_render_delay (GstBaseSink * sink)
1207 GstClockTimeDiff res;
1209 g_return_val_if_fail (GST_IS_BASE_SINK (sink), 0);
1211 GST_OBJECT_LOCK (sink);
1212 res = sink->priv->render_delay;
1213 GST_OBJECT_UNLOCK (sink);
1219 * gst_base_sink_set_blocksize:
1220 * @sink: a #GstBaseSink
1221 * @blocksize: the blocksize in bytes
1223 * Set the number of bytes that the sink will pull when it is operating in pull
1228 /* FIXME 0.11: blocksize property should be int, otherwise min>max.. */
1230 gst_base_sink_set_blocksize (GstBaseSink * sink, guint blocksize)
1232 g_return_if_fail (GST_IS_BASE_SINK (sink));
1234 GST_OBJECT_LOCK (sink);
1235 sink->priv->blocksize = blocksize;
1236 GST_LOG_OBJECT (sink, "set blocksize to %u", blocksize);
1237 GST_OBJECT_UNLOCK (sink);
1241 * gst_base_sink_get_blocksize:
1242 * @sink: a #GstBaseSink
1244 * Get the number of bytes that the sink will pull when it is operating in pull
1247 * Returns: the number of bytes @sink will pull in pull mode.
1251 /* FIXME 0.11: blocksize property should be int, otherwise min>max.. */
1253 gst_base_sink_get_blocksize (GstBaseSink * sink)
1257 g_return_val_if_fail (GST_IS_BASE_SINK (sink), 0);
1259 GST_OBJECT_LOCK (sink);
1260 res = sink->priv->blocksize;
1261 GST_OBJECT_UNLOCK (sink);
1267 * gst_base_sink_set_throttle_time:
1268 * @sink: a #GstBaseSink
1269 * @throttle: the throttle time in nanoseconds
1271 * Set the time that will be inserted between rendered buffers. This
1272 * can be used to control the maximum buffers per second that the sink
1278 gst_base_sink_set_throttle_time (GstBaseSink * sink, guint64 throttle)
1280 g_return_if_fail (GST_IS_BASE_SINK (sink));
1282 GST_OBJECT_LOCK (sink);
1283 sink->priv->throttle_time = throttle;
1284 GST_LOG_OBJECT (sink, "set throttle_time to %" G_GUINT64_FORMAT, throttle);
1285 GST_OBJECT_UNLOCK (sink);
1289 * gst_base_sink_get_throttle_time:
1290 * @sink: a #GstBaseSink
1292 * Get the time that will be inserted between frames to control the
1293 * maximum buffers per second.
1295 * Returns: the number of nanoseconds @sink will put between frames.
1300 gst_base_sink_get_throttle_time (GstBaseSink * sink)
1304 g_return_val_if_fail (GST_IS_BASE_SINK (sink), 0);
1306 GST_OBJECT_LOCK (sink);
1307 res = sink->priv->throttle_time;
1308 GST_OBJECT_UNLOCK (sink);
1314 gst_base_sink_set_property (GObject * object, guint prop_id,
1315 const GValue * value, GParamSpec * pspec)
1317 GstBaseSink *sink = GST_BASE_SINK (object);
1320 case PROP_PREROLL_QUEUE_LEN:
1321 /* preroll lock necessary to serialize with finish_preroll */
1322 GST_BASE_SINK_PREROLL_LOCK (sink);
1323 g_atomic_int_set (&sink->preroll_queue_max_len, g_value_get_uint (value));
1324 GST_BASE_SINK_PREROLL_UNLOCK (sink);
1327 gst_base_sink_set_sync (sink, g_value_get_boolean (value));
1329 case PROP_MAX_LATENESS:
1330 gst_base_sink_set_max_lateness (sink, g_value_get_int64 (value));
1333 gst_base_sink_set_qos_enabled (sink, g_value_get_boolean (value));
1336 gst_base_sink_set_async_enabled (sink, g_value_get_boolean (value));
1338 case PROP_TS_OFFSET:
1339 gst_base_sink_set_ts_offset (sink, g_value_get_int64 (value));
1341 case PROP_BLOCKSIZE:
1342 gst_base_sink_set_blocksize (sink, g_value_get_uint (value));
1344 case PROP_RENDER_DELAY:
1345 gst_base_sink_set_render_delay (sink, g_value_get_uint64 (value));
1347 case PROP_ENABLE_LAST_BUFFER:
1348 gst_base_sink_set_last_buffer_enabled (sink, g_value_get_boolean (value));
1350 case PROP_THROTTLE_TIME:
1351 gst_base_sink_set_throttle_time (sink, g_value_get_uint64 (value));
1354 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1360 gst_base_sink_get_property (GObject * object, guint prop_id, GValue * value,
1363 GstBaseSink *sink = GST_BASE_SINK (object);
1366 case PROP_PREROLL_QUEUE_LEN:
1367 g_value_set_uint (value, g_atomic_int_get (&sink->preroll_queue_max_len));
1370 g_value_set_boolean (value, gst_base_sink_get_sync (sink));
1372 case PROP_MAX_LATENESS:
1373 g_value_set_int64 (value, gst_base_sink_get_max_lateness (sink));
1376 g_value_set_boolean (value, gst_base_sink_is_qos_enabled (sink));
1379 g_value_set_boolean (value, gst_base_sink_is_async_enabled (sink));
1381 case PROP_TS_OFFSET:
1382 g_value_set_int64 (value, gst_base_sink_get_ts_offset (sink));
1384 case PROP_LAST_BUFFER:
1385 gst_value_take_buffer (value, gst_base_sink_get_last_buffer (sink));
1387 case PROP_ENABLE_LAST_BUFFER:
1388 g_value_set_boolean (value, gst_base_sink_is_last_buffer_enabled (sink));
1390 case PROP_BLOCKSIZE:
1391 g_value_set_uint (value, gst_base_sink_get_blocksize (sink));
1393 case PROP_RENDER_DELAY:
1394 g_value_set_uint64 (value, gst_base_sink_get_render_delay (sink));
1396 case PROP_THROTTLE_TIME:
1397 g_value_set_uint64 (value, gst_base_sink_get_throttle_time (sink));
1400 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1407 gst_base_sink_get_caps (GstBaseSink * sink, GstCaps * filter)
1413 gst_base_sink_set_caps (GstBaseSink * sink, GstCaps * caps)
1418 /* with PREROLL_LOCK, STREAM_LOCK */
1420 gst_base_sink_preroll_queue_flush (GstBaseSink * basesink, GstPad * pad)
1424 GST_DEBUG_OBJECT (basesink, "flushing queue %p", basesink);
1425 while ((obj = g_queue_pop_head (basesink->preroll_queue))) {
1426 GST_DEBUG_OBJECT (basesink, "popped %p", obj);
1427 gst_mini_object_unref (obj);
1429 /* we can't have EOS anymore now */
1430 basesink->eos = FALSE;
1431 basesink->priv->received_eos = FALSE;
1432 basesink->have_preroll = FALSE;
1433 basesink->priv->step_unlock = FALSE;
1434 basesink->eos_queued = FALSE;
1435 basesink->preroll_queued = 0;
1436 basesink->buffers_queued = 0;
1437 basesink->events_queued = 0;
1438 /* can't report latency anymore until we preroll again */
1439 if (basesink->priv->async_enabled) {
1440 GST_OBJECT_LOCK (basesink);
1441 basesink->priv->have_latency = FALSE;
1442 GST_OBJECT_UNLOCK (basesink);
1444 /* and signal any waiters now */
1445 GST_BASE_SINK_PREROLL_SIGNAL (basesink);
1448 /* with STREAM_LOCK, configures given segment with the event information. */
1450 gst_base_sink_configure_segment (GstBaseSink * basesink, GstPad * pad,
1451 GstEvent * event, GstSegment * segment)
1453 /* The segment is protected with both the STREAM_LOCK and the OBJECT_LOCK.
1454 * We protect with the OBJECT_LOCK so that we can use the values to
1455 * safely answer a POSITION query. */
1456 GST_OBJECT_LOCK (basesink);
1457 /* the newsegment event is needed to bring the buffer timestamps to the
1458 * stream time and to drop samples outside of the playback segment. */
1459 gst_event_copy_segment (event, segment);
1460 GST_DEBUG_OBJECT (basesink, "configured SEGMENT %" GST_SEGMENT_FORMAT,
1462 GST_OBJECT_UNLOCK (basesink);
1465 /* with PREROLL_LOCK, STREAM_LOCK */
1467 gst_base_sink_commit_state (GstBaseSink * basesink)
1469 /* commit state and proceed to next pending state */
1470 GstState current, next, pending, post_pending;
1471 gboolean post_paused = FALSE;
1472 gboolean post_async_done = FALSE;
1473 gboolean post_playing = FALSE;
1474 gboolean reset_time;
1476 /* we are certainly not playing async anymore now */
1477 basesink->playing_async = FALSE;
1479 GST_OBJECT_LOCK (basesink);
1480 current = GST_STATE (basesink);
1481 next = GST_STATE_NEXT (basesink);
1482 pending = GST_STATE_PENDING (basesink);
1483 post_pending = pending;
1484 reset_time = basesink->priv->reset_time;
1485 basesink->priv->reset_time = FALSE;
1488 case GST_STATE_PLAYING:
1490 GST_DEBUG_OBJECT (basesink, "commiting state to PLAYING");
1492 basesink->need_preroll = FALSE;
1493 post_async_done = TRUE;
1494 basesink->priv->commited = TRUE;
1495 post_playing = TRUE;
1496 /* post PAUSED too when we were READY */
1497 if (current == GST_STATE_READY) {
1502 case GST_STATE_PAUSED:
1503 GST_DEBUG_OBJECT (basesink, "commiting state to PAUSED");
1505 post_async_done = TRUE;
1506 basesink->priv->commited = TRUE;
1507 post_pending = GST_STATE_VOID_PENDING;
1509 case GST_STATE_READY:
1510 case GST_STATE_NULL:
1512 case GST_STATE_VOID_PENDING:
1513 goto nothing_pending;
1518 /* we can report latency queries now */
1519 basesink->priv->have_latency = TRUE;
1521 GST_STATE (basesink) = pending;
1522 GST_STATE_NEXT (basesink) = GST_STATE_VOID_PENDING;
1523 GST_STATE_PENDING (basesink) = GST_STATE_VOID_PENDING;
1524 GST_STATE_RETURN (basesink) = GST_STATE_CHANGE_SUCCESS;
1525 GST_OBJECT_UNLOCK (basesink);
1528 GST_DEBUG_OBJECT (basesink, "posting PAUSED state change message");
1529 gst_element_post_message (GST_ELEMENT_CAST (basesink),
1530 gst_message_new_state_changed (GST_OBJECT_CAST (basesink),
1531 current, next, post_pending));
1533 if (post_async_done) {
1534 GST_DEBUG_OBJECT (basesink, "posting async-done message");
1535 gst_element_post_message (GST_ELEMENT_CAST (basesink),
1536 gst_message_new_async_done (GST_OBJECT_CAST (basesink), reset_time));
1539 GST_DEBUG_OBJECT (basesink, "posting PLAYING state change message");
1540 gst_element_post_message (GST_ELEMENT_CAST (basesink),
1541 gst_message_new_state_changed (GST_OBJECT_CAST (basesink),
1542 next, pending, GST_STATE_VOID_PENDING));
1545 GST_STATE_BROADCAST (basesink);
1551 /* Depending on the state, set our vars. We get in this situation when the
1552 * state change function got a change to update the state vars before the
1553 * streaming thread did. This is fine but we need to make sure that we
1554 * update the need_preroll var since it was TRUE when we got here and might
1555 * become FALSE if we got to PLAYING. */
1556 GST_DEBUG_OBJECT (basesink, "nothing to commit, now in %s",
1557 gst_element_state_get_name (current));
1559 case GST_STATE_PLAYING:
1560 basesink->need_preroll = FALSE;
1562 case GST_STATE_PAUSED:
1563 basesink->need_preroll = TRUE;
1566 basesink->need_preroll = FALSE;
1567 basesink->flushing = TRUE;
1570 /* we can report latency queries now */
1571 basesink->priv->have_latency = TRUE;
1572 GST_OBJECT_UNLOCK (basesink);
1577 /* app is going to READY */
1578 GST_DEBUG_OBJECT (basesink, "stopping");
1579 basesink->need_preroll = FALSE;
1580 basesink->flushing = TRUE;
1581 GST_OBJECT_UNLOCK (basesink);
1587 start_stepping (GstBaseSink * sink, GstSegment * segment,
1588 GstStepInfo * pending, GstStepInfo * current)
1591 GstMessage *message;
1593 GST_DEBUG_OBJECT (sink, "update pending step");
1595 GST_OBJECT_LOCK (sink);
1596 memcpy (current, pending, sizeof (GstStepInfo));
1597 pending->valid = FALSE;
1598 GST_OBJECT_UNLOCK (sink);
1600 /* post message first */
1602 gst_message_new_step_start (GST_OBJECT (sink), TRUE, current->format,
1603 current->amount, current->rate, current->flush, current->intermediate);
1604 gst_message_set_seqnum (message, current->seqnum);
1605 gst_element_post_message (GST_ELEMENT (sink), message);
1607 /* get the running time of where we paused and remember it */
1608 current->start = gst_element_get_start_time (GST_ELEMENT_CAST (sink));
1609 gst_segment_set_running_time (segment, GST_FORMAT_TIME, current->start);
1611 /* set the new rate for the remainder of the segment */
1612 current->start_rate = segment->rate;
1613 segment->rate *= current->rate;
1616 if (segment->rate > 0.0)
1617 current->start_stop = segment->stop;
1619 current->start_start = segment->start;
1621 if (current->format == GST_FORMAT_TIME) {
1622 end = current->start + current->amount;
1623 if (!current->flush) {
1624 /* update the segment clipping regions for non-flushing seeks */
1625 if (segment->rate > 0.0) {
1626 segment->stop = gst_segment_to_position (segment, GST_FORMAT_TIME, end);
1627 segment->position = segment->stop;
1631 position = gst_segment_to_position (segment, GST_FORMAT_TIME, end);
1632 segment->time = position;
1633 segment->start = position;
1634 segment->position = position;
1639 GST_DEBUG_OBJECT (sink, "segment now %" GST_SEGMENT_FORMAT, segment);
1640 GST_DEBUG_OBJECT (sink, "step started at running_time %" GST_TIME_FORMAT,
1641 GST_TIME_ARGS (current->start));
1643 if (current->amount == -1) {
1644 GST_DEBUG_OBJECT (sink, "step amount == -1, stop stepping");
1645 current->valid = FALSE;
1647 GST_DEBUG_OBJECT (sink, "step amount: %" G_GUINT64_FORMAT ", format: %s, "
1648 "rate: %f", current->amount, gst_format_get_name (current->format),
1654 stop_stepping (GstBaseSink * sink, GstSegment * segment,
1655 GstStepInfo * current, gint64 rstart, gint64 rstop, gboolean eos)
1657 gint64 stop, position;
1658 GstMessage *message;
1660 GST_DEBUG_OBJECT (sink, "step complete");
1662 if (segment->rate > 0.0)
1667 GST_DEBUG_OBJECT (sink,
1668 "step stop at running_time %" GST_TIME_FORMAT, GST_TIME_ARGS (stop));
1671 current->duration = current->position;
1673 current->duration = stop - current->start;
1675 GST_DEBUG_OBJECT (sink, "step elapsed running_time %" GST_TIME_FORMAT,
1676 GST_TIME_ARGS (current->duration));
1678 position = current->start + current->duration;
1680 /* now move the segment to the new running time */
1681 gst_segment_set_running_time (segment, GST_FORMAT_TIME, position);
1683 if (current->flush) {
1684 /* and remove the time we flushed, start time did not change */
1685 segment->base = current->start;
1687 /* start time is now the stepped position */
1688 gst_element_set_start_time (GST_ELEMENT_CAST (sink), position);
1691 /* restore the previous rate */
1692 segment->rate = current->start_rate;
1694 if (segment->rate > 0.0)
1695 segment->stop = current->start_stop;
1697 segment->start = current->start_start;
1699 /* the clip segment is used for position report in paused... */
1700 gst_segment_copy_into (segment, sink->clip_segment);
1702 /* post the step done when we know the stepped duration in TIME */
1704 gst_message_new_step_done (GST_OBJECT_CAST (sink), current->format,
1705 current->amount, current->rate, current->flush, current->intermediate,
1706 current->duration, eos);
1707 gst_message_set_seqnum (message, current->seqnum);
1708 gst_element_post_message (GST_ELEMENT_CAST (sink), message);
1710 if (!current->intermediate)
1711 sink->need_preroll = current->need_preroll;
1713 /* and the current step info finished and becomes invalid */
1714 current->valid = FALSE;
1718 handle_stepping (GstBaseSink * sink, GstSegment * segment,
1719 GstStepInfo * current, guint64 * cstart, guint64 * cstop, guint64 * rstart,
1722 gboolean step_end = FALSE;
1724 /* see if we need to skip this buffer because of stepping */
1725 switch (current->format) {
1726 case GST_FORMAT_TIME:
1729 guint64 first, last;
1732 if (segment->rate > 0.0) {
1733 if (segment->stop == *cstop)
1734 *rstop = *rstart + current->amount;
1739 if (segment->start == *cstart)
1740 *rstart = *rstop + current->amount;
1746 end = current->start + current->amount;
1747 current->position = first - current->start;
1749 abs_rate = ABS (segment->rate);
1750 if (G_UNLIKELY (abs_rate != 1.0))
1751 current->position /= abs_rate;
1753 GST_DEBUG_OBJECT (sink,
1754 "buffer: %" GST_TIME_FORMAT "-%" GST_TIME_FORMAT,
1755 GST_TIME_ARGS (first), GST_TIME_ARGS (last));
1756 GST_DEBUG_OBJECT (sink,
1757 "got time step %" GST_TIME_FORMAT "-%" GST_TIME_FORMAT "/%"
1758 GST_TIME_FORMAT, GST_TIME_ARGS (current->position),
1759 GST_TIME_ARGS (last - current->start),
1760 GST_TIME_ARGS (current->amount));
1762 if ((current->flush && current->position >= current->amount)
1764 GST_DEBUG_OBJECT (sink, "step ended, we need clipping");
1766 if (segment->rate > 0.0) {
1768 *cstart = gst_segment_to_position (segment, GST_FORMAT_TIME, end);
1771 *cstop = gst_segment_to_position (segment, GST_FORMAT_TIME, end);
1774 GST_DEBUG_OBJECT (sink,
1775 "cstart %" GST_TIME_FORMAT ", rstart %" GST_TIME_FORMAT,
1776 GST_TIME_ARGS (*cstart), GST_TIME_ARGS (*rstart));
1777 GST_DEBUG_OBJECT (sink,
1778 "cstop %" GST_TIME_FORMAT ", rstop %" GST_TIME_FORMAT,
1779 GST_TIME_ARGS (*cstop), GST_TIME_ARGS (*rstop));
1782 case GST_FORMAT_BUFFERS:
1783 GST_DEBUG_OBJECT (sink,
1784 "got default step %" G_GUINT64_FORMAT "/%" G_GUINT64_FORMAT,
1785 current->position, current->amount);
1787 if (current->position < current->amount) {
1788 current->position++;
1793 case GST_FORMAT_DEFAULT:
1795 GST_DEBUG_OBJECT (sink,
1796 "got unknown step %" G_GUINT64_FORMAT "/%" G_GUINT64_FORMAT,
1797 current->position, current->amount);
1803 /* with STREAM_LOCK, PREROLL_LOCK
1805 * Returns TRUE if the object needs synchronisation and takes therefore
1806 * part in prerolling.
1808 * rsstart/rsstop contain the start/stop in stream time.
1809 * rrstart/rrstop contain the start/stop in running time.
1812 gst_base_sink_get_sync_times (GstBaseSink * basesink, GstMiniObject * obj,
1813 GstClockTime * rsstart, GstClockTime * rsstop,
1814 GstClockTime * rrstart, GstClockTime * rrstop, gboolean * do_sync,
1815 gboolean * stepped, GstSegment * segment, GstStepInfo * step,
1816 gboolean * step_end, guint8 obj_type)
1818 GstBaseSinkClass *bclass;
1820 GstClockTime start, stop; /* raw start/stop timestamps */
1821 guint64 cstart, cstop; /* clipped raw timestamps */
1822 guint64 rstart, rstop; /* clipped timestamps converted to running time */
1823 GstClockTime sstart, sstop; /* clipped timestamps converted to stream time */
1825 GstBaseSinkPrivate *priv;
1828 priv = basesink->priv;
1830 /* start with nothing */
1831 start = stop = GST_CLOCK_TIME_NONE;
1833 if (G_UNLIKELY (OBJ_IS_EVENT (obj_type))) {
1834 GstEvent *event = GST_EVENT_CAST (obj);
1836 switch (GST_EVENT_TYPE (event)) {
1837 /* EOS event needs syncing */
1840 if (basesink->segment.rate >= 0.0) {
1841 sstart = sstop = priv->current_sstop;
1842 if (!GST_CLOCK_TIME_IS_VALID (sstart)) {
1843 /* we have not seen a buffer yet, use the segment values */
1844 sstart = sstop = gst_segment_to_stream_time (&basesink->segment,
1845 basesink->segment.format, basesink->segment.stop);
1848 sstart = sstop = priv->current_sstart;
1849 if (!GST_CLOCK_TIME_IS_VALID (sstart)) {
1850 /* we have not seen a buffer yet, use the segment values */
1851 sstart = sstop = gst_segment_to_stream_time (&basesink->segment,
1852 basesink->segment.format, basesink->segment.start);
1856 rstart = rstop = priv->eos_rtime;
1857 *do_sync = rstart != -1;
1858 GST_DEBUG_OBJECT (basesink, "sync times for EOS %" GST_TIME_FORMAT,
1859 GST_TIME_ARGS (rstart));
1860 /* if we are stepping, we end now */
1861 *step_end = step->valid;
1866 /* other events do not need syncing */
1874 /* else do buffer sync code */
1875 buffer = GST_BUFFER_CAST (obj);
1877 bclass = GST_BASE_SINK_GET_CLASS (basesink);
1879 /* just get the times to see if we need syncing, if the start returns -1 we
1881 if (bclass->get_times)
1882 bclass->get_times (basesink, buffer, &start, &stop);
1884 if (!GST_CLOCK_TIME_IS_VALID (start)) {
1885 /* we don't need to sync but we still want to get the timestamps for
1886 * tracking the position */
1887 gst_base_sink_get_times (basesink, buffer, &start, &stop);
1893 GST_DEBUG_OBJECT (basesink, "got times start: %" GST_TIME_FORMAT
1894 ", stop: %" GST_TIME_FORMAT ", do_sync %d", GST_TIME_ARGS (start),
1895 GST_TIME_ARGS (stop), *do_sync);
1897 /* collect segment and format for code clarity */
1898 format = segment->format;
1901 if (G_UNLIKELY (!gst_segment_clip (segment, format,
1902 start, stop, &cstart, &cstop))) {
1904 GST_DEBUG_OBJECT (basesink, "step out of segment");
1905 /* when we are stepping, pretend we're at the end of the segment */
1906 if (segment->rate > 0.0) {
1907 cstart = segment->stop;
1908 cstop = segment->stop;
1910 cstart = segment->start;
1911 cstop = segment->start;
1915 goto out_of_segment;
1918 if (G_UNLIKELY (start != cstart || stop != cstop)) {
1919 GST_DEBUG_OBJECT (basesink, "clipped to: start %" GST_TIME_FORMAT
1920 ", stop: %" GST_TIME_FORMAT, GST_TIME_ARGS (cstart),
1921 GST_TIME_ARGS (cstop));
1924 /* set last stop position */
1925 if (G_LIKELY (stop != GST_CLOCK_TIME_NONE && cstop != GST_CLOCK_TIME_NONE))
1926 segment->position = cstop;
1928 segment->position = cstart;
1931 rstart = gst_segment_to_running_time (segment, format, cstart);
1932 rstop = gst_segment_to_running_time (segment, format, cstop);
1934 if (G_UNLIKELY (step->valid)) {
1935 if (!(*step_end = handle_stepping (basesink, segment, step, &cstart, &cstop,
1936 &rstart, &rstop))) {
1937 /* step is still busy, we discard data when we are flushing */
1938 *stepped = step->flush;
1939 GST_DEBUG_OBJECT (basesink, "stepping busy");
1942 /* this can produce wrong values if we accumulated non-TIME segments. If this happens,
1943 * upstream is behaving very badly */
1944 sstart = gst_segment_to_stream_time (segment, format, cstart);
1945 sstop = gst_segment_to_stream_time (segment, format, cstop);
1948 /* eos_done label only called when doing EOS, we also stop stepping then */
1949 if (*step_end && step->flush) {
1950 GST_DEBUG_OBJECT (basesink, "flushing step ended");
1951 stop_stepping (basesink, segment, step, rstart, rstop, eos);
1953 /* re-determine running start times for adjusted segment
1954 * (which has a flushed amount of running/accumulated time removed) */
1955 if (!GST_IS_EVENT (obj)) {
1956 GST_DEBUG_OBJECT (basesink, "refresh sync times");
1967 /* buffers and EOS always need syncing and preroll */
1973 /* we usually clip in the chain function already but stepping could cause
1974 * the segment to be updated later. we return FALSE so that we don't try
1976 GST_LOG_OBJECT (basesink, "buffer skipped, not in segment");
1981 /* with STREAM_LOCK, PREROLL_LOCK, LOCK
1982 * adjust a timestamp with the latency and timestamp offset. This function does
1983 * not adjust for the render delay. */
1985 gst_base_sink_adjust_time (GstBaseSink * basesink, GstClockTime time)
1987 GstClockTimeDiff ts_offset;
1989 /* don't do anything funny with invalid timestamps */
1990 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (time)))
1993 time += basesink->priv->latency;
1995 /* apply offset, be carefull for underflows */
1996 ts_offset = basesink->priv->ts_offset;
1997 if (ts_offset < 0) {
1998 ts_offset = -ts_offset;
1999 if (ts_offset < time)
2006 /* subtract the render delay again, which was included in the latency */
2007 if (time > basesink->priv->render_delay)
2008 time -= basesink->priv->render_delay;
2016 * gst_base_sink_wait_clock:
2018 * @time: the running_time to be reached
2019 * @jitter: (out) (allow-none): the jitter to be filled with time diff, or NULL
2021 * This function will block until @time is reached. It is usually called by
2022 * subclasses that use their own internal synchronisation.
2024 * If @time is not valid, no sycnhronisation is done and #GST_CLOCK_BADTIME is
2025 * returned. Likewise, if synchronisation is disabled in the element or there
2026 * is no clock, no synchronisation is done and #GST_CLOCK_BADTIME is returned.
2028 * This function should only be called with the PREROLL_LOCK held, like when
2029 * receiving an EOS event in the #GstBaseSinkClass.event() vmethod or when
2030 * receiving a buffer in
2031 * the #GstBaseSinkClass.render() vmethod.
2033 * The @time argument should be the running_time of when this method should
2034 * return and is not adjusted with any latency or offset configured in the
2039 * Returns: #GstClockReturn
2042 gst_base_sink_wait_clock (GstBaseSink * sink, GstClockTime time,
2043 GstClockTimeDiff * jitter)
2047 GstClockTime base_time;
2049 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (time)))
2052 GST_OBJECT_LOCK (sink);
2053 if (G_UNLIKELY (!sink->sync))
2056 if (G_UNLIKELY ((clock = GST_ELEMENT_CLOCK (sink)) == NULL))
2059 base_time = GST_ELEMENT_CAST (sink)->base_time;
2060 GST_LOG_OBJECT (sink,
2061 "time %" GST_TIME_FORMAT ", base_time %" GST_TIME_FORMAT,
2062 GST_TIME_ARGS (time), GST_TIME_ARGS (base_time));
2064 /* add base_time to running_time to get the time against the clock */
2067 /* Re-use existing clockid if available */
2068 /* FIXME: Casting to GstClockEntry only works because the types
2070 if (G_LIKELY (sink->priv->cached_clock_id != NULL
2071 && GST_CLOCK_ENTRY_CLOCK ((GstClockEntry *) sink->
2072 priv->cached_clock_id) == clock)) {
2073 if (!gst_clock_single_shot_id_reinit (clock, sink->priv->cached_clock_id,
2075 gst_clock_id_unref (sink->priv->cached_clock_id);
2076 sink->priv->cached_clock_id = gst_clock_new_single_shot_id (clock, time);
2079 if (sink->priv->cached_clock_id != NULL)
2080 gst_clock_id_unref (sink->priv->cached_clock_id);
2081 sink->priv->cached_clock_id = gst_clock_new_single_shot_id (clock, time);
2083 GST_OBJECT_UNLOCK (sink);
2085 /* A blocking wait is performed on the clock. We save the ClockID
2086 * so we can unlock the entry at any time. While we are blocking, we
2087 * release the PREROLL_LOCK so that other threads can interrupt the
2089 sink->clock_id = sink->priv->cached_clock_id;
2090 /* release the preroll lock while waiting */
2091 GST_BASE_SINK_PREROLL_UNLOCK (sink);
2093 ret = gst_clock_id_wait (sink->priv->cached_clock_id, jitter);
2095 GST_BASE_SINK_PREROLL_LOCK (sink);
2096 sink->clock_id = NULL;
2100 /* no syncing needed */
2103 GST_DEBUG_OBJECT (sink, "time not valid, no sync needed");
2104 return GST_CLOCK_BADTIME;
2108 GST_DEBUG_OBJECT (sink, "sync disabled");
2109 GST_OBJECT_UNLOCK (sink);
2110 return GST_CLOCK_BADTIME;
2114 GST_DEBUG_OBJECT (sink, "no clock, can't sync");
2115 GST_OBJECT_UNLOCK (sink);
2116 return GST_CLOCK_BADTIME;
2121 * gst_base_sink_wait_preroll:
2124 * If the #GstBaseSinkClass.render() method performs its own synchronisation
2125 * against the clock it must unblock when going from PLAYING to the PAUSED state
2126 * and call this method before continuing to render the remaining data.
2128 * This function will block until a state change to PLAYING happens (in which
2129 * case this function returns #GST_FLOW_OK) or the processing must be stopped due
2130 * to a state change to READY or a FLUSH event (in which case this function
2131 * returns #GST_FLOW_WRONG_STATE).
2133 * This function should only be called with the PREROLL_LOCK held, like in the
2136 * Returns: #GST_FLOW_OK if the preroll completed and processing can
2137 * continue. Any other return value should be returned from the render vmethod.
2142 gst_base_sink_wait_preroll (GstBaseSink * sink)
2144 sink->have_preroll = TRUE;
2145 GST_DEBUG_OBJECT (sink, "waiting in preroll for flush or PLAYING");
2146 /* block until the state changes, or we get a flush, or something */
2147 GST_BASE_SINK_PREROLL_WAIT (sink);
2148 sink->have_preroll = FALSE;
2149 if (G_UNLIKELY (sink->flushing))
2151 if (G_UNLIKELY (sink->priv->step_unlock))
2153 GST_DEBUG_OBJECT (sink, "continue after preroll");
2160 GST_DEBUG_OBJECT (sink, "preroll interrupted because of flush");
2161 return GST_FLOW_WRONG_STATE;
2165 sink->priv->step_unlock = FALSE;
2166 GST_DEBUG_OBJECT (sink, "preroll interrupted because of step");
2167 return GST_FLOW_STEP;
2171 static inline guint8
2172 get_object_type (GstMiniObject * obj)
2176 if (G_LIKELY (GST_IS_BUFFER (obj)))
2177 obj_type = _PR_IS_BUFFER;
2178 else if (GST_IS_EVENT (obj))
2179 obj_type = _PR_IS_EVENT;
2180 else if (GST_IS_BUFFER_LIST (obj))
2181 obj_type = _PR_IS_BUFFERLIST;
2183 obj_type = _PR_IS_NOTHING;
2189 * gst_base_sink_do_preroll:
2191 * @obj: (transfer none): the mini object that caused the preroll
2193 * If the @sink spawns its own thread for pulling buffers from upstream it
2194 * should call this method after it has pulled a buffer. If the element needed
2195 * to preroll, this function will perform the preroll and will then block
2196 * until the element state is changed.
2198 * This function should be called with the PREROLL_LOCK held.
2200 * Returns: #GST_FLOW_OK if the preroll completed and processing can
2201 * continue. Any other return value should be returned from the render vmethod.
2206 gst_base_sink_do_preroll (GstBaseSink * sink, GstMiniObject * obj)
2210 while (G_UNLIKELY (sink->need_preroll)) {
2212 GST_DEBUG_OBJECT (sink, "prerolling object %p", obj);
2214 obj_type = get_object_type (obj);
2216 ret = gst_base_sink_preroll_object (sink, obj_type, obj);
2217 if (ret != GST_FLOW_OK)
2218 goto preroll_failed;
2220 /* need to recheck here because the commit state could have
2221 * made us not need the preroll anymore */
2222 if (G_LIKELY (sink->need_preroll)) {
2223 /* block until the state changes, or we get a flush, or something */
2224 ret = gst_base_sink_wait_preroll (sink);
2225 if ((ret != GST_FLOW_OK) && (ret != GST_FLOW_STEP))
2226 goto preroll_failed;
2234 GST_DEBUG_OBJECT (sink, "preroll failed: %s", gst_flow_get_name (ret));
2240 * gst_base_sink_wait_eos:
2242 * @time: the running_time to be reached
2243 * @jitter: (out) (allow-none): the jitter to be filled with time diff, or NULL
2245 * This function will block until @time is reached. It is usually called by
2246 * subclasses that use their own internal synchronisation but want to let the
2247 * EOS be handled by the base class.
2249 * This function should only be called with the PREROLL_LOCK held, like when
2250 * receiving an EOS event in the ::event vmethod.
2252 * The @time argument should be the running_time of when the EOS should happen
2253 * and will be adjusted with any latency and offset configured in the sink.
2255 * Returns: #GstFlowReturn
2260 gst_base_sink_wait_eos (GstBaseSink * sink, GstClockTime time,
2261 GstClockTimeDiff * jitter)
2263 GstClockReturn status;
2269 GST_DEBUG_OBJECT (sink, "checking preroll");
2271 /* first wait for the playing state before we can continue */
2272 while (G_UNLIKELY (sink->need_preroll)) {
2273 ret = gst_base_sink_wait_preroll (sink);
2274 if ((ret != GST_FLOW_OK) && (ret != GST_FLOW_STEP))
2278 /* preroll done, we can sync since we are in PLAYING now. */
2279 GST_DEBUG_OBJECT (sink, "possibly waiting for clock to reach %"
2280 GST_TIME_FORMAT, GST_TIME_ARGS (time));
2282 /* compensate for latency and ts_offset. We don't adjust for render delay
2283 * because we don't interact with the device on EOS normally. */
2284 stime = gst_base_sink_adjust_time (sink, time);
2286 /* wait for the clock, this can be interrupted because we got shut down or
2288 status = gst_base_sink_wait_clock (sink, stime, jitter);
2290 GST_DEBUG_OBJECT (sink, "clock returned %d", status);
2292 /* invalid time, no clock or sync disabled, just continue then */
2293 if (status == GST_CLOCK_BADTIME)
2296 /* waiting could have been interrupted and we can be flushing now */
2297 if (G_UNLIKELY (sink->flushing))
2300 /* retry if we got unscheduled, which means we did not reach the timeout
2301 * yet. if some other error occures, we continue. */
2302 } while (status == GST_CLOCK_UNSCHEDULED);
2304 GST_DEBUG_OBJECT (sink, "end of stream");
2311 GST_DEBUG_OBJECT (sink, "we are flushing");
2312 return GST_FLOW_WRONG_STATE;
2316 /* with STREAM_LOCK, PREROLL_LOCK
2318 * Make sure we are in PLAYING and synchronize an object to the clock.
2320 * If we need preroll, we are not in PLAYING. We try to commit the state
2321 * if needed and then block if we still are not PLAYING.
2323 * We start waiting on the clock in PLAYING. If we got interrupted, we
2324 * immediatly try to re-preroll.
2326 * Some objects do not need synchronisation (most events) and so this function
2327 * immediatly returns GST_FLOW_OK.
2329 * for objects that arrive later than max-lateness to be synchronized to the
2330 * clock have the @late boolean set to TRUE.
2332 * This function keeps a running average of the jitter (the diff between the
2333 * clock time and the requested sync time). The jitter is negative for
2334 * objects that arrive in time and positive for late buffers.
2336 * does not take ownership of obj.
2338 static GstFlowReturn
2339 gst_base_sink_do_sync (GstBaseSink * basesink, GstPad * pad,
2340 GstMiniObject * obj, gboolean * late, gboolean * step_end, guint8 obj_type)
2342 GstClockTimeDiff jitter = 0;
2344 GstClockReturn status = GST_CLOCK_OK;
2345 GstClockTime rstart, rstop, sstart, sstop, stime;
2347 GstBaseSinkPrivate *priv;
2349 GstStepInfo *current, *pending;
2352 priv = basesink->priv;
2355 sstart = sstop = rstart = rstop = GST_CLOCK_TIME_NONE;
2359 priv->current_rstart = GST_CLOCK_TIME_NONE;
2361 /* get stepping info */
2362 current = &priv->current_step;
2363 pending = &priv->pending_step;
2365 /* get timing information for this object against the render segment */
2366 syncable = gst_base_sink_get_sync_times (basesink, obj,
2367 &sstart, &sstop, &rstart, &rstop, &do_sync, &stepped, &basesink->segment,
2368 current, step_end, obj_type);
2370 if (G_UNLIKELY (stepped))
2373 /* a syncable object needs to participate in preroll and
2374 * clocking. All buffers and EOS are syncable. */
2375 if (G_UNLIKELY (!syncable))
2378 /* store timing info for current object */
2379 priv->current_rstart = rstart;
2380 priv->current_rstop = (GST_CLOCK_TIME_IS_VALID (rstop) ? rstop : rstart);
2382 /* save sync time for eos when the previous object needed sync */
2383 priv->eos_rtime = (do_sync ? priv->current_rstop : GST_CLOCK_TIME_NONE);
2385 /* calculate inter frame spacing */
2386 if (G_UNLIKELY (priv->prev_rstart != -1 && priv->prev_rstart < rstart)) {
2387 GstClockTime in_diff;
2389 in_diff = rstart - priv->prev_rstart;
2391 if (priv->avg_in_diff == -1)
2392 priv->avg_in_diff = in_diff;
2394 priv->avg_in_diff = UPDATE_RUNNING_AVG (priv->avg_in_diff, in_diff);
2396 GST_LOG_OBJECT (basesink, "avg frame diff %" GST_TIME_FORMAT,
2397 GST_TIME_ARGS (priv->avg_in_diff));
2400 priv->prev_rstart = rstart;
2402 if (G_UNLIKELY (priv->earliest_in_time != -1
2403 && rstart < priv->earliest_in_time))
2407 /* first do preroll, this makes sure we commit our state
2408 * to PAUSED and can continue to PLAYING. We cannot perform
2409 * any clock sync in PAUSED because there is no clock. */
2410 ret = gst_base_sink_do_preroll (basesink, obj);
2411 if (G_UNLIKELY (ret != GST_FLOW_OK))
2412 goto preroll_failed;
2414 /* update the segment with a pending step if the current one is invalid and we
2415 * have a new pending one. We only accept new step updates after a preroll */
2416 if (G_UNLIKELY (pending->valid && !current->valid)) {
2417 start_stepping (basesink, &basesink->segment, pending, current);
2421 /* After rendering we store the position of the last buffer so that we can use
2422 * it to report the position. We need to take the lock here. */
2423 GST_OBJECT_LOCK (basesink);
2424 priv->current_sstart = sstart;
2425 priv->current_sstop = (GST_CLOCK_TIME_IS_VALID (sstop) ? sstop : sstart);
2426 GST_OBJECT_UNLOCK (basesink);
2431 /* adjust for latency */
2432 stime = gst_base_sink_adjust_time (basesink, rstart);
2434 /* adjust for render-delay, avoid underflows */
2435 if (GST_CLOCK_TIME_IS_VALID (stime)) {
2436 if (stime > priv->render_delay)
2437 stime -= priv->render_delay;
2442 /* preroll done, we can sync since we are in PLAYING now. */
2443 GST_DEBUG_OBJECT (basesink, "possibly waiting for clock to reach %"
2444 GST_TIME_FORMAT ", adjusted %" GST_TIME_FORMAT,
2445 GST_TIME_ARGS (rstart), GST_TIME_ARGS (stime));
2447 /* This function will return immediatly if start == -1, no clock
2448 * or sync is disabled with GST_CLOCK_BADTIME. */
2449 status = gst_base_sink_wait_clock (basesink, stime, &jitter);
2451 GST_DEBUG_OBJECT (basesink, "clock returned %d, jitter %c%" GST_TIME_FORMAT,
2452 status, (jitter < 0 ? '-' : ' '), GST_TIME_ARGS (ABS (jitter)));
2454 /* invalid time, no clock or sync disabled, just render */
2455 if (status == GST_CLOCK_BADTIME)
2458 /* waiting could have been interrupted and we can be flushing now */
2459 if (G_UNLIKELY (basesink->flushing))
2462 /* check for unlocked by a state change, we are not flushing so
2463 * we can try to preroll on the current buffer. */
2464 if (G_UNLIKELY (status == GST_CLOCK_UNSCHEDULED)) {
2465 GST_DEBUG_OBJECT (basesink, "unscheduled, waiting some more");
2466 priv->call_preroll = TRUE;
2470 /* successful syncing done, record observation */
2471 priv->current_jitter = jitter;
2473 /* check if the object should be dropped */
2474 *late = gst_base_sink_is_too_late (basesink, obj, rstart, rstop,
2483 GST_DEBUG_OBJECT (basesink, "skipped stepped object %p", obj);
2489 GST_DEBUG_OBJECT (basesink, "non syncable object %p", obj);
2494 GST_DEBUG_OBJECT (basesink, "dropped because of QoS %p", obj);
2500 GST_DEBUG_OBJECT (basesink, "we are flushing");
2501 return GST_FLOW_WRONG_STATE;
2505 GST_DEBUG_OBJECT (basesink, "preroll failed");
2512 gst_base_sink_send_qos (GstBaseSink * basesink, GstQOSType type,
2513 gdouble proportion, GstClockTime time, GstClockTimeDiff diff)
2518 /* generate Quality-of-Service event */
2519 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, basesink,
2520 "qos: type %d, proportion: %lf, diff %" G_GINT64_FORMAT ", timestamp %"
2521 GST_TIME_FORMAT, type, proportion, diff, GST_TIME_ARGS (time));
2523 event = gst_event_new_qos (type, proportion, diff, time);
2526 res = gst_pad_push_event (basesink->sinkpad, event);
2532 gst_base_sink_perform_qos (GstBaseSink * sink, gboolean dropped)
2534 GstBaseSinkPrivate *priv;
2535 GstClockTime start, stop;
2536 GstClockTimeDiff jitter;
2537 GstClockTime pt, entered, left;
2538 GstClockTime duration;
2543 start = priv->current_rstart;
2545 if (priv->current_step.valid)
2548 /* if Quality-of-Service disabled, do nothing */
2549 if (!g_atomic_int_get (&priv->qos_enabled) ||
2550 !GST_CLOCK_TIME_IS_VALID (start))
2553 stop = priv->current_rstop;
2554 jitter = priv->current_jitter;
2557 /* this is the time the buffer entered the sink */
2558 if (start < -jitter)
2561 entered = start + jitter;
2564 /* this is the time the buffer entered the sink */
2565 entered = start + jitter;
2566 /* this is the time the buffer left the sink */
2567 left = start + jitter;
2570 /* calculate duration of the buffer */
2571 if (GST_CLOCK_TIME_IS_VALID (stop) && stop != start)
2572 duration = stop - start;
2574 duration = priv->avg_in_diff;
2576 /* if we have the time when the last buffer left us, calculate
2577 * processing time */
2578 if (GST_CLOCK_TIME_IS_VALID (priv->last_left)) {
2579 if (entered > priv->last_left) {
2580 pt = entered - priv->last_left;
2588 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, sink, "start: %" GST_TIME_FORMAT
2589 ", stop %" GST_TIME_FORMAT ", entered %" GST_TIME_FORMAT ", left %"
2590 GST_TIME_FORMAT ", pt: %" GST_TIME_FORMAT ", duration %" GST_TIME_FORMAT
2591 ",jitter %" G_GINT64_FORMAT, GST_TIME_ARGS (start), GST_TIME_ARGS (stop),
2592 GST_TIME_ARGS (entered), GST_TIME_ARGS (left), GST_TIME_ARGS (pt),
2593 GST_TIME_ARGS (duration), jitter);
2595 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, sink, "avg_duration: %" GST_TIME_FORMAT
2596 ", avg_pt: %" GST_TIME_FORMAT ", avg_rate: %g",
2597 GST_TIME_ARGS (priv->avg_duration), GST_TIME_ARGS (priv->avg_pt),
2600 /* collect running averages. for first observations, we copy the
2602 if (!GST_CLOCK_TIME_IS_VALID (priv->avg_duration))
2603 priv->avg_duration = duration;
2605 priv->avg_duration = UPDATE_RUNNING_AVG (priv->avg_duration, duration);
2607 if (!GST_CLOCK_TIME_IS_VALID (priv->avg_pt))
2610 priv->avg_pt = UPDATE_RUNNING_AVG (priv->avg_pt, pt);
2612 if (priv->avg_duration != 0)
2614 gst_guint64_to_gdouble (priv->avg_pt) /
2615 gst_guint64_to_gdouble (priv->avg_duration);
2619 if (GST_CLOCK_TIME_IS_VALID (priv->last_left)) {
2620 if (dropped || priv->avg_rate < 0.0) {
2621 priv->avg_rate = rate;
2624 priv->avg_rate = UPDATE_RUNNING_AVG_N (priv->avg_rate, rate);
2626 priv->avg_rate = UPDATE_RUNNING_AVG_P (priv->avg_rate, rate);
2630 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, sink,
2631 "updated: avg_duration: %" GST_TIME_FORMAT ", avg_pt: %" GST_TIME_FORMAT
2632 ", avg_rate: %g", GST_TIME_ARGS (priv->avg_duration),
2633 GST_TIME_ARGS (priv->avg_pt), priv->avg_rate);
2636 if (priv->avg_rate >= 0.0) {
2638 GstClockTimeDiff diff;
2640 /* if we have a valid rate, start sending QoS messages */
2641 if (priv->current_jitter < 0) {
2642 /* make sure we never go below 0 when adding the jitter to the
2644 if (priv->current_rstart < -priv->current_jitter)
2645 priv->current_jitter = -priv->current_rstart;
2648 if (priv->throttle_time > 0) {
2649 diff = priv->throttle_time;
2650 type = GST_QOS_TYPE_THROTTLE;
2652 diff = priv->current_jitter;
2654 type = GST_QOS_TYPE_OVERFLOW;
2656 type = GST_QOS_TYPE_UNDERFLOW;
2659 gst_base_sink_send_qos (sink, type, priv->avg_rate, priv->current_rstart,
2663 /* record when this buffer will leave us */
2664 priv->last_left = left;
2667 /* reset all qos measuring */
2669 gst_base_sink_reset_qos (GstBaseSink * sink)
2671 GstBaseSinkPrivate *priv;
2675 priv->last_render_time = GST_CLOCK_TIME_NONE;
2676 priv->prev_rstart = GST_CLOCK_TIME_NONE;
2677 priv->earliest_in_time = GST_CLOCK_TIME_NONE;
2678 priv->last_left = GST_CLOCK_TIME_NONE;
2679 priv->avg_duration = GST_CLOCK_TIME_NONE;
2680 priv->avg_pt = GST_CLOCK_TIME_NONE;
2681 priv->avg_rate = -1.0;
2682 priv->avg_render = GST_CLOCK_TIME_NONE;
2683 priv->avg_in_diff = GST_CLOCK_TIME_NONE;
2689 /* Checks if the object was scheduled too late.
2691 * rstart/rstop contain the running_time start and stop values
2694 * status and jitter contain the return values from the clock wait.
2696 * returns TRUE if the buffer was too late.
2699 gst_base_sink_is_too_late (GstBaseSink * basesink, GstMiniObject * obj,
2700 GstClockTime rstart, GstClockTime rstop,
2701 GstClockReturn status, GstClockTimeDiff jitter)
2704 guint64 max_lateness;
2705 GstBaseSinkPrivate *priv;
2707 priv = basesink->priv;
2711 /* only for objects that were too late */
2712 if (G_LIKELY (status != GST_CLOCK_EARLY))
2715 max_lateness = basesink->max_lateness;
2717 /* check if frame dropping is enabled */
2718 if (max_lateness == -1)
2721 /* only check for buffers */
2722 if (G_UNLIKELY (!GST_IS_BUFFER (obj)))
2725 /* can't do check if we don't have a timestamp */
2726 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (rstart)))
2729 /* we can add a valid stop time */
2730 if (GST_CLOCK_TIME_IS_VALID (rstop))
2731 max_lateness += rstop;
2733 max_lateness += rstart;
2734 /* no stop time, use avg frame diff */
2735 if (priv->avg_in_diff != -1)
2736 max_lateness += priv->avg_in_diff;
2739 /* if the jitter bigger than duration and lateness we are too late */
2740 if ((late = rstart + jitter > max_lateness)) {
2741 GST_CAT_DEBUG_OBJECT (GST_CAT_PERFORMANCE, basesink,
2742 "buffer is too late %" GST_TIME_FORMAT
2743 " > %" GST_TIME_FORMAT, GST_TIME_ARGS (rstart + jitter),
2744 GST_TIME_ARGS (max_lateness));
2745 /* !!emergency!!, if we did not receive anything valid for more than a
2746 * second, render it anyway so the user sees something */
2747 if (GST_CLOCK_TIME_IS_VALID (priv->last_render_time) &&
2748 rstart - priv->last_render_time > GST_SECOND) {
2750 GST_ELEMENT_WARNING (basesink, CORE, CLOCK,
2751 (_("A lot of buffers are being dropped.")),
2752 ("There may be a timestamping problem, or this computer is too slow."));
2753 GST_CAT_DEBUG_OBJECT (GST_CAT_PERFORMANCE, basesink,
2754 "**emergency** last buffer at %" GST_TIME_FORMAT " > GST_SECOND",
2755 GST_TIME_ARGS (priv->last_render_time));
2760 if (!late || !GST_CLOCK_TIME_IS_VALID (priv->last_render_time)) {
2761 priv->last_render_time = rstart;
2762 /* the next allowed input timestamp */
2763 if (priv->throttle_time > 0)
2764 priv->earliest_in_time = rstart + priv->throttle_time;
2771 GST_DEBUG_OBJECT (basesink, "object was scheduled in time");
2776 GST_DEBUG_OBJECT (basesink, "frame dropping disabled");
2781 GST_DEBUG_OBJECT (basesink, "object is not a buffer");
2786 GST_DEBUG_OBJECT (basesink, "buffer has no timestamp");
2791 /* called before and after calling the render vmethod. It keeps track of how
2792 * much time was spent in the render method and is used to check if we are
2795 gst_base_sink_do_render_stats (GstBaseSink * basesink, gboolean start)
2797 GstBaseSinkPrivate *priv;
2799 priv = basesink->priv;
2802 priv->start = gst_util_get_timestamp ();
2804 GstClockTime elapsed;
2806 priv->stop = gst_util_get_timestamp ();
2808 elapsed = GST_CLOCK_DIFF (priv->start, priv->stop);
2810 if (!GST_CLOCK_TIME_IS_VALID (priv->avg_render))
2811 priv->avg_render = elapsed;
2813 priv->avg_render = UPDATE_RUNNING_AVG (priv->avg_render, elapsed);
2815 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, basesink,
2816 "avg_render: %" GST_TIME_FORMAT, GST_TIME_ARGS (priv->avg_render));
2820 /* with STREAM_LOCK, PREROLL_LOCK,
2822 * Synchronize the object on the clock and then render it.
2824 * takes ownership of obj.
2826 static GstFlowReturn
2827 gst_base_sink_render_object (GstBaseSink * basesink, GstPad * pad,
2828 guint8 obj_type, gpointer obj)
2831 GstBaseSinkClass *bclass;
2832 gboolean late, step_end;
2834 GstBaseSinkPrivate *priv;
2836 priv = basesink->priv;
2838 if (OBJ_IS_BUFFERLIST (obj_type)) {
2840 * If buffer list, use the first group buffer within the list
2843 sync_obj = gst_buffer_list_get (GST_BUFFER_LIST_CAST (obj), 0);
2844 g_assert (NULL != sync_obj);
2853 /* synchronize this object, non syncable objects return OK
2856 gst_base_sink_do_sync (basesink, pad, sync_obj, &late, &step_end,
2858 if (G_UNLIKELY (ret != GST_FLOW_OK))
2861 /* and now render, event or buffer/buffer list. */
2862 if (G_LIKELY (OBJ_IS_BUFFERFULL (obj_type))) {
2863 /* drop late buffers unconditionally, let's hope it's unlikely */
2864 if (G_UNLIKELY (late))
2867 bclass = GST_BASE_SINK_GET_CLASS (basesink);
2869 if (G_LIKELY ((OBJ_IS_BUFFERLIST (obj_type) && bclass->render_list) ||
2870 (!OBJ_IS_BUFFERLIST (obj_type) && bclass->render))) {
2873 /* read once, to get same value before and after */
2874 do_qos = g_atomic_int_get (&priv->qos_enabled);
2876 GST_DEBUG_OBJECT (basesink, "rendering object %p", obj);
2878 /* record rendering time for QoS and stats */
2880 gst_base_sink_do_render_stats (basesink, TRUE);
2882 if (!OBJ_IS_BUFFERLIST (obj_type)) {
2885 /* For buffer lists do not set last buffer. Creating buffer
2886 * with meaningful data can be done only with memcpy which will
2887 * significantly affect performance */
2888 buf = GST_BUFFER_CAST (obj);
2889 gst_base_sink_set_last_buffer (basesink, buf);
2891 ret = bclass->render (basesink, buf);
2893 GstBufferList *buflist;
2895 buflist = GST_BUFFER_LIST_CAST (obj);
2897 ret = bclass->render_list (basesink, buflist);
2901 gst_base_sink_do_render_stats (basesink, FALSE);
2903 if (ret == GST_FLOW_STEP)
2906 if (G_UNLIKELY (basesink->flushing))
2911 } else if (G_LIKELY (OBJ_IS_EVENT (obj_type))) {
2912 GstEvent *event = GST_EVENT_CAST (obj);
2913 gboolean event_res = TRUE;
2916 bclass = GST_BASE_SINK_GET_CLASS (basesink);
2918 type = GST_EVENT_TYPE (event);
2920 GST_DEBUG_OBJECT (basesink, "rendering event %p, type %s", obj,
2921 gst_event_type_get_name (type));
2924 event_res = bclass->event (basesink, event);
2926 /* when we get here we could be flushing again when the event handler calls
2927 * _wait_eos(). We have to ignore this object in that case. */
2928 if (G_UNLIKELY (basesink->flushing))
2931 if (G_LIKELY (event_res)) {
2934 seqnum = basesink->priv->seqnum = gst_event_get_seqnum (event);
2935 GST_DEBUG_OBJECT (basesink, "Got seqnum #%" G_GUINT32_FORMAT, seqnum);
2940 GstMessage *message;
2942 /* the EOS event is completely handled so we mark
2943 * ourselves as being in the EOS state. eos is also
2944 * protected by the object lock so we can read it when
2945 * answering the POSITION query. */
2946 GST_OBJECT_LOCK (basesink);
2947 basesink->eos = TRUE;
2948 GST_OBJECT_UNLOCK (basesink);
2950 /* ok, now we can post the message */
2951 GST_DEBUG_OBJECT (basesink, "Now posting EOS");
2953 message = gst_message_new_eos (GST_OBJECT_CAST (basesink));
2954 gst_message_set_seqnum (message, seqnum);
2955 gst_element_post_message (GST_ELEMENT_CAST (basesink), message);
2958 case GST_EVENT_SEGMENT:
2959 /* configure the segment */
2960 gst_base_sink_configure_segment (basesink, pad, event,
2961 &basesink->segment);
2963 case GST_EVENT_SINK_MESSAGE:{
2964 GstMessage *msg = NULL;
2966 gst_event_parse_sink_message (event, &msg);
2969 gst_element_post_message (GST_ELEMENT_CAST (basesink), msg);
2976 g_return_val_if_reached (GST_FLOW_ERROR);
2981 /* the step ended, check if we need to activate a new step */
2982 GST_DEBUG_OBJECT (basesink, "step ended");
2983 stop_stepping (basesink, &basesink->segment, &priv->current_step,
2984 priv->current_rstart, priv->current_rstop, basesink->eos);
2988 gst_base_sink_perform_qos (basesink, late);
2990 GST_DEBUG_OBJECT (basesink, "object unref after render %p", obj);
2991 gst_mini_object_unref (GST_MINI_OBJECT_CAST (obj));
2997 GST_DEBUG_OBJECT (basesink, "do_sync returned %s", gst_flow_get_name (ret));
3003 GST_DEBUG_OBJECT (basesink, "buffer late, dropping");
3005 if (g_atomic_int_get (&priv->qos_enabled)) {
3006 GstMessage *qos_msg;
3007 GstClockTime timestamp, duration;
3009 timestamp = GST_BUFFER_TIMESTAMP (GST_BUFFER_CAST (sync_obj));
3010 duration = GST_BUFFER_DURATION (GST_BUFFER_CAST (sync_obj));
3012 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, basesink,
3013 "qos: dropped buffer rt %" GST_TIME_FORMAT ", st %" GST_TIME_FORMAT
3014 ", ts %" GST_TIME_FORMAT ", dur %" GST_TIME_FORMAT,
3015 GST_TIME_ARGS (priv->current_rstart),
3016 GST_TIME_ARGS (priv->current_sstart), GST_TIME_ARGS (timestamp),
3017 GST_TIME_ARGS (duration));
3018 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, basesink,
3019 "qos: rendered %" G_GUINT64_FORMAT ", dropped %" G_GUINT64_FORMAT,
3020 priv->rendered, priv->dropped);
3023 gst_message_new_qos (GST_OBJECT_CAST (basesink), basesink->sync,
3024 priv->current_rstart, priv->current_sstart, timestamp, duration);
3025 gst_message_set_qos_values (qos_msg, priv->current_jitter, priv->avg_rate,
3027 gst_message_set_qos_stats (qos_msg, GST_FORMAT_BUFFERS, priv->rendered,
3029 gst_element_post_message (GST_ELEMENT_CAST (basesink), qos_msg);
3035 GST_DEBUG_OBJECT (basesink, "we are flushing, ignore object");
3036 gst_mini_object_unref (obj);
3037 return GST_FLOW_WRONG_STATE;
3041 /* with STREAM_LOCK, PREROLL_LOCK
3043 * Perform preroll on the given object. For buffers this means
3044 * calling the preroll subclass method.
3045 * If that succeeds, the state will be commited.
3047 * function does not take ownership of obj.
3049 static GstFlowReturn
3050 gst_base_sink_preroll_object (GstBaseSink * basesink, guint8 obj_type,
3051 GstMiniObject * obj)
3055 GST_DEBUG_OBJECT (basesink, "prerolling object %p", obj);
3057 /* if it's a buffer, we need to call the preroll method */
3058 if (G_LIKELY (OBJ_IS_BUFFERFULL (obj_type) && basesink->priv->call_preroll)) {
3059 GstBaseSinkClass *bclass;
3061 GstClockTime timestamp;
3063 if (OBJ_IS_BUFFERLIST (obj_type)) {
3064 buf = gst_buffer_list_get (GST_BUFFER_LIST_CAST (obj), 0);
3065 g_assert (NULL != buf);
3067 buf = GST_BUFFER_CAST (obj);
3070 timestamp = GST_BUFFER_TIMESTAMP (buf);
3072 GST_DEBUG_OBJECT (basesink, "preroll buffer %" GST_TIME_FORMAT,
3073 GST_TIME_ARGS (timestamp));
3076 * For buffer lists do not set last buffer. Creating buffer
3077 * with meaningful data can be done only with memcpy which will
3078 * significantly affect performance
3080 if (!OBJ_IS_BUFFERLIST (obj_type)) {
3081 gst_base_sink_set_last_buffer (basesink, buf);
3084 bclass = GST_BASE_SINK_GET_CLASS (basesink);
3085 if (bclass->preroll)
3086 if ((ret = bclass->preroll (basesink, buf)) != GST_FLOW_OK)
3087 goto preroll_failed;
3089 basesink->priv->call_preroll = FALSE;
3093 if (G_LIKELY (basesink->playing_async)) {
3094 if (G_UNLIKELY (!gst_base_sink_commit_state (basesink)))
3103 GST_DEBUG_OBJECT (basesink, "preroll failed, abort state");
3104 gst_element_abort_state (GST_ELEMENT_CAST (basesink));
3109 GST_DEBUG_OBJECT (basesink, "stopping while commiting state");
3110 return GST_FLOW_WRONG_STATE;
3114 /* with STREAM_LOCK, PREROLL_LOCK
3116 * Queue an object for rendering.
3117 * The first prerollable object queued will complete the preroll. If the
3118 * preroll queue if filled, we render all the objects in the queue.
3120 * This function takes ownership of the object.
3122 static GstFlowReturn
3123 gst_base_sink_queue_object_unlocked (GstBaseSink * basesink, GstPad * pad,
3124 guint8 obj_type, gpointer obj, gboolean prerollable)
3126 GstFlowReturn ret = GST_FLOW_OK;
3130 if (G_UNLIKELY (basesink->need_preroll)) {
3131 if (G_LIKELY (prerollable))
3132 basesink->preroll_queued++;
3134 length = basesink->preroll_queued;
3136 GST_DEBUG_OBJECT (basesink, "now %d prerolled items", length);
3138 /* first prerollable item needs to finish the preroll */
3140 ret = gst_base_sink_preroll_object (basesink, obj_type, obj);
3141 if (G_UNLIKELY (ret != GST_FLOW_OK))
3142 goto preroll_failed;
3144 /* need to recheck if we need preroll, commmit state during preroll
3145 * could have made us not need more preroll. */
3146 if (G_UNLIKELY (basesink->need_preroll)) {
3147 /* see if we can render now, if we can't add the object to the preroll
3149 if (G_UNLIKELY (length <= basesink->preroll_queue_max_len))
3153 /* we can start rendering (or blocking) the queued object
3155 q = basesink->preroll_queue;
3156 while (G_UNLIKELY (!g_queue_is_empty (q))) {
3160 o = g_queue_pop_head (q);
3161 GST_DEBUG_OBJECT (basesink, "rendering queued object %p", o);
3163 ot = get_object_type (o);
3165 /* do something with the return value */
3166 ret = gst_base_sink_render_object (basesink, pad, ot, o);
3167 if (ret != GST_FLOW_OK)
3168 goto dequeue_failed;
3171 /* now render the object */
3172 ret = gst_base_sink_render_object (basesink, pad, obj_type, obj);
3173 basesink->preroll_queued = 0;
3180 GST_DEBUG_OBJECT (basesink, "preroll failed, reason %s",
3181 gst_flow_get_name (ret));
3182 gst_mini_object_unref (GST_MINI_OBJECT_CAST (obj));
3187 /* add object to the queue and return */
3188 GST_DEBUG_OBJECT (basesink, "need more preroll data %d <= %d",
3189 length, basesink->preroll_queue_max_len);
3190 g_queue_push_tail (basesink->preroll_queue, obj);
3195 GST_DEBUG_OBJECT (basesink, "rendering queued objects failed, reason %s",
3196 gst_flow_get_name (ret));
3197 gst_mini_object_unref (GST_MINI_OBJECT_CAST (obj));
3204 * This function grabs the PREROLL_LOCK and adds the object to
3207 * This function takes ownership of obj.
3209 * Note: Only GstEvent seem to be passed to this private method
3211 static GstFlowReturn
3212 gst_base_sink_queue_object (GstBaseSink * basesink, GstPad * pad,
3213 GstMiniObject * obj, gboolean prerollable)
3217 GST_BASE_SINK_PREROLL_LOCK (basesink);
3218 if (G_UNLIKELY (basesink->flushing))
3221 if (G_UNLIKELY (basesink->priv->received_eos))
3225 gst_base_sink_queue_object_unlocked (basesink, pad, _PR_IS_EVENT, obj,
3227 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
3234 GST_DEBUG_OBJECT (basesink, "sink is flushing");
3235 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
3236 gst_mini_object_unref (obj);
3237 return GST_FLOW_WRONG_STATE;
3241 GST_DEBUG_OBJECT (basesink,
3242 "we are EOS, dropping object, return UNEXPECTED");
3243 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
3244 gst_mini_object_unref (obj);
3245 return GST_FLOW_UNEXPECTED;
3250 gst_base_sink_flush_start (GstBaseSink * basesink, GstPad * pad)
3252 /* make sure we are not blocked on the clock also clear any pending
3254 gst_base_sink_set_flushing (basesink, pad, TRUE);
3256 /* we grab the stream lock but that is not needed since setting the
3257 * sink to flushing would make sure no state commit is being done
3259 GST_PAD_STREAM_LOCK (pad);
3260 gst_base_sink_reset_qos (basesink);
3261 /* and we need to commit our state again on the next
3262 * prerolled buffer */
3263 basesink->playing_async = TRUE;
3264 if (basesink->priv->async_enabled) {
3265 basesink->priv->reset_time = TRUE;
3266 gst_element_lost_state (GST_ELEMENT_CAST (basesink));
3268 basesink->priv->have_latency = TRUE;
3270 gst_base_sink_set_last_buffer (basesink, NULL);
3271 GST_PAD_STREAM_UNLOCK (pad);
3275 gst_base_sink_flush_stop (GstBaseSink * basesink, GstPad * pad)
3277 /* unset flushing so we can accept new data, this also flushes out any EOS
3279 gst_base_sink_set_flushing (basesink, pad, FALSE);
3281 /* for position reporting */
3282 GST_OBJECT_LOCK (basesink);
3283 basesink->priv->current_sstart = GST_CLOCK_TIME_NONE;
3284 basesink->priv->current_sstop = GST_CLOCK_TIME_NONE;
3285 basesink->priv->eos_rtime = GST_CLOCK_TIME_NONE;
3286 basesink->priv->call_preroll = TRUE;
3287 basesink->priv->current_step.valid = FALSE;
3288 basesink->priv->pending_step.valid = FALSE;
3289 if (basesink->pad_mode == GST_ACTIVATE_PUSH) {
3290 /* we need new segment info after the flush. */
3291 basesink->have_newsegment = FALSE;
3292 gst_segment_init (&basesink->segment, GST_FORMAT_UNDEFINED);
3293 gst_segment_init (basesink->clip_segment, GST_FORMAT_UNDEFINED);
3295 GST_OBJECT_UNLOCK (basesink);
3299 gst_base_sink_event (GstPad * pad, GstEvent * event)
3301 GstBaseSink *basesink;
3302 gboolean result = TRUE;
3303 GstBaseSinkClass *bclass;
3305 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
3306 if (G_UNLIKELY (basesink == NULL)) {
3307 gst_event_unref (event);
3311 bclass = GST_BASE_SINK_GET_CLASS (basesink);
3313 GST_DEBUG_OBJECT (basesink, "received event %p %" GST_PTR_FORMAT, event,
3316 switch (GST_EVENT_TYPE (event)) {
3321 GST_BASE_SINK_PREROLL_LOCK (basesink);
3322 if (G_UNLIKELY (basesink->flushing))
3325 if (G_UNLIKELY (basesink->priv->received_eos))
3328 /* we set the received EOS flag here so that we can use it when testing if
3329 * we are prerolled and to refuse more buffers. */
3330 basesink->priv->received_eos = TRUE;
3332 /* EOS is a prerollable object, we call the unlocked version because it
3333 * does not check the received_eos flag. */
3334 ret = gst_base_sink_queue_object_unlocked (basesink, pad,
3335 _PR_IS_EVENT, GST_MINI_OBJECT_CAST (event), TRUE);
3336 if (G_UNLIKELY (ret != GST_FLOW_OK))
3339 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
3342 case GST_EVENT_CAPS:
3346 GST_DEBUG_OBJECT (basesink, "caps %p", event);
3348 gst_event_parse_caps (event, &caps);
3349 if (bclass->set_caps)
3350 result = bclass->set_caps (basesink, caps);
3352 gst_event_unref (event);
3355 case GST_EVENT_SEGMENT:
3359 GST_DEBUG_OBJECT (basesink, "segment %p", event);
3361 GST_BASE_SINK_PREROLL_LOCK (basesink);
3362 if (G_UNLIKELY (basesink->flushing))
3365 if (G_UNLIKELY (basesink->priv->received_eos))
3368 /* the new segment is a non prerollable item and does not block anything,
3369 * we need to configure the current clipping segment and insert the event
3370 * in the queue to serialize it with the buffers for rendering. */
3371 gst_base_sink_configure_segment (basesink, pad, event,
3372 basesink->clip_segment);
3375 gst_base_sink_queue_object_unlocked (basesink, pad,
3376 _PR_IS_EVENT, GST_MINI_OBJECT_CAST (event), FALSE);
3377 if (G_UNLIKELY (ret != GST_FLOW_OK))
3380 GST_OBJECT_LOCK (basesink);
3381 basesink->have_newsegment = TRUE;
3382 GST_OBJECT_UNLOCK (basesink);
3384 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
3387 case GST_EVENT_FLUSH_START:
3389 bclass->event (basesink, event);
3391 GST_DEBUG_OBJECT (basesink, "flush-start %p", event);
3393 gst_base_sink_flush_start (basesink, pad);
3395 gst_event_unref (event);
3397 case GST_EVENT_FLUSH_STOP:
3399 bclass->event (basesink, event);
3401 GST_DEBUG_OBJECT (basesink, "flush-stop %p", event);
3403 gst_base_sink_flush_stop (basesink, pad);
3405 gst_event_unref (event);
3408 /* other events are sent to queue or subclass depending on if they
3409 * are serialized. */
3410 if (GST_EVENT_IS_SERIALIZED (event)) {
3411 gst_base_sink_queue_object (basesink, pad,
3412 GST_MINI_OBJECT_CAST (event), FALSE);
3415 bclass->event (basesink, event);
3416 gst_event_unref (event);
3421 gst_object_unref (basesink);
3428 GST_DEBUG_OBJECT (basesink, "we are flushing");
3429 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
3431 gst_event_unref (event);
3437 GST_DEBUG_OBJECT (basesink, "Event received after EOS, dropping");
3438 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
3440 gst_event_unref (event);
3445 /* default implementation to calculate the start and end
3446 * timestamps on a buffer, subclasses can override
3449 gst_base_sink_get_times (GstBaseSink * basesink, GstBuffer * buffer,
3450 GstClockTime * start, GstClockTime * end)
3452 GstClockTime timestamp, duration;
3454 timestamp = GST_BUFFER_TIMESTAMP (buffer);
3455 if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
3457 /* get duration to calculate end time */
3458 duration = GST_BUFFER_DURATION (buffer);
3459 if (GST_CLOCK_TIME_IS_VALID (duration)) {
3460 *end = timestamp + duration;
3466 /* must be called with PREROLL_LOCK */
3468 gst_base_sink_needs_preroll (GstBaseSink * basesink)
3470 gboolean is_prerolled, res;
3472 /* we have 2 cases where the PREROLL_LOCK is released:
3473 * 1) we are blocking in the PREROLL_LOCK and thus are prerolled.
3474 * 2) we are syncing on the clock
3476 is_prerolled = basesink->have_preroll || basesink->priv->received_eos;
3477 res = !is_prerolled;
3479 GST_DEBUG_OBJECT (basesink, "have_preroll: %d, EOS: %d => needs preroll: %d",
3480 basesink->have_preroll, basesink->priv->received_eos, res);
3485 /* with STREAM_LOCK, PREROLL_LOCK
3487 * Takes a buffer and compare the timestamps with the last segment.
3488 * If the buffer falls outside of the segment boundaries, drop it.
3489 * Else queue the buffer for preroll and rendering.
3491 * This function takes ownership of the buffer.
3493 static GstFlowReturn
3494 gst_base_sink_chain_unlocked (GstBaseSink * basesink, GstPad * pad,
3495 guint8 obj_type, gpointer obj)
3497 GstBaseSinkClass *bclass;
3498 GstFlowReturn result;
3499 GstClockTime start = GST_CLOCK_TIME_NONE, end = GST_CLOCK_TIME_NONE;
3500 GstSegment *clip_segment;
3501 GstBuffer *time_buf;
3503 if (G_UNLIKELY (basesink->flushing))
3506 if (G_UNLIKELY (basesink->priv->received_eos))
3509 if (OBJ_IS_BUFFERLIST (obj_type)) {
3510 time_buf = gst_buffer_list_get (GST_BUFFER_LIST_CAST (obj), 0);
3511 g_assert (NULL != time_buf);
3513 time_buf = GST_BUFFER_CAST (obj);
3516 /* for code clarity */
3517 clip_segment = basesink->clip_segment;
3519 if (G_UNLIKELY (!basesink->have_newsegment)) {
3522 sync = gst_base_sink_get_sync (basesink);
3524 GST_ELEMENT_WARNING (basesink, STREAM, FAILED,
3525 (_("Internal data flow problem.")),
3526 ("Received buffer without a new-segment. Assuming timestamps start from 0."));
3529 /* this means this sink will assume timestamps start from 0 */
3530 GST_OBJECT_LOCK (basesink);
3531 clip_segment->start = 0;
3532 clip_segment->stop = -1;
3533 basesink->segment.start = 0;
3534 basesink->segment.stop = -1;
3535 basesink->have_newsegment = TRUE;
3536 GST_OBJECT_UNLOCK (basesink);
3539 bclass = GST_BASE_SINK_GET_CLASS (basesink);
3541 /* check if the buffer needs to be dropped, we first ask the subclass for the
3543 if (bclass->get_times)
3544 bclass->get_times (basesink, time_buf, &start, &end);
3546 if (!GST_CLOCK_TIME_IS_VALID (start)) {
3547 /* if the subclass does not want sync, we use our own values so that we at
3548 * least clip the buffer to the segment */
3549 gst_base_sink_get_times (basesink, time_buf, &start, &end);
3552 GST_DEBUG_OBJECT (basesink, "got times start: %" GST_TIME_FORMAT
3553 ", end: %" GST_TIME_FORMAT, GST_TIME_ARGS (start), GST_TIME_ARGS (end));
3555 /* a dropped buffer does not participate in anything */
3556 if (GST_CLOCK_TIME_IS_VALID (start) &&
3557 (clip_segment->format == GST_FORMAT_TIME)) {
3558 if (G_UNLIKELY (!gst_segment_clip (clip_segment,
3559 GST_FORMAT_TIME, start, end, NULL, NULL)))
3560 goto out_of_segment;
3563 /* now we can process the buffer in the queue, this function takes ownership
3565 result = gst_base_sink_queue_object_unlocked (basesink, pad,
3566 obj_type, obj, TRUE);
3572 GST_DEBUG_OBJECT (basesink, "sink is flushing");
3573 gst_mini_object_unref (GST_MINI_OBJECT_CAST (obj));
3574 return GST_FLOW_WRONG_STATE;
3578 GST_DEBUG_OBJECT (basesink,
3579 "we are EOS, dropping object, return UNEXPECTED");
3580 gst_mini_object_unref (GST_MINI_OBJECT_CAST (obj));
3581 return GST_FLOW_UNEXPECTED;
3585 GST_DEBUG_OBJECT (basesink, "dropping buffer, out of clipping segment");
3586 gst_mini_object_unref (GST_MINI_OBJECT_CAST (obj));
3593 static GstFlowReturn
3594 gst_base_sink_chain_main (GstBaseSink * basesink, GstPad * pad,
3595 guint8 obj_type, gpointer obj)
3597 GstFlowReturn result;
3599 if (G_UNLIKELY (basesink->pad_mode != GST_ACTIVATE_PUSH))
3602 GST_BASE_SINK_PREROLL_LOCK (basesink);
3603 result = gst_base_sink_chain_unlocked (basesink, pad, obj_type, obj);
3604 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
3612 GST_OBJECT_LOCK (pad);
3613 GST_WARNING_OBJECT (basesink,
3614 "Push on pad %s:%s, but it was not activated in push mode",
3615 GST_DEBUG_PAD_NAME (pad));
3616 GST_OBJECT_UNLOCK (pad);
3617 gst_mini_object_unref (GST_MINI_OBJECT_CAST (obj));
3618 /* we don't post an error message this will signal to the peer
3619 * pushing that EOS is reached. */
3620 result = GST_FLOW_UNEXPECTED;
3625 static GstFlowReturn
3626 gst_base_sink_chain (GstPad * pad, GstBuffer * buf)
3628 GstBaseSink *basesink;
3630 basesink = GST_BASE_SINK (GST_OBJECT_PARENT (pad));
3632 return gst_base_sink_chain_main (basesink, pad, _PR_IS_BUFFER, buf);
3635 static GstFlowReturn
3636 gst_base_sink_chain_list (GstPad * pad, GstBufferList * list)
3638 GstBaseSink *basesink;
3639 GstBaseSinkClass *bclass;
3640 GstFlowReturn result;
3642 basesink = GST_BASE_SINK (GST_OBJECT_PARENT (pad));
3643 bclass = GST_BASE_SINK_GET_CLASS (basesink);
3645 if (G_LIKELY (bclass->render_list)) {
3646 result = gst_base_sink_chain_main (basesink, pad, _PR_IS_BUFFERLIST, list);
3651 GST_INFO_OBJECT (pad, "chaining each group in list as a merged buffer");
3653 len = gst_buffer_list_len (list);
3655 result = GST_FLOW_OK;
3656 for (i = 0; i < len; i++) {
3657 buffer = gst_buffer_list_get (list, 0);
3658 result = gst_base_sink_chain_main (basesink, pad, _PR_IS_BUFFER,
3659 gst_buffer_ref (buffer));
3660 if (result != GST_FLOW_OK)
3663 gst_buffer_list_unref (list);
3670 gst_base_sink_default_do_seek (GstBaseSink * sink, GstSegment * segment)
3672 gboolean res = TRUE;
3674 /* update our offset if the start/stop position was updated */
3675 if (segment->format == GST_FORMAT_BYTES) {
3676 segment->time = segment->start;
3677 } else if (segment->start == 0) {
3678 /* seek to start, we can implement a default for this. */
3682 GST_INFO_OBJECT (sink, "Can't do a default seek");
3688 #define SEEK_TYPE_IS_RELATIVE(t) (((t) != GST_SEEK_TYPE_NONE) && ((t) != GST_SEEK_TYPE_SET))
3691 gst_base_sink_default_prepare_seek_segment (GstBaseSink * sink,
3692 GstEvent * event, GstSegment * segment)
3694 /* By default, we try one of 2 things:
3695 * - For absolute seek positions, convert the requested position to our
3696 * configured processing format and place it in the output segment \
3697 * - For relative seek positions, convert our current (input) values to the
3698 * seek format, adjust by the relative seek offset and then convert back to
3699 * the processing format
3701 GstSeekType cur_type, stop_type;
3704 GstFormat seek_format, dest_format;
3707 gboolean res = TRUE;
3709 gst_event_parse_seek (event, &rate, &seek_format, &flags,
3710 &cur_type, &cur, &stop_type, &stop);
3711 dest_format = segment->format;
3713 if (seek_format == dest_format) {
3714 gst_segment_do_seek (segment, rate, seek_format, flags,
3715 cur_type, cur, stop_type, stop, &update);
3719 if (cur_type != GST_SEEK_TYPE_NONE) {
3720 /* FIXME: Handle seek_cur & seek_end by converting the input segment vals */
3722 gst_pad_query_convert (sink->sinkpad, seek_format, cur, &dest_format,
3724 cur_type = GST_SEEK_TYPE_SET;
3727 if (res && stop_type != GST_SEEK_TYPE_NONE) {
3728 /* FIXME: Handle seek_cur & seek_end by converting the input segment vals */
3730 gst_pad_query_convert (sink->sinkpad, seek_format, stop, &dest_format,
3732 stop_type = GST_SEEK_TYPE_SET;
3735 /* And finally, configure our output segment in the desired format */
3736 gst_segment_do_seek (segment, rate, dest_format, flags, cur_type, cur,
3737 stop_type, stop, &update);
3746 GST_DEBUG_OBJECT (sink, "undefined format given, seek aborted.");
3751 /* perform a seek, only executed in pull mode */
3753 gst_base_sink_perform_seek (GstBaseSink * sink, GstPad * pad, GstEvent * event)
3757 GstFormat seek_format, dest_format;
3759 GstSeekType cur_type, stop_type;
3760 gboolean seekseg_configured = FALSE;
3762 gboolean update, res = TRUE;
3763 GstSegment seeksegment;
3765 dest_format = sink->segment.format;
3768 GST_DEBUG_OBJECT (sink, "performing seek with event %p", event);
3769 gst_event_parse_seek (event, &rate, &seek_format, &flags,
3770 &cur_type, &cur, &stop_type, &stop);
3772 flush = flags & GST_SEEK_FLAG_FLUSH;
3774 GST_DEBUG_OBJECT (sink, "performing seek without event");
3779 GST_DEBUG_OBJECT (sink, "flushing upstream");
3780 gst_pad_push_event (pad, gst_event_new_flush_start ());
3781 gst_base_sink_flush_start (sink, pad);
3783 GST_DEBUG_OBJECT (sink, "pausing pulling thread");
3786 GST_PAD_STREAM_LOCK (pad);
3788 /* If we configured the seeksegment above, don't overwrite it now. Otherwise
3789 * copy the current segment info into the temp segment that we can actually
3790 * attempt the seek with. We only update the real segment if the seek suceeds. */
3791 if (!seekseg_configured) {
3792 memcpy (&seeksegment, &sink->segment, sizeof (GstSegment));
3794 /* now configure the final seek segment */
3796 if (sink->segment.format != seek_format) {
3797 /* OK, here's where we give the subclass a chance to convert the relative
3798 * seek into an absolute one in the processing format. We set up any
3799 * absolute seek above, before taking the stream lock. */
3800 if (!gst_base_sink_default_prepare_seek_segment (sink, event,
3802 GST_DEBUG_OBJECT (sink,
3803 "Preparing the seek failed after flushing. " "Aborting seek");
3807 /* The seek format matches our processing format, no need to ask the
3808 * the subclass to configure the segment. */
3809 gst_segment_do_seek (&seeksegment, rate, seek_format, flags,
3810 cur_type, cur, stop_type, stop, &update);
3813 /* Else, no seek event passed, so we're just (re)starting the
3818 GST_DEBUG_OBJECT (sink, "segment configured from %" G_GINT64_FORMAT
3819 " to %" G_GINT64_FORMAT ", position %" G_GINT64_FORMAT,
3820 seeksegment.start, seeksegment.stop, seeksegment.position);
3822 /* do the seek, segment.position contains the new position. */
3823 res = gst_base_sink_default_do_seek (sink, &seeksegment);
3828 GST_DEBUG_OBJECT (sink, "stop flushing upstream");
3829 gst_pad_push_event (pad, gst_event_new_flush_stop ());
3830 gst_base_sink_flush_stop (sink, pad);
3831 } else if (res && sink->running) {
3832 /* we are running the current segment and doing a non-flushing seek,
3833 * close the segment first based on the position. */
3834 GST_DEBUG_OBJECT (sink, "closing running segment %" G_GINT64_FORMAT
3835 " to %" G_GINT64_FORMAT, sink->segment.start, sink->segment.position);
3838 /* The subclass must have converted the segment to the processing format
3840 if (res && seeksegment.format != dest_format) {
3841 GST_DEBUG_OBJECT (sink, "Subclass failed to prepare a seek segment "
3842 "in the correct format. Aborting seek.");
3846 /* if successfull seek, we update our real segment and push
3847 * out the new segment. */
3849 gst_segment_copy_into (&seeksegment, &sink->segment);
3851 if (sink->segment.flags & GST_SEEK_FLAG_SEGMENT) {
3852 gst_element_post_message (GST_ELEMENT (sink),
3853 gst_message_new_segment_start (GST_OBJECT (sink),
3854 sink->segment.format, sink->segment.position));
3858 sink->priv->discont = TRUE;
3859 sink->running = TRUE;
3861 GST_PAD_STREAM_UNLOCK (pad);
3867 set_step_info (GstBaseSink * sink, GstStepInfo * current, GstStepInfo * pending,
3868 guint seqnum, GstFormat format, guint64 amount, gdouble rate,
3869 gboolean flush, gboolean intermediate)
3871 GST_OBJECT_LOCK (sink);
3872 pending->seqnum = seqnum;
3873 pending->format = format;
3874 pending->amount = amount;
3875 pending->position = 0;
3876 pending->rate = rate;
3877 pending->flush = flush;
3878 pending->intermediate = intermediate;
3879 pending->valid = TRUE;
3880 /* flush invalidates the current stepping segment */
3882 current->valid = FALSE;
3883 GST_OBJECT_UNLOCK (sink);
3887 gst_base_sink_perform_step (GstBaseSink * sink, GstPad * pad, GstEvent * event)
3889 GstBaseSinkPrivate *priv;
3890 GstBaseSinkClass *bclass;
3891 gboolean flush, intermediate;
3896 GstStepInfo *pending, *current;
3897 GstMessage *message;
3899 bclass = GST_BASE_SINK_GET_CLASS (sink);
3902 GST_DEBUG_OBJECT (sink, "performing step with event %p", event);
3904 gst_event_parse_step (event, &format, &amount, &rate, &flush, &intermediate);
3905 seqnum = gst_event_get_seqnum (event);
3907 pending = &priv->pending_step;
3908 current = &priv->current_step;
3910 /* post message first */
3911 message = gst_message_new_step_start (GST_OBJECT (sink), FALSE, format,
3912 amount, rate, flush, intermediate);
3913 gst_message_set_seqnum (message, seqnum);
3914 gst_element_post_message (GST_ELEMENT (sink), message);
3917 /* we need to call ::unlock before locking PREROLL_LOCK
3918 * since we lock it before going into ::render */
3920 bclass->unlock (sink);
3922 GST_BASE_SINK_PREROLL_LOCK (sink);
3923 /* now that we have the PREROLL lock, clear our unlock request */
3924 if (bclass->unlock_stop)
3925 bclass->unlock_stop (sink);
3927 /* update the stepinfo and make it valid */
3928 set_step_info (sink, current, pending, seqnum, format, amount, rate, flush,
3931 if (sink->priv->async_enabled) {
3932 /* and we need to commit our state again on the next
3933 * prerolled buffer */
3934 sink->playing_async = TRUE;
3935 priv->pending_step.need_preroll = TRUE;
3936 sink->need_preroll = FALSE;
3937 gst_element_lost_state (GST_ELEMENT_CAST (sink));
3939 sink->priv->have_latency = TRUE;
3940 sink->need_preroll = FALSE;
3942 priv->current_sstart = GST_CLOCK_TIME_NONE;
3943 priv->current_sstop = GST_CLOCK_TIME_NONE;
3944 priv->eos_rtime = GST_CLOCK_TIME_NONE;
3945 priv->call_preroll = TRUE;
3946 gst_base_sink_set_last_buffer (sink, NULL);
3947 gst_base_sink_reset_qos (sink);
3949 if (sink->clock_id) {
3950 gst_clock_id_unschedule (sink->clock_id);
3953 if (sink->have_preroll) {
3954 GST_DEBUG_OBJECT (sink, "signal waiter");
3955 priv->step_unlock = TRUE;
3956 GST_BASE_SINK_PREROLL_SIGNAL (sink);
3958 GST_BASE_SINK_PREROLL_UNLOCK (sink);
3960 /* update the stepinfo and make it valid */
3961 set_step_info (sink, current, pending, seqnum, format, amount, rate, flush,
3971 gst_base_sink_loop (GstPad * pad)
3973 GstBaseSink *basesink;
3974 GstBuffer *buf = NULL;
3975 GstFlowReturn result;
3979 basesink = GST_BASE_SINK (GST_OBJECT_PARENT (pad));
3981 g_assert (basesink->pad_mode == GST_ACTIVATE_PULL);
3983 if ((blocksize = basesink->priv->blocksize) == 0)
3986 offset = basesink->segment.position;
3988 GST_DEBUG_OBJECT (basesink, "pulling %" G_GUINT64_FORMAT ", %u",
3991 result = gst_pad_pull_range (pad, offset, blocksize, &buf);
3992 if (G_UNLIKELY (result != GST_FLOW_OK))
3995 if (G_UNLIKELY (buf == NULL))
3998 offset += gst_buffer_get_size (buf);
4000 basesink->segment.position = offset;
4002 GST_BASE_SINK_PREROLL_LOCK (basesink);
4003 result = gst_base_sink_chain_unlocked (basesink, pad, _PR_IS_BUFFER, buf);
4004 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
4005 if (G_UNLIKELY (result != GST_FLOW_OK))
4013 GST_LOG_OBJECT (basesink, "pausing task, reason %s",
4014 gst_flow_get_name (result));
4015 gst_pad_pause_task (pad);
4016 if (result == GST_FLOW_UNEXPECTED) {
4017 /* perform EOS logic */
4018 if (basesink->segment.flags & GST_SEEK_FLAG_SEGMENT) {
4019 gst_element_post_message (GST_ELEMENT_CAST (basesink),
4020 gst_message_new_segment_done (GST_OBJECT_CAST (basesink),
4021 basesink->segment.format, basesink->segment.position));
4023 gst_base_sink_event (pad, gst_event_new_eos ());
4025 } else if (result == GST_FLOW_NOT_LINKED || result <= GST_FLOW_UNEXPECTED) {
4026 /* for fatal errors we post an error message, post the error
4027 * first so the app knows about the error first.
4028 * wrong-state is not a fatal error because it happens due to
4029 * flushing and posting an error message in that case is the
4030 * wrong thing to do, e.g. when basesrc is doing a flushing
4032 GST_ELEMENT_ERROR (basesink, STREAM, FAILED,
4033 (_("Internal data stream error.")),
4034 ("stream stopped, reason %s", gst_flow_get_name (result)));
4035 gst_base_sink_event (pad, gst_event_new_eos ());
4041 GST_LOG_OBJECT (basesink, "no buffer, pausing");
4042 GST_ELEMENT_ERROR (basesink, STREAM, FAILED,
4043 (_("Internal data flow error.")), ("element returned NULL buffer"));
4044 result = GST_FLOW_ERROR;
4050 gst_base_sink_set_flushing (GstBaseSink * basesink, GstPad * pad,
4053 GstBaseSinkClass *bclass;
4055 bclass = GST_BASE_SINK_GET_CLASS (basesink);
4058 /* unlock any subclasses, we need to do this before grabbing the
4059 * PREROLL_LOCK since we hold this lock before going into ::render. */
4061 bclass->unlock (basesink);
4064 GST_BASE_SINK_PREROLL_LOCK (basesink);
4065 basesink->flushing = flushing;
4067 /* step 1, now that we have the PREROLL lock, clear our unlock request */
4068 if (bclass->unlock_stop)
4069 bclass->unlock_stop (basesink);
4071 /* set need_preroll before we unblock the clock. If the clock is unblocked
4072 * before timing out, we can reuse the buffer for preroll. */
4073 basesink->need_preroll = TRUE;
4075 /* step 2, unblock clock sync (if any) or any other blocking thing */
4076 if (basesink->clock_id) {
4077 gst_clock_id_unschedule (basesink->clock_id);
4080 /* flush out the data thread if it's locked in finish_preroll, this will
4081 * also flush out the EOS state */
4082 GST_DEBUG_OBJECT (basesink,
4083 "flushing out data thread, need preroll to TRUE");
4084 gst_base_sink_preroll_queue_flush (basesink, pad);
4086 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
4092 gst_base_sink_default_activate_pull (GstBaseSink * basesink, gboolean active)
4098 result = gst_pad_start_task (basesink->sinkpad,
4099 (GstTaskFunction) gst_base_sink_loop, basesink->sinkpad);
4101 /* step 2, make sure streaming finishes */
4102 result = gst_pad_stop_task (basesink->sinkpad);
4109 gst_base_sink_pad_activate (GstPad * pad)
4111 gboolean result = FALSE;
4112 GstBaseSink *basesink;
4116 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
4118 GST_DEBUG_OBJECT (basesink, "Trying pull mode first");
4120 gst_base_sink_set_flushing (basesink, pad, FALSE);
4122 /* we need to have the pull mode enabled */
4123 if (!basesink->can_activate_pull) {
4124 GST_DEBUG_OBJECT (basesink, "pull mode disabled");
4128 /* check if downstreams supports pull mode at all */
4129 query = gst_query_new_scheduling ();
4131 if (!gst_pad_peer_query (pad, query)) {
4132 gst_query_unref (query);
4133 GST_DEBUG_OBJECT (basesink, "peer query faild, no pull mode");
4137 /* parse result of the query */
4138 gst_query_parse_scheduling (query, &pull_mode, NULL, NULL, NULL, NULL, NULL);
4139 gst_query_unref (query);
4142 GST_DEBUG_OBJECT (basesink, "pull mode not supported");
4146 /* set the pad mode before starting the task so that it's in the
4147 * correct state for the new thread. also the sink set_caps and get_caps
4148 * function checks this */
4149 basesink->pad_mode = GST_ACTIVATE_PULL;
4151 /* we first try to negotiate a format so that when we try to activate
4152 * downstream, it knows about our format */
4153 if (!gst_base_sink_negotiate_pull (basesink)) {
4154 GST_DEBUG_OBJECT (basesink, "failed to negotiate in pull mode");
4158 /* ok activate now */
4159 if (!gst_pad_activate_pull (pad, TRUE)) {
4160 /* clear any pending caps */
4161 GST_OBJECT_LOCK (basesink);
4162 gst_caps_replace (&basesink->priv->pull_caps, NULL);
4163 GST_OBJECT_UNLOCK (basesink);
4164 GST_DEBUG_OBJECT (basesink, "failed to activate in pull mode");
4168 GST_DEBUG_OBJECT (basesink, "Success activating pull mode");
4172 /* push mode fallback */
4174 GST_DEBUG_OBJECT (basesink, "Falling back to push mode");
4175 if ((result = gst_pad_activate_push (pad, TRUE))) {
4176 GST_DEBUG_OBJECT (basesink, "Success activating push mode");
4181 GST_WARNING_OBJECT (basesink, "Could not activate pad in either mode");
4182 gst_base_sink_set_flushing (basesink, pad, TRUE);
4185 gst_object_unref (basesink);
4191 gst_base_sink_pad_activate_push (GstPad * pad, gboolean active)
4194 GstBaseSink *basesink;
4196 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
4199 if (!basesink->can_activate_push) {
4201 basesink->pad_mode = GST_ACTIVATE_NONE;
4204 basesink->pad_mode = GST_ACTIVATE_PUSH;
4207 if (G_UNLIKELY (basesink->pad_mode != GST_ACTIVATE_PUSH)) {
4208 g_warning ("Internal GStreamer activation error!!!");
4211 gst_base_sink_set_flushing (basesink, pad, TRUE);
4213 basesink->pad_mode = GST_ACTIVATE_NONE;
4217 gst_object_unref (basesink);
4223 gst_base_sink_negotiate_pull (GstBaseSink * basesink)
4230 /* this returns the intersection between our caps and the peer caps. If there
4231 * is no peer, it returns NULL and we can't operate in pull mode so we can
4232 * fail the negotiation. */
4233 caps = gst_pad_get_allowed_caps (GST_BASE_SINK_PAD (basesink));
4234 if (caps == NULL || gst_caps_is_empty (caps))
4235 goto no_caps_possible;
4237 GST_DEBUG_OBJECT (basesink, "allowed caps: %" GST_PTR_FORMAT, caps);
4239 caps = gst_caps_make_writable (caps);
4240 /* get the first (prefered) format */
4241 gst_caps_truncate (caps);
4243 GST_DEBUG_OBJECT (basesink, "have caps: %" GST_PTR_FORMAT, caps);
4245 if (gst_caps_is_any (caps)) {
4246 GST_DEBUG_OBJECT (basesink, "caps were ANY after fixating, "
4248 /* neither side has template caps in this case, so they are prepared for
4249 pull() without setcaps() */
4253 gst_pad_fixate_caps (GST_BASE_SINK_PAD (basesink), caps);
4254 GST_DEBUG_OBJECT (basesink, "fixated to: %" GST_PTR_FORMAT, caps);
4256 if (gst_caps_is_fixed (caps)) {
4257 if (!gst_pad_send_event (GST_BASE_SINK_PAD (basesink),
4258 gst_event_new_caps (caps)))
4259 goto could_not_set_caps;
4261 GST_OBJECT_LOCK (basesink);
4262 gst_caps_replace (&basesink->priv->pull_caps, caps);
4263 GST_OBJECT_UNLOCK (basesink);
4269 gst_caps_unref (caps);
4275 GST_INFO_OBJECT (basesink, "Pipeline could not agree on caps");
4276 GST_DEBUG_OBJECT (basesink, "get_allowed_caps() returned EMPTY");
4278 gst_caps_unref (caps);
4283 GST_INFO_OBJECT (basesink, "Could not set caps: %" GST_PTR_FORMAT, caps);
4284 gst_caps_unref (caps);
4289 /* this won't get called until we implement an activate function */
4291 gst_base_sink_pad_activate_pull (GstPad * pad, gboolean active)
4293 gboolean result = FALSE;
4294 GstBaseSink *basesink;
4295 GstBaseSinkClass *bclass;
4297 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
4298 bclass = GST_BASE_SINK_GET_CLASS (basesink);
4304 /* we mark we have a newsegment here because pull based
4305 * mode works just fine without having a newsegment before the
4307 format = GST_FORMAT_BYTES;
4309 gst_segment_init (&basesink->segment, format);
4310 gst_segment_init (basesink->clip_segment, format);
4311 GST_OBJECT_LOCK (basesink);
4312 basesink->have_newsegment = TRUE;
4313 GST_OBJECT_UNLOCK (basesink);
4315 /* get the peer duration in bytes */
4316 result = gst_pad_query_peer_duration (pad, &format, &duration);
4318 GST_DEBUG_OBJECT (basesink,
4319 "setting duration in bytes to %" G_GINT64_FORMAT, duration);
4320 basesink->clip_segment->duration = duration;
4321 basesink->segment.duration = duration;
4323 GST_DEBUG_OBJECT (basesink, "unknown duration");
4326 if (bclass->activate_pull)
4327 result = bclass->activate_pull (basesink, TRUE);
4332 goto activate_failed;
4335 if (G_UNLIKELY (basesink->pad_mode != GST_ACTIVATE_PULL)) {
4336 g_warning ("Internal GStreamer activation error!!!");
4339 result = gst_base_sink_set_flushing (basesink, pad, TRUE);
4340 if (bclass->activate_pull)
4341 result &= bclass->activate_pull (basesink, FALSE);
4342 basesink->pad_mode = GST_ACTIVATE_NONE;
4343 /* clear any pending caps */
4344 GST_OBJECT_LOCK (basesink);
4345 gst_caps_replace (&basesink->priv->pull_caps, NULL);
4346 GST_OBJECT_UNLOCK (basesink);
4349 gst_object_unref (basesink);
4356 /* reset, as starting the thread failed */
4357 basesink->pad_mode = GST_ACTIVATE_NONE;
4359 GST_ERROR_OBJECT (basesink, "subclass failed to activate in pull mode");
4364 /* send an event to our sinkpad peer. */
4366 gst_base_sink_send_event (GstElement * element, GstEvent * event)
4369 GstBaseSink *basesink = GST_BASE_SINK (element);
4370 gboolean forward, result = TRUE;
4371 GstActivateMode mode;
4373 GST_OBJECT_LOCK (element);
4374 /* get the pad and the scheduling mode */
4375 pad = gst_object_ref (basesink->sinkpad);
4376 mode = basesink->pad_mode;
4377 GST_OBJECT_UNLOCK (element);
4379 /* only push UPSTREAM events upstream */
4380 forward = GST_EVENT_IS_UPSTREAM (event);
4382 GST_DEBUG_OBJECT (basesink, "handling event %p %" GST_PTR_FORMAT, event,
4385 switch (GST_EVENT_TYPE (event)) {
4386 case GST_EVENT_LATENCY:
4388 GstClockTime latency;
4390 gst_event_parse_latency (event, &latency);
4392 /* store the latency. We use this to adjust the running_time before syncing
4393 * it to the clock. */
4394 GST_OBJECT_LOCK (element);
4395 basesink->priv->latency = latency;
4396 if (!basesink->priv->have_latency)
4398 GST_OBJECT_UNLOCK (element);
4399 GST_DEBUG_OBJECT (basesink, "latency set to %" GST_TIME_FORMAT,
4400 GST_TIME_ARGS (latency));
4402 /* We forward this event so that all elements know about the global pipeline
4403 * latency. This is interesting for an element when it wants to figure out
4404 * when a particular piece of data will be rendered. */
4407 case GST_EVENT_SEEK:
4408 /* in pull mode we will execute the seek */
4409 if (mode == GST_ACTIVATE_PULL)
4410 result = gst_base_sink_perform_seek (basesink, pad, event);
4412 case GST_EVENT_STEP:
4413 result = gst_base_sink_perform_step (basesink, pad, event);
4421 result = gst_pad_push_event (pad, event);
4423 /* not forwarded, unref the event */
4424 gst_event_unref (event);
4427 gst_object_unref (pad);
4432 gst_base_sink_get_position (GstBaseSink * basesink, GstFormat format,
4433 gint64 * cur, gboolean * upstream)
4435 GstClock *clock = NULL;
4436 gboolean res = FALSE;
4437 GstFormat oformat, tformat;
4438 GstSegment *segment;
4439 GstClockTime now, latency;
4440 GstClockTimeDiff base_time;
4441 gint64 time, base, duration;
4444 gboolean last_seen, with_clock, in_paused;
4446 GST_OBJECT_LOCK (basesink);
4447 /* we can only get the segment when we are not NULL or READY */
4448 if (!basesink->have_newsegment)
4452 /* when not in PLAYING or when we're busy with a state change, we
4453 * cannot read from the clock so we report time based on the
4454 * last seen timestamp. */
4455 if (GST_STATE (basesink) != GST_STATE_PLAYING ||
4456 GST_STATE_PENDING (basesink) != GST_STATE_VOID_PENDING) {
4460 /* we don't use the clip segment in pull mode, when seeking we update the
4461 * main segment directly with the new segment values without it having to be
4462 * activated by the rendering after preroll */
4463 if (basesink->pad_mode == GST_ACTIVATE_PUSH)
4464 segment = basesink->clip_segment;
4466 segment = &basesink->segment;
4468 /* our intermediate time format */
4469 tformat = GST_FORMAT_TIME;
4470 /* get the format in the segment */
4471 oformat = segment->format;
4473 /* report with last seen position when EOS */
4474 last_seen = basesink->eos;
4476 /* assume we will use the clock for getting the current position */
4478 if (basesink->sync == FALSE)
4481 /* and we need a clock */
4482 if (G_UNLIKELY ((clock = GST_ELEMENT_CLOCK (basesink)) == NULL))
4485 gst_object_ref (clock);
4487 /* collect all data we need holding the lock */
4488 if (GST_CLOCK_TIME_IS_VALID (segment->time))
4489 time = segment->time;
4493 if (GST_CLOCK_TIME_IS_VALID (segment->stop))
4494 duration = segment->stop - segment->start;
4498 base = segment->base;
4499 rate = segment->rate * segment->applied_rate;
4500 latency = basesink->priv->latency;
4502 if (oformat == GST_FORMAT_TIME) {
4505 start = basesink->priv->current_sstart;
4506 stop = basesink->priv->current_sstop;
4509 /* in paused we use the last position as a lower bound */
4510 if (stop == -1 || segment->rate > 0.0)
4515 /* in playing, use last stop time as upper bound */
4516 if (start == -1 || segment->rate > 0.0)
4522 /* convert last stop to stream time */
4523 last = gst_segment_to_stream_time (segment, oformat, segment->position);
4527 /* in paused, use start_time */
4528 base_time = GST_ELEMENT_START_TIME (basesink);
4529 GST_DEBUG_OBJECT (basesink, "in paused, using start time %" GST_TIME_FORMAT,
4530 GST_TIME_ARGS (base_time));
4531 } else if (with_clock) {
4532 /* else use clock when needed */
4533 base_time = GST_ELEMENT_CAST (basesink)->base_time;
4534 GST_DEBUG_OBJECT (basesink, "using clock and base time %" GST_TIME_FORMAT,
4535 GST_TIME_ARGS (base_time));
4537 /* else, no sync or clock -> no base time */
4538 GST_DEBUG_OBJECT (basesink, "no sync or no clock");
4542 /* no base_time, we can't calculate running_time, use last seem timestamp to report
4544 if (base_time == -1)
4547 /* need to release the object lock before we can get the time,
4548 * a clock might take the LOCK of the provider, which could be
4549 * a basesink subclass. */
4550 GST_OBJECT_UNLOCK (basesink);
4553 /* in EOS or when no valid stream_time, report the value of last seen
4556 /* no timestamp, we need to ask upstream */
4557 GST_DEBUG_OBJECT (basesink, "no last seen timestamp, asking upstream");
4562 GST_DEBUG_OBJECT (basesink, "using last seen timestamp %" GST_TIME_FORMAT,
4563 GST_TIME_ARGS (last));
4566 if (oformat != tformat) {
4567 /* convert base, time and duration to time */
4568 if (!gst_pad_query_convert (basesink->sinkpad, oformat, base, &tformat,
4570 goto convert_failed;
4571 if (!gst_pad_query_convert (basesink->sinkpad, oformat, duration,
4572 &tformat, &duration))
4573 goto convert_failed;
4574 if (!gst_pad_query_convert (basesink->sinkpad, oformat, time, &tformat,
4576 goto convert_failed;
4577 if (!gst_pad_query_convert (basesink->sinkpad, oformat, last, &tformat,
4579 goto convert_failed;
4581 /* assume time format from now on */
4585 if (!in_paused && with_clock) {
4586 now = gst_clock_get_time (clock);
4592 /* subtract base time and base time from the clock time.
4593 * Make sure we don't go negative. This is the current time in
4594 * the segment which we need to scale with the combined
4595 * rate and applied rate. */
4597 base_time += latency;
4598 if (GST_CLOCK_DIFF (base_time, now) < 0)
4601 /* for negative rates we need to count back from the segment
4606 *cur = time + gst_guint64_to_gdouble (now - base_time) * rate;
4609 /* never report less than segment values in paused */
4611 *cur = MAX (last, *cur);
4613 /* never report more than last seen position in playing */
4615 *cur = MIN (last, *cur);
4618 GST_DEBUG_OBJECT (basesink,
4619 "now %" GST_TIME_FORMAT " - base_time %" GST_TIME_FORMAT " - base %"
4620 GST_TIME_FORMAT " + time %" GST_TIME_FORMAT " last %" GST_TIME_FORMAT,
4621 GST_TIME_ARGS (now), GST_TIME_ARGS (base_time), GST_TIME_ARGS (base),
4622 GST_TIME_ARGS (time), GST_TIME_ARGS (last));
4625 if (oformat != format) {
4626 /* convert to final format */
4627 if (!gst_pad_query_convert (basesink->sinkpad, oformat, *cur, &format, cur))
4628 goto convert_failed;
4634 GST_DEBUG_OBJECT (basesink, "res: %d, POSITION: %" GST_TIME_FORMAT,
4635 res, GST_TIME_ARGS (*cur));
4638 gst_object_unref (clock);
4645 /* in NULL or READY we always return FALSE and -1 */
4646 GST_DEBUG_OBJECT (basesink, "position in wrong state, return -1");
4649 GST_OBJECT_UNLOCK (basesink);
4654 GST_DEBUG_OBJECT (basesink, "convert failed, try upstream");
4662 gst_base_sink_get_duration (GstBaseSink * basesink, GstFormat format,
4663 gint64 * dur, gboolean * upstream)
4665 gboolean res = FALSE;
4667 if (basesink->pad_mode == GST_ACTIVATE_PULL) {
4668 GstFormat uformat = GST_FORMAT_BYTES;
4671 /* get the duration in bytes, in pull mode that's all we are sure to
4672 * know. We have to explicitly get this value from upstream instead of
4673 * using our cached value because it might change. Duration caching
4674 * should be done at a higher level. */
4675 res = gst_pad_query_peer_duration (basesink->sinkpad, &uformat, &uduration);
4677 basesink->segment.duration = uduration;
4678 if (format != uformat) {
4679 /* convert to the requested format */
4680 res = gst_pad_query_convert (basesink->sinkpad, uformat, uduration,
4694 static const GstQueryType *
4695 gst_base_sink_get_query_types (GstElement * element)
4697 static const GstQueryType query_types[] = {
4709 gst_base_sink_query (GstElement * element, GstQuery * query)
4711 gboolean res = FALSE;
4713 GstBaseSink *basesink = GST_BASE_SINK (element);
4715 switch (GST_QUERY_TYPE (query)) {
4716 case GST_QUERY_POSITION:
4720 gboolean upstream = FALSE;
4722 gst_query_parse_position (query, &format, NULL);
4724 GST_DEBUG_OBJECT (basesink, "position query in format %s",
4725 gst_format_get_name (format));
4727 /* first try to get the position based on the clock */
4729 gst_base_sink_get_position (basesink, format, &cur, &upstream))) {
4730 gst_query_set_position (query, format, cur);
4731 } else if (upstream) {
4732 /* fallback to peer query */
4733 res = gst_pad_peer_query (basesink->sinkpad, query);
4736 /* we can handle a few things if upstream failed */
4737 if (format == GST_FORMAT_PERCENT) {
4739 GstFormat uformat = GST_FORMAT_TIME;
4741 res = gst_base_sink_get_position (basesink, GST_FORMAT_TIME, &cur,
4743 if (!res && upstream) {
4744 res = gst_pad_query_peer_position (basesink->sinkpad, &uformat,
4748 res = gst_base_sink_get_duration (basesink, GST_FORMAT_TIME, &dur,
4750 if (!res && upstream) {
4751 res = gst_pad_query_peer_duration (basesink->sinkpad, &uformat,
4758 pos = gst_util_uint64_scale (100 * GST_FORMAT_PERCENT_SCALE, cur,
4760 gst_query_set_position (query, GST_FORMAT_PERCENT, pos);
4766 case GST_QUERY_DURATION:
4770 gboolean upstream = FALSE;
4772 gst_query_parse_duration (query, &format, NULL);
4774 GST_DEBUG_OBJECT (basesink, "duration query in format %s",
4775 gst_format_get_name (format));
4778 gst_base_sink_get_duration (basesink, format, &dur, &upstream))) {
4779 gst_query_set_duration (query, format, dur);
4780 } else if (upstream) {
4781 /* fallback to peer query */
4782 res = gst_pad_peer_query (basesink->sinkpad, query);
4785 /* we can handle a few things if upstream failed */
4786 if (format == GST_FORMAT_PERCENT) {
4787 gst_query_set_duration (query, GST_FORMAT_PERCENT,
4788 GST_FORMAT_PERCENT_MAX);
4794 case GST_QUERY_LATENCY:
4796 gboolean live, us_live;
4797 GstClockTime min, max;
4799 if ((res = gst_base_sink_query_latency (basesink, &live, &us_live, &min,
4801 gst_query_set_latency (query, live, min, max);
4805 case GST_QUERY_JITTER:
4807 case GST_QUERY_RATE:
4808 /* gst_query_set_rate (query, basesink->segment_rate); */
4811 case GST_QUERY_SEGMENT:
4813 if (basesink->pad_mode == GST_ACTIVATE_PULL) {
4814 gst_query_set_segment (query, basesink->segment.rate,
4815 GST_FORMAT_TIME, basesink->segment.start, basesink->segment.stop);
4818 res = gst_pad_peer_query (basesink->sinkpad, query);
4822 case GST_QUERY_SEEKING:
4823 case GST_QUERY_CONVERT:
4824 case GST_QUERY_FORMATS:
4826 res = gst_pad_peer_query (basesink->sinkpad, query);
4829 GST_DEBUG_OBJECT (basesink, "query %s returns %d",
4830 GST_QUERY_TYPE_NAME (query), res);
4834 static GstStateChangeReturn
4835 gst_base_sink_change_state (GstElement * element, GstStateChange transition)
4837 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
4838 GstBaseSink *basesink = GST_BASE_SINK (element);
4839 GstBaseSinkClass *bclass;
4840 GstBaseSinkPrivate *priv;
4842 priv = basesink->priv;
4844 bclass = GST_BASE_SINK_GET_CLASS (basesink);
4846 switch (transition) {
4847 case GST_STATE_CHANGE_NULL_TO_READY:
4849 if (!bclass->start (basesink))
4852 case GST_STATE_CHANGE_READY_TO_PAUSED:
4853 /* need to complete preroll before this state change completes, there
4854 * is no data flow in READY so we can safely assume we need to preroll. */
4855 GST_BASE_SINK_PREROLL_LOCK (basesink);
4856 GST_DEBUG_OBJECT (basesink, "READY to PAUSED");
4857 basesink->have_newsegment = FALSE;
4858 gst_segment_init (&basesink->segment, GST_FORMAT_UNDEFINED);
4859 gst_segment_init (basesink->clip_segment, GST_FORMAT_UNDEFINED);
4860 basesink->offset = 0;
4861 basesink->have_preroll = FALSE;
4862 priv->step_unlock = FALSE;
4863 basesink->need_preroll = TRUE;
4864 basesink->playing_async = TRUE;
4865 basesink->priv->reset_time = FALSE;
4866 priv->current_sstart = GST_CLOCK_TIME_NONE;
4867 priv->current_sstop = GST_CLOCK_TIME_NONE;
4868 priv->eos_rtime = GST_CLOCK_TIME_NONE;
4870 basesink->eos = FALSE;
4871 priv->received_eos = FALSE;
4872 gst_base_sink_reset_qos (basesink);
4873 priv->commited = FALSE;
4874 priv->call_preroll = TRUE;
4875 priv->current_step.valid = FALSE;
4876 priv->pending_step.valid = FALSE;
4877 if (priv->async_enabled) {
4878 GST_DEBUG_OBJECT (basesink, "doing async state change");
4879 /* when async enabled, post async-start message and return ASYNC from
4880 * the state change function */
4881 ret = GST_STATE_CHANGE_ASYNC;
4882 gst_element_post_message (GST_ELEMENT_CAST (basesink),
4883 gst_message_new_async_start (GST_OBJECT_CAST (basesink)));
4885 priv->have_latency = TRUE;
4887 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
4889 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
4890 GST_BASE_SINK_PREROLL_LOCK (basesink);
4891 if (!gst_base_sink_needs_preroll (basesink)) {
4892 GST_DEBUG_OBJECT (basesink, "PAUSED to PLAYING, don't need preroll");
4893 /* no preroll needed anymore now. */
4894 basesink->playing_async = FALSE;
4895 basesink->need_preroll = FALSE;
4896 if (basesink->eos) {
4897 GstMessage *message;
4899 /* need to post EOS message here */
4900 GST_DEBUG_OBJECT (basesink, "Now posting EOS");
4901 message = gst_message_new_eos (GST_OBJECT_CAST (basesink));
4902 gst_message_set_seqnum (message, basesink->priv->seqnum);
4903 gst_element_post_message (GST_ELEMENT_CAST (basesink), message);
4905 GST_DEBUG_OBJECT (basesink, "signal preroll");
4906 GST_BASE_SINK_PREROLL_SIGNAL (basesink);
4909 GST_DEBUG_OBJECT (basesink, "PAUSED to PLAYING, we are not prerolled");
4910 basesink->need_preroll = TRUE;
4911 basesink->playing_async = TRUE;
4912 priv->call_preroll = TRUE;
4913 priv->commited = FALSE;
4914 if (priv->async_enabled) {
4915 GST_DEBUG_OBJECT (basesink, "doing async state change");
4916 ret = GST_STATE_CHANGE_ASYNC;
4917 gst_element_post_message (GST_ELEMENT_CAST (basesink),
4918 gst_message_new_async_start (GST_OBJECT_CAST (basesink)));
4921 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
4928 GstStateChangeReturn bret;
4930 bret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
4931 if (G_UNLIKELY (bret == GST_STATE_CHANGE_FAILURE))
4932 goto activate_failed;
4935 switch (transition) {
4936 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
4937 GST_DEBUG_OBJECT (basesink, "PLAYING to PAUSED");
4938 /* FIXME, make sure we cannot enter _render first */
4940 /* we need to call ::unlock before locking PREROLL_LOCK
4941 * since we lock it before going into ::render */
4943 bclass->unlock (basesink);
4945 GST_BASE_SINK_PREROLL_LOCK (basesink);
4946 GST_DEBUG_OBJECT (basesink, "got preroll lock");
4947 /* now that we have the PREROLL lock, clear our unlock request */
4948 if (bclass->unlock_stop)
4949 bclass->unlock_stop (basesink);
4951 /* we need preroll again and we set the flag before unlocking the clockid
4952 * because if the clockid is unlocked before a current buffer expired, we
4953 * can use that buffer to preroll with */
4954 basesink->need_preroll = TRUE;
4956 if (basesink->clock_id) {
4957 GST_DEBUG_OBJECT (basesink, "unschedule clock");
4958 gst_clock_id_unschedule (basesink->clock_id);
4961 /* if we don't have a preroll buffer we need to wait for a preroll and
4963 if (!gst_base_sink_needs_preroll (basesink)) {
4964 GST_DEBUG_OBJECT (basesink, "PLAYING to PAUSED, we are prerolled");
4965 basesink->playing_async = FALSE;
4967 if (GST_STATE_TARGET (GST_ELEMENT (basesink)) <= GST_STATE_READY) {
4968 GST_DEBUG_OBJECT (basesink, "element is <= READY");
4969 ret = GST_STATE_CHANGE_SUCCESS;
4971 GST_DEBUG_OBJECT (basesink,
4972 "PLAYING to PAUSED, we are not prerolled");
4973 basesink->playing_async = TRUE;
4974 priv->commited = FALSE;
4975 priv->call_preroll = TRUE;
4976 if (priv->async_enabled) {
4977 GST_DEBUG_OBJECT (basesink, "doing async state change");
4978 ret = GST_STATE_CHANGE_ASYNC;
4979 gst_element_post_message (GST_ELEMENT_CAST (basesink),
4980 gst_message_new_async_start (GST_OBJECT_CAST (basesink)));
4984 GST_DEBUG_OBJECT (basesink, "rendered: %" G_GUINT64_FORMAT
4985 ", dropped: %" G_GUINT64_FORMAT, priv->rendered, priv->dropped);
4987 gst_base_sink_reset_qos (basesink);
4988 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
4990 case GST_STATE_CHANGE_PAUSED_TO_READY:
4991 GST_BASE_SINK_PREROLL_LOCK (basesink);
4992 /* start by reseting our position state with the object lock so that the
4993 * position query gets the right idea. We do this before we post the
4994 * messages so that the message handlers pick this up. */
4995 GST_OBJECT_LOCK (basesink);
4996 basesink->have_newsegment = FALSE;
4997 priv->current_sstart = GST_CLOCK_TIME_NONE;
4998 priv->current_sstop = GST_CLOCK_TIME_NONE;
4999 priv->have_latency = FALSE;
5000 if (priv->cached_clock_id) {
5001 gst_clock_id_unref (priv->cached_clock_id);
5002 priv->cached_clock_id = NULL;
5004 GST_OBJECT_UNLOCK (basesink);
5006 gst_base_sink_set_last_buffer (basesink, NULL);
5007 priv->call_preroll = FALSE;
5009 if (!priv->commited) {
5010 if (priv->async_enabled) {
5011 GST_DEBUG_OBJECT (basesink, "PAUSED to READY, posting async-done");
5013 gst_element_post_message (GST_ELEMENT_CAST (basesink),
5014 gst_message_new_state_changed (GST_OBJECT_CAST (basesink),
5015 GST_STATE_PLAYING, GST_STATE_PAUSED, GST_STATE_READY));
5017 gst_element_post_message (GST_ELEMENT_CAST (basesink),
5018 gst_message_new_async_done (GST_OBJECT_CAST (basesink), FALSE));
5020 priv->commited = TRUE;
5022 GST_DEBUG_OBJECT (basesink, "PAUSED to READY, don't need_preroll");
5024 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
5026 case GST_STATE_CHANGE_READY_TO_NULL:
5028 if (!bclass->stop (basesink)) {
5029 GST_WARNING_OBJECT (basesink, "failed to stop");
5032 gst_base_sink_set_last_buffer (basesink, NULL);
5033 priv->call_preroll = FALSE;
5044 GST_DEBUG_OBJECT (basesink, "failed to start");
5045 return GST_STATE_CHANGE_FAILURE;
5049 GST_DEBUG_OBJECT (basesink,
5050 "element failed to change states -- activation problem?");
5051 return GST_STATE_CHANGE_FAILURE;