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., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, 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:
40 * |[<!-- language="C" -->
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_static_pad_template (gstelement_class, &sinktemplate);
50 * gst_element_class_set_static_metadata (gstelement_class,
54 * "The author <my.sink@my.email>");
58 * #GstBaseSink will handle the prerolling correctly. This means that it will
59 * return %GST_STATE_CHANGE_ASYNC from a state change to PAUSED until the first
60 * buffer arrives in this element. The base class will call the
61 * #GstBaseSinkClass.preroll() vmethod with this preroll buffer and will then
62 * commit the state change to the next asynchronously pending state.
64 * When the element is set to PLAYING, #GstBaseSink will synchronise on the
65 * clock using the times returned from #GstBaseSinkClass.get_times(). If this
66 * function returns %GST_CLOCK_TIME_NONE for the start time, no synchronisation
67 * will be done. Synchronisation can be disabled entirely by setting the object
68 * #GstBaseSink:sync property to %FALSE.
70 * After synchronisation the virtual method #GstBaseSinkClass.render() will be
71 * called. Subclasses should minimally implement this method.
73 * Subclasses that synchronise on the clock in the #GstBaseSinkClass.render()
74 * method are supported as well. These classes typically receive a buffer in
75 * the render method and can then potentially block on the clock while
76 * rendering. A typical example is an audiosink.
77 * These subclasses can use gst_base_sink_wait_preroll() to perform the
80 * Upon receiving the EOS event in the PLAYING state, #GstBaseSink will wait
81 * for the clock to reach the time indicated by the stop time of the last
82 * #GstBaseSinkClass.get_times() call before posting an EOS message. When the
83 * element receives EOS in PAUSED, preroll completes, the event is queued and an
84 * EOS message is posted when going to PLAYING.
86 * #GstBaseSink will internally use the %GST_EVENT_SEGMENT events to schedule
87 * synchronisation and clipping of buffers. Buffers that fall completely outside
88 * of the current segment are dropped. Buffers that fall partially in the
89 * segment are rendered (and prerolled). Subclasses should do any subbuffer
90 * clipping themselves when needed.
92 * #GstBaseSink will by default report the current playback position in
93 * %GST_FORMAT_TIME based on the current clock time and segment information.
94 * If no clock has been set on the element, the query will be forwarded
97 * The #GstBaseSinkClass.set_caps() function will be called when the subclass
98 * should configure itself to process a specific media type.
100 * The #GstBaseSinkClass.start() and #GstBaseSinkClass.stop() virtual methods
101 * will be called when resources should be allocated. Any
102 * #GstBaseSinkClass.preroll(), #GstBaseSinkClass.render() and
103 * #GstBaseSinkClass.set_caps() function will be called between the
104 * #GstBaseSinkClass.start() and #GstBaseSinkClass.stop() calls.
106 * The #GstBaseSinkClass.event() virtual method will be called when an event is
107 * received by #GstBaseSink. Normally this method should only be overridden by
108 * very specific elements (such as file sinks) which need to handle the
109 * newsegment event specially.
111 * The #GstBaseSinkClass.unlock() method is called when the elements should
112 * unblock any blocking operations they perform in the
113 * #GstBaseSinkClass.render() method. This is mostly useful when the
114 * #GstBaseSinkClass.render() method performs a blocking write on a file
115 * descriptor, for example.
117 * The #GstBaseSink:max-lateness property affects how the sink deals with
118 * buffers that arrive too late in the sink. A buffer arrives too late in the
119 * sink when the presentation time (as a combination of the last segment, buffer
120 * timestamp and element base_time) plus the duration is before the current
122 * If the frame is later than max-lateness, the sink will drop the buffer
123 * without calling the render method.
124 * This feature is disabled if sync is disabled, the
125 * #GstBaseSinkClass.get_times() method does not return a valid start time or
126 * max-lateness is set to -1 (the default).
127 * Subclasses can use gst_base_sink_set_max_lateness() to configure the
128 * max-lateness value.
130 * The #GstBaseSink:qos property will enable the quality-of-service features of
131 * the basesink which gather statistics about the real-time performance of the
132 * clock synchronisation. For each buffer received in the sink, statistics are
133 * gathered and a QOS event is sent upstream with these numbers. This
134 * information can then be used by upstream elements to reduce their processing
137 * The #GstBaseSink:async property can be used to instruct the sink to never
138 * perform an ASYNC state change. This feature is mostly usable when dealing
139 * with non-synchronized streams or sparse streams.
146 #include <gst/gst_private.h>
148 #include "gstbasesink.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 struct _GstBaseSinkPrivate
179 gint qos_enabled; /* ATOMIC */
180 gboolean async_enabled;
181 GstClockTimeDiff ts_offset;
182 GstClockTime render_delay;
184 /* start, stop of current buffer, stream time, used to report position */
185 GstClockTime current_sstart;
186 GstClockTime current_sstop;
188 /* start, stop and jitter of current buffer, running time */
189 GstClockTime current_rstart;
190 GstClockTime current_rstop;
191 GstClockTimeDiff current_jitter;
192 /* the running time of the previous buffer */
193 GstClockTime prev_rstart;
195 /* EOS sync time in running time */
196 GstClockTime eos_rtime;
198 /* last buffer that arrived in time, running time */
199 GstClockTime last_render_time;
200 /* when the last buffer left the sink, running time */
201 GstClockTime last_left;
203 /* running averages go here these are done on running time */
205 GstClockTime avg_duration;
207 GstClockTime avg_in_diff;
209 /* these are done on system time. avg_jitter and avg_render are
210 * compared to eachother to see if the rendering time takes a
211 * huge amount of the processing, If so we are flooded with
213 GstClockTime last_left_systime;
214 GstClockTime avg_jitter;
215 GstClockTime start, stop;
216 GstClockTime avg_render;
218 /* number of rendered and dropped frames */
223 GstClockTime latency;
225 /* if we already commited the state */
227 /* state change to playing ongoing */
230 /* when we received EOS */
231 gboolean received_eos;
233 /* when we are prerolled and able to report latency */
234 gboolean have_latency;
236 /* the last buffer we prerolled or rendered. Useful for making snapshots */
237 gint enable_last_sample; /* atomic */
238 GstBuffer *last_buffer;
240 GstBufferList *last_buffer_list;
242 /* negotiated caps */
245 /* blocksize for pulling */
250 /* seqnum of the stream */
253 gboolean call_preroll;
254 gboolean step_unlock;
256 /* we have a pending and a current step operation */
257 GstStepInfo current_step;
258 GstStepInfo pending_step;
260 /* Cached GstClockID */
261 GstClockID cached_clock_id;
263 /* for throttling and QoS */
264 GstClockTime earliest_in_time;
265 GstClockTime throttle_time;
267 /* for rate control */
269 GstClockTime rc_time;
270 GstClockTime rc_next;
271 gsize rc_accumulated;
274 #define DO_RUNNING_AVG(avg,val,size) (((val) + ((size)-1) * (avg)) / (size))
276 /* generic running average, this has a neutral window size */
277 #define UPDATE_RUNNING_AVG(avg,val) DO_RUNNING_AVG(avg,val,8)
279 /* the windows for these running averages are experimentally obtained.
280 * positive values get averaged more while negative values use a small
281 * window so we can react faster to badness. */
282 #define UPDATE_RUNNING_AVG_P(avg,val) DO_RUNNING_AVG(avg,val,16)
283 #define UPDATE_RUNNING_AVG_N(avg,val) DO_RUNNING_AVG(avg,val,4)
285 /* BaseSink properties */
287 #define DEFAULT_CAN_ACTIVATE_PULL FALSE /* fixme: enable me */
288 #define DEFAULT_CAN_ACTIVATE_PUSH TRUE
290 #define DEFAULT_SYNC TRUE
291 #define DEFAULT_MAX_LATENESS -1
292 #define DEFAULT_QOS FALSE
293 #define DEFAULT_ASYNC TRUE
294 #define DEFAULT_TS_OFFSET 0
295 #define DEFAULT_BLOCKSIZE 4096
296 #define DEFAULT_RENDER_DELAY 0
297 #define DEFAULT_ENABLE_LAST_SAMPLE TRUE
298 #define DEFAULT_THROTTLE_TIME 0
299 #define DEFAULT_MAX_BITRATE 0
309 PROP_ENABLE_LAST_SAMPLE,
318 static GstElementClass *parent_class = NULL;
320 static void gst_base_sink_class_init (GstBaseSinkClass * klass);
321 static void gst_base_sink_init (GstBaseSink * trans, gpointer g_class);
322 static void gst_base_sink_finalize (GObject * object);
325 gst_base_sink_get_type (void)
327 static volatile gsize base_sink_type = 0;
329 if (g_once_init_enter (&base_sink_type)) {
331 static const GTypeInfo base_sink_info = {
332 sizeof (GstBaseSinkClass),
335 (GClassInitFunc) gst_base_sink_class_init,
338 sizeof (GstBaseSink),
340 (GInstanceInitFunc) gst_base_sink_init,
343 _type = g_type_register_static (GST_TYPE_ELEMENT,
344 "GstBaseSink", &base_sink_info, G_TYPE_FLAG_ABSTRACT);
345 g_once_init_leave (&base_sink_type, _type);
347 return base_sink_type;
350 static void gst_base_sink_set_property (GObject * object, guint prop_id,
351 const GValue * value, GParamSpec * pspec);
352 static void gst_base_sink_get_property (GObject * object, guint prop_id,
353 GValue * value, GParamSpec * pspec);
355 static gboolean gst_base_sink_send_event (GstElement * element,
357 static gboolean default_element_query (GstElement * element, GstQuery * query);
359 static GstCaps *gst_base_sink_default_get_caps (GstBaseSink * sink,
361 static gboolean gst_base_sink_default_set_caps (GstBaseSink * sink,
363 static void gst_base_sink_default_get_times (GstBaseSink * basesink,
364 GstBuffer * buffer, GstClockTime * start, GstClockTime * end);
365 static gboolean gst_base_sink_set_flushing (GstBaseSink * basesink,
366 GstPad * pad, gboolean flushing);
367 static gboolean gst_base_sink_default_activate_pull (GstBaseSink * basesink,
369 static gboolean gst_base_sink_default_do_seek (GstBaseSink * sink,
370 GstSegment * segment);
371 static gboolean gst_base_sink_default_prepare_seek_segment (GstBaseSink * sink,
372 GstEvent * event, GstSegment * segment);
374 static GstStateChangeReturn gst_base_sink_change_state (GstElement * element,
375 GstStateChange transition);
377 static gboolean gst_base_sink_sink_query (GstPad * pad, GstObject * parent,
379 static GstFlowReturn gst_base_sink_chain (GstPad * pad, GstObject * parent,
381 static GstFlowReturn gst_base_sink_chain_list (GstPad * pad, GstObject * parent,
382 GstBufferList * list);
384 static void gst_base_sink_loop (GstPad * pad);
385 static gboolean gst_base_sink_pad_activate (GstPad * pad, GstObject * parent);
386 static gboolean gst_base_sink_pad_activate_mode (GstPad * pad,
387 GstObject * parent, GstPadMode mode, gboolean active);
388 static gboolean gst_base_sink_default_event (GstBaseSink * basesink,
390 static GstFlowReturn gst_base_sink_default_wait_event (GstBaseSink * basesink,
392 static gboolean gst_base_sink_event (GstPad * pad, GstObject * parent,
395 static gboolean gst_base_sink_default_query (GstBaseSink * sink,
398 static gboolean gst_base_sink_negotiate_pull (GstBaseSink * basesink);
399 static GstCaps *gst_base_sink_default_fixate (GstBaseSink * bsink,
401 static GstCaps *gst_base_sink_fixate (GstBaseSink * bsink, GstCaps * caps);
403 /* check if an object was too late */
404 static gboolean gst_base_sink_is_too_late (GstBaseSink * basesink,
405 GstMiniObject * obj, GstClockTime rstart, GstClockTime rstop,
406 GstClockReturn status, GstClockTimeDiff jitter, gboolean render);
409 gst_base_sink_class_init (GstBaseSinkClass * klass)
411 GObjectClass *gobject_class;
412 GstElementClass *gstelement_class;
414 gobject_class = G_OBJECT_CLASS (klass);
415 gstelement_class = GST_ELEMENT_CLASS (klass);
417 GST_DEBUG_CATEGORY_INIT (gst_base_sink_debug, "basesink", 0,
420 g_type_class_add_private (klass, sizeof (GstBaseSinkPrivate));
422 parent_class = g_type_class_peek_parent (klass);
424 gobject_class->finalize = gst_base_sink_finalize;
425 gobject_class->set_property = gst_base_sink_set_property;
426 gobject_class->get_property = gst_base_sink_get_property;
428 g_object_class_install_property (gobject_class, PROP_SYNC,
429 g_param_spec_boolean ("sync", "Sync", "Sync on the clock", DEFAULT_SYNC,
430 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
432 g_object_class_install_property (gobject_class, PROP_MAX_LATENESS,
433 g_param_spec_int64 ("max-lateness", "Max Lateness",
434 "Maximum number of nanoseconds that a buffer can be late before it "
435 "is dropped (-1 unlimited)", -1, G_MAXINT64, DEFAULT_MAX_LATENESS,
436 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
438 g_object_class_install_property (gobject_class, PROP_QOS,
439 g_param_spec_boolean ("qos", "Qos",
440 "Generate Quality-of-Service events upstream", DEFAULT_QOS,
441 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
445 * If set to %TRUE, the basesink will perform asynchronous state changes.
446 * When set to %FALSE, the sink will not signal the parent when it prerolls.
447 * Use this option when dealing with sparse streams or when synchronisation is
450 g_object_class_install_property (gobject_class, PROP_ASYNC,
451 g_param_spec_boolean ("async", "Async",
452 "Go asynchronously to PAUSED", DEFAULT_ASYNC,
453 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
455 * GstBaseSink:ts-offset:
457 * Controls the final synchronisation, a negative value will render the buffer
458 * earlier while a positive value delays playback. This property can be
459 * used to fix synchronisation in bad files.
461 g_object_class_install_property (gobject_class, PROP_TS_OFFSET,
462 g_param_spec_int64 ("ts-offset", "TS Offset",
463 "Timestamp offset in nanoseconds", G_MININT64, G_MAXINT64,
464 DEFAULT_TS_OFFSET, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
467 * GstBaseSink:enable-last-sample:
469 * Enable the last-sample property. If %FALSE, basesink doesn't keep a
470 * reference to the last buffer arrived and the last-sample property is always
471 * set to %NULL. This can be useful if you need buffers to be released as soon
472 * as possible, eg. if you're using a buffer pool.
474 g_object_class_install_property (gobject_class, PROP_ENABLE_LAST_SAMPLE,
475 g_param_spec_boolean ("enable-last-sample", "Enable Last Buffer",
476 "Enable the last-sample property", DEFAULT_ENABLE_LAST_SAMPLE,
477 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
480 * GstBaseSink:last-sample:
482 * The last buffer that arrived in the sink and was used for preroll or for
483 * rendering. This property can be used to generate thumbnails. This property
484 * can be %NULL when the sink has not yet received a buffer.
486 g_object_class_install_property (gobject_class, PROP_LAST_SAMPLE,
487 g_param_spec_boxed ("last-sample", "Last Sample",
488 "The last sample received in the sink", GST_TYPE_SAMPLE,
489 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
491 * GstBaseSink:blocksize:
493 * The amount of bytes to pull when operating in pull mode.
495 /* FIXME 2.0: blocksize property should be int, otherwise min>max.. */
496 g_object_class_install_property (gobject_class, PROP_BLOCKSIZE,
497 g_param_spec_uint ("blocksize", "Block size",
498 "Size in bytes to pull per buffer (0 = default)", 0, G_MAXUINT,
499 DEFAULT_BLOCKSIZE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
501 * GstBaseSink:render-delay:
503 * The additional delay between synchronisation and actual rendering of the
504 * media. This property will add additional latency to the device in order to
505 * make other sinks compensate for the delay.
507 g_object_class_install_property (gobject_class, PROP_RENDER_DELAY,
508 g_param_spec_uint64 ("render-delay", "Render Delay",
509 "Additional render delay of the sink in nanoseconds", 0, G_MAXUINT64,
510 DEFAULT_RENDER_DELAY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
512 * GstBaseSink:throttle-time:
514 * The time to insert between buffers. This property can be used to control
515 * the maximum amount of buffers per second to render. Setting this property
516 * to a value bigger than 0 will make the sink create THROTTLE QoS events.
518 g_object_class_install_property (gobject_class, PROP_THROTTLE_TIME,
519 g_param_spec_uint64 ("throttle-time", "Throttle time",
520 "The time to keep between rendered buffers (0 = disabled)", 0,
521 G_MAXUINT64, DEFAULT_THROTTLE_TIME,
522 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
524 * GstBaseSink:max-bitrate:
526 * Control the maximum amount of bits that will be rendered per second.
527 * Setting this property to a value bigger than 0 will make the sink delay
528 * rendering of the buffers when it would exceed to max-bitrate.
532 g_object_class_install_property (gobject_class, PROP_MAX_BITRATE,
533 g_param_spec_uint64 ("max-bitrate", "Max Bitrate",
534 "The maximum bits per second to render (0 = disabled)", 0,
535 G_MAXUINT64, DEFAULT_MAX_BITRATE,
536 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
538 gstelement_class->change_state =
539 GST_DEBUG_FUNCPTR (gst_base_sink_change_state);
540 gstelement_class->send_event = GST_DEBUG_FUNCPTR (gst_base_sink_send_event);
541 gstelement_class->query = GST_DEBUG_FUNCPTR (default_element_query);
543 klass->get_caps = GST_DEBUG_FUNCPTR (gst_base_sink_default_get_caps);
544 klass->set_caps = GST_DEBUG_FUNCPTR (gst_base_sink_default_set_caps);
545 klass->fixate = GST_DEBUG_FUNCPTR (gst_base_sink_default_fixate);
546 klass->activate_pull =
547 GST_DEBUG_FUNCPTR (gst_base_sink_default_activate_pull);
548 klass->get_times = GST_DEBUG_FUNCPTR (gst_base_sink_default_get_times);
549 klass->query = GST_DEBUG_FUNCPTR (gst_base_sink_default_query);
550 klass->event = GST_DEBUG_FUNCPTR (gst_base_sink_default_event);
551 klass->wait_event = GST_DEBUG_FUNCPTR (gst_base_sink_default_wait_event);
553 /* Registering debug symbols for function pointers */
554 GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_fixate);
555 GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_pad_activate);
556 GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_pad_activate_mode);
557 GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_event);
558 GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_chain);
559 GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_chain_list);
560 GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_sink_query);
564 gst_base_sink_query_caps (GstBaseSink * bsink, GstPad * pad, GstCaps * filter)
566 GstBaseSinkClass *bclass;
567 GstCaps *caps = NULL;
570 bclass = GST_BASE_SINK_GET_CLASS (bsink);
571 fixed = GST_PAD_IS_FIXED_CAPS (pad);
573 if (fixed || bsink->pad_mode == GST_PAD_MODE_PULL) {
574 /* if we are operating in pull mode or fixed caps, we only accept the
575 * currently negotiated caps */
576 caps = gst_pad_get_current_caps (pad);
579 if (bclass->get_caps)
580 caps = bclass->get_caps (bsink, filter);
583 GstPadTemplate *pad_template;
586 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (bclass),
588 if (pad_template != NULL) {
589 caps = gst_pad_template_get_caps (pad_template);
592 GstCaps *intersection;
595 gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
596 gst_caps_unref (caps);
607 gst_base_sink_default_fixate (GstBaseSink * bsink, GstCaps * caps)
609 GST_DEBUG_OBJECT (bsink, "using default caps fixate function");
610 return gst_caps_fixate (caps);
614 gst_base_sink_fixate (GstBaseSink * bsink, GstCaps * caps)
616 GstBaseSinkClass *bclass;
618 bclass = GST_BASE_SINK_GET_CLASS (bsink);
621 caps = bclass->fixate (bsink, caps);
627 gst_base_sink_init (GstBaseSink * basesink, gpointer g_class)
629 GstPadTemplate *pad_template;
630 GstBaseSinkPrivate *priv;
632 basesink->priv = priv = GST_BASE_SINK_GET_PRIVATE (basesink);
635 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (g_class), "sink");
636 g_return_if_fail (pad_template != NULL);
638 basesink->sinkpad = gst_pad_new_from_template (pad_template, "sink");
640 gst_pad_set_activate_function (basesink->sinkpad, gst_base_sink_pad_activate);
641 gst_pad_set_activatemode_function (basesink->sinkpad,
642 gst_base_sink_pad_activate_mode);
643 gst_pad_set_query_function (basesink->sinkpad, gst_base_sink_sink_query);
644 gst_pad_set_event_function (basesink->sinkpad, gst_base_sink_event);
645 gst_pad_set_chain_function (basesink->sinkpad, gst_base_sink_chain);
646 gst_pad_set_chain_list_function (basesink->sinkpad, gst_base_sink_chain_list);
647 gst_element_add_pad (GST_ELEMENT_CAST (basesink), basesink->sinkpad);
649 basesink->pad_mode = GST_PAD_MODE_NONE;
650 g_mutex_init (&basesink->preroll_lock);
651 g_cond_init (&basesink->preroll_cond);
652 priv->have_latency = FALSE;
654 basesink->can_activate_push = DEFAULT_CAN_ACTIVATE_PUSH;
655 basesink->can_activate_pull = DEFAULT_CAN_ACTIVATE_PULL;
657 basesink->sync = DEFAULT_SYNC;
658 basesink->max_lateness = DEFAULT_MAX_LATENESS;
659 g_atomic_int_set (&priv->qos_enabled, DEFAULT_QOS);
660 priv->async_enabled = DEFAULT_ASYNC;
661 priv->ts_offset = DEFAULT_TS_OFFSET;
662 priv->render_delay = DEFAULT_RENDER_DELAY;
663 priv->blocksize = DEFAULT_BLOCKSIZE;
664 priv->cached_clock_id = NULL;
665 g_atomic_int_set (&priv->enable_last_sample, DEFAULT_ENABLE_LAST_SAMPLE);
666 priv->throttle_time = DEFAULT_THROTTLE_TIME;
667 priv->max_bitrate = DEFAULT_MAX_BITRATE;
669 GST_OBJECT_FLAG_SET (basesink, GST_ELEMENT_FLAG_SINK);
673 gst_base_sink_finalize (GObject * object)
675 GstBaseSink *basesink;
677 basesink = GST_BASE_SINK (object);
679 g_mutex_clear (&basesink->preroll_lock);
680 g_cond_clear (&basesink->preroll_cond);
682 G_OBJECT_CLASS (parent_class)->finalize (object);
686 * gst_base_sink_set_sync:
688 * @sync: the new sync value.
690 * Configures @sink to synchronize on the clock or not. When
691 * @sync is %FALSE, incoming samples will be played as fast as
692 * possible. If @sync is %TRUE, the timestamps of the incoming
693 * buffers will be used to schedule the exact render time of its
697 gst_base_sink_set_sync (GstBaseSink * sink, gboolean sync)
699 g_return_if_fail (GST_IS_BASE_SINK (sink));
701 GST_OBJECT_LOCK (sink);
703 GST_OBJECT_UNLOCK (sink);
707 * gst_base_sink_get_sync:
710 * Checks if @sink is currently configured to synchronize against the
713 * Returns: %TRUE if the sink is configured to synchronize against the clock.
716 gst_base_sink_get_sync (GstBaseSink * sink)
720 g_return_val_if_fail (GST_IS_BASE_SINK (sink), FALSE);
722 GST_OBJECT_LOCK (sink);
724 GST_OBJECT_UNLOCK (sink);
730 * gst_base_sink_set_max_lateness:
732 * @max_lateness: the new max lateness value.
734 * Sets the new max lateness value to @max_lateness. This value is
735 * used to decide if a buffer should be dropped or not based on the
736 * buffer timestamp and the current clock time. A value of -1 means
740 gst_base_sink_set_max_lateness (GstBaseSink * sink, gint64 max_lateness)
742 g_return_if_fail (GST_IS_BASE_SINK (sink));
744 GST_OBJECT_LOCK (sink);
745 sink->max_lateness = max_lateness;
746 GST_OBJECT_UNLOCK (sink);
750 * gst_base_sink_get_max_lateness:
753 * Gets the max lateness value. See gst_base_sink_set_max_lateness for
756 * Returns: The maximum time in nanoseconds that a buffer can be late
757 * before it is dropped and not rendered. A value of -1 means an
761 gst_base_sink_get_max_lateness (GstBaseSink * sink)
765 g_return_val_if_fail (GST_IS_BASE_SINK (sink), -1);
767 GST_OBJECT_LOCK (sink);
768 res = sink->max_lateness;
769 GST_OBJECT_UNLOCK (sink);
775 * gst_base_sink_set_qos_enabled:
777 * @enabled: the new qos value.
779 * Configures @sink to send Quality-of-Service events upstream.
782 gst_base_sink_set_qos_enabled (GstBaseSink * sink, gboolean enabled)
784 g_return_if_fail (GST_IS_BASE_SINK (sink));
786 g_atomic_int_set (&sink->priv->qos_enabled, enabled);
790 * gst_base_sink_is_qos_enabled:
793 * Checks if @sink is currently configured to send Quality-of-Service events
796 * Returns: %TRUE if the sink is configured to perform Quality-of-Service.
799 gst_base_sink_is_qos_enabled (GstBaseSink * sink)
803 g_return_val_if_fail (GST_IS_BASE_SINK (sink), FALSE);
805 res = g_atomic_int_get (&sink->priv->qos_enabled);
811 * gst_base_sink_set_async_enabled:
813 * @enabled: the new async value.
815 * Configures @sink to perform all state changes asynchronously. When async is
816 * disabled, the sink will immediately go to PAUSED instead of waiting for a
817 * preroll buffer. This feature is useful if the sink does not synchronize
818 * against the clock or when it is dealing with sparse streams.
821 gst_base_sink_set_async_enabled (GstBaseSink * sink, gboolean enabled)
823 g_return_if_fail (GST_IS_BASE_SINK (sink));
825 GST_BASE_SINK_PREROLL_LOCK (sink);
826 g_atomic_int_set (&sink->priv->async_enabled, enabled);
827 GST_LOG_OBJECT (sink, "set async enabled to %d", enabled);
828 GST_BASE_SINK_PREROLL_UNLOCK (sink);
832 * gst_base_sink_is_async_enabled:
835 * Checks if @sink is currently configured to perform asynchronous state
838 * Returns: %TRUE if the sink is configured to perform asynchronous state
842 gst_base_sink_is_async_enabled (GstBaseSink * sink)
846 g_return_val_if_fail (GST_IS_BASE_SINK (sink), FALSE);
848 res = g_atomic_int_get (&sink->priv->async_enabled);
854 * gst_base_sink_set_ts_offset:
856 * @offset: the new offset
858 * Adjust the synchronisation of @sink with @offset. A negative value will
859 * render buffers earlier than their timestamp. A positive value will delay
860 * rendering. This function can be used to fix playback of badly timestamped
864 gst_base_sink_set_ts_offset (GstBaseSink * sink, GstClockTimeDiff offset)
866 g_return_if_fail (GST_IS_BASE_SINK (sink));
868 GST_OBJECT_LOCK (sink);
869 sink->priv->ts_offset = offset;
870 GST_LOG_OBJECT (sink, "set time offset to %" G_GINT64_FORMAT, offset);
871 GST_OBJECT_UNLOCK (sink);
875 * gst_base_sink_get_ts_offset:
878 * Get the synchronisation offset of @sink.
880 * Returns: The synchronisation offset.
883 gst_base_sink_get_ts_offset (GstBaseSink * sink)
885 GstClockTimeDiff res;
887 g_return_val_if_fail (GST_IS_BASE_SINK (sink), 0);
889 GST_OBJECT_LOCK (sink);
890 res = sink->priv->ts_offset;
891 GST_OBJECT_UNLOCK (sink);
897 * gst_base_sink_get_last_sample:
900 * Get the last sample that arrived in the sink and was used for preroll or for
901 * rendering. This property can be used to generate thumbnails.
903 * The #GstCaps on the sample can be used to determine the type of the buffer.
905 * Free-function: gst_sample_unref
907 * Returns: (transfer full) (nullable): a #GstSample. gst_sample_unref() after
908 * usage. This function returns %NULL when no buffer has arrived in the
909 * sink yet or when the sink is not in PAUSED or PLAYING.
912 gst_base_sink_get_last_sample (GstBaseSink * sink)
914 GstSample *res = NULL;
916 g_return_val_if_fail (GST_IS_BASE_SINK (sink), NULL);
918 GST_OBJECT_LOCK (sink);
919 if (sink->priv->last_buffer_list) {
920 GstBuffer *first_buffer = NULL;
922 /* Set the first buffer in the list to last sample's buffer */
923 first_buffer = gst_buffer_list_get (sink->priv->last_buffer_list, 0);
925 gst_sample_new (first_buffer, sink->priv->last_caps, &sink->segment,
927 gst_sample_set_buffer_list (res, sink->priv->last_buffer_list);
928 } else if (sink->priv->last_buffer) {
929 res = gst_sample_new (sink->priv->last_buffer,
930 sink->priv->last_caps, &sink->segment, NULL);
932 GST_OBJECT_UNLOCK (sink);
937 /* with OBJECT_LOCK */
939 gst_base_sink_set_last_buffer_unlocked (GstBaseSink * sink, GstBuffer * buffer)
943 old = sink->priv->last_buffer;
944 if (G_LIKELY (old != buffer)) {
945 GST_DEBUG_OBJECT (sink, "setting last buffer to %p", buffer);
946 if (G_LIKELY (buffer))
947 gst_buffer_ref (buffer);
948 sink->priv->last_buffer = buffer;
950 /* copy over the caps */
951 gst_caps_replace (&sink->priv->last_caps, sink->priv->caps);
953 gst_caps_replace (&sink->priv->last_caps, NULL);
957 /* avoid unreffing with the lock because cleanup code might want to take the
959 if (G_LIKELY (old)) {
960 GST_OBJECT_UNLOCK (sink);
961 gst_buffer_unref (old);
962 GST_OBJECT_LOCK (sink);
966 /* with OBJECT_LOCK */
968 gst_base_sink_set_last_buffer_list_unlocked (GstBaseSink * sink,
969 GstBufferList * buffer_list)
973 old = sink->priv->last_buffer_list;
974 if (G_LIKELY (old != buffer_list)) {
975 GST_DEBUG_OBJECT (sink, "setting last buffer list to %p", buffer_list);
976 if (G_LIKELY (buffer_list))
977 gst_mini_object_ref (GST_MINI_OBJECT_CAST (buffer_list));
978 sink->priv->last_buffer_list = buffer_list;
983 /* avoid unreffing with the lock because cleanup code might want to take the
985 if (G_LIKELY (old)) {
986 GST_OBJECT_UNLOCK (sink);
987 gst_mini_object_unref (GST_MINI_OBJECT_CAST (old));
988 GST_OBJECT_LOCK (sink);
993 gst_base_sink_set_last_buffer (GstBaseSink * sink, GstBuffer * buffer)
995 if (!g_atomic_int_get (&sink->priv->enable_last_sample))
998 GST_OBJECT_LOCK (sink);
999 gst_base_sink_set_last_buffer_unlocked (sink, buffer);
1000 GST_OBJECT_UNLOCK (sink);
1004 gst_base_sink_set_last_buffer_list (GstBaseSink * sink,
1005 GstBufferList * buffer_list)
1007 if (!g_atomic_int_get (&sink->priv->enable_last_sample))
1010 GST_OBJECT_LOCK (sink);
1011 gst_base_sink_set_last_buffer_list_unlocked (sink, buffer_list);
1012 GST_OBJECT_UNLOCK (sink);
1016 * gst_base_sink_set_last_sample_enabled:
1018 * @enabled: the new enable-last-sample value.
1020 * Configures @sink to store the last received sample in the last-sample
1024 gst_base_sink_set_last_sample_enabled (GstBaseSink * sink, gboolean enabled)
1026 g_return_if_fail (GST_IS_BASE_SINK (sink));
1028 /* Only take lock if we change the value */
1029 if (g_atomic_int_compare_and_exchange (&sink->priv->enable_last_sample,
1030 !enabled, enabled) && !enabled) {
1031 GST_OBJECT_LOCK (sink);
1032 gst_base_sink_set_last_buffer_unlocked (sink, NULL);
1033 gst_base_sink_set_last_buffer_list_unlocked (sink, NULL);
1034 GST_OBJECT_UNLOCK (sink);
1039 * gst_base_sink_is_last_sample_enabled:
1042 * Checks if @sink is currently configured to store the last received sample in
1043 * the last-sample property.
1045 * Returns: %TRUE if the sink is configured to store the last received sample.
1048 gst_base_sink_is_last_sample_enabled (GstBaseSink * sink)
1050 g_return_val_if_fail (GST_IS_BASE_SINK (sink), FALSE);
1052 return g_atomic_int_get (&sink->priv->enable_last_sample);
1056 * gst_base_sink_get_latency:
1059 * Get the currently configured latency.
1061 * Returns: The configured latency.
1064 gst_base_sink_get_latency (GstBaseSink * sink)
1068 GST_OBJECT_LOCK (sink);
1069 res = sink->priv->latency;
1070 GST_OBJECT_UNLOCK (sink);
1076 * gst_base_sink_query_latency:
1078 * @live: (out) (allow-none): if the sink is live
1079 * @upstream_live: (out) (allow-none): if an upstream element is live
1080 * @min_latency: (out) (allow-none): the min latency of the upstream elements
1081 * @max_latency: (out) (allow-none): the max latency of the upstream elements
1083 * Query the sink for the latency parameters. The latency will be queried from
1084 * the upstream elements. @live will be %TRUE if @sink is configured to
1085 * synchronize against the clock. @upstream_live will be %TRUE if an upstream
1088 * If both @live and @upstream_live are %TRUE, the sink will want to compensate
1089 * for the latency introduced by the upstream elements by setting the
1090 * @min_latency to a strictly positive value.
1092 * This function is mostly used by subclasses.
1094 * Returns: %TRUE if the query succeeded.
1097 gst_base_sink_query_latency (GstBaseSink * sink, gboolean * live,
1098 gboolean * upstream_live, GstClockTime * min_latency,
1099 GstClockTime * max_latency)
1101 gboolean l, us_live, res, have_latency;
1102 GstClockTime min, max, render_delay;
1104 GstClockTime us_min, us_max;
1106 /* we are live when we sync to the clock */
1107 GST_OBJECT_LOCK (sink);
1109 have_latency = sink->priv->have_latency;
1110 render_delay = sink->priv->render_delay;
1111 GST_OBJECT_UNLOCK (sink);
1113 /* assume no latency */
1119 GST_DEBUG_OBJECT (sink, "we are ready for LATENCY query");
1120 /* we are ready for a latency query this is when we preroll or when we are
1122 query = gst_query_new_latency ();
1124 /* ask the peer for the latency */
1125 if ((res = gst_pad_peer_query (sink->sinkpad, query))) {
1126 /* get upstream min and max latency */
1127 gst_query_parse_latency (query, &us_live, &us_min, &us_max);
1130 /* upstream live, use its latency, subclasses should use these
1131 * values to create the complete latency. */
1136 /* we need to add the render delay if we are live */
1137 min += render_delay;
1139 max += render_delay;
1142 gst_query_unref (query);
1144 GST_DEBUG_OBJECT (sink, "we are not yet ready for LATENCY query");
1148 /* not live, we tried to do the query, if it failed we return TRUE anyway */
1152 GST_DEBUG_OBJECT (sink, "latency query failed but we are not live");
1154 GST_DEBUG_OBJECT (sink, "latency query failed and we are live");
1159 GST_DEBUG_OBJECT (sink, "latency query: live: %d, have_latency %d,"
1160 " upstream: %d, min %" GST_TIME_FORMAT ", max %" GST_TIME_FORMAT, l,
1161 have_latency, us_live, GST_TIME_ARGS (min), GST_TIME_ARGS (max));
1166 *upstream_live = us_live;
1176 * gst_base_sink_set_render_delay:
1177 * @sink: a #GstBaseSink
1178 * @delay: the new delay
1180 * Set the render delay in @sink to @delay. The render delay is the time
1181 * between actual rendering of a buffer and its synchronisation time. Some
1182 * devices might delay media rendering which can be compensated for with this
1185 * After calling this function, this sink will report additional latency and
1186 * other sinks will adjust their latency to delay the rendering of their media.
1188 * This function is usually called by subclasses.
1191 gst_base_sink_set_render_delay (GstBaseSink * sink, GstClockTime delay)
1193 GstClockTime old_render_delay;
1195 g_return_if_fail (GST_IS_BASE_SINK (sink));
1197 GST_OBJECT_LOCK (sink);
1198 old_render_delay = sink->priv->render_delay;
1199 sink->priv->render_delay = delay;
1200 GST_LOG_OBJECT (sink, "set render delay to %" GST_TIME_FORMAT,
1201 GST_TIME_ARGS (delay));
1202 GST_OBJECT_UNLOCK (sink);
1204 if (delay != old_render_delay) {
1205 GST_DEBUG_OBJECT (sink, "posting latency changed");
1206 gst_element_post_message (GST_ELEMENT_CAST (sink),
1207 gst_message_new_latency (GST_OBJECT_CAST (sink)));
1212 * gst_base_sink_get_render_delay:
1213 * @sink: a #GstBaseSink
1215 * Get the render delay of @sink. see gst_base_sink_set_render_delay() for more
1216 * information about the render delay.
1218 * Returns: the render delay of @sink.
1221 gst_base_sink_get_render_delay (GstBaseSink * sink)
1223 GstClockTimeDiff res;
1225 g_return_val_if_fail (GST_IS_BASE_SINK (sink), 0);
1227 GST_OBJECT_LOCK (sink);
1228 res = sink->priv->render_delay;
1229 GST_OBJECT_UNLOCK (sink);
1235 * gst_base_sink_set_blocksize:
1236 * @sink: a #GstBaseSink
1237 * @blocksize: the blocksize in bytes
1239 * Set the number of bytes that the sink will pull when it is operating in pull
1242 /* FIXME 2.0: blocksize property should be int, otherwise min>max.. */
1244 gst_base_sink_set_blocksize (GstBaseSink * sink, guint blocksize)
1246 g_return_if_fail (GST_IS_BASE_SINK (sink));
1248 GST_OBJECT_LOCK (sink);
1249 sink->priv->blocksize = blocksize;
1250 GST_LOG_OBJECT (sink, "set blocksize to %u", blocksize);
1251 GST_OBJECT_UNLOCK (sink);
1255 * gst_base_sink_get_blocksize:
1256 * @sink: a #GstBaseSink
1258 * Get the number of bytes that the sink will pull when it is operating in pull
1261 * Returns: the number of bytes @sink will pull in pull mode.
1263 /* FIXME 2.0: blocksize property should be int, otherwise min>max.. */
1265 gst_base_sink_get_blocksize (GstBaseSink * sink)
1269 g_return_val_if_fail (GST_IS_BASE_SINK (sink), 0);
1271 GST_OBJECT_LOCK (sink);
1272 res = sink->priv->blocksize;
1273 GST_OBJECT_UNLOCK (sink);
1279 * gst_base_sink_set_throttle_time:
1280 * @sink: a #GstBaseSink
1281 * @throttle: the throttle time in nanoseconds
1283 * Set the time that will be inserted between rendered buffers. This
1284 * can be used to control the maximum buffers per second that the sink
1288 gst_base_sink_set_throttle_time (GstBaseSink * sink, guint64 throttle)
1290 g_return_if_fail (GST_IS_BASE_SINK (sink));
1292 GST_OBJECT_LOCK (sink);
1293 sink->priv->throttle_time = throttle;
1294 GST_LOG_OBJECT (sink, "set throttle_time to %" G_GUINT64_FORMAT, throttle);
1295 GST_OBJECT_UNLOCK (sink);
1299 * gst_base_sink_get_throttle_time:
1300 * @sink: a #GstBaseSink
1302 * Get the time that will be inserted between frames to control the
1303 * maximum buffers per second.
1305 * Returns: the number of nanoseconds @sink will put between frames.
1308 gst_base_sink_get_throttle_time (GstBaseSink * sink)
1312 g_return_val_if_fail (GST_IS_BASE_SINK (sink), 0);
1314 GST_OBJECT_LOCK (sink);
1315 res = sink->priv->throttle_time;
1316 GST_OBJECT_UNLOCK (sink);
1322 * gst_base_sink_set_max_bitrate:
1323 * @sink: a #GstBaseSink
1324 * @max_bitrate: the max_bitrate in bits per second
1326 * Set the maximum amount of bits per second that the sink will render.
1331 gst_base_sink_set_max_bitrate (GstBaseSink * sink, guint64 max_bitrate)
1333 g_return_if_fail (GST_IS_BASE_SINK (sink));
1335 GST_OBJECT_LOCK (sink);
1336 sink->priv->max_bitrate = max_bitrate;
1337 GST_LOG_OBJECT (sink, "set max_bitrate to %" G_GUINT64_FORMAT, max_bitrate);
1338 GST_OBJECT_UNLOCK (sink);
1342 * gst_base_sink_get_max_bitrate:
1343 * @sink: a #GstBaseSink
1345 * Get the maximum amount of bits per second that the sink will render.
1347 * Returns: the maximum number of bits per second @sink will render.
1352 gst_base_sink_get_max_bitrate (GstBaseSink * sink)
1356 g_return_val_if_fail (GST_IS_BASE_SINK (sink), 0);
1358 GST_OBJECT_LOCK (sink);
1359 res = sink->priv->max_bitrate;
1360 GST_OBJECT_UNLOCK (sink);
1366 gst_base_sink_set_property (GObject * object, guint prop_id,
1367 const GValue * value, GParamSpec * pspec)
1369 GstBaseSink *sink = GST_BASE_SINK (object);
1373 gst_base_sink_set_sync (sink, g_value_get_boolean (value));
1375 case PROP_MAX_LATENESS:
1376 gst_base_sink_set_max_lateness (sink, g_value_get_int64 (value));
1379 gst_base_sink_set_qos_enabled (sink, g_value_get_boolean (value));
1382 gst_base_sink_set_async_enabled (sink, g_value_get_boolean (value));
1384 case PROP_TS_OFFSET:
1385 gst_base_sink_set_ts_offset (sink, g_value_get_int64 (value));
1387 case PROP_BLOCKSIZE:
1388 gst_base_sink_set_blocksize (sink, g_value_get_uint (value));
1390 case PROP_RENDER_DELAY:
1391 gst_base_sink_set_render_delay (sink, g_value_get_uint64 (value));
1393 case PROP_ENABLE_LAST_SAMPLE:
1394 gst_base_sink_set_last_sample_enabled (sink, g_value_get_boolean (value));
1396 case PROP_THROTTLE_TIME:
1397 gst_base_sink_set_throttle_time (sink, g_value_get_uint64 (value));
1399 case PROP_MAX_BITRATE:
1400 gst_base_sink_set_max_bitrate (sink, g_value_get_uint64 (value));
1403 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1409 gst_base_sink_get_property (GObject * object, guint prop_id, GValue * value,
1412 GstBaseSink *sink = GST_BASE_SINK (object);
1416 g_value_set_boolean (value, gst_base_sink_get_sync (sink));
1418 case PROP_MAX_LATENESS:
1419 g_value_set_int64 (value, gst_base_sink_get_max_lateness (sink));
1422 g_value_set_boolean (value, gst_base_sink_is_qos_enabled (sink));
1425 g_value_set_boolean (value, gst_base_sink_is_async_enabled (sink));
1427 case PROP_TS_OFFSET:
1428 g_value_set_int64 (value, gst_base_sink_get_ts_offset (sink));
1430 case PROP_LAST_SAMPLE:
1431 gst_value_take_sample (value, gst_base_sink_get_last_sample (sink));
1433 case PROP_ENABLE_LAST_SAMPLE:
1434 g_value_set_boolean (value, gst_base_sink_is_last_sample_enabled (sink));
1436 case PROP_BLOCKSIZE:
1437 g_value_set_uint (value, gst_base_sink_get_blocksize (sink));
1439 case PROP_RENDER_DELAY:
1440 g_value_set_uint64 (value, gst_base_sink_get_render_delay (sink));
1442 case PROP_THROTTLE_TIME:
1443 g_value_set_uint64 (value, gst_base_sink_get_throttle_time (sink));
1445 case PROP_MAX_BITRATE:
1446 g_value_set_uint64 (value, gst_base_sink_get_max_bitrate (sink));
1449 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1456 gst_base_sink_default_get_caps (GstBaseSink * sink, GstCaps * filter)
1462 gst_base_sink_default_set_caps (GstBaseSink * sink, GstCaps * caps)
1467 /* with PREROLL_LOCK, STREAM_LOCK */
1469 gst_base_sink_commit_state (GstBaseSink * basesink)
1471 /* commit state and proceed to next pending state */
1472 GstState current, next, pending, post_pending;
1473 gboolean post_paused = FALSE;
1474 gboolean post_async_done = FALSE;
1475 gboolean post_playing = FALSE;
1477 /* we are certainly not playing async anymore now */
1478 basesink->playing_async = FALSE;
1480 GST_OBJECT_LOCK (basesink);
1481 current = GST_STATE (basesink);
1482 next = GST_STATE_NEXT (basesink);
1483 pending = GST_STATE_PENDING (basesink);
1484 post_pending = pending;
1487 case GST_STATE_PLAYING:
1489 GST_DEBUG_OBJECT (basesink, "commiting state to PLAYING");
1491 basesink->need_preroll = FALSE;
1492 post_async_done = TRUE;
1493 basesink->priv->commited = TRUE;
1494 post_playing = TRUE;
1495 /* post PAUSED too when we were READY */
1496 if (current == GST_STATE_READY) {
1501 case GST_STATE_PAUSED:
1502 GST_DEBUG_OBJECT (basesink, "commiting state to PAUSED");
1504 post_async_done = TRUE;
1505 basesink->priv->commited = TRUE;
1506 post_pending = GST_STATE_VOID_PENDING;
1508 case GST_STATE_READY:
1509 case GST_STATE_NULL:
1511 case GST_STATE_VOID_PENDING:
1512 goto nothing_pending;
1517 /* we can report latency queries now */
1518 basesink->priv->have_latency = TRUE;
1520 GST_STATE (basesink) = pending;
1521 GST_STATE_NEXT (basesink) = GST_STATE_VOID_PENDING;
1522 GST_STATE_PENDING (basesink) = GST_STATE_VOID_PENDING;
1523 GST_STATE_RETURN (basesink) = GST_STATE_CHANGE_SUCCESS;
1524 GST_OBJECT_UNLOCK (basesink);
1527 GST_DEBUG_OBJECT (basesink, "posting PAUSED state change message");
1528 gst_element_post_message (GST_ELEMENT_CAST (basesink),
1529 gst_message_new_state_changed (GST_OBJECT_CAST (basesink),
1530 current, next, post_pending));
1532 if (post_async_done) {
1533 GST_DEBUG_OBJECT (basesink, "posting async-done message");
1534 gst_element_post_message (GST_ELEMENT_CAST (basesink),
1535 gst_message_new_async_done (GST_OBJECT_CAST (basesink),
1536 GST_CLOCK_TIME_NONE));
1540 GstElementClass *klass;
1542 klass = GST_ELEMENT_GET_CLASS (basesink);
1543 basesink->have_preroll = TRUE;
1544 /* after releasing this lock, the state change function
1545 * can execute concurrently with this thread. There is nothing we do to
1546 * prevent this for now. subclasses should be prepared to handle it. */
1547 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
1549 if (klass->change_state)
1550 klass->change_state (GST_ELEMENT_CAST (basesink),
1551 GST_STATE_CHANGE_PAUSED_TO_PLAYING);
1553 GST_BASE_SINK_PREROLL_LOCK (basesink);
1554 /* state change function could have been executed and we could be
1556 if (G_UNLIKELY (basesink->flushing))
1557 goto stopping_unlocked;
1559 GST_DEBUG_OBJECT (basesink, "posting PLAYING state change message");
1560 /* FIXME, we released the PREROLL lock above, it's possible that this
1561 * message is not correct anymore when the element went back to PAUSED */
1562 gst_element_post_message (GST_ELEMENT_CAST (basesink),
1563 gst_message_new_state_changed (GST_OBJECT_CAST (basesink),
1564 next, pending, GST_STATE_VOID_PENDING));
1567 GST_STATE_BROADCAST (basesink);
1573 /* Depending on the state, set our vars. We get in this situation when the
1574 * state change function got a change to update the state vars before the
1575 * streaming thread did. This is fine but we need to make sure that we
1576 * update the need_preroll var since it was %TRUE when we got here and might
1577 * become %FALSE if we got to PLAYING. */
1578 GST_DEBUG_OBJECT (basesink, "nothing to commit, now in %s",
1579 gst_element_state_get_name (current));
1581 case GST_STATE_PLAYING:
1582 basesink->need_preroll = FALSE;
1584 case GST_STATE_PAUSED:
1585 basesink->need_preroll = TRUE;
1588 basesink->need_preroll = FALSE;
1589 basesink->flushing = TRUE;
1592 /* we can report latency queries now */
1593 basesink->priv->have_latency = TRUE;
1594 GST_OBJECT_UNLOCK (basesink);
1599 GST_OBJECT_LOCK (basesink);
1604 /* app is going to READY */
1605 GST_DEBUG_OBJECT (basesink, "stopping");
1606 basesink->need_preroll = FALSE;
1607 basesink->flushing = TRUE;
1608 GST_OBJECT_UNLOCK (basesink);
1614 start_stepping (GstBaseSink * sink, GstSegment * segment,
1615 GstStepInfo * pending, GstStepInfo * current)
1618 GstMessage *message;
1620 GST_DEBUG_OBJECT (sink, "update pending step");
1622 GST_OBJECT_LOCK (sink);
1623 memcpy (current, pending, sizeof (GstStepInfo));
1624 pending->valid = FALSE;
1625 GST_OBJECT_UNLOCK (sink);
1627 /* post message first */
1629 gst_message_new_step_start (GST_OBJECT (sink), TRUE, current->format,
1630 current->amount, current->rate, current->flush, current->intermediate);
1631 gst_message_set_seqnum (message, current->seqnum);
1632 gst_element_post_message (GST_ELEMENT (sink), message);
1634 /* get the running time of where we paused and remember it */
1635 current->start = gst_element_get_start_time (GST_ELEMENT_CAST (sink));
1636 gst_segment_set_running_time (segment, GST_FORMAT_TIME, current->start);
1638 /* set the new rate for the remainder of the segment */
1639 current->start_rate = segment->rate;
1640 segment->rate *= current->rate;
1643 if (segment->rate > 0.0)
1644 current->start_stop = segment->stop;
1646 current->start_start = segment->start;
1648 if (current->format == GST_FORMAT_TIME) {
1649 /* calculate the running-time when the step operation should stop */
1650 if (current->amount != -1)
1651 end = current->start + current->amount;
1655 if (!current->flush) {
1658 /* update the segment clipping regions for non-flushing seeks */
1659 if (segment->rate > 0.0) {
1662 gst_segment_position_from_running_time (segment, GST_FORMAT_TIME,
1665 position = segment->stop;
1667 segment->stop = position;
1668 segment->position = position;
1672 gst_segment_position_from_running_time (segment, GST_FORMAT_TIME,
1675 position = segment->start;
1677 segment->time = position;
1678 segment->start = position;
1679 segment->position = position;
1684 GST_DEBUG_OBJECT (sink, "segment now %" GST_SEGMENT_FORMAT, segment);
1685 GST_DEBUG_OBJECT (sink, "step started at running_time %" GST_TIME_FORMAT,
1686 GST_TIME_ARGS (current->start));
1688 GST_DEBUG_OBJECT (sink, "step amount: %" G_GUINT64_FORMAT ", format: %s, "
1689 "rate: %f", current->amount, gst_format_get_name (current->format),
1694 stop_stepping (GstBaseSink * sink, GstSegment * segment,
1695 GstStepInfo * current, gint64 rstart, gint64 rstop, gboolean eos)
1697 gint64 stop, position;
1698 GstMessage *message;
1700 GST_DEBUG_OBJECT (sink, "step complete");
1702 if (segment->rate > 0.0)
1707 GST_DEBUG_OBJECT (sink,
1708 "step stop at running_time %" GST_TIME_FORMAT, GST_TIME_ARGS (stop));
1711 current->duration = current->position;
1713 current->duration = stop - current->start;
1715 GST_DEBUG_OBJECT (sink, "step elapsed running_time %" GST_TIME_FORMAT,
1716 GST_TIME_ARGS (current->duration));
1718 position = current->start + current->duration;
1720 /* now move the segment to the new running time */
1721 gst_segment_set_running_time (segment, GST_FORMAT_TIME, position);
1723 if (current->flush) {
1724 /* and remove the time we flushed, start time did not change */
1725 segment->base = current->start;
1727 /* start time is now the stepped position */
1728 gst_element_set_start_time (GST_ELEMENT_CAST (sink), position);
1731 /* restore the previous rate */
1732 segment->rate = current->start_rate;
1734 if (segment->rate > 0.0)
1735 segment->stop = current->start_stop;
1737 segment->start = current->start_start;
1739 /* post the step done when we know the stepped duration in TIME */
1741 gst_message_new_step_done (GST_OBJECT_CAST (sink), current->format,
1742 current->amount, current->rate, current->flush, current->intermediate,
1743 current->duration, eos);
1744 gst_message_set_seqnum (message, current->seqnum);
1745 gst_element_post_message (GST_ELEMENT_CAST (sink), message);
1747 if (!current->intermediate)
1748 sink->need_preroll = current->need_preroll;
1750 /* and the current step info finished and becomes invalid */
1751 current->valid = FALSE;
1755 handle_stepping (GstBaseSink * sink, GstSegment * segment,
1756 GstStepInfo * current, guint64 * cstart, guint64 * cstop, guint64 * rstart,
1759 gboolean step_end = FALSE;
1761 /* stepping never stops */
1762 if (current->amount == -1)
1765 /* see if we need to skip this buffer because of stepping */
1766 switch (current->format) {
1767 case GST_FORMAT_TIME:
1770 guint64 first, last;
1773 if (segment->rate > 0.0) {
1774 if (segment->stop == *cstop)
1775 *rstop = *rstart + current->amount;
1780 if (segment->start == *cstart)
1781 *rstart = *rstop + current->amount;
1787 end = current->start + current->amount;
1788 current->position = first - current->start;
1790 abs_rate = ABS (segment->rate);
1791 if (G_UNLIKELY (abs_rate != 1.0))
1792 current->position /= abs_rate;
1794 GST_DEBUG_OBJECT (sink,
1795 "buffer: %" GST_TIME_FORMAT "-%" GST_TIME_FORMAT,
1796 GST_TIME_ARGS (first), GST_TIME_ARGS (last));
1797 GST_DEBUG_OBJECT (sink,
1798 "got time step %" GST_TIME_FORMAT "-%" GST_TIME_FORMAT "/%"
1799 GST_TIME_FORMAT, GST_TIME_ARGS (current->position),
1800 GST_TIME_ARGS (last - current->start),
1801 GST_TIME_ARGS (current->amount));
1803 if ((current->flush && current->position >= current->amount)
1805 GST_DEBUG_OBJECT (sink, "step ended, we need clipping");
1807 if (segment->rate > 0.0) {
1810 gst_segment_position_from_running_time (segment, GST_FORMAT_TIME,
1815 gst_segment_position_from_running_time (segment, GST_FORMAT_TIME,
1819 GST_DEBUG_OBJECT (sink,
1820 "cstart %" GST_TIME_FORMAT ", rstart %" GST_TIME_FORMAT,
1821 GST_TIME_ARGS (*cstart), GST_TIME_ARGS (*rstart));
1822 GST_DEBUG_OBJECT (sink,
1823 "cstop %" GST_TIME_FORMAT ", rstop %" GST_TIME_FORMAT,
1824 GST_TIME_ARGS (*cstop), GST_TIME_ARGS (*rstop));
1827 case GST_FORMAT_BUFFERS:
1828 GST_DEBUG_OBJECT (sink,
1829 "got default step %" G_GUINT64_FORMAT "/%" G_GUINT64_FORMAT,
1830 current->position, current->amount);
1832 if (current->position < current->amount) {
1833 current->position++;
1838 case GST_FORMAT_DEFAULT:
1840 GST_DEBUG_OBJECT (sink,
1841 "got unknown step %" G_GUINT64_FORMAT "/%" G_GUINT64_FORMAT,
1842 current->position, current->amount);
1848 /* with STREAM_LOCK, PREROLL_LOCK
1850 * Returns %TRUE if the object needs synchronisation and takes therefore
1851 * part in prerolling.
1853 * rsstart/rsstop contain the start/stop in stream time.
1854 * rrstart/rrstop contain the start/stop in running time.
1857 gst_base_sink_get_sync_times (GstBaseSink * basesink, GstMiniObject * obj,
1858 GstClockTime * rsstart, GstClockTime * rsstop,
1859 GstClockTime * rrstart, GstClockTime * rrstop, GstClockTime * rrnext,
1860 gboolean * do_sync, gboolean * stepped, GstStepInfo * step,
1861 gboolean * step_end)
1863 GstBaseSinkClass *bclass;
1864 GstClockTime start, stop; /* raw start/stop timestamps */
1865 guint64 cstart, cstop; /* clipped raw timestamps */
1866 guint64 rstart, rstop, rnext; /* clipped timestamps converted to running time */
1867 GstClockTime sstart, sstop; /* clipped timestamps converted to stream time */
1869 GstBaseSinkPrivate *priv;
1870 GstSegment *segment;
1873 priv = basesink->priv;
1874 segment = &basesink->segment;
1876 bclass = GST_BASE_SINK_GET_CLASS (basesink);
1879 /* start with nothing */
1880 start = stop = GST_CLOCK_TIME_NONE;
1883 if (G_UNLIKELY (GST_IS_EVENT (obj))) {
1884 GstEvent *event = GST_EVENT_CAST (obj);
1886 switch (GST_EVENT_TYPE (event)) {
1887 /* EOS event needs syncing */
1890 if (segment->rate >= 0.0) {
1891 sstart = sstop = priv->current_sstop;
1892 if (!GST_CLOCK_TIME_IS_VALID (sstart)) {
1893 /* we have not seen a buffer yet, use the segment values */
1894 sstart = sstop = gst_segment_to_stream_time (segment,
1895 segment->format, segment->stop);
1898 sstart = sstop = priv->current_sstart;
1899 if (!GST_CLOCK_TIME_IS_VALID (sstart)) {
1900 /* we have not seen a buffer yet, use the segment values */
1901 sstart = sstop = gst_segment_to_stream_time (segment,
1902 segment->format, segment->start);
1906 rstart = rstop = rnext = priv->eos_rtime;
1907 *do_sync = rstart != -1;
1908 GST_DEBUG_OBJECT (basesink, "sync times for EOS %" GST_TIME_FORMAT,
1909 GST_TIME_ARGS (rstart));
1910 /* if we are stepping, we end now */
1911 *step_end = step->valid;
1917 GstClockTime timestamp, duration;
1918 gst_event_parse_gap (event, ×tamp, &duration);
1920 GST_DEBUG_OBJECT (basesink, "Got Gap time %" GST_TIME_FORMAT
1921 " duration %" GST_TIME_FORMAT,
1922 GST_TIME_ARGS (timestamp), GST_TIME_ARGS (duration));
1924 if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
1926 if (GST_CLOCK_TIME_IS_VALID (duration))
1927 stop = start + duration;
1933 /* other events do not need syncing */
1937 /* else do buffer sync code */
1938 GstBuffer *buffer = GST_BUFFER_CAST (obj);
1940 /* just get the times to see if we need syncing, if the start returns -1 we
1942 if (bclass->get_times)
1943 bclass->get_times (basesink, buffer, &start, &stop);
1945 if (!GST_CLOCK_TIME_IS_VALID (start)) {
1946 /* we don't need to sync but we still want to get the timestamps for
1947 * tracking the position */
1948 gst_base_sink_default_get_times (basesink, buffer, &start, &stop);
1955 GST_DEBUG_OBJECT (basesink, "got times start: %" GST_TIME_FORMAT
1956 ", stop: %" GST_TIME_FORMAT ", do_sync %d", GST_TIME_ARGS (start),
1957 GST_TIME_ARGS (stop), *do_sync);
1959 /* collect segment and format for code clarity */
1960 format = segment->format;
1963 if (G_UNLIKELY (!gst_segment_clip (segment, format,
1964 start, stop, &cstart, &cstop))) {
1966 GST_DEBUG_OBJECT (basesink, "step out of segment");
1967 /* when we are stepping, pretend we're at the end of the segment */
1968 if (segment->rate > 0.0) {
1969 cstart = segment->stop;
1970 cstop = segment->stop;
1972 cstart = segment->start;
1973 cstop = segment->start;
1977 goto out_of_segment;
1980 if (G_UNLIKELY (start != cstart || stop != cstop)) {
1981 GST_DEBUG_OBJECT (basesink, "clipped to: start %" GST_TIME_FORMAT
1982 ", stop: %" GST_TIME_FORMAT, GST_TIME_ARGS (cstart),
1983 GST_TIME_ARGS (cstop));
1986 /* set last stop position */
1987 if (G_LIKELY (stop != GST_CLOCK_TIME_NONE && cstop != GST_CLOCK_TIME_NONE))
1988 segment->position = cstop;
1990 segment->position = cstart;
1993 rstart = gst_segment_to_running_time (segment, format, cstart);
1994 rstop = gst_segment_to_running_time (segment, format, cstop);
1996 if (GST_CLOCK_TIME_IS_VALID (stop))
2001 if (G_UNLIKELY (step->valid)) {
2002 if (!(*step_end = handle_stepping (basesink, segment, step, &cstart, &cstop,
2003 &rstart, &rstop))) {
2004 /* step is still busy, we discard data when we are flushing */
2005 *stepped = step->flush;
2006 GST_DEBUG_OBJECT (basesink, "stepping busy");
2009 /* this can produce wrong values if we accumulated non-TIME segments. If this happens,
2010 * upstream is behaving very badly */
2011 sstart = gst_segment_to_stream_time (segment, format, cstart);
2012 sstop = gst_segment_to_stream_time (segment, format, cstop);
2015 /* eos_done label only called when doing EOS, we also stop stepping then */
2016 if (*step_end && step->flush) {
2017 GST_DEBUG_OBJECT (basesink, "flushing step ended");
2018 stop_stepping (basesink, segment, step, rstart, rstop, eos);
2020 /* re-determine running start times for adjusted segment
2021 * (which has a flushed amount of running/accumulated time removed) */
2022 if (!GST_IS_EVENT (obj)) {
2023 GST_DEBUG_OBJECT (basesink, "refresh sync times");
2035 /* buffers and EOS always need syncing and preroll */
2041 /* we usually clip in the chain function already but stepping could cause
2042 * the segment to be updated later. we return %FALSE so that we don't try
2044 GST_LOG_OBJECT (basesink, "buffer skipped, not in segment");
2049 /* with STREAM_LOCK, PREROLL_LOCK, LOCK
2050 * adjust a timestamp with the latency and timestamp offset. This function does
2051 * not adjust for the render delay. */
2053 gst_base_sink_adjust_time (GstBaseSink * basesink, GstClockTime time)
2055 GstClockTimeDiff ts_offset;
2057 /* don't do anything funny with invalid timestamps */
2058 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (time)))
2061 time += basesink->priv->latency;
2063 /* apply offset, be careful for underflows */
2064 ts_offset = basesink->priv->ts_offset;
2065 if (ts_offset < 0) {
2066 ts_offset = -ts_offset;
2067 if (ts_offset < time)
2074 /* subtract the render delay again, which was included in the latency */
2075 if (time > basesink->priv->render_delay)
2076 time -= basesink->priv->render_delay;
2084 * gst_base_sink_wait_clock:
2086 * @time: the running_time to be reached
2087 * @jitter: (out) (allow-none): the jitter to be filled with time diff, or %NULL
2089 * This function will block until @time is reached. It is usually called by
2090 * subclasses that use their own internal synchronisation.
2092 * If @time is not valid, no synchronisation is done and %GST_CLOCK_BADTIME is
2093 * returned. Likewise, if synchronisation is disabled in the element or there
2094 * is no clock, no synchronisation is done and %GST_CLOCK_BADTIME is returned.
2096 * This function should only be called with the PREROLL_LOCK held, like when
2097 * receiving an EOS event in the #GstBaseSinkClass.event() vmethod or when
2098 * receiving a buffer in
2099 * the #GstBaseSinkClass.render() vmethod.
2101 * The @time argument should be the running_time of when this method should
2102 * return and is not adjusted with any latency or offset configured in the
2105 * Returns: #GstClockReturn
2108 gst_base_sink_wait_clock (GstBaseSink * sink, GstClockTime time,
2109 GstClockTimeDiff * jitter)
2113 GstClockTime base_time;
2115 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (time)))
2118 GST_OBJECT_LOCK (sink);
2119 if (G_UNLIKELY (!sink->sync))
2122 if (G_UNLIKELY ((clock = GST_ELEMENT_CLOCK (sink)) == NULL))
2125 base_time = GST_ELEMENT_CAST (sink)->base_time;
2126 GST_LOG_OBJECT (sink,
2127 "time %" GST_TIME_FORMAT ", base_time %" GST_TIME_FORMAT,
2128 GST_TIME_ARGS (time), GST_TIME_ARGS (base_time));
2130 /* add base_time to running_time to get the time against the clock */
2133 /* Re-use existing clockid if available */
2134 /* FIXME: Casting to GstClockEntry only works because the types
2136 if (G_LIKELY (sink->priv->cached_clock_id != NULL
2137 && GST_CLOCK_ENTRY_CLOCK ((GstClockEntry *) sink->
2138 priv->cached_clock_id) == clock)) {
2139 if (!gst_clock_single_shot_id_reinit (clock, sink->priv->cached_clock_id,
2141 gst_clock_id_unref (sink->priv->cached_clock_id);
2142 sink->priv->cached_clock_id = gst_clock_new_single_shot_id (clock, time);
2145 if (sink->priv->cached_clock_id != NULL)
2146 gst_clock_id_unref (sink->priv->cached_clock_id);
2147 sink->priv->cached_clock_id = gst_clock_new_single_shot_id (clock, time);
2149 GST_OBJECT_UNLOCK (sink);
2151 /* A blocking wait is performed on the clock. We save the ClockID
2152 * so we can unlock the entry at any time. While we are blocking, we
2153 * release the PREROLL_LOCK so that other threads can interrupt the
2155 sink->clock_id = sink->priv->cached_clock_id;
2156 /* release the preroll lock while waiting */
2157 GST_BASE_SINK_PREROLL_UNLOCK (sink);
2159 ret = gst_clock_id_wait (sink->priv->cached_clock_id, jitter);
2161 GST_BASE_SINK_PREROLL_LOCK (sink);
2162 sink->clock_id = NULL;
2166 /* no syncing needed */
2169 GST_DEBUG_OBJECT (sink, "time not valid, no sync needed");
2170 return GST_CLOCK_BADTIME;
2174 GST_DEBUG_OBJECT (sink, "sync disabled");
2175 GST_OBJECT_UNLOCK (sink);
2176 return GST_CLOCK_BADTIME;
2180 GST_DEBUG_OBJECT (sink, "no clock, can't sync");
2181 GST_OBJECT_UNLOCK (sink);
2182 return GST_CLOCK_BADTIME;
2187 * gst_base_sink_wait_preroll:
2190 * If the #GstBaseSinkClass.render() method performs its own synchronisation
2191 * against the clock it must unblock when going from PLAYING to the PAUSED state
2192 * and call this method before continuing to render the remaining data.
2194 * This function will block until a state change to PLAYING happens (in which
2195 * case this function returns %GST_FLOW_OK) or the processing must be stopped due
2196 * to a state change to READY or a FLUSH event (in which case this function
2197 * returns %GST_FLOW_FLUSHING).
2199 * This function should only be called with the PREROLL_LOCK held, like in the
2202 * Returns: %GST_FLOW_OK if the preroll completed and processing can
2203 * continue. Any other return value should be returned from the render vmethod.
2206 gst_base_sink_wait_preroll (GstBaseSink * sink)
2208 sink->have_preroll = TRUE;
2209 GST_DEBUG_OBJECT (sink, "waiting in preroll for flush or PLAYING");
2210 /* block until the state changes, or we get a flush, or something */
2211 GST_BASE_SINK_PREROLL_WAIT (sink);
2212 sink->have_preroll = FALSE;
2213 if (G_UNLIKELY (sink->flushing))
2215 if (G_UNLIKELY (sink->priv->step_unlock))
2217 GST_DEBUG_OBJECT (sink, "continue after preroll");
2224 GST_DEBUG_OBJECT (sink, "preroll interrupted because of flush");
2225 return GST_FLOW_FLUSHING;
2229 sink->priv->step_unlock = FALSE;
2230 GST_DEBUG_OBJECT (sink, "preroll interrupted because of step");
2231 return GST_FLOW_STEP;
2236 * gst_base_sink_do_preroll:
2238 * @obj: (transfer none): the mini object that caused the preroll
2240 * If the @sink spawns its own thread for pulling buffers from upstream it
2241 * should call this method after it has pulled a buffer. If the element needed
2242 * to preroll, this function will perform the preroll and will then block
2243 * until the element state is changed.
2245 * This function should be called with the PREROLL_LOCK held.
2247 * Returns: %GST_FLOW_OK if the preroll completed and processing can
2248 * continue. Any other return value should be returned from the render vmethod.
2251 gst_base_sink_do_preroll (GstBaseSink * sink, GstMiniObject * obj)
2255 while (G_UNLIKELY (sink->need_preroll)) {
2256 GST_DEBUG_OBJECT (sink, "prerolling object %p", obj);
2258 /* if it's a buffer, we need to call the preroll method */
2259 if (sink->priv->call_preroll) {
2260 GstBaseSinkClass *bclass;
2263 if (GST_IS_BUFFER_LIST (obj)) {
2264 buf = gst_buffer_list_get (GST_BUFFER_LIST_CAST (obj), 0);
2265 gst_base_sink_set_last_buffer (sink, buf);
2266 gst_base_sink_set_last_buffer_list (sink, GST_BUFFER_LIST_CAST (obj));
2267 g_assert (NULL != buf);
2268 } else if (GST_IS_BUFFER (obj)) {
2269 buf = GST_BUFFER_CAST (obj);
2270 /* For buffer lists do not set last buffer for now */
2271 gst_base_sink_set_last_buffer (sink, buf);
2272 gst_base_sink_set_last_buffer_list (sink, NULL);
2278 GST_DEBUG_OBJECT (sink, "preroll buffer %" GST_TIME_FORMAT,
2279 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)));
2281 bclass = GST_BASE_SINK_GET_CLASS (sink);
2283 if (bclass->prepare)
2284 if ((ret = bclass->prepare (sink, buf)) != GST_FLOW_OK)
2285 goto prepare_canceled;
2287 if (bclass->preroll)
2288 if ((ret = bclass->preroll (sink, buf)) != GST_FLOW_OK)
2289 goto preroll_canceled;
2291 sink->priv->call_preroll = FALSE;
2296 if (G_LIKELY (sink->playing_async)) {
2297 if (G_UNLIKELY (!gst_base_sink_commit_state (sink)))
2301 /* need to recheck here because the commit state could have
2302 * made us not need the preroll anymore */
2303 if (G_LIKELY (sink->need_preroll)) {
2304 /* block until the state changes, or we get a flush, or something */
2305 ret = gst_base_sink_wait_preroll (sink);
2306 if ((ret != GST_FLOW_OK) && (ret != GST_FLOW_STEP))
2307 goto preroll_failed;
2315 GST_DEBUG_OBJECT (sink, "prepare failed, abort state");
2316 gst_element_abort_state (GST_ELEMENT_CAST (sink));
2321 GST_DEBUG_OBJECT (sink, "preroll failed, abort state");
2322 gst_element_abort_state (GST_ELEMENT_CAST (sink));
2327 GST_DEBUG_OBJECT (sink, "stopping while commiting state");
2328 return GST_FLOW_FLUSHING;
2332 GST_DEBUG_OBJECT (sink, "preroll failed: %s", gst_flow_get_name (ret));
2338 * gst_base_sink_wait:
2340 * @time: the running_time to be reached
2341 * @jitter: (out) (allow-none): the jitter to be filled with time diff, or %NULL
2343 * This function will wait for preroll to complete and will then block until @time
2344 * is reached. It is usually called by subclasses that use their own internal
2345 * synchronisation but want to let some synchronization (like EOS) be handled
2346 * by the base class.
2348 * This function should only be called with the PREROLL_LOCK held (like when
2349 * receiving an EOS event in the ::event vmethod or when handling buffers in
2352 * The @time argument should be the running_time of when the timeout should happen
2353 * and will be adjusted with any latency and offset configured in the sink.
2355 * Returns: #GstFlowReturn
2358 gst_base_sink_wait (GstBaseSink * sink, GstClockTime time,
2359 GstClockTimeDiff * jitter)
2361 GstClockReturn status;
2367 GST_DEBUG_OBJECT (sink, "checking preroll");
2369 /* first wait for the playing state before we can continue */
2370 while (G_UNLIKELY (sink->need_preroll)) {
2371 ret = gst_base_sink_wait_preroll (sink);
2372 if ((ret != GST_FLOW_OK) && (ret != GST_FLOW_STEP))
2376 /* preroll done, we can sync since we are in PLAYING now. */
2377 GST_DEBUG_OBJECT (sink, "possibly waiting for clock to reach %"
2378 GST_TIME_FORMAT, GST_TIME_ARGS (time));
2380 /* compensate for latency, ts_offset and render delay */
2381 stime = gst_base_sink_adjust_time (sink, time);
2383 /* wait for the clock, this can be interrupted because we got shut down or
2385 status = gst_base_sink_wait_clock (sink, stime, jitter);
2387 GST_DEBUG_OBJECT (sink, "clock returned %d", status);
2389 /* invalid time, no clock or sync disabled, just continue then */
2390 if (status == GST_CLOCK_BADTIME)
2393 /* waiting could have been interrupted and we can be flushing now */
2394 if (G_UNLIKELY (sink->flushing))
2397 /* retry if we got unscheduled, which means we did not reach the timeout
2398 * yet. if some other error occures, we continue. */
2399 } while (status == GST_CLOCK_UNSCHEDULED);
2401 GST_DEBUG_OBJECT (sink, "end of stream");
2408 GST_DEBUG_OBJECT (sink, "we are flushing");
2409 return GST_FLOW_FLUSHING;
2413 /* with STREAM_LOCK, PREROLL_LOCK
2415 * Make sure we are in PLAYING and synchronize an object to the clock.
2417 * If we need preroll, we are not in PLAYING. We try to commit the state
2418 * if needed and then block if we still are not PLAYING.
2420 * We start waiting on the clock in PLAYING. If we got interrupted, we
2421 * immediately try to re-preroll.
2423 * Some objects do not need synchronisation (most events) and so this function
2424 * immediately returns GST_FLOW_OK.
2426 * for objects that arrive later than max-lateness to be synchronized to the
2427 * clock have the @late boolean set to %TRUE.
2429 * This function keeps a running average of the jitter (the diff between the
2430 * clock time and the requested sync time). The jitter is negative for
2431 * objects that arrive in time and positive for late buffers.
2433 * does not take ownership of obj.
2435 static GstFlowReturn
2436 gst_base_sink_do_sync (GstBaseSink * basesink,
2437 GstMiniObject * obj, gboolean * late, gboolean * step_end)
2439 GstClockTimeDiff jitter = 0;
2441 GstClockReturn status = GST_CLOCK_OK;
2442 GstClockTime rstart, rstop, rnext, sstart, sstop, stime;
2444 GstBaseSinkPrivate *priv;
2446 GstStepInfo *current, *pending;
2449 priv = basesink->priv;
2452 sstart = sstop = rstart = rstop = rnext = GST_CLOCK_TIME_NONE;
2456 priv->current_rstart = GST_CLOCK_TIME_NONE;
2458 /* get stepping info */
2459 current = &priv->current_step;
2460 pending = &priv->pending_step;
2462 /* get timing information for this object against the render segment */
2463 syncable = gst_base_sink_get_sync_times (basesink, obj,
2464 &sstart, &sstop, &rstart, &rstop, &rnext, &do_sync, &stepped, current,
2467 if (G_UNLIKELY (stepped))
2470 /* a syncable object needs to participate in preroll and
2471 * clocking. All buffers and EOS are syncable. */
2472 if (G_UNLIKELY (!syncable))
2475 /* store timing info for current object */
2476 priv->current_rstart = rstart;
2477 priv->current_rstop = (GST_CLOCK_TIME_IS_VALID (rstop) ? rstop : rstart);
2479 /* save sync time for eos when the previous object needed sync */
2480 priv->eos_rtime = (do_sync ? rnext : GST_CLOCK_TIME_NONE);
2482 /* calculate inter frame spacing */
2483 if (G_UNLIKELY (priv->prev_rstart != -1 && priv->prev_rstart < rstart)) {
2484 GstClockTime in_diff;
2486 in_diff = rstart - priv->prev_rstart;
2488 if (priv->avg_in_diff == -1)
2489 priv->avg_in_diff = in_diff;
2491 priv->avg_in_diff = UPDATE_RUNNING_AVG (priv->avg_in_diff, in_diff);
2493 GST_LOG_OBJECT (basesink, "avg frame diff %" GST_TIME_FORMAT,
2494 GST_TIME_ARGS (priv->avg_in_diff));
2497 priv->prev_rstart = rstart;
2499 if (G_UNLIKELY (priv->earliest_in_time != -1
2500 && rstart < priv->earliest_in_time))
2504 /* first do preroll, this makes sure we commit our state
2505 * to PAUSED and can continue to PLAYING. We cannot perform
2506 * any clock sync in PAUSED because there is no clock. */
2507 ret = gst_base_sink_do_preroll (basesink, obj);
2508 if (G_UNLIKELY (ret != GST_FLOW_OK))
2509 goto preroll_failed;
2511 /* update the segment with a pending step if the current one is invalid and we
2512 * have a new pending one. We only accept new step updates after a preroll */
2513 if (G_UNLIKELY (pending->valid && !current->valid)) {
2514 start_stepping (basesink, &basesink->segment, pending, current);
2518 /* After rendering we store the position of the last buffer so that we can use
2519 * it to report the position. We need to take the lock here. */
2520 GST_OBJECT_LOCK (basesink);
2521 priv->current_sstart = sstart;
2522 priv->current_sstop = (GST_CLOCK_TIME_IS_VALID (sstop) ? sstop : sstart);
2523 GST_OBJECT_UNLOCK (basesink);
2528 /* adjust for latency */
2529 stime = gst_base_sink_adjust_time (basesink, rstart);
2531 /* adjust for rate control */
2532 if (priv->rc_next == -1 || (stime != -1 && stime >= priv->rc_next)) {
2533 GST_DEBUG_OBJECT (basesink, "reset rc_time to time %" GST_TIME_FORMAT,
2534 GST_TIME_ARGS (stime));
2535 priv->rc_time = stime;
2536 priv->rc_accumulated = 0;
2538 GST_DEBUG_OBJECT (basesink, "rate control next %" GST_TIME_FORMAT,
2539 GST_TIME_ARGS (priv->rc_next));
2540 stime = priv->rc_next;
2543 /* preroll done, we can sync since we are in PLAYING now. */
2544 GST_DEBUG_OBJECT (basesink, "possibly waiting for clock to reach %"
2545 GST_TIME_FORMAT ", adjusted %" GST_TIME_FORMAT,
2546 GST_TIME_ARGS (rstart), GST_TIME_ARGS (stime));
2548 /* This function will return immediately if start == -1, no clock
2549 * or sync is disabled with GST_CLOCK_BADTIME. */
2550 status = gst_base_sink_wait_clock (basesink, stime, &jitter);
2552 GST_DEBUG_OBJECT (basesink, "clock returned %d, jitter %c%" GST_TIME_FORMAT,
2553 status, (jitter < 0 ? '-' : ' '), GST_TIME_ARGS (ABS (jitter)));
2555 /* invalid time, no clock or sync disabled, just render */
2556 if (status == GST_CLOCK_BADTIME)
2559 /* waiting could have been interrupted and we can be flushing now */
2560 if (G_UNLIKELY (basesink->flushing))
2563 /* check for unlocked by a state change, we are not flushing so
2564 * we can try to preroll on the current buffer. */
2565 if (G_UNLIKELY (status == GST_CLOCK_UNSCHEDULED)) {
2566 GST_DEBUG_OBJECT (basesink, "unscheduled, waiting some more");
2567 priv->call_preroll = TRUE;
2571 /* successful syncing done, record observation */
2572 priv->current_jitter = jitter;
2574 /* check if the object should be dropped */
2575 *late = gst_base_sink_is_too_late (basesink, obj, rstart, rstop,
2576 status, jitter, TRUE);
2584 GST_DEBUG_OBJECT (basesink, "skipped stepped object %p", obj);
2590 GST_DEBUG_OBJECT (basesink, "non syncable object %p", obj);
2595 GST_DEBUG_OBJECT (basesink, "dropped because of QoS %p", obj);
2601 GST_DEBUG_OBJECT (basesink, "we are flushing");
2602 return GST_FLOW_FLUSHING;
2606 GST_DEBUG_OBJECT (basesink, "preroll failed");
2613 gst_base_sink_send_qos (GstBaseSink * basesink, GstQOSType type,
2614 gdouble proportion, GstClockTime time, GstClockTimeDiff diff)
2619 /* generate Quality-of-Service event */
2620 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, basesink,
2621 "qos: type %d, proportion: %lf, diff %" G_GINT64_FORMAT ", timestamp %"
2622 GST_TIME_FORMAT, type, proportion, diff, GST_TIME_ARGS (time));
2624 event = gst_event_new_qos (type, proportion, diff, time);
2627 res = gst_pad_push_event (basesink->sinkpad, event);
2633 gst_base_sink_perform_qos (GstBaseSink * sink, gboolean dropped)
2635 GstBaseSinkPrivate *priv;
2636 GstClockTime start, stop;
2637 GstClockTimeDiff jitter;
2638 GstClockTime pt, entered, left;
2639 GstClockTime duration;
2644 start = priv->current_rstart;
2646 if (priv->current_step.valid)
2649 /* if Quality-of-Service disabled, do nothing */
2650 if (!g_atomic_int_get (&priv->qos_enabled) ||
2651 !GST_CLOCK_TIME_IS_VALID (start))
2654 stop = priv->current_rstop;
2655 jitter = priv->current_jitter;
2658 /* this is the time the buffer entered the sink */
2659 if (start < -jitter)
2662 entered = start + jitter;
2665 /* this is the time the buffer entered the sink */
2666 entered = start + jitter;
2667 /* this is the time the buffer left the sink */
2668 left = start + jitter;
2671 /* calculate duration of the buffer */
2672 if (GST_CLOCK_TIME_IS_VALID (stop) && stop != start)
2673 duration = stop - start;
2675 duration = priv->avg_in_diff;
2677 /* if we have the time when the last buffer left us, calculate
2678 * processing time */
2679 if (GST_CLOCK_TIME_IS_VALID (priv->last_left)) {
2680 if (entered > priv->last_left) {
2681 pt = entered - priv->last_left;
2689 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, sink, "start: %" GST_TIME_FORMAT
2690 ", stop %" GST_TIME_FORMAT ", entered %" GST_TIME_FORMAT ", left %"
2691 GST_TIME_FORMAT ", pt: %" GST_TIME_FORMAT ", duration %" GST_TIME_FORMAT
2692 ",jitter %" G_GINT64_FORMAT, GST_TIME_ARGS (start), GST_TIME_ARGS (stop),
2693 GST_TIME_ARGS (entered), GST_TIME_ARGS (left), GST_TIME_ARGS (pt),
2694 GST_TIME_ARGS (duration), jitter);
2696 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, sink, "avg_duration: %" GST_TIME_FORMAT
2697 ", avg_pt: %" GST_TIME_FORMAT ", avg_rate: %g",
2698 GST_TIME_ARGS (priv->avg_duration), GST_TIME_ARGS (priv->avg_pt),
2701 /* collect running averages. for first observations, we copy the
2703 if (!GST_CLOCK_TIME_IS_VALID (priv->avg_duration))
2704 priv->avg_duration = duration;
2706 priv->avg_duration = UPDATE_RUNNING_AVG (priv->avg_duration, duration);
2708 if (!GST_CLOCK_TIME_IS_VALID (priv->avg_pt))
2711 priv->avg_pt = UPDATE_RUNNING_AVG (priv->avg_pt, pt);
2713 if (priv->avg_duration != 0)
2715 gst_guint64_to_gdouble (priv->avg_pt) /
2716 gst_guint64_to_gdouble (priv->avg_duration);
2720 if (GST_CLOCK_TIME_IS_VALID (priv->last_left)) {
2721 if (dropped || priv->avg_rate < 0.0) {
2722 priv->avg_rate = rate;
2725 priv->avg_rate = UPDATE_RUNNING_AVG_N (priv->avg_rate, rate);
2727 priv->avg_rate = UPDATE_RUNNING_AVG_P (priv->avg_rate, rate);
2731 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, sink,
2732 "updated: avg_duration: %" GST_TIME_FORMAT ", avg_pt: %" GST_TIME_FORMAT
2733 ", avg_rate: %g", GST_TIME_ARGS (priv->avg_duration),
2734 GST_TIME_ARGS (priv->avg_pt), priv->avg_rate);
2737 if (priv->avg_rate >= 0.0) {
2739 GstClockTimeDiff diff;
2741 /* if we have a valid rate, start sending QoS messages */
2742 if (priv->current_jitter < 0) {
2743 /* make sure we never go below 0 when adding the jitter to the
2745 if (priv->current_rstart < -priv->current_jitter)
2746 priv->current_jitter = -priv->current_rstart;
2749 if (priv->throttle_time > 0) {
2750 diff = priv->throttle_time;
2751 type = GST_QOS_TYPE_THROTTLE;
2753 diff = priv->current_jitter;
2755 type = GST_QOS_TYPE_OVERFLOW;
2757 type = GST_QOS_TYPE_UNDERFLOW;
2760 gst_base_sink_send_qos (sink, type, priv->avg_rate, priv->current_rstart,
2764 /* record when this buffer will leave us */
2765 priv->last_left = left;
2768 /* reset all qos measuring */
2770 gst_base_sink_reset_qos (GstBaseSink * sink)
2772 GstBaseSinkPrivate *priv;
2776 priv->last_render_time = GST_CLOCK_TIME_NONE;
2777 priv->prev_rstart = GST_CLOCK_TIME_NONE;
2778 priv->earliest_in_time = GST_CLOCK_TIME_NONE;
2779 priv->last_left = GST_CLOCK_TIME_NONE;
2780 priv->avg_duration = GST_CLOCK_TIME_NONE;
2781 priv->avg_pt = GST_CLOCK_TIME_NONE;
2782 priv->avg_rate = -1.0;
2783 priv->avg_render = GST_CLOCK_TIME_NONE;
2784 priv->avg_in_diff = GST_CLOCK_TIME_NONE;
2790 /* Checks if the object was scheduled too late.
2792 * rstart/rstop contain the running_time start and stop values
2795 * status and jitter contain the return values from the clock wait.
2797 * returns %TRUE if the buffer was too late.
2800 gst_base_sink_is_too_late (GstBaseSink * basesink, GstMiniObject * obj,
2801 GstClockTime rstart, GstClockTime rstop,
2802 GstClockReturn status, GstClockTimeDiff jitter, gboolean render)
2805 guint64 max_lateness;
2806 GstBaseSinkPrivate *priv;
2808 priv = basesink->priv;
2812 /* only for objects that were too late */
2813 if (G_LIKELY (status != GST_CLOCK_EARLY))
2816 max_lateness = basesink->max_lateness;
2818 /* check if frame dropping is enabled */
2819 if (max_lateness == -1)
2822 /* only check for buffers */
2823 if (G_UNLIKELY (!GST_IS_BUFFER (obj)))
2826 /* can't do check if we don't have a timestamp */
2827 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (rstart)))
2830 /* we can add a valid stop time */
2831 if (GST_CLOCK_TIME_IS_VALID (rstop))
2832 max_lateness += rstop;
2834 max_lateness += rstart;
2835 /* no stop time, use avg frame diff */
2836 if (priv->avg_in_diff != -1)
2837 max_lateness += priv->avg_in_diff;
2840 /* if the jitter bigger than duration and lateness we are too late */
2841 if ((late = rstart + jitter > max_lateness)) {
2842 GST_CAT_DEBUG_OBJECT (GST_CAT_PERFORMANCE, basesink,
2843 "buffer is too late %" GST_TIME_FORMAT
2844 " > %" GST_TIME_FORMAT, GST_TIME_ARGS (rstart + jitter),
2845 GST_TIME_ARGS (max_lateness));
2846 /* !!emergency!!, if we did not receive anything valid for more than a
2847 * second, render it anyway so the user sees something */
2848 if (GST_CLOCK_TIME_IS_VALID (priv->last_render_time) &&
2849 rstart - priv->last_render_time > GST_SECOND) {
2851 GST_ELEMENT_WARNING (basesink, CORE, CLOCK,
2852 (_("A lot of buffers are being dropped.")),
2853 ("There may be a timestamping problem, or this computer is too slow."));
2854 GST_CAT_DEBUG_OBJECT (GST_CAT_PERFORMANCE, basesink,
2855 "**emergency** last buffer at %" GST_TIME_FORMAT " > GST_SECOND",
2856 GST_TIME_ARGS (priv->last_render_time));
2861 if (render && (!late || !GST_CLOCK_TIME_IS_VALID (priv->last_render_time))) {
2862 priv->last_render_time = rstart;
2863 /* the next allowed input timestamp */
2864 if (priv->throttle_time > 0)
2865 priv->earliest_in_time = rstart + priv->throttle_time;
2872 GST_DEBUG_OBJECT (basesink, "object was scheduled in time");
2877 GST_DEBUG_OBJECT (basesink, "frame dropping disabled");
2882 GST_DEBUG_OBJECT (basesink, "object is not a buffer");
2887 GST_DEBUG_OBJECT (basesink, "buffer has no timestamp");
2892 /* called before and after calling the render vmethod. It keeps track of how
2893 * much time was spent in the render method and is used to check if we are
2896 gst_base_sink_do_render_stats (GstBaseSink * basesink, gboolean start)
2898 GstBaseSinkPrivate *priv;
2900 priv = basesink->priv;
2903 priv->start = gst_util_get_timestamp ();
2905 GstClockTime elapsed;
2907 priv->stop = gst_util_get_timestamp ();
2909 elapsed = GST_CLOCK_DIFF (priv->start, priv->stop);
2911 if (!GST_CLOCK_TIME_IS_VALID (priv->avg_render))
2912 priv->avg_render = elapsed;
2914 priv->avg_render = UPDATE_RUNNING_AVG (priv->avg_render, elapsed);
2916 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, basesink,
2917 "avg_render: %" GST_TIME_FORMAT, GST_TIME_ARGS (priv->avg_render));
2922 gst_base_sink_update_start_time (GstBaseSink * basesink)
2926 GST_OBJECT_LOCK (basesink);
2927 if ((clock = GST_ELEMENT_CLOCK (basesink))) {
2930 gst_object_ref (clock);
2931 GST_OBJECT_UNLOCK (basesink);
2933 /* calculate the time when we stopped */
2934 now = gst_clock_get_time (clock);
2935 gst_object_unref (clock);
2937 GST_OBJECT_LOCK (basesink);
2938 /* store the current running time */
2939 if (GST_ELEMENT_START_TIME (basesink) != GST_CLOCK_TIME_NONE) {
2940 if (now != GST_CLOCK_TIME_NONE)
2941 GST_ELEMENT_START_TIME (basesink) =
2942 now - GST_ELEMENT_CAST (basesink)->base_time;
2944 GST_WARNING_OBJECT (basesink,
2945 "Clock %s returned invalid time, can't calculate "
2946 "running_time when going to the PAUSED state",
2947 GST_OBJECT_NAME (clock));
2949 GST_DEBUG_OBJECT (basesink,
2950 "start_time=%" GST_TIME_FORMAT ", now=%" GST_TIME_FORMAT
2951 ", base_time %" GST_TIME_FORMAT,
2952 GST_TIME_ARGS (GST_ELEMENT_START_TIME (basesink)),
2953 GST_TIME_ARGS (now),
2954 GST_TIME_ARGS (GST_ELEMENT_CAST (basesink)->base_time));
2956 GST_OBJECT_UNLOCK (basesink);
2960 gst_base_sink_flush_start (GstBaseSink * basesink, GstPad * pad)
2962 /* make sure we are not blocked on the clock also clear any pending
2964 gst_base_sink_set_flushing (basesink, pad, TRUE);
2966 /* we grab the stream lock but that is not needed since setting the
2967 * sink to flushing would make sure no state commit is being done
2969 GST_PAD_STREAM_LOCK (pad);
2970 gst_base_sink_reset_qos (basesink);
2971 /* and we need to commit our state again on the next
2972 * prerolled buffer */
2973 basesink->playing_async = TRUE;
2974 if (basesink->priv->async_enabled) {
2975 gst_base_sink_update_start_time (basesink);
2976 gst_element_lost_state (GST_ELEMENT_CAST (basesink));
2978 /* start time reset in above case as well;
2979 * arranges for a.o. proper position reporting when flushing in PAUSED */
2980 gst_element_set_start_time (GST_ELEMENT_CAST (basesink), 0);
2981 basesink->priv->have_latency = TRUE;
2983 gst_base_sink_set_last_buffer (basesink, NULL);
2984 gst_base_sink_set_last_buffer_list (basesink, NULL);
2985 GST_PAD_STREAM_UNLOCK (pad);
2989 gst_base_sink_flush_stop (GstBaseSink * basesink, GstPad * pad,
2990 gboolean reset_time)
2992 /* unset flushing so we can accept new data, this also flushes out any EOS
2994 gst_base_sink_set_flushing (basesink, pad, FALSE);
2996 /* for position reporting */
2997 GST_OBJECT_LOCK (basesink);
2998 basesink->priv->current_sstart = GST_CLOCK_TIME_NONE;
2999 basesink->priv->current_sstop = GST_CLOCK_TIME_NONE;
3000 basesink->priv->eos_rtime = GST_CLOCK_TIME_NONE;
3001 basesink->priv->call_preroll = TRUE;
3002 basesink->priv->current_step.valid = FALSE;
3003 basesink->priv->pending_step.valid = FALSE;
3004 if (basesink->pad_mode == GST_PAD_MODE_PUSH) {
3005 /* we need new segment info after the flush. */
3006 basesink->have_newsegment = FALSE;
3008 gst_segment_init (&basesink->segment, GST_FORMAT_UNDEFINED);
3009 GST_ELEMENT_START_TIME (basesink) = 0;
3012 GST_OBJECT_UNLOCK (basesink);
3015 GST_DEBUG_OBJECT (basesink, "posting reset-time message");
3016 gst_element_post_message (GST_ELEMENT_CAST (basesink),
3017 gst_message_new_reset_time (GST_OBJECT_CAST (basesink), 0));
3021 static GstFlowReturn
3022 gst_base_sink_default_wait_event (GstBaseSink * basesink, GstEvent * event)
3025 gboolean late, step_end = FALSE;
3027 ret = gst_base_sink_do_sync (basesink, GST_MINI_OBJECT_CAST (event),
3033 static GstFlowReturn
3034 gst_base_sink_wait_event (GstBaseSink * basesink, GstEvent * event)
3037 GstBaseSinkClass *bclass;
3039 bclass = GST_BASE_SINK_GET_CLASS (basesink);
3041 if (G_LIKELY (bclass->wait_event))
3042 ret = bclass->wait_event (basesink, event);
3044 ret = GST_FLOW_NOT_SUPPORTED;
3050 gst_base_sink_default_event (GstBaseSink * basesink, GstEvent * event)
3052 gboolean result = TRUE;
3053 GstBaseSinkClass *bclass;
3055 bclass = GST_BASE_SINK_GET_CLASS (basesink);
3057 switch (GST_EVENT_TYPE (event)) {
3058 case GST_EVENT_FLUSH_START:
3060 GST_DEBUG_OBJECT (basesink, "flush-start %p", event);
3061 gst_base_sink_flush_start (basesink, basesink->sinkpad);
3064 case GST_EVENT_FLUSH_STOP:
3066 gboolean reset_time;
3068 gst_event_parse_flush_stop (event, &reset_time);
3069 GST_DEBUG_OBJECT (basesink, "flush-stop %p, reset_time: %d", event,
3071 gst_base_sink_flush_stop (basesink, basesink->sinkpad, reset_time);
3076 GstMessage *message;
3079 /* we set the received EOS flag here so that we can use it when testing if
3080 * we are prerolled and to refuse more buffers. */
3081 basesink->priv->received_eos = TRUE;
3084 if (G_UNLIKELY (gst_base_sink_wait_event (basesink,
3085 event) != GST_FLOW_OK)) {
3090 /* the EOS event is completely handled so we mark
3091 * ourselves as being in the EOS state. eos is also
3092 * protected by the object lock so we can read it when
3093 * answering the POSITION query. */
3094 GST_OBJECT_LOCK (basesink);
3095 basesink->eos = TRUE;
3096 GST_OBJECT_UNLOCK (basesink);
3098 /* ok, now we can post the message */
3099 GST_DEBUG_OBJECT (basesink, "Now posting EOS");
3101 seqnum = basesink->priv->seqnum = gst_event_get_seqnum (event);
3102 GST_DEBUG_OBJECT (basesink, "Got seqnum #%" G_GUINT32_FORMAT, seqnum);
3104 message = gst_message_new_eos (GST_OBJECT_CAST (basesink));
3105 gst_message_set_seqnum (message, seqnum);
3106 gst_element_post_message (GST_ELEMENT_CAST (basesink), message);
3109 case GST_EVENT_STREAM_START:
3111 GstMessage *message;
3115 seqnum = gst_event_get_seqnum (event);
3116 GST_DEBUG_OBJECT (basesink, "Now posting STREAM_START (seqnum:%d)",
3118 message = gst_message_new_stream_start (GST_OBJECT_CAST (basesink));
3119 if (gst_event_parse_group_id (event, &group_id)) {
3120 gst_message_set_group_id (message, group_id);
3122 GST_FIXME_OBJECT (basesink, "stream-start event without group-id. "
3123 "Consider implementing group-id handling in the upstream "
3126 gst_message_set_seqnum (message, seqnum);
3127 gst_element_post_message (GST_ELEMENT_CAST (basesink), message);
3130 case GST_EVENT_CAPS:
3132 GstCaps *caps, *current_caps;
3134 GST_DEBUG_OBJECT (basesink, "caps %p", event);
3136 gst_event_parse_caps (event, &caps);
3137 current_caps = gst_pad_get_current_caps (GST_BASE_SINK_PAD (basesink));
3139 if (current_caps && gst_caps_is_equal (current_caps, caps)) {
3140 GST_DEBUG_OBJECT (basesink,
3141 "New caps equal to old ones: %" GST_PTR_FORMAT, caps);
3143 if (bclass->set_caps)
3144 result = bclass->set_caps (basesink, caps);
3147 GST_OBJECT_LOCK (basesink);
3148 gst_caps_replace (&basesink->priv->caps, caps);
3149 GST_OBJECT_UNLOCK (basesink);
3153 gst_caps_unref (current_caps);
3156 case GST_EVENT_SEGMENT:
3157 /* configure the segment */
3158 /* The segment is protected with both the STREAM_LOCK and the OBJECT_LOCK.
3159 * We protect with the OBJECT_LOCK so that we can use the values to
3160 * safely answer a POSITION query. */
3161 GST_OBJECT_LOCK (basesink);
3162 /* the newsegment event is needed to bring the buffer timestamps to the
3163 * stream time and to drop samples outside of the playback segment. */
3164 gst_event_copy_segment (event, &basesink->segment);
3165 GST_DEBUG_OBJECT (basesink, "configured segment %" GST_SEGMENT_FORMAT,
3166 &basesink->segment);
3167 basesink->have_newsegment = TRUE;
3168 gst_base_sink_reset_qos (basesink);
3169 GST_OBJECT_UNLOCK (basesink);
3173 if (G_UNLIKELY (gst_base_sink_wait_event (basesink,
3174 event) != GST_FLOW_OK))
3180 GstTagList *taglist;
3182 gst_event_parse_tag (event, &taglist);
3184 gst_element_post_message (GST_ELEMENT_CAST (basesink),
3185 gst_message_new_tag (GST_OBJECT_CAST (basesink),
3186 gst_tag_list_copy (taglist)));
3194 gst_event_parse_toc (event, &toc, &updated);
3196 gst_element_post_message (GST_ELEMENT_CAST (basesink),
3197 gst_message_new_toc (GST_OBJECT_CAST (basesink), toc, updated));
3199 gst_toc_unref (toc);
3202 case GST_EVENT_SINK_MESSAGE:
3204 GstMessage *msg = NULL;
3206 gst_event_parse_sink_message (event, &msg);
3208 gst_element_post_message (GST_ELEMENT_CAST (basesink), msg);
3215 gst_event_unref (event);
3221 gst_base_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
3223 GstBaseSink *basesink;
3224 gboolean result = TRUE;
3225 GstBaseSinkClass *bclass;
3227 basesink = GST_BASE_SINK_CAST (parent);
3228 bclass = GST_BASE_SINK_GET_CLASS (basesink);
3230 GST_DEBUG_OBJECT (basesink, "received event %p %" GST_PTR_FORMAT, event,
3233 switch (GST_EVENT_TYPE (event)) {
3234 case GST_EVENT_FLUSH_STOP:
3235 /* special case for this serialized event because we don't want to grab
3236 * the PREROLL lock or check if we were flushing */
3238 result = bclass->event (basesink, event);
3241 if (GST_EVENT_IS_SERIALIZED (event)) {
3242 GST_BASE_SINK_PREROLL_LOCK (basesink);
3243 if (G_UNLIKELY (basesink->flushing))
3246 if (G_UNLIKELY (basesink->priv->received_eos))
3250 result = bclass->event (basesink, event);
3252 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
3255 result = bclass->event (basesink, event);
3265 GST_DEBUG_OBJECT (basesink, "we are flushing");
3266 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
3267 gst_event_unref (event);
3274 GST_DEBUG_OBJECT (basesink, "Event received after EOS, dropping");
3275 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
3276 gst_event_unref (event);
3282 /* default implementation to calculate the start and end
3283 * timestamps on a buffer, subclasses can override
3286 gst_base_sink_default_get_times (GstBaseSink * basesink, GstBuffer * buffer,
3287 GstClockTime * start, GstClockTime * end)
3289 GstClockTime timestamp, duration;
3291 /* first sync on DTS, else use PTS */
3292 timestamp = GST_BUFFER_DTS (buffer);
3293 if (!GST_CLOCK_TIME_IS_VALID (timestamp))
3294 timestamp = GST_BUFFER_PTS (buffer);
3296 if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
3297 /* get duration to calculate end time */
3298 duration = GST_BUFFER_DURATION (buffer);
3299 if (GST_CLOCK_TIME_IS_VALID (duration)) {
3300 *end = timestamp + duration;
3306 /* must be called with PREROLL_LOCK */
3308 gst_base_sink_needs_preroll (GstBaseSink * basesink)
3310 gboolean is_prerolled, res;
3312 /* we have 2 cases where the PREROLL_LOCK is released:
3313 * 1) we are blocking in the PREROLL_LOCK and thus are prerolled.
3314 * 2) we are syncing on the clock
3316 is_prerolled = basesink->have_preroll || basesink->priv->received_eos;
3317 res = !is_prerolled;
3319 GST_DEBUG_OBJECT (basesink, "have_preroll: %d, EOS: %d => needs preroll: %d",
3320 basesink->have_preroll, basesink->priv->received_eos, res);
3326 count_list_bytes (GstBuffer ** buffer, guint idx, GstBaseSinkPrivate * priv)
3328 priv->rc_accumulated += gst_buffer_get_size (*buffer);
3332 /* with STREAM_LOCK, PREROLL_LOCK
3334 * Takes a buffer and compare the timestamps with the last segment.
3335 * If the buffer falls outside of the segment boundaries, drop it.
3336 * Else send the buffer for preroll and rendering.
3338 * This function takes ownership of the buffer.
3340 static GstFlowReturn
3341 gst_base_sink_chain_unlocked (GstBaseSink * basesink, GstPad * pad,
3342 gpointer obj, gboolean is_list)
3344 GstBaseSinkClass *bclass;
3345 GstBaseSinkPrivate *priv = basesink->priv;
3346 GstFlowReturn ret = GST_FLOW_OK;
3347 GstClockTime start = GST_CLOCK_TIME_NONE, end = GST_CLOCK_TIME_NONE;
3348 GstSegment *segment;
3349 GstBuffer *sync_buf;
3351 gboolean late, step_end, prepared = FALSE;
3353 if (G_UNLIKELY (basesink->flushing))
3356 if (G_UNLIKELY (priv->received_eos))
3360 GstBufferList *buffer_list = GST_BUFFER_LIST_CAST (obj);
3362 if (gst_buffer_list_length (buffer_list) == 0)
3365 sync_buf = gst_buffer_list_get (buffer_list, 0);
3366 g_assert (NULL != sync_buf);
3368 sync_buf = GST_BUFFER_CAST (obj);
3371 /* for code clarity */
3372 segment = &basesink->segment;
3374 if (G_UNLIKELY (!basesink->have_newsegment)) {
3377 sync = gst_base_sink_get_sync (basesink);
3379 GST_ELEMENT_WARNING (basesink, STREAM, FAILED,
3380 (_("Internal data flow problem.")),
3381 ("Received buffer without a new-segment. Assuming timestamps start from 0."));
3384 /* this means this sink will assume timestamps start from 0 */
3385 GST_OBJECT_LOCK (basesink);
3388 basesink->segment.start = 0;
3389 basesink->segment.stop = -1;
3390 basesink->have_newsegment = TRUE;
3391 GST_OBJECT_UNLOCK (basesink);
3394 bclass = GST_BASE_SINK_GET_CLASS (basesink);
3396 /* check if the buffer needs to be dropped, we first ask the subclass for the
3398 if (bclass->get_times)
3399 bclass->get_times (basesink, sync_buf, &start, &end);
3401 if (!GST_CLOCK_TIME_IS_VALID (start)) {
3402 /* if the subclass does not want sync, we use our own values so that we at
3403 * least clip the buffer to the segment */
3404 gst_base_sink_default_get_times (basesink, sync_buf, &start, &end);
3407 GST_DEBUG_OBJECT (basesink, "got times start: %" GST_TIME_FORMAT
3408 ", end: %" GST_TIME_FORMAT, GST_TIME_ARGS (start), GST_TIME_ARGS (end));
3410 /* a dropped buffer does not participate in anything. Buffer can only be
3411 * dropped if their PTS falls completly outside the segment, while we sync
3412 * preferably on DTS */
3413 if (GST_CLOCK_TIME_IS_VALID (start) && (segment->format == GST_FORMAT_TIME)) {
3414 GstClockTime pts = GST_BUFFER_PTS (sync_buf);
3415 GstClockTime pts_end = GST_CLOCK_TIME_NONE;
3417 if (!GST_CLOCK_TIME_IS_VALID (pts))
3420 if (GST_CLOCK_TIME_IS_VALID (end))
3421 pts_end = pts + (end - start);
3423 if (G_UNLIKELY (!gst_segment_clip (segment,
3424 GST_FORMAT_TIME, pts, pts_end, NULL, NULL)))
3425 goto out_of_segment;
3428 if (bclass->prepare || bclass->prepare_list) {
3429 gboolean do_sync = TRUE, stepped = FALSE, syncable = TRUE;
3430 GstClockTime sstart, sstop, rstart, rstop, rnext;
3431 GstStepInfo *current;
3436 current = &priv->current_step;
3438 gst_base_sink_get_sync_times (basesink, obj, &sstart, &sstop, &rstart,
3439 &rstop, &rnext, &do_sync, &stepped, current, &step_end);
3441 if (G_UNLIKELY (stepped))
3444 if (syncable && do_sync && gst_base_sink_get_sync (basesink)) {
3447 GST_OBJECT_LOCK (basesink);
3448 clock = GST_ELEMENT_CLOCK (basesink);
3449 if (clock && GST_STATE (basesink) == GST_STATE_PLAYING) {
3450 GstClockTime base_time;
3454 base_time = GST_ELEMENT_CAST (basesink)->base_time;
3455 stime = base_time + gst_base_sink_adjust_time (basesink, rstart);
3456 now = gst_clock_get_time (clock);
3457 GST_OBJECT_UNLOCK (basesink);
3460 gst_base_sink_is_too_late (basesink, obj, rstart, rstop,
3461 GST_CLOCK_EARLY, GST_CLOCK_DIFF (stime, now), FALSE);
3463 GST_OBJECT_UNLOCK (basesink);
3467 if (G_UNLIKELY (late))
3471 if (bclass->prepare) {
3472 ret = bclass->prepare (basesink, GST_BUFFER_CAST (obj));
3473 if (G_UNLIKELY (ret != GST_FLOW_OK))
3474 goto prepare_failed;
3477 if (bclass->prepare_list) {
3478 ret = bclass->prepare_list (basesink, GST_BUFFER_LIST_CAST (obj));
3479 if (G_UNLIKELY (ret != GST_FLOW_OK))
3480 goto prepare_failed;
3491 /* synchronize this object, non syncable objects return OK
3493 ret = gst_base_sink_do_sync (basesink, GST_MINI_OBJECT_CAST (sync_buf),
3495 if (G_UNLIKELY (ret != GST_FLOW_OK))
3498 /* Don't skip if prepare() was called on time */
3499 late = late && !prepared;
3501 /* drop late buffers unconditionally, let's hope it's unlikely */
3502 if (G_UNLIKELY (late))
3505 if (priv->max_bitrate) {
3507 gst_buffer_list_foreach (GST_BUFFER_LIST_CAST (obj),
3508 (GstBufferListFunc) count_list_bytes, priv);
3510 priv->rc_accumulated += gst_buffer_get_size (GST_BUFFER_CAST (obj));
3512 priv->rc_next = priv->rc_time + gst_util_uint64_scale (priv->rc_accumulated,
3513 8 * GST_SECOND, priv->max_bitrate);
3516 /* read once, to get same value before and after */
3517 do_qos = g_atomic_int_get (&priv->qos_enabled);
3519 GST_DEBUG_OBJECT (basesink, "rendering object %p", obj);
3521 /* record rendering time for QoS and stats */
3523 gst_base_sink_do_render_stats (basesink, TRUE);
3526 /* For buffer lists do not set last buffer for now. */
3527 gst_base_sink_set_last_buffer (basesink, GST_BUFFER_CAST (obj));
3528 gst_base_sink_set_last_buffer_list (basesink, NULL);
3531 ret = bclass->render (basesink, GST_BUFFER_CAST (obj));
3533 GstBufferList *buffer_list = GST_BUFFER_LIST_CAST (obj);
3535 if (bclass->render_list)
3536 ret = bclass->render_list (basesink, buffer_list);
3538 /* Set the first buffer and buffer list to be included in last sample */
3539 gst_base_sink_set_last_buffer (basesink, sync_buf);
3540 gst_base_sink_set_last_buffer_list (basesink, buffer_list);
3544 gst_base_sink_do_render_stats (basesink, FALSE);
3546 if (ret == GST_FLOW_STEP)
3549 if (G_UNLIKELY (basesink->flushing))
3556 /* the step ended, check if we need to activate a new step */
3557 GST_DEBUG_OBJECT (basesink, "step ended");
3558 stop_stepping (basesink, &basesink->segment, &priv->current_step,
3559 priv->current_rstart, priv->current_rstop, basesink->eos);
3563 gst_base_sink_perform_qos (basesink, late);
3565 GST_DEBUG_OBJECT (basesink, "object unref after render %p", obj);
3566 gst_mini_object_unref (GST_MINI_OBJECT_CAST (obj));
3573 GST_DEBUG_OBJECT (basesink, "sink is flushing");
3574 gst_mini_object_unref (GST_MINI_OBJECT_CAST (obj));
3575 return GST_FLOW_FLUSHING;
3579 GST_DEBUG_OBJECT (basesink, "we are EOS, dropping object, return EOS");
3580 gst_mini_object_unref (GST_MINI_OBJECT_CAST (obj));
3581 return GST_FLOW_EOS;
3585 GST_DEBUG_OBJECT (basesink, "buffer list with no buffers");
3586 gst_mini_object_unref (GST_MINI_OBJECT_CAST (obj));
3591 GST_DEBUG_OBJECT (basesink, "dropping buffer, out of clipping segment");
3592 gst_mini_object_unref (GST_MINI_OBJECT_CAST (obj));
3597 GST_DEBUG_OBJECT (basesink, "prepare buffer failed %s",
3598 gst_flow_get_name (ret));
3599 gst_mini_object_unref (GST_MINI_OBJECT_CAST (obj));
3604 GST_DEBUG_OBJECT (basesink, "do_sync returned %s", gst_flow_get_name (ret));
3610 GST_DEBUG_OBJECT (basesink, "buffer late, dropping");
3612 if (g_atomic_int_get (&priv->qos_enabled)) {
3613 GstMessage *qos_msg;
3614 GstClockTime timestamp, duration;
3616 timestamp = GST_BUFFER_TIMESTAMP (GST_BUFFER_CAST (sync_buf));
3617 duration = GST_BUFFER_DURATION (GST_BUFFER_CAST (sync_buf));
3619 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, basesink,
3620 "qos: dropped buffer rt %" GST_TIME_FORMAT ", st %" GST_TIME_FORMAT
3621 ", ts %" GST_TIME_FORMAT ", dur %" GST_TIME_FORMAT,
3622 GST_TIME_ARGS (priv->current_rstart),
3623 GST_TIME_ARGS (priv->current_sstart), GST_TIME_ARGS (timestamp),
3624 GST_TIME_ARGS (duration));
3625 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, basesink,
3626 "qos: rendered %" G_GUINT64_FORMAT ", dropped %" G_GUINT64_FORMAT,
3627 priv->rendered, priv->dropped);
3630 gst_message_new_qos (GST_OBJECT_CAST (basesink), basesink->sync,
3631 priv->current_rstart, priv->current_sstart, timestamp, duration);
3632 gst_message_set_qos_values (qos_msg, priv->current_jitter, priv->avg_rate,
3634 gst_message_set_qos_stats (qos_msg, GST_FORMAT_BUFFERS, priv->rendered,
3636 gst_element_post_message (GST_ELEMENT_CAST (basesink), qos_msg);
3644 static GstFlowReturn
3645 gst_base_sink_chain_main (GstBaseSink * basesink, GstPad * pad, gpointer obj,
3648 GstFlowReturn result;
3650 if (G_UNLIKELY (basesink->pad_mode != GST_PAD_MODE_PUSH))
3653 GST_BASE_SINK_PREROLL_LOCK (basesink);
3654 result = gst_base_sink_chain_unlocked (basesink, pad, obj, is_list);
3655 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
3663 GST_OBJECT_LOCK (pad);
3664 GST_WARNING_OBJECT (basesink,
3665 "Push on pad %s:%s, but it was not activated in push mode",
3666 GST_DEBUG_PAD_NAME (pad));
3667 GST_OBJECT_UNLOCK (pad);
3668 gst_mini_object_unref (GST_MINI_OBJECT_CAST (obj));
3669 /* we don't post an error message this will signal to the peer
3670 * pushing that EOS is reached. */
3671 result = GST_FLOW_EOS;
3676 static GstFlowReturn
3677 gst_base_sink_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
3679 GstBaseSink *basesink;
3681 basesink = GST_BASE_SINK (parent);
3683 return gst_base_sink_chain_main (basesink, pad, buf, FALSE);
3686 static GstFlowReturn
3687 gst_base_sink_chain_list (GstPad * pad, GstObject * parent,
3688 GstBufferList * list)
3690 GstBaseSink *basesink;
3691 GstBaseSinkClass *bclass;
3692 GstFlowReturn result;
3694 basesink = GST_BASE_SINK (parent);
3695 bclass = GST_BASE_SINK_GET_CLASS (basesink);
3697 if (G_LIKELY (bclass->render_list)) {
3698 result = gst_base_sink_chain_main (basesink, pad, list, TRUE);
3703 GST_LOG_OBJECT (pad, "chaining each buffer in list");
3705 len = gst_buffer_list_length (list);
3707 result = GST_FLOW_OK;
3708 for (i = 0; i < len; i++) {
3709 buffer = gst_buffer_list_get (list, i);
3710 result = gst_base_sink_chain_main (basesink, pad,
3711 gst_buffer_ref (buffer), FALSE);
3712 if (result != GST_FLOW_OK)
3715 gst_buffer_list_unref (list);
3722 gst_base_sink_default_do_seek (GstBaseSink * sink, GstSegment * segment)
3724 gboolean res = TRUE;
3726 /* update our offset if the start/stop position was updated */
3727 if (segment->format == GST_FORMAT_BYTES) {
3728 segment->time = segment->start;
3729 } else if (segment->start == 0) {
3730 /* seek to start, we can implement a default for this. */
3734 GST_INFO_OBJECT (sink, "Can't do a default seek");
3740 #define SEEK_TYPE_IS_RELATIVE(t) (((t) != GST_SEEK_TYPE_NONE) && ((t) != GST_SEEK_TYPE_SET))
3743 gst_base_sink_default_prepare_seek_segment (GstBaseSink * sink,
3744 GstEvent * event, GstSegment * segment)
3746 /* By default, we try one of 2 things:
3747 * - For absolute seek positions, convert the requested position to our
3748 * configured processing format and place it in the output segment \
3749 * - For relative seek positions, convert our current (input) values to the
3750 * seek format, adjust by the relative seek offset and then convert back to
3751 * the processing format
3753 GstSeekType start_type, stop_type;
3756 GstFormat seek_format;
3759 gboolean res = TRUE;
3761 gst_event_parse_seek (event, &rate, &seek_format, &flags,
3762 &start_type, &start, &stop_type, &stop);
3764 if (seek_format == segment->format) {
3765 gst_segment_do_seek (segment, rate, seek_format, flags,
3766 start_type, start, stop_type, stop, &update);
3770 if (start_type != GST_SEEK_TYPE_NONE) {
3771 /* FIXME: Handle seek_end by converting the input segment vals */
3773 gst_pad_query_convert (sink->sinkpad, seek_format, start,
3774 segment->format, &start);
3775 start_type = GST_SEEK_TYPE_SET;
3778 if (res && stop_type != GST_SEEK_TYPE_NONE) {
3779 /* FIXME: Handle seek_end by converting the input segment vals */
3781 gst_pad_query_convert (sink->sinkpad, seek_format, stop,
3782 segment->format, &stop);
3783 stop_type = GST_SEEK_TYPE_SET;
3786 /* And finally, configure our output segment in the desired format */
3787 gst_segment_do_seek (segment, rate, segment->format, flags, start_type, start,
3788 stop_type, stop, &update);
3797 GST_DEBUG_OBJECT (sink, "undefined format given, seek aborted.");
3802 /* perform a seek, only executed in pull mode */
3804 gst_base_sink_perform_seek (GstBaseSink * sink, GstPad * pad, GstEvent * event)
3808 GstFormat seek_format, dest_format;
3810 GstSeekType start_type, stop_type;
3811 gboolean seekseg_configured = FALSE;
3813 gboolean update, res = TRUE;
3814 GstSegment seeksegment;
3816 dest_format = sink->segment.format;
3819 GST_DEBUG_OBJECT (sink, "performing seek with event %p", event);
3820 gst_event_parse_seek (event, &rate, &seek_format, &flags,
3821 &start_type, &start, &stop_type, &stop);
3823 flush = flags & GST_SEEK_FLAG_FLUSH;
3825 GST_DEBUG_OBJECT (sink, "performing seek without event");
3830 GST_DEBUG_OBJECT (sink, "flushing upstream");
3831 gst_pad_push_event (pad, gst_event_new_flush_start ());
3832 gst_base_sink_flush_start (sink, pad);
3834 GST_DEBUG_OBJECT (sink, "pausing pulling thread");
3837 GST_PAD_STREAM_LOCK (pad);
3839 /* If we configured the seeksegment above, don't overwrite it now. Otherwise
3840 * copy the current segment info into the temp segment that we can actually
3841 * attempt the seek with. We only update the real segment if the seek succeeds. */
3842 if (!seekseg_configured) {
3843 memcpy (&seeksegment, &sink->segment, sizeof (GstSegment));
3845 /* now configure the final seek segment */
3847 if (sink->segment.format != seek_format) {
3848 /* OK, here's where we give the subclass a chance to convert the relative
3849 * seek into an absolute one in the processing format. We set up any
3850 * absolute seek above, before taking the stream lock. */
3851 if (!gst_base_sink_default_prepare_seek_segment (sink, event,
3853 GST_DEBUG_OBJECT (sink,
3854 "Preparing the seek failed after flushing. " "Aborting seek");
3858 /* The seek format matches our processing format, no need to ask the
3859 * the subclass to configure the segment. */
3860 gst_segment_do_seek (&seeksegment, rate, seek_format, flags,
3861 start_type, start, stop_type, stop, &update);
3864 /* Else, no seek event passed, so we're just (re)starting the
3869 GST_DEBUG_OBJECT (sink, "segment configured from %" G_GINT64_FORMAT
3870 " to %" G_GINT64_FORMAT ", position %" G_GINT64_FORMAT,
3871 seeksegment.start, seeksegment.stop, seeksegment.position);
3873 /* do the seek, segment.position contains the new position. */
3874 res = gst_base_sink_default_do_seek (sink, &seeksegment);
3878 GST_DEBUG_OBJECT (sink, "stop flushing upstream");
3879 gst_pad_push_event (pad, gst_event_new_flush_stop (TRUE));
3880 gst_base_sink_flush_stop (sink, pad, TRUE);
3881 } else if (res && sink->running) {
3882 /* we are running the current segment and doing a non-flushing seek,
3883 * close the segment first based on the position. */
3884 GST_DEBUG_OBJECT (sink, "closing running segment %" G_GINT64_FORMAT
3885 " to %" G_GINT64_FORMAT, sink->segment.start, sink->segment.position);
3888 /* The subclass must have converted the segment to the processing format
3890 if (res && seeksegment.format != dest_format) {
3891 GST_DEBUG_OBJECT (sink, "Subclass failed to prepare a seek segment "
3892 "in the correct format. Aborting seek.");
3896 GST_INFO_OBJECT (sink, "seeking done %d: %" GST_SEGMENT_FORMAT, res,
3899 /* if successful seek, we update our real segment and push
3900 * out the new segment. */
3902 gst_segment_copy_into (&seeksegment, &sink->segment);
3904 if (sink->segment.flags & GST_SEGMENT_FLAG_SEGMENT) {
3905 gst_element_post_message (GST_ELEMENT (sink),
3906 gst_message_new_segment_start (GST_OBJECT (sink),
3907 sink->segment.format, sink->segment.position));
3911 sink->priv->discont = TRUE;
3912 sink->running = TRUE;
3914 GST_PAD_STREAM_UNLOCK (pad);
3920 set_step_info (GstBaseSink * sink, GstStepInfo * current, GstStepInfo * pending,
3921 guint seqnum, GstFormat format, guint64 amount, gdouble rate,
3922 gboolean flush, gboolean intermediate)
3924 GST_OBJECT_LOCK (sink);
3925 pending->seqnum = seqnum;
3926 pending->format = format;
3927 pending->amount = amount;
3928 pending->position = 0;
3929 pending->rate = rate;
3930 pending->flush = flush;
3931 pending->intermediate = intermediate;
3932 pending->valid = TRUE;
3933 /* flush invalidates the current stepping segment */
3935 current->valid = FALSE;
3936 GST_OBJECT_UNLOCK (sink);
3940 gst_base_sink_perform_step (GstBaseSink * sink, GstPad * pad, GstEvent * event)
3942 GstBaseSinkPrivate *priv;
3943 GstBaseSinkClass *bclass;
3944 gboolean flush, intermediate;
3949 GstStepInfo *pending, *current;
3950 GstMessage *message;
3952 bclass = GST_BASE_SINK_GET_CLASS (sink);
3955 GST_DEBUG_OBJECT (sink, "performing step with event %p", event);
3957 gst_event_parse_step (event, &format, &amount, &rate, &flush, &intermediate);
3958 seqnum = gst_event_get_seqnum (event);
3960 pending = &priv->pending_step;
3961 current = &priv->current_step;
3963 /* post message first */
3964 message = gst_message_new_step_start (GST_OBJECT (sink), FALSE, format,
3965 amount, rate, flush, intermediate);
3966 gst_message_set_seqnum (message, seqnum);
3967 gst_element_post_message (GST_ELEMENT (sink), message);
3970 /* we need to call ::unlock before locking PREROLL_LOCK
3971 * since we lock it before going into ::render */
3973 bclass->unlock (sink);
3975 GST_BASE_SINK_PREROLL_LOCK (sink);
3976 /* now that we have the PREROLL lock, clear our unlock request */
3977 if (bclass->unlock_stop)
3978 bclass->unlock_stop (sink);
3980 /* update the stepinfo and make it valid */
3981 set_step_info (sink, current, pending, seqnum, format, amount, rate, flush,
3984 if (sink->priv->async_enabled) {
3985 /* and we need to commit our state again on the next
3986 * prerolled buffer */
3987 sink->playing_async = TRUE;
3988 priv->pending_step.need_preroll = TRUE;
3989 sink->need_preroll = FALSE;
3990 gst_base_sink_update_start_time (sink);
3991 gst_element_lost_state (GST_ELEMENT_CAST (sink));
3993 sink->priv->have_latency = TRUE;
3994 sink->need_preroll = FALSE;
3996 priv->current_sstart = GST_CLOCK_TIME_NONE;
3997 priv->current_sstop = GST_CLOCK_TIME_NONE;
3998 priv->eos_rtime = GST_CLOCK_TIME_NONE;
3999 priv->call_preroll = TRUE;
4000 gst_base_sink_set_last_buffer (sink, NULL);
4001 gst_base_sink_set_last_buffer_list (sink, NULL);
4002 gst_base_sink_reset_qos (sink);
4004 if (sink->clock_id) {
4005 gst_clock_id_unschedule (sink->clock_id);
4008 if (sink->have_preroll) {
4009 GST_DEBUG_OBJECT (sink, "signal waiter");
4010 priv->step_unlock = TRUE;
4011 GST_BASE_SINK_PREROLL_SIGNAL (sink);
4013 GST_BASE_SINK_PREROLL_UNLOCK (sink);
4015 /* update the stepinfo and make it valid */
4016 set_step_info (sink, current, pending, seqnum, format, amount, rate, flush,
4026 gst_base_sink_loop (GstPad * pad)
4029 GstBaseSink *basesink;
4030 GstBuffer *buf = NULL;
4031 GstFlowReturn result;
4035 parent = GST_OBJECT_PARENT (pad);
4036 basesink = GST_BASE_SINK (parent);
4038 g_assert (basesink->pad_mode == GST_PAD_MODE_PULL);
4040 if ((blocksize = basesink->priv->blocksize) == 0)
4043 offset = basesink->segment.position;
4045 GST_DEBUG_OBJECT (basesink, "pulling %" G_GUINT64_FORMAT ", %u",
4048 result = gst_pad_pull_range (pad, offset, blocksize, &buf);
4049 if (G_UNLIKELY (result != GST_FLOW_OK))
4052 if (G_UNLIKELY (buf == NULL))
4055 offset += gst_buffer_get_size (buf);
4057 basesink->segment.position = offset;
4059 GST_BASE_SINK_PREROLL_LOCK (basesink);
4060 result = gst_base_sink_chain_unlocked (basesink, pad, buf, FALSE);
4061 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
4062 if (G_UNLIKELY (result != GST_FLOW_OK))
4070 GST_LOG_OBJECT (basesink, "pausing task, reason %s",
4071 gst_flow_get_name (result));
4072 gst_pad_pause_task (pad);
4073 if (result == GST_FLOW_EOS) {
4074 /* perform EOS logic */
4075 if (basesink->segment.flags & GST_SEGMENT_FLAG_SEGMENT) {
4076 gst_element_post_message (GST_ELEMENT_CAST (basesink),
4077 gst_message_new_segment_done (GST_OBJECT_CAST (basesink),
4078 basesink->segment.format, basesink->segment.position));
4079 gst_base_sink_event (pad, parent,
4080 gst_event_new_segment_done (basesink->segment.format,
4081 basesink->segment.position));
4083 gst_base_sink_event (pad, parent, gst_event_new_eos ());
4085 } else if (result == GST_FLOW_NOT_LINKED || result <= GST_FLOW_EOS) {
4086 /* for fatal errors we post an error message, post the error
4087 * first so the app knows about the error first.
4088 * wrong-state is not a fatal error because it happens due to
4089 * flushing and posting an error message in that case is the
4090 * wrong thing to do, e.g. when basesrc is doing a flushing
4092 GST_ELEMENT_ERROR (basesink, STREAM, FAILED,
4093 (_("Internal data stream error.")),
4094 ("stream stopped, reason %s", gst_flow_get_name (result)));
4095 gst_base_sink_event (pad, parent, gst_event_new_eos ());
4101 GST_LOG_OBJECT (basesink, "no buffer, pausing");
4102 GST_ELEMENT_ERROR (basesink, STREAM, FAILED,
4103 (_("Internal data flow error.")), ("element returned NULL buffer"));
4104 result = GST_FLOW_ERROR;
4110 gst_base_sink_set_flushing (GstBaseSink * basesink, GstPad * pad,
4113 GstBaseSinkClass *bclass;
4115 bclass = GST_BASE_SINK_GET_CLASS (basesink);
4118 /* unlock any subclasses, we need to do this before grabbing the
4119 * PREROLL_LOCK since we hold this lock before going into ::render. */
4121 bclass->unlock (basesink);
4124 GST_BASE_SINK_PREROLL_LOCK (basesink);
4125 basesink->flushing = flushing;
4127 /* step 1, now that we have the PREROLL lock, clear our unlock request */
4128 if (bclass->unlock_stop)
4129 bclass->unlock_stop (basesink);
4131 /* set need_preroll before we unblock the clock. If the clock is unblocked
4132 * before timing out, we can reuse the buffer for preroll. */
4133 basesink->need_preroll = TRUE;
4135 /* step 2, unblock clock sync (if any) or any other blocking thing */
4136 if (basesink->clock_id) {
4137 gst_clock_id_unschedule (basesink->clock_id);
4140 /* flush out the data thread if it's locked in finish_preroll, this will
4141 * also flush out the EOS state */
4142 GST_DEBUG_OBJECT (basesink,
4143 "flushing out data thread, need preroll to TRUE");
4145 /* we can't have EOS anymore now */
4146 basesink->eos = FALSE;
4147 basesink->priv->received_eos = FALSE;
4148 basesink->have_preroll = FALSE;
4149 basesink->priv->step_unlock = FALSE;
4150 /* can't report latency anymore until we preroll again */
4151 if (basesink->priv->async_enabled) {
4152 GST_OBJECT_LOCK (basesink);
4153 basesink->priv->have_latency = FALSE;
4154 GST_OBJECT_UNLOCK (basesink);
4156 /* and signal any waiters now */
4157 GST_BASE_SINK_PREROLL_SIGNAL (basesink);
4159 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
4165 gst_base_sink_default_activate_pull (GstBaseSink * basesink, gboolean active)
4171 result = gst_pad_start_task (basesink->sinkpad,
4172 (GstTaskFunction) gst_base_sink_loop, basesink->sinkpad, NULL);
4174 /* step 2, make sure streaming finishes */
4175 result = gst_pad_stop_task (basesink->sinkpad);
4182 gst_base_sink_pad_activate (GstPad * pad, GstObject * parent)
4184 gboolean result = FALSE;
4185 GstBaseSink *basesink;
4189 basesink = GST_BASE_SINK (parent);
4191 GST_DEBUG_OBJECT (basesink, "Trying pull mode first");
4193 gst_base_sink_set_flushing (basesink, pad, FALSE);
4195 /* we need to have the pull mode enabled */
4196 if (!basesink->can_activate_pull) {
4197 GST_DEBUG_OBJECT (basesink, "pull mode disabled");
4201 /* check if downstreams supports pull mode at all */
4202 query = gst_query_new_scheduling ();
4204 if (!gst_pad_peer_query (pad, query)) {
4205 gst_query_unref (query);
4206 GST_DEBUG_OBJECT (basesink, "peer query faild, no pull mode");
4210 /* parse result of the query */
4211 pull_mode = gst_query_has_scheduling_mode (query, GST_PAD_MODE_PULL);
4212 gst_query_unref (query);
4215 GST_DEBUG_OBJECT (basesink, "pull mode not supported");
4219 /* set the pad mode before starting the task so that it's in the
4220 * correct state for the new thread. also the sink set_caps and get_caps
4221 * function checks this */
4222 basesink->pad_mode = GST_PAD_MODE_PULL;
4224 /* we first try to negotiate a format so that when we try to activate
4225 * downstream, it knows about our format */
4226 if (!gst_base_sink_negotiate_pull (basesink)) {
4227 GST_DEBUG_OBJECT (basesink, "failed to negotiate in pull mode");
4231 /* ok activate now */
4232 if (!gst_pad_activate_mode (pad, GST_PAD_MODE_PULL, TRUE)) {
4233 /* clear any pending caps */
4234 GST_OBJECT_LOCK (basesink);
4235 gst_caps_replace (&basesink->priv->caps, NULL);
4236 GST_OBJECT_UNLOCK (basesink);
4237 GST_DEBUG_OBJECT (basesink, "failed to activate in pull mode");
4241 GST_DEBUG_OBJECT (basesink, "Success activating pull mode");
4245 /* push mode fallback */
4247 GST_DEBUG_OBJECT (basesink, "Falling back to push mode");
4248 if ((result = gst_pad_activate_mode (pad, GST_PAD_MODE_PUSH, TRUE))) {
4249 GST_DEBUG_OBJECT (basesink, "Success activating push mode");
4254 GST_WARNING_OBJECT (basesink, "Could not activate pad in either mode");
4255 gst_base_sink_set_flushing (basesink, pad, TRUE);
4262 gst_base_sink_pad_activate_push (GstPad * pad, GstObject * parent,
4266 GstBaseSink *basesink;
4268 basesink = GST_BASE_SINK (parent);
4271 if (!basesink->can_activate_push) {
4273 basesink->pad_mode = GST_PAD_MODE_NONE;
4276 basesink->pad_mode = GST_PAD_MODE_PUSH;
4279 if (G_UNLIKELY (basesink->pad_mode != GST_PAD_MODE_PUSH)) {
4280 g_warning ("Internal GStreamer activation error!!!");
4283 gst_base_sink_set_flushing (basesink, pad, TRUE);
4285 basesink->pad_mode = GST_PAD_MODE_NONE;
4293 gst_base_sink_negotiate_pull (GstBaseSink * basesink)
4300 /* this returns the intersection between our caps and the peer caps. If there
4301 * is no peer, it returns %NULL and we can't operate in pull mode so we can
4302 * fail the negotiation. */
4303 caps = gst_pad_get_allowed_caps (GST_BASE_SINK_PAD (basesink));
4304 if (caps == NULL || gst_caps_is_empty (caps))
4305 goto no_caps_possible;
4307 GST_DEBUG_OBJECT (basesink, "allowed caps: %" GST_PTR_FORMAT, caps);
4309 if (gst_caps_is_any (caps)) {
4310 GST_DEBUG_OBJECT (basesink, "caps were ANY after fixating, "
4312 /* neither side has template caps in this case, so they are prepared for
4313 pull() without setcaps() */
4317 caps = gst_base_sink_fixate (basesink, caps);
4318 GST_DEBUG_OBJECT (basesink, "fixated to: %" GST_PTR_FORMAT, caps);
4320 if (gst_caps_is_fixed (caps)) {
4321 if (!gst_pad_set_caps (GST_BASE_SINK_PAD (basesink), caps))
4322 goto could_not_set_caps;
4328 gst_caps_unref (caps);
4334 GST_INFO_OBJECT (basesink, "Pipeline could not agree on caps");
4335 GST_DEBUG_OBJECT (basesink, "get_allowed_caps() returned EMPTY");
4337 gst_caps_unref (caps);
4342 GST_INFO_OBJECT (basesink, "Could not set caps: %" GST_PTR_FORMAT, caps);
4343 gst_caps_unref (caps);
4348 /* this won't get called until we implement an activate function */
4350 gst_base_sink_pad_activate_pull (GstPad * pad, GstObject * parent,
4353 gboolean result = FALSE;
4354 GstBaseSink *basesink;
4355 GstBaseSinkClass *bclass;
4357 basesink = GST_BASE_SINK (parent);
4358 bclass = GST_BASE_SINK_GET_CLASS (basesink);
4363 /* we mark we have a newsegment here because pull based
4364 * mode works just fine without having a newsegment before the
4366 gst_segment_init (&basesink->segment, GST_FORMAT_BYTES);
4367 GST_OBJECT_LOCK (basesink);
4368 basesink->have_newsegment = TRUE;
4369 GST_OBJECT_UNLOCK (basesink);
4371 /* get the peer duration in bytes */
4372 result = gst_pad_peer_query_duration (pad, GST_FORMAT_BYTES, &duration);
4374 GST_DEBUG_OBJECT (basesink,
4375 "setting duration in bytes to %" G_GINT64_FORMAT, duration);
4376 basesink->segment.duration = duration;
4378 GST_DEBUG_OBJECT (basesink, "unknown duration");
4381 if (bclass->activate_pull)
4382 result = bclass->activate_pull (basesink, TRUE);
4387 goto activate_failed;
4390 if (G_UNLIKELY (basesink->pad_mode != GST_PAD_MODE_PULL)) {
4391 g_warning ("Internal GStreamer activation error!!!");
4394 result = gst_base_sink_set_flushing (basesink, pad, TRUE);
4395 if (bclass->activate_pull)
4396 result &= bclass->activate_pull (basesink, FALSE);
4397 basesink->pad_mode = GST_PAD_MODE_NONE;
4406 /* reset, as starting the thread failed */
4407 basesink->pad_mode = GST_PAD_MODE_NONE;
4409 GST_ERROR_OBJECT (basesink, "subclass failed to activate in pull mode");
4415 gst_base_sink_pad_activate_mode (GstPad * pad, GstObject * parent,
4416 GstPadMode mode, gboolean active)
4421 case GST_PAD_MODE_PULL:
4422 res = gst_base_sink_pad_activate_pull (pad, parent, active);
4424 case GST_PAD_MODE_PUSH:
4425 res = gst_base_sink_pad_activate_push (pad, parent, active);
4428 GST_LOG_OBJECT (pad, "unknown activation mode %d", mode);
4435 /* send an event to our sinkpad peer. */
4437 gst_base_sink_send_event (GstElement * element, GstEvent * event)
4440 GstBaseSink *basesink = GST_BASE_SINK (element);
4441 gboolean forward, result = TRUE;
4444 GST_OBJECT_LOCK (element);
4445 /* get the pad and the scheduling mode */
4446 pad = gst_object_ref (basesink->sinkpad);
4447 mode = basesink->pad_mode;
4448 GST_OBJECT_UNLOCK (element);
4450 /* only push UPSTREAM events upstream */
4451 forward = GST_EVENT_IS_UPSTREAM (event);
4453 GST_DEBUG_OBJECT (basesink, "handling event %p %" GST_PTR_FORMAT, event,
4456 switch (GST_EVENT_TYPE (event)) {
4457 case GST_EVENT_LATENCY:
4459 GstClockTime latency;
4461 gst_event_parse_latency (event, &latency);
4463 /* store the latency. We use this to adjust the running_time before syncing
4464 * it to the clock. */
4465 GST_OBJECT_LOCK (element);
4466 basesink->priv->latency = latency;
4467 if (!basesink->priv->have_latency)
4469 GST_OBJECT_UNLOCK (element);
4470 GST_DEBUG_OBJECT (basesink, "latency set to %" GST_TIME_FORMAT,
4471 GST_TIME_ARGS (latency));
4473 /* We forward this event so that all elements know about the global pipeline
4474 * latency. This is interesting for an element when it wants to figure out
4475 * when a particular piece of data will be rendered. */
4478 case GST_EVENT_SEEK:
4479 /* in pull mode we will execute the seek */
4480 if (mode == GST_PAD_MODE_PULL)
4481 result = gst_base_sink_perform_seek (basesink, pad, event);
4483 case GST_EVENT_STEP:
4484 result = gst_base_sink_perform_step (basesink, pad, event);
4492 result = gst_pad_push_event (pad, event);
4494 /* not forwarded, unref the event */
4495 gst_event_unref (event);
4498 gst_object_unref (pad);
4500 GST_DEBUG_OBJECT (basesink, "handled event %p %" GST_PTR_FORMAT ": %d", event,
4507 gst_base_sink_get_position (GstBaseSink * basesink, GstFormat format,
4508 gint64 * cur, gboolean * upstream)
4510 GstClock *clock = NULL;
4511 gboolean res = FALSE;
4513 GstSegment *segment;
4514 GstClockTime now, latency;
4515 GstClockTimeDiff base_time;
4516 gint64 time, base, duration;
4519 gboolean last_seen, with_clock, in_paused;
4521 GST_OBJECT_LOCK (basesink);
4522 /* we can only get the segment when we are not NULL or READY */
4523 if (!basesink->have_newsegment)
4527 /* when not in PLAYING or when we're busy with a state change, we
4528 * cannot read from the clock so we report time based on the
4529 * last seen timestamp. */
4530 if (GST_STATE (basesink) != GST_STATE_PLAYING ||
4531 GST_STATE_PENDING (basesink) != GST_STATE_VOID_PENDING) {
4535 segment = &basesink->segment;
4537 /* get the format in the segment */
4538 oformat = segment->format;
4540 /* report with last seen position when EOS */
4541 last_seen = basesink->eos;
4543 /* assume we will use the clock for getting the current position */
4545 if (!basesink->sync)
4548 /* and we need a clock */
4549 if (G_UNLIKELY ((clock = GST_ELEMENT_CLOCK (basesink)) == NULL))
4552 gst_object_ref (clock);
4554 /* mainloop might be querying position when going to playing async,
4555 * while (audio) rendering might be quickly advancing stream position,
4556 * so use clock asap rather than last reported position */
4557 if (in_paused && with_clock && g_atomic_int_get (&basesink->priv->to_playing)) {
4558 GST_DEBUG_OBJECT (basesink, "going to PLAYING, so not PAUSED");
4562 /* collect all data we need holding the lock */
4563 if (GST_CLOCK_TIME_IS_VALID (segment->time))
4564 time = segment->time;
4568 if (GST_CLOCK_TIME_IS_VALID (segment->stop))
4569 duration = segment->stop - segment->start;
4573 base = segment->base;
4574 rate = segment->rate * segment->applied_rate;
4575 latency = basesink->priv->latency;
4578 /* in paused, use start_time */
4579 base_time = GST_ELEMENT_START_TIME (basesink);
4580 GST_DEBUG_OBJECT (basesink, "in paused, using start time %" GST_TIME_FORMAT,
4581 GST_TIME_ARGS (base_time));
4582 } else if (with_clock) {
4583 /* else use clock when needed */
4584 base_time = GST_ELEMENT_CAST (basesink)->base_time;
4585 GST_DEBUG_OBJECT (basesink, "using clock and base time %" GST_TIME_FORMAT,
4586 GST_TIME_ARGS (base_time));
4588 /* else, no sync or clock -> no base time */
4589 GST_DEBUG_OBJECT (basesink, "no sync or no clock");
4593 /* no base_time, we can't calculate running_time, use last seem timestamp to report
4595 if (base_time == -1)
4598 if (oformat == GST_FORMAT_TIME) {
4601 start = basesink->priv->current_sstart;
4602 stop = basesink->priv->current_sstop;
4605 /* when we don't use the clock, we use the last position as a lower bound */
4606 if (stop == -1 || segment->rate > 0.0)
4611 GST_DEBUG_OBJECT (basesink, "in PAUSED using last %" GST_TIME_FORMAT,
4612 GST_TIME_ARGS (last));
4614 /* in playing and paused, use last stop time as upper bound */
4615 if (start == -1 || segment->rate > 0.0)
4620 GST_DEBUG_OBJECT (basesink, "in PLAYING using last %" GST_TIME_FORMAT,
4621 GST_TIME_ARGS (last));
4624 /* convert position to stream time */
4625 last = gst_segment_to_stream_time (segment, oformat, segment->position);
4627 GST_DEBUG_OBJECT (basesink, "in using last %" G_GINT64_FORMAT, last);
4630 /* need to release the object lock before we can get the time,
4631 * a clock might take the LOCK of the provider, which could be
4632 * a basesink subclass. */
4633 GST_OBJECT_UNLOCK (basesink);
4636 /* in EOS or when no valid stream_time, report the value of last seen
4639 /* no timestamp, we need to ask upstream */
4640 GST_DEBUG_OBJECT (basesink, "no last seen timestamp, asking upstream");
4645 GST_DEBUG_OBJECT (basesink, "using last seen timestamp %" GST_TIME_FORMAT,
4646 GST_TIME_ARGS (last));
4649 if (oformat != GST_FORMAT_TIME) {
4650 /* convert base, time and duration to time */
4651 if (!gst_pad_query_convert (basesink->sinkpad, oformat, base,
4652 GST_FORMAT_TIME, &base))
4653 goto convert_failed;
4654 if (!gst_pad_query_convert (basesink->sinkpad, oformat, duration,
4655 GST_FORMAT_TIME, &duration))
4656 goto convert_failed;
4657 if (!gst_pad_query_convert (basesink->sinkpad, oformat, time,
4658 GST_FORMAT_TIME, &time))
4659 goto convert_failed;
4660 if (!gst_pad_query_convert (basesink->sinkpad, oformat, last,
4661 GST_FORMAT_TIME, &last))
4662 goto convert_failed;
4664 /* assume time format from now on */
4665 oformat = GST_FORMAT_TIME;
4668 if (!in_paused && with_clock) {
4669 now = gst_clock_get_time (clock);
4675 /* subtract base time and base time from the clock time.
4676 * Make sure we don't go negative. This is the current time in
4677 * the segment which we need to scale with the combined
4678 * rate and applied rate. */
4680 base_time += latency;
4681 if (GST_CLOCK_DIFF (base_time, now) < 0)
4684 /* for negative rates we need to count back from the segment
4689 *cur = time + gst_guint64_to_gdouble (now - base_time) * rate;
4691 /* never report more than last seen position */
4694 *cur = MIN (last, *cur);
4696 *cur = MAX (last, *cur);
4699 GST_DEBUG_OBJECT (basesink,
4700 "now %" GST_TIME_FORMAT " - base_time %" GST_TIME_FORMAT " - base %"
4701 GST_TIME_FORMAT " + time %" GST_TIME_FORMAT " last %" GST_TIME_FORMAT,
4702 GST_TIME_ARGS (now), GST_TIME_ARGS (base_time), GST_TIME_ARGS (base),
4703 GST_TIME_ARGS (time), GST_TIME_ARGS (last));
4706 if (oformat != format) {
4707 /* convert to final format */
4708 if (!gst_pad_query_convert (basesink->sinkpad, oformat, *cur, format, cur))
4709 goto convert_failed;
4715 GST_DEBUG_OBJECT (basesink, "res: %d, POSITION: %" GST_TIME_FORMAT,
4716 res, GST_TIME_ARGS (*cur));
4719 gst_object_unref (clock);
4726 /* in NULL or READY we always return FALSE and -1 */
4727 GST_DEBUG_OBJECT (basesink, "position in wrong state, return -1");
4730 GST_OBJECT_UNLOCK (basesink);
4735 GST_DEBUG_OBJECT (basesink, "convert failed, try upstream");
4743 gst_base_sink_get_duration (GstBaseSink * basesink, GstFormat format,
4744 gint64 * dur, gboolean * upstream)
4746 gboolean res = FALSE;
4748 if (basesink->pad_mode == GST_PAD_MODE_PULL) {
4751 /* get the duration in bytes, in pull mode that's all we are sure to
4752 * know. We have to explicitly get this value from upstream instead of
4753 * using our cached value because it might change. Duration caching
4754 * should be done at a higher level. */
4756 gst_pad_peer_query_duration (basesink->sinkpad, GST_FORMAT_BYTES,
4759 basesink->segment.duration = uduration;
4760 if (format != GST_FORMAT_BYTES) {
4761 /* convert to the requested format */
4763 gst_pad_query_convert (basesink->sinkpad, GST_FORMAT_BYTES,
4764 uduration, format, dur);
4778 default_element_query (GstElement * element, GstQuery * query)
4780 gboolean res = FALSE;
4782 GstBaseSink *basesink = GST_BASE_SINK (element);
4784 switch (GST_QUERY_TYPE (query)) {
4785 case GST_QUERY_POSITION:
4789 gboolean upstream = FALSE;
4791 gst_query_parse_position (query, &format, NULL);
4793 GST_DEBUG_OBJECT (basesink, "position query in format %s",
4794 gst_format_get_name (format));
4796 /* first try to get the position based on the clock */
4798 gst_base_sink_get_position (basesink, format, &cur, &upstream))) {
4799 gst_query_set_position (query, format, cur);
4800 } else if (upstream) {
4801 /* fallback to peer query */
4802 res = gst_pad_peer_query (basesink->sinkpad, query);
4805 /* we can handle a few things if upstream failed */
4806 if (format == GST_FORMAT_PERCENT) {
4809 res = gst_base_sink_get_position (basesink, GST_FORMAT_TIME, &cur,
4811 if (!res && upstream) {
4813 gst_pad_peer_query_position (basesink->sinkpad, GST_FORMAT_TIME,
4817 res = gst_base_sink_get_duration (basesink, GST_FORMAT_TIME, &dur,
4819 if (!res && upstream) {
4821 gst_pad_peer_query_duration (basesink->sinkpad,
4822 GST_FORMAT_TIME, &dur);
4828 pos = gst_util_uint64_scale (100 * GST_FORMAT_PERCENT_SCALE, cur,
4830 gst_query_set_position (query, GST_FORMAT_PERCENT, pos);
4836 case GST_QUERY_DURATION:
4840 gboolean upstream = FALSE;
4842 gst_query_parse_duration (query, &format, NULL);
4844 GST_DEBUG_OBJECT (basesink, "duration query in format %s",
4845 gst_format_get_name (format));
4848 gst_base_sink_get_duration (basesink, format, &dur, &upstream))) {
4849 gst_query_set_duration (query, format, dur);
4850 } else if (upstream) {
4851 /* fallback to peer query */
4852 res = gst_pad_peer_query (basesink->sinkpad, query);
4855 /* we can handle a few things if upstream failed */
4856 if (format == GST_FORMAT_PERCENT) {
4857 gst_query_set_duration (query, GST_FORMAT_PERCENT,
4858 GST_FORMAT_PERCENT_MAX);
4864 case GST_QUERY_LATENCY:
4866 gboolean live, us_live;
4867 GstClockTime min, max;
4869 if ((res = gst_base_sink_query_latency (basesink, &live, &us_live, &min,
4871 gst_query_set_latency (query, live, min, max);
4875 case GST_QUERY_JITTER:
4877 case GST_QUERY_RATE:
4878 /* gst_query_set_rate (query, basesink->segment_rate); */
4881 case GST_QUERY_SEGMENT:
4883 if (basesink->pad_mode == GST_PAD_MODE_PULL) {
4887 format = basesink->segment.format;
4890 gst_segment_to_stream_time (&basesink->segment, format,
4891 basesink->segment.start);
4892 if ((stop = basesink->segment.stop) == -1)
4893 stop = basesink->segment.duration;
4895 stop = gst_segment_to_stream_time (&basesink->segment, format, stop);
4897 gst_query_set_segment (query, basesink->segment.rate, format, start,
4901 res = gst_pad_peer_query (basesink->sinkpad, query);
4905 case GST_QUERY_SEEKING:
4906 case GST_QUERY_CONVERT:
4907 case GST_QUERY_FORMATS:
4909 res = gst_pad_peer_query (basesink->sinkpad, query);
4912 GST_DEBUG_OBJECT (basesink, "query %s returns %d",
4913 GST_QUERY_TYPE_NAME (query), res);
4918 gst_base_sink_drain (GstBaseSink * basesink)
4921 GstBufferList *old_list;
4923 GST_OBJECT_LOCK (basesink);
4924 if ((old = basesink->priv->last_buffer))
4925 basesink->priv->last_buffer = gst_buffer_copy_deep (old);
4927 if ((old_list = basesink->priv->last_buffer_list))
4928 basesink->priv->last_buffer_list = gst_buffer_list_copy_deep (old_list);
4929 GST_OBJECT_UNLOCK (basesink);
4932 gst_buffer_unref (old);
4934 gst_mini_object_unref (GST_MINI_OBJECT_CAST (old_list));
4938 gst_base_sink_default_query (GstBaseSink * basesink, GstQuery * query)
4941 GstBaseSinkClass *bclass;
4943 bclass = GST_BASE_SINK_GET_CLASS (basesink);
4945 switch (GST_QUERY_TYPE (query)) {
4946 case GST_QUERY_ALLOCATION:
4948 gst_base_sink_drain (basesink);
4949 if (bclass->propose_allocation)
4950 res = bclass->propose_allocation (basesink, query);
4955 case GST_QUERY_CAPS:
4957 GstCaps *caps, *filter;
4959 gst_query_parse_caps (query, &filter);
4960 caps = gst_base_sink_query_caps (basesink, basesink->sinkpad, filter);
4961 gst_query_set_caps_result (query, caps);
4962 gst_caps_unref (caps);
4966 case GST_QUERY_ACCEPT_CAPS:
4968 GstCaps *caps, *allowed;
4971 /* slightly faster than the default implementation */
4972 gst_query_parse_accept_caps (query, &caps);
4973 allowed = gst_base_sink_query_caps (basesink, basesink->sinkpad, NULL);
4974 subset = gst_caps_is_subset (caps, allowed);
4975 GST_DEBUG_OBJECT (basesink, "Checking if requested caps %" GST_PTR_FORMAT
4976 " are a subset of pad caps %" GST_PTR_FORMAT " result %d", caps,
4978 gst_caps_unref (allowed);
4979 gst_query_set_accept_caps_result (query, subset);
4983 case GST_QUERY_DRAIN:
4985 gst_base_sink_drain (basesink);
4989 case GST_QUERY_POSITION:
4991 res = default_element_query (GST_ELEMENT (basesink), query);
4996 gst_pad_query_default (basesink->sinkpad, GST_OBJECT_CAST (basesink),
5004 gst_base_sink_sink_query (GstPad * pad, GstObject * parent, GstQuery * query)
5006 GstBaseSink *basesink;
5007 GstBaseSinkClass *bclass;
5010 basesink = GST_BASE_SINK_CAST (parent);
5011 bclass = GST_BASE_SINK_GET_CLASS (basesink);
5014 res = bclass->query (basesink, query);
5021 static GstStateChangeReturn
5022 gst_base_sink_change_state (GstElement * element, GstStateChange transition)
5024 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
5025 GstBaseSink *basesink = GST_BASE_SINK (element);
5026 GstBaseSinkClass *bclass;
5027 GstBaseSinkPrivate *priv;
5029 priv = basesink->priv;
5031 bclass = GST_BASE_SINK_GET_CLASS (basesink);
5033 switch (transition) {
5034 case GST_STATE_CHANGE_NULL_TO_READY:
5036 if (!bclass->start (basesink))
5039 case GST_STATE_CHANGE_READY_TO_PAUSED:
5040 /* need to complete preroll before this state change completes, there
5041 * is no data flow in READY so we can safely assume we need to preroll. */
5042 GST_BASE_SINK_PREROLL_LOCK (basesink);
5043 GST_DEBUG_OBJECT (basesink, "READY to PAUSED");
5044 basesink->have_newsegment = FALSE;
5045 gst_segment_init (&basesink->segment, GST_FORMAT_UNDEFINED);
5046 basesink->offset = 0;
5047 basesink->have_preroll = FALSE;
5048 priv->step_unlock = FALSE;
5049 basesink->need_preroll = TRUE;
5050 basesink->playing_async = TRUE;
5051 priv->current_sstart = GST_CLOCK_TIME_NONE;
5052 priv->current_sstop = GST_CLOCK_TIME_NONE;
5053 priv->eos_rtime = GST_CLOCK_TIME_NONE;
5055 basesink->eos = FALSE;
5056 priv->received_eos = FALSE;
5057 gst_base_sink_reset_qos (basesink);
5059 priv->commited = FALSE;
5060 priv->call_preroll = TRUE;
5061 priv->current_step.valid = FALSE;
5062 priv->pending_step.valid = FALSE;
5063 if (priv->async_enabled) {
5064 GST_DEBUG_OBJECT (basesink, "doing async state change");
5065 /* when async enabled, post async-start message and return ASYNC from
5066 * the state change function */
5067 ret = GST_STATE_CHANGE_ASYNC;
5068 gst_element_post_message (GST_ELEMENT_CAST (basesink),
5069 gst_message_new_async_start (GST_OBJECT_CAST (basesink)));
5071 priv->have_latency = TRUE;
5073 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
5075 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
5076 GST_BASE_SINK_PREROLL_LOCK (basesink);
5077 g_atomic_int_set (&basesink->priv->to_playing, TRUE);
5078 if (!gst_base_sink_needs_preroll (basesink)) {
5079 GST_DEBUG_OBJECT (basesink, "PAUSED to PLAYING, don't need preroll");
5080 /* no preroll needed anymore now. */
5081 basesink->playing_async = FALSE;
5082 basesink->need_preroll = FALSE;
5083 if (basesink->eos) {
5084 GstMessage *message;
5086 /* need to post EOS message here */
5087 GST_DEBUG_OBJECT (basesink, "Now posting EOS");
5088 message = gst_message_new_eos (GST_OBJECT_CAST (basesink));
5089 gst_message_set_seqnum (message, basesink->priv->seqnum);
5090 gst_element_post_message (GST_ELEMENT_CAST (basesink), message);
5092 GST_DEBUG_OBJECT (basesink, "signal preroll");
5093 GST_BASE_SINK_PREROLL_SIGNAL (basesink);
5096 GST_DEBUG_OBJECT (basesink, "PAUSED to PLAYING, we are not prerolled");
5097 basesink->need_preroll = TRUE;
5098 basesink->playing_async = TRUE;
5099 priv->call_preroll = TRUE;
5100 priv->commited = FALSE;
5101 if (priv->async_enabled) {
5102 GST_DEBUG_OBJECT (basesink, "doing async state change");
5103 ret = GST_STATE_CHANGE_ASYNC;
5104 gst_element_post_message (GST_ELEMENT_CAST (basesink),
5105 gst_message_new_async_start (GST_OBJECT_CAST (basesink)));
5108 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
5115 GstStateChangeReturn bret;
5117 bret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
5118 if (G_UNLIKELY (bret == GST_STATE_CHANGE_FAILURE))
5119 goto activate_failed;
5122 switch (transition) {
5123 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
5124 /* completed transition, so need not be marked any longer
5125 * And it should be unmarked, since e.g. losing our position upon flush
5126 * does not really change state to PAUSED ... */
5127 g_atomic_int_set (&basesink->priv->to_playing, FALSE);
5129 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
5130 g_atomic_int_set (&basesink->priv->to_playing, FALSE);
5131 GST_DEBUG_OBJECT (basesink, "PLAYING to PAUSED");
5132 /* FIXME, make sure we cannot enter _render first */
5134 /* we need to call ::unlock before locking PREROLL_LOCK
5135 * since we lock it before going into ::render */
5137 bclass->unlock (basesink);
5139 GST_BASE_SINK_PREROLL_LOCK (basesink);
5140 GST_DEBUG_OBJECT (basesink, "got preroll lock");
5141 /* now that we have the PREROLL lock, clear our unlock request */
5142 if (bclass->unlock_stop)
5143 bclass->unlock_stop (basesink);
5145 if (basesink->clock_id) {
5146 GST_DEBUG_OBJECT (basesink, "unschedule clock");
5147 gst_clock_id_unschedule (basesink->clock_id);
5150 /* if we don't have a preroll buffer we need to wait for a preroll and
5152 if (!gst_base_sink_needs_preroll (basesink)) {
5153 GST_DEBUG_OBJECT (basesink, "PLAYING to PAUSED, we are prerolled");
5154 basesink->playing_async = FALSE;
5155 basesink->need_preroll = FALSE;
5157 if (GST_STATE_TARGET (GST_ELEMENT (basesink)) <= GST_STATE_READY) {
5158 GST_DEBUG_OBJECT (basesink, "element is <= READY");
5159 ret = GST_STATE_CHANGE_SUCCESS;
5161 GST_DEBUG_OBJECT (basesink,
5162 "PLAYING to PAUSED, we are not prerolled");
5163 basesink->playing_async = TRUE;
5164 basesink->need_preroll = TRUE;
5165 priv->commited = FALSE;
5166 priv->call_preroll = TRUE;
5167 if (priv->async_enabled) {
5168 GST_DEBUG_OBJECT (basesink, "doing async state change");
5169 ret = GST_STATE_CHANGE_ASYNC;
5170 gst_element_post_message (GST_ELEMENT_CAST (basesink),
5171 gst_message_new_async_start (GST_OBJECT_CAST (basesink)));
5175 GST_DEBUG_OBJECT (basesink, "rendered: %" G_GUINT64_FORMAT
5176 ", dropped: %" G_GUINT64_FORMAT, priv->rendered, priv->dropped);
5178 gst_base_sink_reset_qos (basesink);
5179 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
5181 case GST_STATE_CHANGE_PAUSED_TO_READY:
5182 GST_BASE_SINK_PREROLL_LOCK (basesink);
5183 /* start by resetting our position state with the object lock so that the
5184 * position query gets the right idea. We do this before we post the
5185 * messages so that the message handlers pick this up. */
5186 GST_OBJECT_LOCK (basesink);
5187 basesink->have_newsegment = FALSE;
5188 priv->current_sstart = GST_CLOCK_TIME_NONE;
5189 priv->current_sstop = GST_CLOCK_TIME_NONE;
5190 priv->have_latency = FALSE;
5191 if (priv->cached_clock_id) {
5192 gst_clock_id_unref (priv->cached_clock_id);
5193 priv->cached_clock_id = NULL;
5195 gst_caps_replace (&basesink->priv->caps, NULL);
5196 GST_OBJECT_UNLOCK (basesink);
5198 gst_base_sink_set_last_buffer (basesink, NULL);
5199 gst_base_sink_set_last_buffer_list (basesink, NULL);
5200 priv->call_preroll = FALSE;
5202 if (!priv->commited) {
5203 if (priv->async_enabled) {
5204 GST_DEBUG_OBJECT (basesink, "PAUSED to READY, posting async-done");
5206 gst_element_post_message (GST_ELEMENT_CAST (basesink),
5207 gst_message_new_state_changed (GST_OBJECT_CAST (basesink),
5208 GST_STATE_PLAYING, GST_STATE_PAUSED, GST_STATE_READY));
5210 gst_element_post_message (GST_ELEMENT_CAST (basesink),
5211 gst_message_new_async_done (GST_OBJECT_CAST (basesink),
5212 GST_CLOCK_TIME_NONE));
5214 priv->commited = TRUE;
5216 GST_DEBUG_OBJECT (basesink, "PAUSED to READY, don't need_preroll");
5218 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
5220 case GST_STATE_CHANGE_READY_TO_NULL:
5222 if (!bclass->stop (basesink)) {
5223 GST_WARNING_OBJECT (basesink, "failed to stop");
5226 gst_base_sink_set_last_buffer (basesink, NULL);
5227 gst_base_sink_set_last_buffer_list (basesink, NULL);
5228 priv->call_preroll = FALSE;
5239 GST_DEBUG_OBJECT (basesink, "failed to start");
5240 return GST_STATE_CHANGE_FAILURE;
5244 GST_DEBUG_OBJECT (basesink,
5245 "element failed to change states -- activation problem?");
5246 return GST_STATE_CHANGE_FAILURE;