2 * Copyright (C) 2005-2007 Wim Taymans <wim.taymans@gmail.com>
4 * gstbasesink.c: Base class for sink elements
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
24 * @short_description: Base class for sink elements
25 * @see_also: #GstBaseTransform, #GstBaseSrc
27 * #GstBaseSink is the base class for sink elements in GStreamer, such as
28 * xvimagesink or filesink. It is a layer on top of #GstElement that provides a
29 * simplified interface to plugin writers. #GstBaseSink handles many details
30 * for you, for example: preroll, clock synchronization, state changes,
31 * activation in push or pull mode, and queries.
33 * In most cases, when writing sink elements, there is no need to implement
34 * class methods from #GstElement or to set functions on pads, because the
35 * #GstBaseSink infrastructure should be sufficient.
37 * #GstBaseSink provides support for exactly one sink pad, which should be
38 * named "sink". A sink implementation (subclass of #GstBaseSink) should
39 * install a pad template in its class_init function, like so:
42 * my_element_class_init (GstMyElementClass *klass)
44 * GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
46 * // sinktemplate should be a #GstStaticPadTemplate with direction
47 * // #GST_PAD_SINK and name "sink"
48 * gst_element_class_add_pad_template (gstelement_class,
49 * gst_static_pad_template_get (&sinktemplate));
50 * // see #GstElementDetails
51 * gst_element_class_set_details (gstelement_class, &details);
55 * #GstBaseSink will handle the prerolling correctly. This means that it will
56 * return #GST_STATE_CHANGE_ASYNC from a state change to PAUSED until the first
57 * buffer arrives in this element. The base class will call the
58 * #GstBaseSinkClass.preroll() vmethod with this preroll buffer and will then
59 * commit the state change to the next asynchronously pending state.
61 * When the element is set to PLAYING, #GstBaseSink will synchronise on the
62 * clock using the times returned from #GstBaseSinkClass.get_times(). If this
63 * function returns #GST_CLOCK_TIME_NONE for the start time, no synchronisation
64 * will be done. Synchronisation can be disabled entirely by setting the object
65 * #GstBaseSink:sync property to %FALSE.
67 * After synchronisation the virtual method #GstBaseSinkClass.render() will be
68 * called. Subclasses should minimally implement this method.
70 * Since 0.10.3 subclasses that synchronise on the clock in the
71 * #GstBaseSinkClass.render() method are supported as well. These classes
72 * typically receive a buffer in the render method and can then potentially
73 * block on the clock while rendering. A typical example is an audiosink.
74 * Since 0.10.11 these subclasses can use gst_base_sink_wait_preroll() to
75 * perform the blocking wait.
77 * Upon receiving the EOS event in the PLAYING state, #GstBaseSink will wait
78 * for the clock to reach the time indicated by the stop time of the last
79 * #GstBaseSinkClass.get_times() call before posting an EOS message. When the
80 * element receives EOS in PAUSED, preroll completes, the event is queued and an
81 * EOS message is posted when going to PLAYING.
83 * #GstBaseSink will internally use the #GST_EVENT_NEWSEGMENT events to schedule
84 * synchronisation and clipping of buffers. Buffers that fall completely outside
85 * of the current segment are dropped. Buffers that fall partially in the
86 * segment are rendered (and prerolled). Subclasses should do any subbuffer
87 * clipping themselves when needed.
89 * #GstBaseSink will by default report the current playback position in
90 * #GST_FORMAT_TIME based on the current clock time and segment information.
91 * If no clock has been set on the element, the query will be forwarded
94 * The #GstBaseSinkClass.set_caps() function will be called when the subclass
95 * should configure itself to process a specific media type.
97 * The #GstBaseSinkClass.start() and #GstBaseSinkClass.stop() virtual methods
98 * will be called when resources should be allocated. Any
99 * #GstBaseSinkClass.preroll(), #GstBaseSinkClass.render() and
100 * #GstBaseSinkClass.set_caps() function will be called between the
101 * #GstBaseSinkClass.start() and #GstBaseSinkClass.stop() calls.
103 * The #GstBaseSinkClass.event() virtual method will be called when an event is
104 * received by #GstBaseSink. Normally this method should only be overriden by
105 * very specific elements (such as file sinks) which need to handle the
106 * newsegment event specially.
108 * The #GstBaseSinkClass.unlock() method is called when the elements should
109 * unblock any blocking operations they perform in the
110 * #GstBaseSinkClass.render() method. This is mostly useful when the
111 * #GstBaseSinkClass.render() method performs a blocking write on a file
112 * descriptor, for example.
114 * The #GstBaseSink:max-lateness property affects how the sink deals with
115 * buffers that arrive too late in the sink. A buffer arrives too late in the
116 * sink when the presentation time (as a combination of the last segment, buffer
117 * timestamp and element base_time) plus the duration is before the current
119 * If the frame is later than max-lateness, the sink will drop the buffer
120 * without calling the render method.
121 * This feature is disabled if sync is disabled, the
122 * #GstBaseSinkClass.get_times() method does not return a valid start time or
123 * max-lateness is set to -1 (the default).
124 * Subclasses can use gst_base_sink_set_max_lateness() to configure the
125 * max-lateness value.
127 * The #GstBaseSink:qos property will enable the quality-of-service features of
128 * the basesink which gather statistics about the real-time performance of the
129 * clock synchronisation. For each buffer received in the sink, statistics are
130 * gathered and a QOS event is sent upstream with these numbers. This
131 * information can then be used by upstream elements to reduce their processing
134 * Since 0.10.15 the #GstBaseSink:async property can be used to instruct the
135 * sink to never perform an ASYNC state change. This feature is mostly usable
136 * when dealing with non-synchronized streams or sparse streams.
138 * Last reviewed on 2007-08-29 (0.10.15)
145 #include <gst/gst_private.h>
147 #include "gstbasesink.h"
148 #include <gst/gstmarshal.h>
149 #include <gst/gst-i18n-lib.h>
151 GST_DEBUG_CATEGORY_STATIC (gst_base_sink_debug);
152 #define GST_CAT_DEFAULT gst_base_sink_debug
154 #define GST_BASE_SINK_GET_PRIVATE(obj) \
155 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_BASE_SINK, GstBaseSinkPrivate))
157 #define GST_FLOW_STEP GST_FLOW_CUSTOM_ERROR
161 gboolean valid; /* if this info is valid */
162 guint32 seqnum; /* the seqnum of the STEP event */
163 GstFormat format; /* the format of the amount */
164 guint64 amount; /* the total amount of data to skip */
165 guint64 position; /* the position in the stepped data */
166 guint64 duration; /* the duration in time of the skipped data */
167 guint64 start; /* running_time of the start */
168 gdouble rate; /* rate of skipping */
169 gdouble start_rate; /* rate before skipping */
170 guint64 start_start; /* start position skipping */
171 guint64 start_stop; /* stop position skipping */
172 gboolean flush; /* if this was a flushing step */
173 gboolean intermediate; /* if this is an intermediate step */
174 gboolean need_preroll; /* if we need preroll after this step */
177 /* FIXME, some stuff in ABI.data and other in Private...
178 * Make up your mind please.
180 struct _GstBaseSinkPrivate
182 gint qos_enabled; /* ATOMIC */
183 gboolean async_enabled;
184 GstClockTimeDiff ts_offset;
185 GstClockTime render_delay;
187 /* start, stop of current buffer, stream time, used to report position */
188 GstClockTime current_sstart;
189 GstClockTime current_sstop;
191 /* start, stop and jitter of current buffer, running time */
192 GstClockTime current_rstart;
193 GstClockTime current_rstop;
194 GstClockTimeDiff current_jitter;
195 /* the running time of the previous buffer */
196 GstClockTime prev_rstart;
198 /* EOS sync time in running time */
199 GstClockTime eos_rtime;
201 /* last buffer that arrived in time, running time */
202 GstClockTime last_render_time;
203 /* when the last buffer left the sink, running time */
204 GstClockTime last_left;
206 /* running averages go here these are done on running time */
208 GstClockTime avg_duration;
210 GstClockTime avg_in_diff;
212 /* these are done on system time. avg_jitter and avg_render are
213 * compared to eachother to see if the rendering time takes a
214 * huge amount of the processing, If so we are flooded with
216 GstClockTime last_left_systime;
217 GstClockTime avg_jitter;
218 GstClockTime start, stop;
219 GstClockTime avg_render;
221 /* number of rendered and dropped frames */
226 GstClockTime latency;
228 /* if we already commited the state */
231 /* when we received EOS */
232 gboolean received_eos;
234 /* when we are prerolled and able to report latency */
235 gboolean have_latency;
237 /* the last buffer we prerolled or rendered. Useful for making snapshots */
238 gint enable_last_buffer; /* atomic */
239 GstBuffer *last_buffer;
241 /* caps for pull based scheduling */
244 /* blocksize for pulling */
249 /* seqnum of the stream */
252 gboolean call_preroll;
253 gboolean step_unlock;
255 /* we have a pending and a current step operation */
256 GstStepInfo current_step;
257 GstStepInfo pending_step;
259 /* Cached GstClockID */
260 GstClockID cached_clock_id;
262 /* for throttling and QoS */
263 GstClockTime earliest_in_time;
264 GstClockTime throttle_time;
267 #define DO_RUNNING_AVG(avg,val,size) (((val) + ((size)-1) * (avg)) / (size))
269 /* generic running average, this has a neutral window size */
270 #define UPDATE_RUNNING_AVG(avg,val) DO_RUNNING_AVG(avg,val,8)
272 /* the windows for these running averages are experimentally obtained.
273 * possitive values get averaged more while negative values use a small
274 * window so we can react faster to badness. */
275 #define UPDATE_RUNNING_AVG_P(avg,val) DO_RUNNING_AVG(avg,val,16)
276 #define UPDATE_RUNNING_AVG_N(avg,val) DO_RUNNING_AVG(avg,val,4)
280 _PR_IS_NOTHING = 1 << 0,
281 _PR_IS_BUFFER = 1 << 1,
282 _PR_IS_BUFFERLIST = 1 << 2,
283 _PR_IS_EVENT = 1 << 3
286 #define OBJ_IS_BUFFER(a) ((a) & _PR_IS_BUFFER)
287 #define OBJ_IS_BUFFERLIST(a) ((a) & _PR_IS_BUFFERLIST)
288 #define OBJ_IS_EVENT(a) ((a) & _PR_IS_EVENT)
289 #define OBJ_IS_BUFFERFULL(a) ((a) & (_PR_IS_BUFFER | _PR_IS_BUFFERLIST))
291 /* BaseSink properties */
293 #define DEFAULT_CAN_ACTIVATE_PULL FALSE /* fixme: enable me */
294 #define DEFAULT_CAN_ACTIVATE_PUSH TRUE
296 #define DEFAULT_PREROLL_QUEUE_LEN 0
297 #define DEFAULT_SYNC TRUE
298 #define DEFAULT_MAX_LATENESS -1
299 #define DEFAULT_QOS FALSE
300 #define DEFAULT_ASYNC TRUE
301 #define DEFAULT_TS_OFFSET 0
302 #define DEFAULT_BLOCKSIZE 4096
303 #define DEFAULT_RENDER_DELAY 0
304 #define DEFAULT_ENABLE_LAST_BUFFER TRUE
305 #define DEFAULT_THROTTLE_TIME 0
310 PROP_PREROLL_QUEUE_LEN,
316 PROP_ENABLE_LAST_BUFFER,
324 static GstElementClass *parent_class = NULL;
326 static void gst_base_sink_class_init (GstBaseSinkClass * klass);
327 static void gst_base_sink_init (GstBaseSink * trans, gpointer g_class);
328 static void gst_base_sink_finalize (GObject * object);
331 gst_base_sink_get_type (void)
333 static volatile gsize base_sink_type = 0;
335 if (g_once_init_enter (&base_sink_type)) {
337 static const GTypeInfo base_sink_info = {
338 sizeof (GstBaseSinkClass),
341 (GClassInitFunc) gst_base_sink_class_init,
344 sizeof (GstBaseSink),
346 (GInstanceInitFunc) gst_base_sink_init,
349 _type = g_type_register_static (GST_TYPE_ELEMENT,
350 "GstBaseSink", &base_sink_info, G_TYPE_FLAG_ABSTRACT);
351 g_once_init_leave (&base_sink_type, _type);
353 return base_sink_type;
356 static void gst_base_sink_set_property (GObject * object, guint prop_id,
357 const GValue * value, GParamSpec * pspec);
358 static void gst_base_sink_get_property (GObject * object, guint prop_id,
359 GValue * value, GParamSpec * pspec);
361 static gboolean gst_base_sink_send_event (GstElement * element,
363 static gboolean gst_base_sink_query (GstElement * element, GstQuery * query);
364 static const GstQueryType *gst_base_sink_get_query_types (GstElement * element);
366 static GstCaps *gst_base_sink_get_caps (GstBaseSink * sink, GstCaps * caps);
367 static gboolean gst_base_sink_set_caps (GstBaseSink * sink, GstCaps * caps);
368 static void gst_base_sink_get_times (GstBaseSink * basesink, GstBuffer * buffer,
369 GstClockTime * start, GstClockTime * end);
370 static gboolean gst_base_sink_set_flushing (GstBaseSink * basesink,
371 GstPad * pad, gboolean flushing);
372 static gboolean gst_base_sink_default_activate_pull (GstBaseSink * basesink,
374 static gboolean gst_base_sink_default_do_seek (GstBaseSink * sink,
375 GstSegment * segment);
376 static gboolean gst_base_sink_default_prepare_seek_segment (GstBaseSink * sink,
377 GstEvent * event, GstSegment * segment);
379 static GstStateChangeReturn gst_base_sink_change_state (GstElement * element,
380 GstStateChange transition);
382 static GstFlowReturn gst_base_sink_chain (GstPad * pad, GstBuffer * buffer);
383 static GstFlowReturn gst_base_sink_chain_list (GstPad * pad,
384 GstBufferList * list);
386 static void gst_base_sink_loop (GstPad * pad);
387 static gboolean gst_base_sink_pad_activate (GstPad * pad);
388 static gboolean gst_base_sink_pad_activate_push (GstPad * pad, gboolean active);
389 static gboolean gst_base_sink_pad_activate_pull (GstPad * pad, gboolean active);
390 static gboolean gst_base_sink_event (GstPad * pad, GstEvent * event);
392 static gboolean gst_base_sink_negotiate_pull (GstBaseSink * basesink);
393 static GstCaps *gst_base_sink_pad_getcaps (GstPad * pad, GstCaps * filter);
394 static void gst_base_sink_pad_fixate (GstPad * pad, GstCaps * caps);
396 /* check if an object was too late */
397 static gboolean gst_base_sink_is_too_late (GstBaseSink * basesink,
398 GstMiniObject * obj, GstClockTime rstart, GstClockTime rstop,
399 GstClockReturn status, GstClockTimeDiff jitter);
400 static GstFlowReturn gst_base_sink_preroll_object (GstBaseSink * basesink,
401 guint8 obj_type, GstMiniObject * obj);
404 gst_base_sink_class_init (GstBaseSinkClass * klass)
406 GObjectClass *gobject_class;
407 GstElementClass *gstelement_class;
409 gobject_class = G_OBJECT_CLASS (klass);
410 gstelement_class = GST_ELEMENT_CLASS (klass);
412 GST_DEBUG_CATEGORY_INIT (gst_base_sink_debug, "basesink", 0,
415 g_type_class_add_private (klass, sizeof (GstBaseSinkPrivate));
417 parent_class = g_type_class_peek_parent (klass);
419 gobject_class->finalize = gst_base_sink_finalize;
420 gobject_class->set_property = gst_base_sink_set_property;
421 gobject_class->get_property = gst_base_sink_get_property;
423 /* FIXME, this next value should be configured using an event from the
424 * upstream element, ie, the BUFFER_SIZE event. */
425 g_object_class_install_property (gobject_class, PROP_PREROLL_QUEUE_LEN,
426 g_param_spec_uint ("preroll-queue-len", "Preroll queue length",
427 "Number of buffers to queue during preroll", 0, G_MAXUINT,
428 DEFAULT_PREROLL_QUEUE_LEN,
429 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
431 g_object_class_install_property (gobject_class, PROP_SYNC,
432 g_param_spec_boolean ("sync", "Sync", "Sync on the clock", DEFAULT_SYNC,
433 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
435 g_object_class_install_property (gobject_class, PROP_MAX_LATENESS,
436 g_param_spec_int64 ("max-lateness", "Max Lateness",
437 "Maximum number of nanoseconds that a buffer can be late before it "
438 "is dropped (-1 unlimited)", -1, G_MAXINT64, DEFAULT_MAX_LATENESS,
439 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
441 g_object_class_install_property (gobject_class, PROP_QOS,
442 g_param_spec_boolean ("qos", "Qos",
443 "Generate Quality-of-Service events upstream", DEFAULT_QOS,
444 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
448 * If set to #TRUE, the basesink will perform asynchronous state changes.
449 * When set to #FALSE, the sink will not signal the parent when it prerolls.
450 * Use this option when dealing with sparse streams or when synchronisation is
455 g_object_class_install_property (gobject_class, PROP_ASYNC,
456 g_param_spec_boolean ("async", "Async",
457 "Go asynchronously to PAUSED", DEFAULT_ASYNC,
458 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
460 * GstBaseSink:ts-offset
462 * Controls the final synchronisation, a negative value will render the buffer
463 * earlier while a positive value delays playback. This property can be
464 * used to fix synchronisation in bad files.
468 g_object_class_install_property (gobject_class, PROP_TS_OFFSET,
469 g_param_spec_int64 ("ts-offset", "TS Offset",
470 "Timestamp offset in nanoseconds", G_MININT64, G_MAXINT64,
471 DEFAULT_TS_OFFSET, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
474 * GstBaseSink:enable-last-buffer
476 * Enable the last-buffer property. If FALSE, basesink doesn't keep a
477 * reference to the last buffer arrived and the last-buffer property is always
478 * set to NULL. This can be useful if you need buffers to be released as soon
479 * as possible, eg. if you're using a buffer pool.
483 g_object_class_install_property (gobject_class, PROP_ENABLE_LAST_BUFFER,
484 g_param_spec_boolean ("enable-last-buffer", "Enable Last Buffer",
485 "Enable the last-buffer property", DEFAULT_ENABLE_LAST_BUFFER,
486 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
489 * GstBaseSink:last-buffer
491 * The last buffer that arrived in the sink and was used for preroll or for
492 * rendering. This property can be used to generate thumbnails. This property
493 * can be NULL when the sink has not yet received a bufer.
497 g_object_class_install_property (gobject_class, PROP_LAST_BUFFER,
498 g_param_spec_boxed ("last-buffer", "Last Buffer",
499 "The last buffer received in the sink", GST_TYPE_BUFFER,
500 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
502 * GstBaseSink:blocksize
504 * The amount of bytes to pull when operating in pull mode.
508 /* FIXME 0.11: blocksize property should be int, otherwise min>max.. */
509 g_object_class_install_property (gobject_class, PROP_BLOCKSIZE,
510 g_param_spec_uint ("blocksize", "Block size",
511 "Size in bytes to pull per buffer (0 = default)", 0, G_MAXUINT,
512 DEFAULT_BLOCKSIZE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
514 * GstBaseSink:render-delay
516 * The additional delay between synchronisation and actual rendering of the
517 * media. This property will add additional latency to the device in order to
518 * make other sinks compensate for the delay.
522 g_object_class_install_property (gobject_class, PROP_RENDER_DELAY,
523 g_param_spec_uint64 ("render-delay", "Render Delay",
524 "Additional render delay of the sink in nanoseconds", 0, G_MAXUINT64,
525 DEFAULT_RENDER_DELAY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
527 * GstBaseSink:throttle-time
529 * The time to insert between buffers. This property can be used to control
530 * the maximum amount of buffers per second to render. Setting this property
531 * to a value bigger than 0 will make the sink create THROTTLE QoS events.
535 g_object_class_install_property (gobject_class, PROP_THROTTLE_TIME,
536 g_param_spec_uint64 ("throttle-time", "Throttle time",
537 "The time to keep between rendered buffers (unused)", 0, G_MAXUINT64,
538 DEFAULT_THROTTLE_TIME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
540 gstelement_class->change_state =
541 GST_DEBUG_FUNCPTR (gst_base_sink_change_state);
542 gstelement_class->send_event = GST_DEBUG_FUNCPTR (gst_base_sink_send_event);
543 gstelement_class->query = GST_DEBUG_FUNCPTR (gst_base_sink_query);
544 gstelement_class->get_query_types =
545 GST_DEBUG_FUNCPTR (gst_base_sink_get_query_types);
547 klass->get_caps = GST_DEBUG_FUNCPTR (gst_base_sink_get_caps);
548 klass->set_caps = GST_DEBUG_FUNCPTR (gst_base_sink_set_caps);
549 klass->get_times = GST_DEBUG_FUNCPTR (gst_base_sink_get_times);
550 klass->activate_pull =
551 GST_DEBUG_FUNCPTR (gst_base_sink_default_activate_pull);
553 /* Registering debug symbols for function pointers */
554 GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_pad_getcaps);
555 GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_pad_fixate);
556 GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_pad_activate);
557 GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_pad_activate_push);
558 GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_pad_activate_pull);
559 GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_event);
560 GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_chain);
561 GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_chain_list);
565 gst_base_sink_pad_getcaps (GstPad * pad, GstCaps * filter)
567 GstBaseSinkClass *bclass;
569 GstCaps *caps = NULL;
571 bsink = GST_BASE_SINK (gst_pad_get_parent (pad));
572 bclass = GST_BASE_SINK_GET_CLASS (bsink);
574 if (bsink->pad_mode == GST_ACTIVATE_PULL) {
575 /* if we are operating in pull mode we only accept the 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);
602 gst_object_unref (bsink);
608 gst_base_sink_pad_fixate (GstPad * pad, GstCaps * caps)
610 GstBaseSinkClass *bclass;
613 bsink = GST_BASE_SINK (gst_pad_get_parent (pad));
614 bclass = GST_BASE_SINK_GET_CLASS (bsink);
617 bclass->fixate (bsink, caps);
619 gst_object_unref (bsink);
623 gst_base_sink_init (GstBaseSink * basesink, gpointer g_class)
625 GstPadTemplate *pad_template;
626 GstBaseSinkPrivate *priv;
628 basesink->priv = priv = GST_BASE_SINK_GET_PRIVATE (basesink);
631 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (g_class), "sink");
632 g_return_if_fail (pad_template != NULL);
634 basesink->sinkpad = gst_pad_new_from_template (pad_template, "sink");
636 gst_pad_set_getcaps_function (basesink->sinkpad, gst_base_sink_pad_getcaps);
637 gst_pad_set_fixatecaps_function (basesink->sinkpad, gst_base_sink_pad_fixate);
638 gst_pad_set_activate_function (basesink->sinkpad, gst_base_sink_pad_activate);
639 gst_pad_set_activatepush_function (basesink->sinkpad,
640 gst_base_sink_pad_activate_push);
641 gst_pad_set_activatepull_function (basesink->sinkpad,
642 gst_base_sink_pad_activate_pull);
643 gst_pad_set_event_function (basesink->sinkpad, gst_base_sink_event);
644 gst_pad_set_chain_function (basesink->sinkpad, gst_base_sink_chain);
645 gst_pad_set_chain_list_function (basesink->sinkpad, gst_base_sink_chain_list);
646 gst_element_add_pad (GST_ELEMENT_CAST (basesink), basesink->sinkpad);
648 basesink->pad_mode = GST_ACTIVATE_NONE;
649 basesink->preroll_lock = g_mutex_new ();
650 basesink->preroll_cond = g_cond_new ();
651 basesink->preroll_queue = g_queue_new ();
652 basesink->clip_segment = gst_segment_new ();
653 priv->have_latency = FALSE;
655 basesink->can_activate_push = DEFAULT_CAN_ACTIVATE_PUSH;
656 basesink->can_activate_pull = DEFAULT_CAN_ACTIVATE_PULL;
658 basesink->sync = DEFAULT_SYNC;
659 basesink->max_lateness = DEFAULT_MAX_LATENESS;
660 g_atomic_int_set (&priv->qos_enabled, DEFAULT_QOS);
661 priv->async_enabled = DEFAULT_ASYNC;
662 priv->ts_offset = DEFAULT_TS_OFFSET;
663 priv->render_delay = DEFAULT_RENDER_DELAY;
664 priv->blocksize = DEFAULT_BLOCKSIZE;
665 priv->cached_clock_id = NULL;
666 g_atomic_int_set (&priv->enable_last_buffer, DEFAULT_ENABLE_LAST_BUFFER);
667 priv->throttle_time = DEFAULT_THROTTLE_TIME;
669 GST_OBJECT_FLAG_SET (basesink, GST_ELEMENT_IS_SINK);
673 gst_base_sink_finalize (GObject * object)
675 GstBaseSink *basesink;
677 basesink = GST_BASE_SINK (object);
679 g_mutex_free (basesink->preroll_lock);
680 g_cond_free (basesink->preroll_cond);
681 g_queue_free (basesink->preroll_queue);
682 gst_segment_free (basesink->clip_segment);
684 G_OBJECT_CLASS (parent_class)->finalize (object);
688 * gst_base_sink_set_sync:
690 * @sync: the new sync value.
692 * Configures @sink to synchronize on the clock or not. When
693 * @sync is FALSE, incomming samples will be played as fast as
694 * possible. If @sync is TRUE, the timestamps of the incomming
695 * buffers will be used to schedule the exact render time of its
701 gst_base_sink_set_sync (GstBaseSink * sink, gboolean sync)
703 g_return_if_fail (GST_IS_BASE_SINK (sink));
705 GST_OBJECT_LOCK (sink);
707 GST_OBJECT_UNLOCK (sink);
711 * gst_base_sink_get_sync:
714 * Checks if @sink is currently configured to synchronize against the
717 * Returns: TRUE if the sink is configured to synchronize against the clock.
722 gst_base_sink_get_sync (GstBaseSink * sink)
726 g_return_val_if_fail (GST_IS_BASE_SINK (sink), FALSE);
728 GST_OBJECT_LOCK (sink);
730 GST_OBJECT_UNLOCK (sink);
736 * gst_base_sink_set_max_lateness:
738 * @max_lateness: the new max lateness value.
740 * Sets the new max lateness value to @max_lateness. This value is
741 * used to decide if a buffer should be dropped or not based on the
742 * buffer timestamp and the current clock time. A value of -1 means
748 gst_base_sink_set_max_lateness (GstBaseSink * sink, gint64 max_lateness)
750 g_return_if_fail (GST_IS_BASE_SINK (sink));
752 GST_OBJECT_LOCK (sink);
753 sink->max_lateness = max_lateness;
754 GST_OBJECT_UNLOCK (sink);
758 * gst_base_sink_get_max_lateness:
761 * Gets the max lateness value. See gst_base_sink_set_max_lateness for
764 * Returns: The maximum time in nanoseconds that a buffer can be late
765 * before it is dropped and not rendered. A value of -1 means an
771 gst_base_sink_get_max_lateness (GstBaseSink * sink)
775 g_return_val_if_fail (GST_IS_BASE_SINK (sink), -1);
777 GST_OBJECT_LOCK (sink);
778 res = sink->max_lateness;
779 GST_OBJECT_UNLOCK (sink);
785 * gst_base_sink_set_qos_enabled:
787 * @enabled: the new qos value.
789 * Configures @sink to send Quality-of-Service events upstream.
794 gst_base_sink_set_qos_enabled (GstBaseSink * sink, gboolean enabled)
796 g_return_if_fail (GST_IS_BASE_SINK (sink));
798 g_atomic_int_set (&sink->priv->qos_enabled, enabled);
802 * gst_base_sink_is_qos_enabled:
805 * Checks if @sink is currently configured to send Quality-of-Service events
808 * Returns: TRUE if the sink is configured to perform Quality-of-Service.
813 gst_base_sink_is_qos_enabled (GstBaseSink * sink)
817 g_return_val_if_fail (GST_IS_BASE_SINK (sink), FALSE);
819 res = g_atomic_int_get (&sink->priv->qos_enabled);
825 * gst_base_sink_set_async_enabled:
827 * @enabled: the new async value.
829 * Configures @sink to perform all state changes asynchronusly. When async is
830 * disabled, the sink will immediatly go to PAUSED instead of waiting for a
831 * preroll buffer. This feature is usefull if the sink does not synchronize
832 * against the clock or when it is dealing with sparse streams.
837 gst_base_sink_set_async_enabled (GstBaseSink * sink, gboolean enabled)
839 g_return_if_fail (GST_IS_BASE_SINK (sink));
841 GST_BASE_SINK_PREROLL_LOCK (sink);
842 g_atomic_int_set (&sink->priv->async_enabled, enabled);
843 GST_LOG_OBJECT (sink, "set async enabled to %d", enabled);
844 GST_BASE_SINK_PREROLL_UNLOCK (sink);
848 * gst_base_sink_is_async_enabled:
851 * Checks if @sink is currently configured to perform asynchronous state
854 * Returns: TRUE if the sink is configured to perform asynchronous state
860 gst_base_sink_is_async_enabled (GstBaseSink * sink)
864 g_return_val_if_fail (GST_IS_BASE_SINK (sink), FALSE);
866 res = g_atomic_int_get (&sink->priv->async_enabled);
872 * gst_base_sink_set_ts_offset:
874 * @offset: the new offset
876 * Adjust the synchronisation of @sink with @offset. A negative value will
877 * render buffers earlier than their timestamp. A positive value will delay
878 * rendering. This function can be used to fix playback of badly timestamped
884 gst_base_sink_set_ts_offset (GstBaseSink * sink, GstClockTimeDiff offset)
886 g_return_if_fail (GST_IS_BASE_SINK (sink));
888 GST_OBJECT_LOCK (sink);
889 sink->priv->ts_offset = offset;
890 GST_LOG_OBJECT (sink, "set time offset to %" G_GINT64_FORMAT, offset);
891 GST_OBJECT_UNLOCK (sink);
895 * gst_base_sink_get_ts_offset:
898 * Get the synchronisation offset of @sink.
900 * Returns: The synchronisation offset.
905 gst_base_sink_get_ts_offset (GstBaseSink * sink)
907 GstClockTimeDiff res;
909 g_return_val_if_fail (GST_IS_BASE_SINK (sink), 0);
911 GST_OBJECT_LOCK (sink);
912 res = sink->priv->ts_offset;
913 GST_OBJECT_UNLOCK (sink);
919 * gst_base_sink_get_last_buffer:
922 * Get the last buffer that arrived in the sink and was used for preroll or for
923 * rendering. This property can be used to generate thumbnails.
925 * The #GstCaps on the buffer can be used to determine the type of the buffer.
927 * Free-function: gst_buffer_unref
929 * Returns: (transfer full): a #GstBuffer. gst_buffer_unref() after usage.
930 * This function returns NULL when no buffer has arrived in the sink yet
931 * or when the sink is not in PAUSED or PLAYING.
936 gst_base_sink_get_last_buffer (GstBaseSink * sink)
940 g_return_val_if_fail (GST_IS_BASE_SINK (sink), NULL);
942 GST_OBJECT_LOCK (sink);
943 if ((res = sink->priv->last_buffer))
944 gst_buffer_ref (res);
945 GST_OBJECT_UNLOCK (sink);
950 /* with OBJECT_LOCK */
952 gst_base_sink_set_last_buffer_unlocked (GstBaseSink * sink, GstBuffer * buffer)
956 old = sink->priv->last_buffer;
957 if (G_LIKELY (old != buffer)) {
958 GST_DEBUG_OBJECT (sink, "setting last buffer to %p", buffer);
959 if (G_LIKELY (buffer))
960 gst_buffer_ref (buffer);
961 sink->priv->last_buffer = buffer;
965 /* avoid unreffing with the lock because cleanup code might want to take the
967 if (G_LIKELY (old)) {
968 GST_OBJECT_UNLOCK (sink);
969 gst_buffer_unref (old);
970 GST_OBJECT_LOCK (sink);
975 gst_base_sink_set_last_buffer (GstBaseSink * sink, GstBuffer * buffer)
977 if (!g_atomic_int_get (&sink->priv->enable_last_buffer))
980 GST_OBJECT_LOCK (sink);
981 gst_base_sink_set_last_buffer_unlocked (sink, buffer);
982 GST_OBJECT_UNLOCK (sink);
986 * gst_base_sink_set_last_buffer_enabled:
988 * @enabled: the new enable-last-buffer value.
990 * Configures @sink to store the last received buffer in the last-buffer
996 gst_base_sink_set_last_buffer_enabled (GstBaseSink * sink, gboolean enabled)
998 g_return_if_fail (GST_IS_BASE_SINK (sink));
1000 /* Only take lock if we change the value */
1001 if (g_atomic_int_compare_and_exchange (&sink->priv->enable_last_buffer,
1002 !enabled, enabled) && !enabled) {
1003 GST_OBJECT_LOCK (sink);
1004 gst_base_sink_set_last_buffer_unlocked (sink, NULL);
1005 GST_OBJECT_UNLOCK (sink);
1010 * gst_base_sink_is_last_buffer_enabled:
1013 * Checks if @sink is currently configured to store the last received buffer in
1014 * the last-buffer property.
1016 * Returns: TRUE if the sink is configured to store the last received buffer.
1021 gst_base_sink_is_last_buffer_enabled (GstBaseSink * sink)
1023 g_return_val_if_fail (GST_IS_BASE_SINK (sink), FALSE);
1025 return g_atomic_int_get (&sink->priv->enable_last_buffer);
1029 * gst_base_sink_get_latency:
1032 * Get the currently configured latency.
1034 * Returns: The configured latency.
1039 gst_base_sink_get_latency (GstBaseSink * sink)
1043 GST_OBJECT_LOCK (sink);
1044 res = sink->priv->latency;
1045 GST_OBJECT_UNLOCK (sink);
1051 * gst_base_sink_query_latency:
1053 * @live: (out) (allow-none): if the sink is live
1054 * @upstream_live: (out) (allow-none): if an upstream element is live
1055 * @min_latency: (out) (allow-none): the min latency of the upstream elements
1056 * @max_latency: (out) (allow-none): the max latency of the upstream elements
1058 * Query the sink for the latency parameters. The latency will be queried from
1059 * the upstream elements. @live will be TRUE if @sink is configured to
1060 * synchronize against the clock. @upstream_live will be TRUE if an upstream
1063 * If both @live and @upstream_live are TRUE, the sink will want to compensate
1064 * for the latency introduced by the upstream elements by setting the
1065 * @min_latency to a strictly possitive value.
1067 * This function is mostly used by subclasses.
1069 * Returns: TRUE if the query succeeded.
1074 gst_base_sink_query_latency (GstBaseSink * sink, gboolean * live,
1075 gboolean * upstream_live, GstClockTime * min_latency,
1076 GstClockTime * max_latency)
1078 gboolean l, us_live, res, have_latency;
1079 GstClockTime min, max, render_delay;
1081 GstClockTime us_min, us_max;
1083 /* we are live when we sync to the clock */
1084 GST_OBJECT_LOCK (sink);
1086 have_latency = sink->priv->have_latency;
1087 render_delay = sink->priv->render_delay;
1088 GST_OBJECT_UNLOCK (sink);
1090 /* assume no latency */
1096 GST_DEBUG_OBJECT (sink, "we are ready for LATENCY query");
1097 /* we are ready for a latency query this is when we preroll or when we are
1099 query = gst_query_new_latency ();
1101 /* ask the peer for the latency */
1102 if ((res = gst_pad_peer_query (sink->sinkpad, query))) {
1103 /* get upstream min and max latency */
1104 gst_query_parse_latency (query, &us_live, &us_min, &us_max);
1107 /* upstream live, use its latency, subclasses should use these
1108 * values to create the complete latency. */
1113 /* we need to add the render delay if we are live */
1115 min += render_delay;
1117 max += render_delay;
1120 gst_query_unref (query);
1122 GST_DEBUG_OBJECT (sink, "we are not yet ready for LATENCY query");
1126 /* not live, we tried to do the query, if it failed we return TRUE anyway */
1130 GST_DEBUG_OBJECT (sink, "latency query failed but we are not live");
1132 GST_DEBUG_OBJECT (sink, "latency query failed and we are live");
1137 GST_DEBUG_OBJECT (sink, "latency query: live: %d, have_latency %d,"
1138 " upstream: %d, min %" GST_TIME_FORMAT ", max %" GST_TIME_FORMAT, l,
1139 have_latency, us_live, GST_TIME_ARGS (min), GST_TIME_ARGS (max));
1144 *upstream_live = us_live;
1154 * gst_base_sink_set_render_delay:
1155 * @sink: a #GstBaseSink
1156 * @delay: the new delay
1158 * Set the render delay in @sink to @delay. The render delay is the time
1159 * between actual rendering of a buffer and its synchronisation time. Some
1160 * devices might delay media rendering which can be compensated for with this
1163 * After calling this function, this sink will report additional latency and
1164 * other sinks will adjust their latency to delay the rendering of their media.
1166 * This function is usually called by subclasses.
1171 gst_base_sink_set_render_delay (GstBaseSink * sink, GstClockTime delay)
1173 GstClockTime old_render_delay;
1175 g_return_if_fail (GST_IS_BASE_SINK (sink));
1177 GST_OBJECT_LOCK (sink);
1178 old_render_delay = sink->priv->render_delay;
1179 sink->priv->render_delay = delay;
1180 GST_LOG_OBJECT (sink, "set render delay to %" GST_TIME_FORMAT,
1181 GST_TIME_ARGS (delay));
1182 GST_OBJECT_UNLOCK (sink);
1184 if (delay != old_render_delay) {
1185 GST_DEBUG_OBJECT (sink, "posting latency changed");
1186 gst_element_post_message (GST_ELEMENT_CAST (sink),
1187 gst_message_new_latency (GST_OBJECT_CAST (sink)));
1192 * gst_base_sink_get_render_delay:
1193 * @sink: a #GstBaseSink
1195 * Get the render delay of @sink. see gst_base_sink_set_render_delay() for more
1196 * information about the render delay.
1198 * Returns: the render delay of @sink.
1203 gst_base_sink_get_render_delay (GstBaseSink * sink)
1205 GstClockTimeDiff res;
1207 g_return_val_if_fail (GST_IS_BASE_SINK (sink), 0);
1209 GST_OBJECT_LOCK (sink);
1210 res = sink->priv->render_delay;
1211 GST_OBJECT_UNLOCK (sink);
1217 * gst_base_sink_set_blocksize:
1218 * @sink: a #GstBaseSink
1219 * @blocksize: the blocksize in bytes
1221 * Set the number of bytes that the sink will pull when it is operating in pull
1226 /* FIXME 0.11: blocksize property should be int, otherwise min>max.. */
1228 gst_base_sink_set_blocksize (GstBaseSink * sink, guint blocksize)
1230 g_return_if_fail (GST_IS_BASE_SINK (sink));
1232 GST_OBJECT_LOCK (sink);
1233 sink->priv->blocksize = blocksize;
1234 GST_LOG_OBJECT (sink, "set blocksize to %u", blocksize);
1235 GST_OBJECT_UNLOCK (sink);
1239 * gst_base_sink_get_blocksize:
1240 * @sink: a #GstBaseSink
1242 * Get the number of bytes that the sink will pull when it is operating in pull
1245 * Returns: the number of bytes @sink will pull in pull mode.
1249 /* FIXME 0.11: blocksize property should be int, otherwise min>max.. */
1251 gst_base_sink_get_blocksize (GstBaseSink * sink)
1255 g_return_val_if_fail (GST_IS_BASE_SINK (sink), 0);
1257 GST_OBJECT_LOCK (sink);
1258 res = sink->priv->blocksize;
1259 GST_OBJECT_UNLOCK (sink);
1265 * gst_base_sink_set_throttle_time:
1266 * @sink: a #GstBaseSink
1267 * @throttle: the throttle time in nanoseconds
1269 * Set the time that will be inserted between rendered buffers. This
1270 * can be used to control the maximum buffers per second that the sink
1276 gst_base_sink_set_throttle_time (GstBaseSink * sink, guint64 throttle)
1278 g_return_if_fail (GST_IS_BASE_SINK (sink));
1280 GST_OBJECT_LOCK (sink);
1281 sink->priv->throttle_time = throttle;
1282 GST_LOG_OBJECT (sink, "set throttle_time to %" G_GUINT64_FORMAT, throttle);
1283 GST_OBJECT_UNLOCK (sink);
1287 * gst_base_sink_get_throttle_time:
1288 * @sink: a #GstBaseSink
1290 * Get the time that will be inserted between frames to control the
1291 * maximum buffers per second.
1293 * Returns: the number of nanoseconds @sink will put between frames.
1298 gst_base_sink_get_throttle_time (GstBaseSink * sink)
1302 g_return_val_if_fail (GST_IS_BASE_SINK (sink), 0);
1304 GST_OBJECT_LOCK (sink);
1305 res = sink->priv->throttle_time;
1306 GST_OBJECT_UNLOCK (sink);
1312 gst_base_sink_set_property (GObject * object, guint prop_id,
1313 const GValue * value, GParamSpec * pspec)
1315 GstBaseSink *sink = GST_BASE_SINK (object);
1318 case PROP_PREROLL_QUEUE_LEN:
1319 /* preroll lock necessary to serialize with finish_preroll */
1320 GST_BASE_SINK_PREROLL_LOCK (sink);
1321 g_atomic_int_set (&sink->preroll_queue_max_len, g_value_get_uint (value));
1322 GST_BASE_SINK_PREROLL_UNLOCK (sink);
1325 gst_base_sink_set_sync (sink, g_value_get_boolean (value));
1327 case PROP_MAX_LATENESS:
1328 gst_base_sink_set_max_lateness (sink, g_value_get_int64 (value));
1331 gst_base_sink_set_qos_enabled (sink, g_value_get_boolean (value));
1334 gst_base_sink_set_async_enabled (sink, g_value_get_boolean (value));
1336 case PROP_TS_OFFSET:
1337 gst_base_sink_set_ts_offset (sink, g_value_get_int64 (value));
1339 case PROP_BLOCKSIZE:
1340 gst_base_sink_set_blocksize (sink, g_value_get_uint (value));
1342 case PROP_RENDER_DELAY:
1343 gst_base_sink_set_render_delay (sink, g_value_get_uint64 (value));
1345 case PROP_ENABLE_LAST_BUFFER:
1346 gst_base_sink_set_last_buffer_enabled (sink, g_value_get_boolean (value));
1348 case PROP_THROTTLE_TIME:
1349 gst_base_sink_set_throttle_time (sink, g_value_get_uint64 (value));
1352 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1358 gst_base_sink_get_property (GObject * object, guint prop_id, GValue * value,
1361 GstBaseSink *sink = GST_BASE_SINK (object);
1364 case PROP_PREROLL_QUEUE_LEN:
1365 g_value_set_uint (value, g_atomic_int_get (&sink->preroll_queue_max_len));
1368 g_value_set_boolean (value, gst_base_sink_get_sync (sink));
1370 case PROP_MAX_LATENESS:
1371 g_value_set_int64 (value, gst_base_sink_get_max_lateness (sink));
1374 g_value_set_boolean (value, gst_base_sink_is_qos_enabled (sink));
1377 g_value_set_boolean (value, gst_base_sink_is_async_enabled (sink));
1379 case PROP_TS_OFFSET:
1380 g_value_set_int64 (value, gst_base_sink_get_ts_offset (sink));
1382 case PROP_LAST_BUFFER:
1383 gst_value_take_buffer (value, gst_base_sink_get_last_buffer (sink));
1385 case PROP_ENABLE_LAST_BUFFER:
1386 g_value_set_boolean (value, gst_base_sink_is_last_buffer_enabled (sink));
1388 case PROP_BLOCKSIZE:
1389 g_value_set_uint (value, gst_base_sink_get_blocksize (sink));
1391 case PROP_RENDER_DELAY:
1392 g_value_set_uint64 (value, gst_base_sink_get_render_delay (sink));
1394 case PROP_THROTTLE_TIME:
1395 g_value_set_uint64 (value, gst_base_sink_get_throttle_time (sink));
1398 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1405 gst_base_sink_get_caps (GstBaseSink * sink, GstCaps * filter)
1411 gst_base_sink_set_caps (GstBaseSink * sink, GstCaps * caps)
1416 /* with PREROLL_LOCK, STREAM_LOCK */
1418 gst_base_sink_preroll_queue_flush (GstBaseSink * basesink, GstPad * pad)
1422 GST_DEBUG_OBJECT (basesink, "flushing queue %p", basesink);
1423 while ((obj = g_queue_pop_head (basesink->preroll_queue))) {
1424 GST_DEBUG_OBJECT (basesink, "popped %p", obj);
1425 gst_mini_object_unref (obj);
1427 /* we can't have EOS anymore now */
1428 basesink->eos = FALSE;
1429 basesink->priv->received_eos = FALSE;
1430 basesink->have_preroll = FALSE;
1431 basesink->priv->step_unlock = FALSE;
1432 basesink->eos_queued = FALSE;
1433 basesink->preroll_queued = 0;
1434 basesink->buffers_queued = 0;
1435 basesink->events_queued = 0;
1436 /* can't report latency anymore until we preroll again */
1437 if (basesink->priv->async_enabled) {
1438 GST_OBJECT_LOCK (basesink);
1439 basesink->priv->have_latency = FALSE;
1440 GST_OBJECT_UNLOCK (basesink);
1442 /* and signal any waiters now */
1443 GST_BASE_SINK_PREROLL_SIGNAL (basesink);
1446 /* with STREAM_LOCK, configures given segment with the event information. */
1448 gst_base_sink_configure_segment (GstBaseSink * basesink, GstPad * pad,
1449 GstEvent * event, GstSegment * segment)
1451 /* The segment is protected with both the STREAM_LOCK and the OBJECT_LOCK.
1452 * We protect with the OBJECT_LOCK so that we can use the values to
1453 * safely answer a POSITION query. */
1454 GST_OBJECT_LOCK (basesink);
1455 /* the newsegment event is needed to bring the buffer timestamps to the
1456 * stream time and to drop samples outside of the playback segment. */
1457 gst_event_copy_segment (event, segment);
1458 GST_DEBUG_OBJECT (basesink, "configured SEGMENT %" GST_SEGMENT_FORMAT,
1460 GST_OBJECT_UNLOCK (basesink);
1463 /* with PREROLL_LOCK, STREAM_LOCK */
1465 gst_base_sink_commit_state (GstBaseSink * basesink)
1467 /* commit state and proceed to next pending state */
1468 GstState current, next, pending, post_pending;
1469 gboolean post_paused = FALSE;
1470 gboolean post_async_done = FALSE;
1471 gboolean post_playing = FALSE;
1473 /* we are certainly not playing async anymore now */
1474 basesink->playing_async = FALSE;
1476 GST_OBJECT_LOCK (basesink);
1477 current = GST_STATE (basesink);
1478 next = GST_STATE_NEXT (basesink);
1479 pending = GST_STATE_PENDING (basesink);
1480 post_pending = pending;
1483 case GST_STATE_PLAYING:
1485 GstBaseSinkClass *bclass;
1487 bclass = GST_BASE_SINK_GET_CLASS (basesink);
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)));
1538 GST_DEBUG_OBJECT (basesink, "posting PLAYING state change message");
1539 gst_element_post_message (GST_ELEMENT_CAST (basesink),
1540 gst_message_new_state_changed (GST_OBJECT_CAST (basesink),
1541 next, pending, GST_STATE_VOID_PENDING));
1544 GST_STATE_BROADCAST (basesink);
1550 /* Depending on the state, set our vars. We get in this situation when the
1551 * state change function got a change to update the state vars before the
1552 * streaming thread did. This is fine but we need to make sure that we
1553 * update the need_preroll var since it was TRUE when we got here and might
1554 * become FALSE if we got to PLAYING. */
1555 GST_DEBUG_OBJECT (basesink, "nothing to commit, now in %s",
1556 gst_element_state_get_name (current));
1558 case GST_STATE_PLAYING:
1559 basesink->need_preroll = FALSE;
1561 case GST_STATE_PAUSED:
1562 basesink->need_preroll = TRUE;
1565 basesink->need_preroll = FALSE;
1566 basesink->flushing = TRUE;
1569 /* we can report latency queries now */
1570 basesink->priv->have_latency = TRUE;
1571 GST_OBJECT_UNLOCK (basesink);
1576 /* app is going to READY */
1577 GST_DEBUG_OBJECT (basesink, "stopping");
1578 basesink->need_preroll = FALSE;
1579 basesink->flushing = TRUE;
1580 GST_OBJECT_UNLOCK (basesink);
1586 start_stepping (GstBaseSink * sink, GstSegment * segment,
1587 GstStepInfo * pending, GstStepInfo * current)
1590 GstMessage *message;
1592 GST_DEBUG_OBJECT (sink, "update pending step");
1594 GST_OBJECT_LOCK (sink);
1595 memcpy (current, pending, sizeof (GstStepInfo));
1596 pending->valid = FALSE;
1597 GST_OBJECT_UNLOCK (sink);
1599 /* post message first */
1601 gst_message_new_step_start (GST_OBJECT (sink), TRUE, current->format,
1602 current->amount, current->rate, current->flush, current->intermediate);
1603 gst_message_set_seqnum (message, current->seqnum);
1604 gst_element_post_message (GST_ELEMENT (sink), message);
1606 /* get the running time of where we paused and remember it */
1607 current->start = gst_element_get_start_time (GST_ELEMENT_CAST (sink));
1608 gst_segment_set_running_time (segment, GST_FORMAT_TIME, current->start);
1610 /* set the new rate for the remainder of the segment */
1611 current->start_rate = segment->rate;
1612 segment->rate *= current->rate;
1615 if (segment->rate > 0.0)
1616 current->start_stop = segment->stop;
1618 current->start_start = segment->start;
1620 if (current->format == GST_FORMAT_TIME) {
1621 end = current->start + current->amount;
1622 if (!current->flush) {
1623 /* update the segment clipping regions for non-flushing seeks */
1624 if (segment->rate > 0.0) {
1625 segment->stop = gst_segment_to_position (segment, GST_FORMAT_TIME, end);
1626 segment->position = segment->stop;
1630 position = gst_segment_to_position (segment, GST_FORMAT_TIME, end);
1631 segment->time = position;
1632 segment->start = position;
1633 segment->position = position;
1638 GST_DEBUG_OBJECT (sink, "segment now %" GST_SEGMENT_FORMAT, segment);
1639 GST_DEBUG_OBJECT (sink, "step started at running_time %" GST_TIME_FORMAT,
1640 GST_TIME_ARGS (current->start));
1642 if (current->amount == -1) {
1643 GST_DEBUG_OBJECT (sink, "step amount == -1, stop stepping");
1644 current->valid = FALSE;
1646 GST_DEBUG_OBJECT (sink, "step amount: %" G_GUINT64_FORMAT ", format: %s, "
1647 "rate: %f", current->amount, gst_format_get_name (current->format),
1653 stop_stepping (GstBaseSink * sink, GstSegment * segment,
1654 GstStepInfo * current, gint64 rstart, gint64 rstop, gboolean eos)
1656 gint64 stop, position;
1657 GstMessage *message;
1659 GST_DEBUG_OBJECT (sink, "step complete");
1661 if (segment->rate > 0.0)
1666 GST_DEBUG_OBJECT (sink,
1667 "step stop at running_time %" GST_TIME_FORMAT, GST_TIME_ARGS (stop));
1670 current->duration = current->position;
1672 current->duration = stop - current->start;
1674 GST_DEBUG_OBJECT (sink, "step elapsed running_time %" GST_TIME_FORMAT,
1675 GST_TIME_ARGS (current->duration));
1677 position = current->start + current->duration;
1679 /* now move the segment to the new running time */
1680 gst_segment_set_running_time (segment, GST_FORMAT_TIME, position);
1682 if (current->flush) {
1683 /* and remove the time we flushed, start time did not change */
1684 segment->base = current->start;
1686 /* start time is now the stepped position */
1687 gst_element_set_start_time (GST_ELEMENT_CAST (sink), position);
1690 /* restore the previous rate */
1691 segment->rate = current->start_rate;
1693 if (segment->rate > 0.0)
1694 segment->stop = current->start_stop;
1696 segment->start = current->start_start;
1698 /* the clip segment is used for position report in paused... */
1699 gst_segment_copy_into (segment, sink->clip_segment);
1701 /* post the step done when we know the stepped duration in TIME */
1703 gst_message_new_step_done (GST_OBJECT_CAST (sink), current->format,
1704 current->amount, current->rate, current->flush, current->intermediate,
1705 current->duration, eos);
1706 gst_message_set_seqnum (message, current->seqnum);
1707 gst_element_post_message (GST_ELEMENT_CAST (sink), message);
1709 if (!current->intermediate)
1710 sink->need_preroll = current->need_preroll;
1712 /* and the current step info finished and becomes invalid */
1713 current->valid = FALSE;
1717 handle_stepping (GstBaseSink * sink, GstSegment * segment,
1718 GstStepInfo * current, guint64 * cstart, guint64 * cstop, guint64 * rstart,
1721 gboolean step_end = FALSE;
1723 /* see if we need to skip this buffer because of stepping */
1724 switch (current->format) {
1725 case GST_FORMAT_TIME:
1728 guint64 first, last;
1731 if (segment->rate > 0.0) {
1732 if (segment->stop == *cstop)
1733 *rstop = *rstart + current->amount;
1738 if (segment->start == *cstart)
1739 *rstart = *rstop + current->amount;
1745 end = current->start + current->amount;
1746 current->position = first - current->start;
1748 abs_rate = ABS (segment->rate);
1749 if (G_UNLIKELY (abs_rate != 1.0))
1750 current->position /= abs_rate;
1752 GST_DEBUG_OBJECT (sink,
1753 "buffer: %" GST_TIME_FORMAT "-%" GST_TIME_FORMAT,
1754 GST_TIME_ARGS (first), GST_TIME_ARGS (last));
1755 GST_DEBUG_OBJECT (sink,
1756 "got time step %" GST_TIME_FORMAT "-%" GST_TIME_FORMAT "/%"
1757 GST_TIME_FORMAT, GST_TIME_ARGS (current->position),
1758 GST_TIME_ARGS (last - current->start),
1759 GST_TIME_ARGS (current->amount));
1761 if ((current->flush && current->position >= current->amount)
1763 GST_DEBUG_OBJECT (sink, "step ended, we need clipping");
1765 if (segment->rate > 0.0) {
1767 *cstart = gst_segment_to_position (segment, GST_FORMAT_TIME, end);
1770 *cstop = gst_segment_to_position (segment, GST_FORMAT_TIME, end);
1773 GST_DEBUG_OBJECT (sink,
1774 "cstart %" GST_TIME_FORMAT ", rstart %" GST_TIME_FORMAT,
1775 GST_TIME_ARGS (*cstart), GST_TIME_ARGS (*rstart));
1776 GST_DEBUG_OBJECT (sink,
1777 "cstop %" GST_TIME_FORMAT ", rstop %" GST_TIME_FORMAT,
1778 GST_TIME_ARGS (*cstop), GST_TIME_ARGS (*rstop));
1781 case GST_FORMAT_BUFFERS:
1782 GST_DEBUG_OBJECT (sink,
1783 "got default step %" G_GUINT64_FORMAT "/%" G_GUINT64_FORMAT,
1784 current->position, current->amount);
1786 if (current->position < current->amount) {
1787 current->position++;
1792 case GST_FORMAT_DEFAULT:
1794 GST_DEBUG_OBJECT (sink,
1795 "got unknown step %" G_GUINT64_FORMAT "/%" G_GUINT64_FORMAT,
1796 current->position, current->amount);
1802 /* with STREAM_LOCK, PREROLL_LOCK
1804 * Returns TRUE if the object needs synchronisation and takes therefore
1805 * part in prerolling.
1807 * rsstart/rsstop contain the start/stop in stream time.
1808 * rrstart/rrstop contain the start/stop in running time.
1811 gst_base_sink_get_sync_times (GstBaseSink * basesink, GstMiniObject * obj,
1812 GstClockTime * rsstart, GstClockTime * rsstop,
1813 GstClockTime * rrstart, GstClockTime * rrstop, gboolean * do_sync,
1814 gboolean * stepped, GstSegment * segment, GstStepInfo * step,
1815 gboolean * step_end, guint8 obj_type)
1817 GstBaseSinkClass *bclass;
1819 GstClockTime start, stop; /* raw start/stop timestamps */
1820 guint64 cstart, cstop; /* clipped raw timestamps */
1821 guint64 rstart, rstop; /* clipped timestamps converted to running time */
1822 GstClockTime sstart, sstop; /* clipped timestamps converted to stream time */
1824 GstBaseSinkPrivate *priv;
1827 priv = basesink->priv;
1829 /* start with nothing */
1830 start = stop = GST_CLOCK_TIME_NONE;
1832 if (G_UNLIKELY (OBJ_IS_EVENT (obj_type))) {
1833 GstEvent *event = GST_EVENT_CAST (obj);
1835 switch (GST_EVENT_TYPE (event)) {
1836 /* EOS event needs syncing */
1839 if (basesink->segment.rate >= 0.0) {
1840 sstart = sstop = priv->current_sstop;
1841 if (!GST_CLOCK_TIME_IS_VALID (sstart)) {
1842 /* we have not seen a buffer yet, use the segment values */
1843 sstart = sstop = gst_segment_to_stream_time (&basesink->segment,
1844 basesink->segment.format, basesink->segment.stop);
1847 sstart = sstop = priv->current_sstart;
1848 if (!GST_CLOCK_TIME_IS_VALID (sstart)) {
1849 /* we have not seen a buffer yet, use the segment values */
1850 sstart = sstop = gst_segment_to_stream_time (&basesink->segment,
1851 basesink->segment.format, basesink->segment.start);
1855 rstart = rstop = priv->eos_rtime;
1856 *do_sync = rstart != -1;
1857 GST_DEBUG_OBJECT (basesink, "sync times for EOS %" GST_TIME_FORMAT,
1858 GST_TIME_ARGS (rstart));
1859 /* if we are stepping, we end now */
1860 *step_end = step->valid;
1865 /* other events do not need syncing */
1873 /* else do buffer sync code */
1874 buffer = GST_BUFFER_CAST (obj);
1876 bclass = GST_BASE_SINK_GET_CLASS (basesink);
1878 /* just get the times to see if we need syncing, if the start returns -1 we
1880 if (bclass->get_times)
1881 bclass->get_times (basesink, buffer, &start, &stop);
1883 if (!GST_CLOCK_TIME_IS_VALID (start)) {
1884 /* we don't need to sync but we still want to get the timestamps for
1885 * tracking the position */
1886 gst_base_sink_get_times (basesink, buffer, &start, &stop);
1892 GST_DEBUG_OBJECT (basesink, "got times start: %" GST_TIME_FORMAT
1893 ", stop: %" GST_TIME_FORMAT ", do_sync %d", GST_TIME_ARGS (start),
1894 GST_TIME_ARGS (stop), *do_sync);
1896 /* collect segment and format for code clarity */
1897 format = segment->format;
1900 if (G_UNLIKELY (!gst_segment_clip (segment, format,
1901 start, stop, &cstart, &cstop))) {
1903 GST_DEBUG_OBJECT (basesink, "step out of segment");
1904 /* when we are stepping, pretend we're at the end of the segment */
1905 if (segment->rate > 0.0) {
1906 cstart = segment->stop;
1907 cstop = segment->stop;
1909 cstart = segment->start;
1910 cstop = segment->start;
1914 goto out_of_segment;
1917 if (G_UNLIKELY (start != cstart || stop != cstop)) {
1918 GST_DEBUG_OBJECT (basesink, "clipped to: start %" GST_TIME_FORMAT
1919 ", stop: %" GST_TIME_FORMAT, GST_TIME_ARGS (cstart),
1920 GST_TIME_ARGS (cstop));
1923 /* set last stop position */
1924 if (G_LIKELY (stop != GST_CLOCK_TIME_NONE && cstop != GST_CLOCK_TIME_NONE))
1925 segment->position = cstop;
1927 segment->position = cstart;
1930 rstart = gst_segment_to_running_time (segment, format, cstart);
1931 rstop = gst_segment_to_running_time (segment, format, cstop);
1933 if (G_UNLIKELY (step->valid)) {
1934 if (!(*step_end = handle_stepping (basesink, segment, step, &cstart, &cstop,
1935 &rstart, &rstop))) {
1936 /* step is still busy, we discard data when we are flushing */
1937 *stepped = step->flush;
1938 GST_DEBUG_OBJECT (basesink, "stepping busy");
1941 /* this can produce wrong values if we accumulated non-TIME segments. If this happens,
1942 * upstream is behaving very badly */
1943 sstart = gst_segment_to_stream_time (segment, format, cstart);
1944 sstop = gst_segment_to_stream_time (segment, format, cstop);
1947 /* eos_done label only called when doing EOS, we also stop stepping then */
1948 if (*step_end && step->flush) {
1949 GST_DEBUG_OBJECT (basesink, "flushing step ended");
1950 stop_stepping (basesink, segment, step, rstart, rstop, eos);
1952 /* re-determine running start times for adjusted segment
1953 * (which has a flushed amount of running/accumulated time removed) */
1954 if (!GST_IS_EVENT (obj)) {
1955 GST_DEBUG_OBJECT (basesink, "refresh sync times");
1966 /* buffers and EOS always need syncing and preroll */
1972 /* we usually clip in the chain function already but stepping could cause
1973 * the segment to be updated later. we return FALSE so that we don't try
1975 GST_LOG_OBJECT (basesink, "buffer skipped, not in segment");
1980 /* with STREAM_LOCK, PREROLL_LOCK, LOCK
1981 * adjust a timestamp with the latency and timestamp offset. This function does
1982 * not adjust for the render delay. */
1984 gst_base_sink_adjust_time (GstBaseSink * basesink, GstClockTime time)
1986 GstClockTimeDiff ts_offset;
1988 /* don't do anything funny with invalid timestamps */
1989 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (time)))
1992 time += basesink->priv->latency;
1994 /* apply offset, be carefull for underflows */
1995 ts_offset = basesink->priv->ts_offset;
1996 if (ts_offset < 0) {
1997 ts_offset = -ts_offset;
1998 if (ts_offset < time)
2005 /* subtract the render delay again, which was included in the latency */
2006 if (time > basesink->priv->render_delay)
2007 time -= basesink->priv->render_delay;
2015 * gst_base_sink_wait_clock:
2017 * @time: the running_time to be reached
2018 * @jitter: (out) (allow-none): the jitter to be filled with time diff, or NULL
2020 * This function will block until @time is reached. It is usually called by
2021 * subclasses that use their own internal synchronisation.
2023 * If @time is not valid, no sycnhronisation is done and #GST_CLOCK_BADTIME is
2024 * returned. Likewise, if synchronisation is disabled in the element or there
2025 * is no clock, no synchronisation is done and #GST_CLOCK_BADTIME is returned.
2027 * This function should only be called with the PREROLL_LOCK held, like when
2028 * receiving an EOS event in the #GstBaseSinkClass.event() vmethod or when
2029 * receiving a buffer in
2030 * the #GstBaseSinkClass.render() vmethod.
2032 * The @time argument should be the running_time of when this method should
2033 * return and is not adjusted with any latency or offset configured in the
2038 * Returns: #GstClockReturn
2041 gst_base_sink_wait_clock (GstBaseSink * sink, GstClockTime time,
2042 GstClockTimeDiff * jitter)
2046 GstClockTime base_time;
2048 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (time)))
2051 GST_OBJECT_LOCK (sink);
2052 if (G_UNLIKELY (!sink->sync))
2055 if (G_UNLIKELY ((clock = GST_ELEMENT_CLOCK (sink)) == NULL))
2058 base_time = GST_ELEMENT_CAST (sink)->base_time;
2059 GST_LOG_OBJECT (sink,
2060 "time %" GST_TIME_FORMAT ", base_time %" GST_TIME_FORMAT,
2061 GST_TIME_ARGS (time), GST_TIME_ARGS (base_time));
2063 /* add base_time to running_time to get the time against the clock */
2066 /* Re-use existing clockid if available */
2067 /* FIXME: Casting to GstClockEntry only works because the types
2069 if (G_LIKELY (sink->priv->cached_clock_id != NULL
2070 && GST_CLOCK_ENTRY_CLOCK ((GstClockEntry *) sink->
2071 priv->cached_clock_id) == clock)) {
2072 if (!gst_clock_single_shot_id_reinit (clock, sink->priv->cached_clock_id,
2074 gst_clock_id_unref (sink->priv->cached_clock_id);
2075 sink->priv->cached_clock_id = gst_clock_new_single_shot_id (clock, time);
2078 if (sink->priv->cached_clock_id != NULL)
2079 gst_clock_id_unref (sink->priv->cached_clock_id);
2080 sink->priv->cached_clock_id = gst_clock_new_single_shot_id (clock, time);
2082 GST_OBJECT_UNLOCK (sink);
2084 /* A blocking wait is performed on the clock. We save the ClockID
2085 * so we can unlock the entry at any time. While we are blocking, we
2086 * release the PREROLL_LOCK so that other threads can interrupt the
2088 sink->clock_id = sink->priv->cached_clock_id;
2089 /* release the preroll lock while waiting */
2090 GST_BASE_SINK_PREROLL_UNLOCK (sink);
2092 ret = gst_clock_id_wait (sink->priv->cached_clock_id, jitter);
2094 GST_BASE_SINK_PREROLL_LOCK (sink);
2095 sink->clock_id = NULL;
2099 /* no syncing needed */
2102 GST_DEBUG_OBJECT (sink, "time not valid, no sync needed");
2103 return GST_CLOCK_BADTIME;
2107 GST_DEBUG_OBJECT (sink, "sync disabled");
2108 GST_OBJECT_UNLOCK (sink);
2109 return GST_CLOCK_BADTIME;
2113 GST_DEBUG_OBJECT (sink, "no clock, can't sync");
2114 GST_OBJECT_UNLOCK (sink);
2115 return GST_CLOCK_BADTIME;
2120 * gst_base_sink_wait_preroll:
2123 * If the #GstBaseSinkClass.render() method performs its own synchronisation
2124 * against the clock it must unblock when going from PLAYING to the PAUSED state
2125 * and call this method before continuing to render the remaining data.
2127 * This function will block until a state change to PLAYING happens (in which
2128 * case this function returns #GST_FLOW_OK) or the processing must be stopped due
2129 * to a state change to READY or a FLUSH event (in which case this function
2130 * returns #GST_FLOW_WRONG_STATE).
2132 * This function should only be called with the PREROLL_LOCK held, like in the
2135 * Returns: #GST_FLOW_OK if the preroll completed and processing can
2136 * continue. Any other return value should be returned from the render vmethod.
2141 gst_base_sink_wait_preroll (GstBaseSink * sink)
2143 sink->have_preroll = TRUE;
2144 GST_DEBUG_OBJECT (sink, "waiting in preroll for flush or PLAYING");
2145 /* block until the state changes, or we get a flush, or something */
2146 GST_BASE_SINK_PREROLL_WAIT (sink);
2147 sink->have_preroll = FALSE;
2148 if (G_UNLIKELY (sink->flushing))
2150 if (G_UNLIKELY (sink->priv->step_unlock))
2152 GST_DEBUG_OBJECT (sink, "continue after preroll");
2159 GST_DEBUG_OBJECT (sink, "preroll interrupted because of flush");
2160 return GST_FLOW_WRONG_STATE;
2164 sink->priv->step_unlock = FALSE;
2165 GST_DEBUG_OBJECT (sink, "preroll interrupted because of step");
2166 return GST_FLOW_STEP;
2170 static inline guint8
2171 get_object_type (GstMiniObject * obj)
2175 if (G_LIKELY (GST_IS_BUFFER (obj)))
2176 obj_type = _PR_IS_BUFFER;
2177 else if (GST_IS_EVENT (obj))
2178 obj_type = _PR_IS_EVENT;
2179 else if (GST_IS_BUFFER_LIST (obj))
2180 obj_type = _PR_IS_BUFFERLIST;
2182 obj_type = _PR_IS_NOTHING;
2188 * gst_base_sink_do_preroll:
2190 * @obj: (transfer none): the mini object that caused the preroll
2192 * If the @sink spawns its own thread for pulling buffers from upstream it
2193 * should call this method after it has pulled a buffer. If the element needed
2194 * to preroll, this function will perform the preroll and will then block
2195 * until the element state is changed.
2197 * This function should be called with the PREROLL_LOCK held.
2199 * Returns: #GST_FLOW_OK if the preroll completed and processing can
2200 * continue. Any other return value should be returned from the render vmethod.
2205 gst_base_sink_do_preroll (GstBaseSink * sink, GstMiniObject * obj)
2209 while (G_UNLIKELY (sink->need_preroll)) {
2211 GST_DEBUG_OBJECT (sink, "prerolling object %p", obj);
2213 obj_type = get_object_type (obj);
2215 ret = gst_base_sink_preroll_object (sink, obj_type, obj);
2216 if (ret != GST_FLOW_OK)
2217 goto preroll_failed;
2219 /* need to recheck here because the commit state could have
2220 * made us not need the preroll anymore */
2221 if (G_LIKELY (sink->need_preroll)) {
2222 /* block until the state changes, or we get a flush, or something */
2223 ret = gst_base_sink_wait_preroll (sink);
2224 if ((ret != GST_FLOW_OK) && (ret != GST_FLOW_STEP))
2225 goto preroll_failed;
2233 GST_DEBUG_OBJECT (sink, "preroll failed: %s", gst_flow_get_name (ret));
2239 * gst_base_sink_wait_eos:
2241 * @time: the running_time to be reached
2242 * @jitter: (out) (allow-none): the jitter to be filled with time diff, or NULL
2244 * This function will block until @time is reached. It is usually called by
2245 * subclasses that use their own internal synchronisation but want to let the
2246 * EOS be handled by the base class.
2248 * This function should only be called with the PREROLL_LOCK held, like when
2249 * receiving an EOS event in the ::event vmethod.
2251 * The @time argument should be the running_time of when the EOS should happen
2252 * and will be adjusted with any latency and offset configured in the sink.
2254 * Returns: #GstFlowReturn
2259 gst_base_sink_wait_eos (GstBaseSink * sink, GstClockTime time,
2260 GstClockTimeDiff * jitter)
2262 GstClockReturn status;
2268 GST_DEBUG_OBJECT (sink, "checking preroll");
2270 /* first wait for the playing state before we can continue */
2271 while (G_UNLIKELY (sink->need_preroll)) {
2272 ret = gst_base_sink_wait_preroll (sink);
2273 if ((ret != GST_FLOW_OK) && (ret != GST_FLOW_STEP))
2277 /* preroll done, we can sync since we are in PLAYING now. */
2278 GST_DEBUG_OBJECT (sink, "possibly waiting for clock to reach %"
2279 GST_TIME_FORMAT, GST_TIME_ARGS (time));
2281 /* compensate for latency and ts_offset. We don't adjust for render delay
2282 * because we don't interact with the device on EOS normally. */
2283 stime = gst_base_sink_adjust_time (sink, time);
2285 /* wait for the clock, this can be interrupted because we got shut down or
2287 status = gst_base_sink_wait_clock (sink, stime, jitter);
2289 GST_DEBUG_OBJECT (sink, "clock returned %d", status);
2291 /* invalid time, no clock or sync disabled, just continue then */
2292 if (status == GST_CLOCK_BADTIME)
2295 /* waiting could have been interrupted and we can be flushing now */
2296 if (G_UNLIKELY (sink->flushing))
2299 /* retry if we got unscheduled, which means we did not reach the timeout
2300 * yet. if some other error occures, we continue. */
2301 } while (status == GST_CLOCK_UNSCHEDULED);
2303 GST_DEBUG_OBJECT (sink, "end of stream");
2310 GST_DEBUG_OBJECT (sink, "we are flushing");
2311 return GST_FLOW_WRONG_STATE;
2315 /* with STREAM_LOCK, PREROLL_LOCK
2317 * Make sure we are in PLAYING and synchronize an object to the clock.
2319 * If we need preroll, we are not in PLAYING. We try to commit the state
2320 * if needed and then block if we still are not PLAYING.
2322 * We start waiting on the clock in PLAYING. If we got interrupted, we
2323 * immediatly try to re-preroll.
2325 * Some objects do not need synchronisation (most events) and so this function
2326 * immediatly returns GST_FLOW_OK.
2328 * for objects that arrive later than max-lateness to be synchronized to the
2329 * clock have the @late boolean set to TRUE.
2331 * This function keeps a running average of the jitter (the diff between the
2332 * clock time and the requested sync time). The jitter is negative for
2333 * objects that arrive in time and positive for late buffers.
2335 * does not take ownership of obj.
2337 static GstFlowReturn
2338 gst_base_sink_do_sync (GstBaseSink * basesink, GstPad * pad,
2339 GstMiniObject * obj, gboolean * late, gboolean * step_end, guint8 obj_type)
2341 GstClockTimeDiff jitter = 0;
2343 GstClockReturn status = GST_CLOCK_OK;
2344 GstClockTime rstart, rstop, sstart, sstop, stime;
2346 GstBaseSinkPrivate *priv;
2348 GstStepInfo *current, *pending;
2351 priv = basesink->priv;
2354 sstart = sstop = rstart = rstop = GST_CLOCK_TIME_NONE;
2358 priv->current_rstart = GST_CLOCK_TIME_NONE;
2360 /* get stepping info */
2361 current = &priv->current_step;
2362 pending = &priv->pending_step;
2364 /* get timing information for this object against the render segment */
2365 syncable = gst_base_sink_get_sync_times (basesink, obj,
2366 &sstart, &sstop, &rstart, &rstop, &do_sync, &stepped, &basesink->segment,
2367 current, step_end, obj_type);
2369 if (G_UNLIKELY (stepped))
2372 /* a syncable object needs to participate in preroll and
2373 * clocking. All buffers and EOS are syncable. */
2374 if (G_UNLIKELY (!syncable))
2377 /* store timing info for current object */
2378 priv->current_rstart = rstart;
2379 priv->current_rstop = (GST_CLOCK_TIME_IS_VALID (rstop) ? rstop : rstart);
2381 /* save sync time for eos when the previous object needed sync */
2382 priv->eos_rtime = (do_sync ? priv->current_rstop : GST_CLOCK_TIME_NONE);
2384 /* calculate inter frame spacing */
2385 if (G_UNLIKELY (priv->prev_rstart != -1 && priv->prev_rstart < rstart)) {
2386 GstClockTime in_diff;
2388 in_diff = rstart - priv->prev_rstart;
2390 if (priv->avg_in_diff == -1)
2391 priv->avg_in_diff = in_diff;
2393 priv->avg_in_diff = UPDATE_RUNNING_AVG (priv->avg_in_diff, in_diff);
2395 GST_LOG_OBJECT (basesink, "avg frame diff %" GST_TIME_FORMAT,
2396 GST_TIME_ARGS (priv->avg_in_diff));
2399 priv->prev_rstart = rstart;
2401 if (G_UNLIKELY (priv->earliest_in_time != -1
2402 && rstart < priv->earliest_in_time))
2406 /* first do preroll, this makes sure we commit our state
2407 * to PAUSED and can continue to PLAYING. We cannot perform
2408 * any clock sync in PAUSED because there is no clock. */
2409 ret = gst_base_sink_do_preroll (basesink, obj);
2410 if (G_UNLIKELY (ret != GST_FLOW_OK))
2411 goto preroll_failed;
2413 /* update the segment with a pending step if the current one is invalid and we
2414 * have a new pending one. We only accept new step updates after a preroll */
2415 if (G_UNLIKELY (pending->valid && !current->valid)) {
2416 start_stepping (basesink, &basesink->segment, pending, current);
2420 /* After rendering we store the position of the last buffer so that we can use
2421 * it to report the position. We need to take the lock here. */
2422 GST_OBJECT_LOCK (basesink);
2423 priv->current_sstart = sstart;
2424 priv->current_sstop = (GST_CLOCK_TIME_IS_VALID (sstop) ? sstop : sstart);
2425 GST_OBJECT_UNLOCK (basesink);
2430 /* adjust for latency */
2431 stime = gst_base_sink_adjust_time (basesink, rstart);
2433 /* adjust for render-delay, avoid underflows */
2434 if (GST_CLOCK_TIME_IS_VALID (stime)) {
2435 if (stime > priv->render_delay)
2436 stime -= priv->render_delay;
2441 /* preroll done, we can sync since we are in PLAYING now. */
2442 GST_DEBUG_OBJECT (basesink, "possibly waiting for clock to reach %"
2443 GST_TIME_FORMAT ", adjusted %" GST_TIME_FORMAT,
2444 GST_TIME_ARGS (rstart), GST_TIME_ARGS (stime));
2446 /* This function will return immediatly if start == -1, no clock
2447 * or sync is disabled with GST_CLOCK_BADTIME. */
2448 status = gst_base_sink_wait_clock (basesink, stime, &jitter);
2450 GST_DEBUG_OBJECT (basesink, "clock returned %d, jitter %c%" GST_TIME_FORMAT,
2451 status, (jitter < 0 ? '-' : ' '), GST_TIME_ARGS (ABS (jitter)));
2453 /* invalid time, no clock or sync disabled, just render */
2454 if (status == GST_CLOCK_BADTIME)
2457 /* waiting could have been interrupted and we can be flushing now */
2458 if (G_UNLIKELY (basesink->flushing))
2461 /* check for unlocked by a state change, we are not flushing so
2462 * we can try to preroll on the current buffer. */
2463 if (G_UNLIKELY (status == GST_CLOCK_UNSCHEDULED)) {
2464 GST_DEBUG_OBJECT (basesink, "unscheduled, waiting some more");
2465 priv->call_preroll = TRUE;
2469 /* successful syncing done, record observation */
2470 priv->current_jitter = jitter;
2472 /* check if the object should be dropped */
2473 *late = gst_base_sink_is_too_late (basesink, obj, rstart, rstop,
2482 GST_DEBUG_OBJECT (basesink, "skipped stepped object %p", obj);
2488 GST_DEBUG_OBJECT (basesink, "non syncable object %p", obj);
2493 GST_DEBUG_OBJECT (basesink, "dropped because of QoS %p", obj);
2499 GST_DEBUG_OBJECT (basesink, "we are flushing");
2500 return GST_FLOW_WRONG_STATE;
2504 GST_DEBUG_OBJECT (basesink, "preroll failed");
2511 gst_base_sink_send_qos (GstBaseSink * basesink, GstQOSType type,
2512 gdouble proportion, GstClockTime time, GstClockTimeDiff diff)
2517 /* generate Quality-of-Service event */
2518 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, basesink,
2519 "qos: type %d, proportion: %lf, diff %" G_GINT64_FORMAT ", timestamp %"
2520 GST_TIME_FORMAT, type, proportion, diff, GST_TIME_ARGS (time));
2522 event = gst_event_new_qos (type, proportion, diff, time);
2525 res = gst_pad_push_event (basesink->sinkpad, event);
2531 gst_base_sink_perform_qos (GstBaseSink * sink, gboolean dropped)
2533 GstBaseSinkPrivate *priv;
2534 GstClockTime start, stop;
2535 GstClockTimeDiff jitter;
2536 GstClockTime pt, entered, left;
2537 GstClockTime duration;
2542 start = priv->current_rstart;
2544 if (priv->current_step.valid)
2547 /* if Quality-of-Service disabled, do nothing */
2548 if (!g_atomic_int_get (&priv->qos_enabled) ||
2549 !GST_CLOCK_TIME_IS_VALID (start))
2552 stop = priv->current_rstop;
2553 jitter = priv->current_jitter;
2556 /* this is the time the buffer entered the sink */
2557 if (start < -jitter)
2560 entered = start + jitter;
2563 /* this is the time the buffer entered the sink */
2564 entered = start + jitter;
2565 /* this is the time the buffer left the sink */
2566 left = start + jitter;
2569 /* calculate duration of the buffer */
2570 if (GST_CLOCK_TIME_IS_VALID (stop) && stop != start)
2571 duration = stop - start;
2573 duration = priv->avg_in_diff;
2575 /* if we have the time when the last buffer left us, calculate
2576 * processing time */
2577 if (GST_CLOCK_TIME_IS_VALID (priv->last_left)) {
2578 if (entered > priv->last_left) {
2579 pt = entered - priv->last_left;
2587 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, sink, "start: %" GST_TIME_FORMAT
2588 ", stop %" GST_TIME_FORMAT ", entered %" GST_TIME_FORMAT ", left %"
2589 GST_TIME_FORMAT ", pt: %" GST_TIME_FORMAT ", duration %" GST_TIME_FORMAT
2590 ",jitter %" G_GINT64_FORMAT, GST_TIME_ARGS (start), GST_TIME_ARGS (stop),
2591 GST_TIME_ARGS (entered), GST_TIME_ARGS (left), GST_TIME_ARGS (pt),
2592 GST_TIME_ARGS (duration), jitter);
2594 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, sink, "avg_duration: %" GST_TIME_FORMAT
2595 ", avg_pt: %" GST_TIME_FORMAT ", avg_rate: %g",
2596 GST_TIME_ARGS (priv->avg_duration), GST_TIME_ARGS (priv->avg_pt),
2599 /* collect running averages. for first observations, we copy the
2601 if (!GST_CLOCK_TIME_IS_VALID (priv->avg_duration))
2602 priv->avg_duration = duration;
2604 priv->avg_duration = UPDATE_RUNNING_AVG (priv->avg_duration, duration);
2606 if (!GST_CLOCK_TIME_IS_VALID (priv->avg_pt))
2609 priv->avg_pt = UPDATE_RUNNING_AVG (priv->avg_pt, pt);
2611 if (priv->avg_duration != 0)
2613 gst_guint64_to_gdouble (priv->avg_pt) /
2614 gst_guint64_to_gdouble (priv->avg_duration);
2618 if (GST_CLOCK_TIME_IS_VALID (priv->last_left)) {
2619 if (dropped || priv->avg_rate < 0.0) {
2620 priv->avg_rate = rate;
2623 priv->avg_rate = UPDATE_RUNNING_AVG_N (priv->avg_rate, rate);
2625 priv->avg_rate = UPDATE_RUNNING_AVG_P (priv->avg_rate, rate);
2629 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, sink,
2630 "updated: avg_duration: %" GST_TIME_FORMAT ", avg_pt: %" GST_TIME_FORMAT
2631 ", avg_rate: %g", GST_TIME_ARGS (priv->avg_duration),
2632 GST_TIME_ARGS (priv->avg_pt), priv->avg_rate);
2635 if (priv->avg_rate >= 0.0) {
2637 GstClockTimeDiff diff;
2639 /* if we have a valid rate, start sending QoS messages */
2640 if (priv->current_jitter < 0) {
2641 /* make sure we never go below 0 when adding the jitter to the
2643 if (priv->current_rstart < -priv->current_jitter)
2644 priv->current_jitter = -priv->current_rstart;
2647 if (priv->throttle_time > 0) {
2648 diff = priv->throttle_time;
2649 type = GST_QOS_TYPE_THROTTLE;
2651 diff = priv->current_jitter;
2653 type = GST_QOS_TYPE_OVERFLOW;
2655 type = GST_QOS_TYPE_UNDERFLOW;
2658 gst_base_sink_send_qos (sink, type, priv->avg_rate, priv->current_rstart,
2662 /* record when this buffer will leave us */
2663 priv->last_left = left;
2666 /* reset all qos measuring */
2668 gst_base_sink_reset_qos (GstBaseSink * sink)
2670 GstBaseSinkPrivate *priv;
2674 priv->last_render_time = GST_CLOCK_TIME_NONE;
2675 priv->prev_rstart = GST_CLOCK_TIME_NONE;
2676 priv->earliest_in_time = GST_CLOCK_TIME_NONE;
2677 priv->last_left = GST_CLOCK_TIME_NONE;
2678 priv->avg_duration = GST_CLOCK_TIME_NONE;
2679 priv->avg_pt = GST_CLOCK_TIME_NONE;
2680 priv->avg_rate = -1.0;
2681 priv->avg_render = GST_CLOCK_TIME_NONE;
2682 priv->avg_in_diff = GST_CLOCK_TIME_NONE;
2688 /* Checks if the object was scheduled too late.
2690 * rstart/rstop contain the running_time start and stop values
2693 * status and jitter contain the return values from the clock wait.
2695 * returns TRUE if the buffer was too late.
2698 gst_base_sink_is_too_late (GstBaseSink * basesink, GstMiniObject * obj,
2699 GstClockTime rstart, GstClockTime rstop,
2700 GstClockReturn status, GstClockTimeDiff jitter)
2703 guint64 max_lateness;
2704 GstBaseSinkPrivate *priv;
2706 priv = basesink->priv;
2710 /* only for objects that were too late */
2711 if (G_LIKELY (status != GST_CLOCK_EARLY))
2714 max_lateness = basesink->max_lateness;
2716 /* check if frame dropping is enabled */
2717 if (max_lateness == -1)
2720 /* only check for buffers */
2721 if (G_UNLIKELY (!GST_IS_BUFFER (obj)))
2724 /* can't do check if we don't have a timestamp */
2725 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (rstart)))
2728 /* we can add a valid stop time */
2729 if (GST_CLOCK_TIME_IS_VALID (rstop))
2730 max_lateness += rstop;
2732 max_lateness += rstart;
2733 /* no stop time, use avg frame diff */
2734 if (priv->avg_in_diff != -1)
2735 max_lateness += priv->avg_in_diff;
2738 /* if the jitter bigger than duration and lateness we are too late */
2739 if ((late = rstart + jitter > max_lateness)) {
2740 GST_CAT_DEBUG_OBJECT (GST_CAT_PERFORMANCE, basesink,
2741 "buffer is too late %" GST_TIME_FORMAT
2742 " > %" GST_TIME_FORMAT, GST_TIME_ARGS (rstart + jitter),
2743 GST_TIME_ARGS (max_lateness));
2744 /* !!emergency!!, if we did not receive anything valid for more than a
2745 * second, render it anyway so the user sees something */
2746 if (GST_CLOCK_TIME_IS_VALID (priv->last_render_time) &&
2747 rstart - priv->last_render_time > GST_SECOND) {
2749 GST_ELEMENT_WARNING (basesink, CORE, CLOCK,
2750 (_("A lot of buffers are being dropped.")),
2751 ("There may be a timestamping problem, or this computer is too slow."));
2752 GST_CAT_DEBUG_OBJECT (GST_CAT_PERFORMANCE, basesink,
2753 "**emergency** last buffer at %" GST_TIME_FORMAT " > GST_SECOND",
2754 GST_TIME_ARGS (priv->last_render_time));
2759 if (!late || !GST_CLOCK_TIME_IS_VALID (priv->last_render_time)) {
2760 priv->last_render_time = rstart;
2761 /* the next allowed input timestamp */
2762 if (priv->throttle_time > 0)
2763 priv->earliest_in_time = rstart + priv->throttle_time;
2770 GST_DEBUG_OBJECT (basesink, "object was scheduled in time");
2775 GST_DEBUG_OBJECT (basesink, "frame dropping disabled");
2780 GST_DEBUG_OBJECT (basesink, "object is not a buffer");
2785 GST_DEBUG_OBJECT (basesink, "buffer has no timestamp");
2790 /* called before and after calling the render vmethod. It keeps track of how
2791 * much time was spent in the render method and is used to check if we are
2794 gst_base_sink_do_render_stats (GstBaseSink * basesink, gboolean start)
2796 GstBaseSinkPrivate *priv;
2798 priv = basesink->priv;
2801 priv->start = gst_util_get_timestamp ();
2803 GstClockTime elapsed;
2805 priv->stop = gst_util_get_timestamp ();
2807 elapsed = GST_CLOCK_DIFF (priv->start, priv->stop);
2809 if (!GST_CLOCK_TIME_IS_VALID (priv->avg_render))
2810 priv->avg_render = elapsed;
2812 priv->avg_render = UPDATE_RUNNING_AVG (priv->avg_render, elapsed);
2814 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, basesink,
2815 "avg_render: %" GST_TIME_FORMAT, GST_TIME_ARGS (priv->avg_render));
2819 /* with STREAM_LOCK, PREROLL_LOCK,
2821 * Synchronize the object on the clock and then render it.
2823 * takes ownership of obj.
2825 static GstFlowReturn
2826 gst_base_sink_render_object (GstBaseSink * basesink, GstPad * pad,
2827 guint8 obj_type, gpointer obj)
2830 GstBaseSinkClass *bclass;
2831 gboolean late, step_end;
2833 GstBaseSinkPrivate *priv;
2835 priv = basesink->priv;
2837 if (OBJ_IS_BUFFERLIST (obj_type)) {
2839 * If buffer list, use the first group buffer within the list
2842 sync_obj = gst_buffer_list_get (GST_BUFFER_LIST_CAST (obj), 0);
2843 g_assert (NULL != sync_obj);
2852 /* synchronize this object, non syncable objects return OK
2855 gst_base_sink_do_sync (basesink, pad, sync_obj, &late, &step_end,
2857 if (G_UNLIKELY (ret != GST_FLOW_OK))
2860 /* and now render, event or buffer/buffer list. */
2861 if (G_LIKELY (OBJ_IS_BUFFERFULL (obj_type))) {
2862 /* drop late buffers unconditionally, let's hope it's unlikely */
2863 if (G_UNLIKELY (late))
2866 bclass = GST_BASE_SINK_GET_CLASS (basesink);
2868 if (G_LIKELY ((OBJ_IS_BUFFERLIST (obj_type) && bclass->render_list) ||
2869 (!OBJ_IS_BUFFERLIST (obj_type) && bclass->render))) {
2872 /* read once, to get same value before and after */
2873 do_qos = g_atomic_int_get (&priv->qos_enabled);
2875 GST_DEBUG_OBJECT (basesink, "rendering object %p", obj);
2877 /* record rendering time for QoS and stats */
2879 gst_base_sink_do_render_stats (basesink, TRUE);
2881 if (!OBJ_IS_BUFFERLIST (obj_type)) {
2884 /* For buffer lists do not set last buffer. Creating buffer
2885 * with meaningful data can be done only with memcpy which will
2886 * significantly affect performance */
2887 buf = GST_BUFFER_CAST (obj);
2888 gst_base_sink_set_last_buffer (basesink, buf);
2890 ret = bclass->render (basesink, buf);
2892 GstBufferList *buflist;
2894 buflist = GST_BUFFER_LIST_CAST (obj);
2896 ret = bclass->render_list (basesink, buflist);
2900 gst_base_sink_do_render_stats (basesink, FALSE);
2902 if (ret == GST_FLOW_STEP)
2905 if (G_UNLIKELY (basesink->flushing))
2910 } else if (G_LIKELY (OBJ_IS_EVENT (obj_type))) {
2911 GstEvent *event = GST_EVENT_CAST (obj);
2912 gboolean event_res = TRUE;
2915 bclass = GST_BASE_SINK_GET_CLASS (basesink);
2917 type = GST_EVENT_TYPE (event);
2919 GST_DEBUG_OBJECT (basesink, "rendering event %p, type %s", obj,
2920 gst_event_type_get_name (type));
2923 event_res = bclass->event (basesink, event);
2925 /* when we get here we could be flushing again when the event handler calls
2926 * _wait_eos(). We have to ignore this object in that case. */
2927 if (G_UNLIKELY (basesink->flushing))
2930 if (G_LIKELY (event_res)) {
2933 seqnum = basesink->priv->seqnum = gst_event_get_seqnum (event);
2934 GST_DEBUG_OBJECT (basesink, "Got seqnum #%" G_GUINT32_FORMAT, seqnum);
2939 GstMessage *message;
2941 /* the EOS event is completely handled so we mark
2942 * ourselves as being in the EOS state. eos is also
2943 * protected by the object lock so we can read it when
2944 * answering the POSITION query. */
2945 GST_OBJECT_LOCK (basesink);
2946 basesink->eos = TRUE;
2947 GST_OBJECT_UNLOCK (basesink);
2949 /* ok, now we can post the message */
2950 GST_DEBUG_OBJECT (basesink, "Now posting EOS");
2952 message = gst_message_new_eos (GST_OBJECT_CAST (basesink));
2953 gst_message_set_seqnum (message, seqnum);
2954 gst_element_post_message (GST_ELEMENT_CAST (basesink), message);
2957 case GST_EVENT_SEGMENT:
2958 /* configure the segment */
2959 gst_base_sink_configure_segment (basesink, pad, event,
2960 &basesink->segment);
2962 case GST_EVENT_SINK_MESSAGE:{
2963 GstMessage *msg = NULL;
2965 gst_event_parse_sink_message (event, &msg);
2968 gst_element_post_message (GST_ELEMENT_CAST (basesink), msg);
2975 g_return_val_if_reached (GST_FLOW_ERROR);
2980 /* the step ended, check if we need to activate a new step */
2981 GST_DEBUG_OBJECT (basesink, "step ended");
2982 stop_stepping (basesink, &basesink->segment, &priv->current_step,
2983 priv->current_rstart, priv->current_rstop, basesink->eos);
2987 gst_base_sink_perform_qos (basesink, late);
2989 GST_DEBUG_OBJECT (basesink, "object unref after render %p", obj);
2990 gst_mini_object_unref (GST_MINI_OBJECT_CAST (obj));
2996 GST_DEBUG_OBJECT (basesink, "do_sync returned %s", gst_flow_get_name (ret));
3002 GST_DEBUG_OBJECT (basesink, "buffer late, dropping");
3004 if (g_atomic_int_get (&priv->qos_enabled)) {
3005 GstMessage *qos_msg;
3006 GstClockTime timestamp, duration;
3008 timestamp = GST_BUFFER_TIMESTAMP (GST_BUFFER_CAST (sync_obj));
3009 duration = GST_BUFFER_DURATION (GST_BUFFER_CAST (sync_obj));
3011 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, basesink,
3012 "qos: dropped buffer rt %" GST_TIME_FORMAT ", st %" GST_TIME_FORMAT
3013 ", ts %" GST_TIME_FORMAT ", dur %" GST_TIME_FORMAT,
3014 GST_TIME_ARGS (priv->current_rstart),
3015 GST_TIME_ARGS (priv->current_sstart), GST_TIME_ARGS (timestamp),
3016 GST_TIME_ARGS (duration));
3017 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, basesink,
3018 "qos: rendered %" G_GUINT64_FORMAT ", dropped %" G_GUINT64_FORMAT,
3019 priv->rendered, priv->dropped);
3022 gst_message_new_qos (GST_OBJECT_CAST (basesink), basesink->sync,
3023 priv->current_rstart, priv->current_sstart, timestamp, duration);
3024 gst_message_set_qos_values (qos_msg, priv->current_jitter, priv->avg_rate,
3026 gst_message_set_qos_stats (qos_msg, GST_FORMAT_BUFFERS, priv->rendered,
3028 gst_element_post_message (GST_ELEMENT_CAST (basesink), qos_msg);
3034 GST_DEBUG_OBJECT (basesink, "we are flushing, ignore object");
3035 gst_mini_object_unref (obj);
3036 return GST_FLOW_WRONG_STATE;
3040 /* with STREAM_LOCK, PREROLL_LOCK
3042 * Perform preroll on the given object. For buffers this means
3043 * calling the preroll subclass method.
3044 * If that succeeds, the state will be commited.
3046 * function does not take ownership of obj.
3048 static GstFlowReturn
3049 gst_base_sink_preroll_object (GstBaseSink * basesink, guint8 obj_type,
3050 GstMiniObject * obj)
3054 GST_DEBUG_OBJECT (basesink, "prerolling object %p", obj);
3056 /* if it's a buffer, we need to call the preroll method */
3057 if (G_LIKELY (OBJ_IS_BUFFERFULL (obj_type) && basesink->priv->call_preroll)) {
3058 GstBaseSinkClass *bclass;
3060 GstClockTime timestamp;
3062 if (OBJ_IS_BUFFERLIST (obj_type)) {
3063 buf = gst_buffer_list_get (GST_BUFFER_LIST_CAST (obj), 0);
3064 g_assert (NULL != buf);
3066 buf = GST_BUFFER_CAST (obj);
3069 timestamp = GST_BUFFER_TIMESTAMP (buf);
3071 GST_DEBUG_OBJECT (basesink, "preroll buffer %" GST_TIME_FORMAT,
3072 GST_TIME_ARGS (timestamp));
3075 * For buffer lists do not set last buffer. Creating buffer
3076 * with meaningful data can be done only with memcpy which will
3077 * significantly affect performance
3079 if (!OBJ_IS_BUFFERLIST (obj_type)) {
3080 gst_base_sink_set_last_buffer (basesink, buf);
3083 bclass = GST_BASE_SINK_GET_CLASS (basesink);
3084 if (bclass->preroll)
3085 if ((ret = bclass->preroll (basesink, buf)) != GST_FLOW_OK)
3086 goto preroll_failed;
3088 basesink->priv->call_preroll = FALSE;
3092 if (G_LIKELY (basesink->playing_async)) {
3093 if (G_UNLIKELY (!gst_base_sink_commit_state (basesink)))
3102 GST_DEBUG_OBJECT (basesink, "preroll failed, abort state");
3103 gst_element_abort_state (GST_ELEMENT_CAST (basesink));
3108 GST_DEBUG_OBJECT (basesink, "stopping while commiting state");
3109 return GST_FLOW_WRONG_STATE;
3113 /* with STREAM_LOCK, PREROLL_LOCK
3115 * Queue an object for rendering.
3116 * The first prerollable object queued will complete the preroll. If the
3117 * preroll queue if filled, we render all the objects in the queue.
3119 * This function takes ownership of the object.
3121 static GstFlowReturn
3122 gst_base_sink_queue_object_unlocked (GstBaseSink * basesink, GstPad * pad,
3123 guint8 obj_type, gpointer obj, gboolean prerollable)
3125 GstFlowReturn ret = GST_FLOW_OK;
3129 if (G_UNLIKELY (basesink->need_preroll)) {
3130 if (G_LIKELY (prerollable))
3131 basesink->preroll_queued++;
3133 length = basesink->preroll_queued;
3135 GST_DEBUG_OBJECT (basesink, "now %d prerolled items", length);
3137 /* first prerollable item needs to finish the preroll */
3139 ret = gst_base_sink_preroll_object (basesink, obj_type, obj);
3140 if (G_UNLIKELY (ret != GST_FLOW_OK))
3141 goto preroll_failed;
3143 /* need to recheck if we need preroll, commmit state during preroll
3144 * could have made us not need more preroll. */
3145 if (G_UNLIKELY (basesink->need_preroll)) {
3146 /* see if we can render now, if we can't add the object to the preroll
3148 if (G_UNLIKELY (length <= basesink->preroll_queue_max_len))
3152 /* we can start rendering (or blocking) the queued object
3154 q = basesink->preroll_queue;
3155 while (G_UNLIKELY (!g_queue_is_empty (q))) {
3159 o = g_queue_pop_head (q);
3160 GST_DEBUG_OBJECT (basesink, "rendering queued object %p", o);
3162 ot = get_object_type (o);
3164 /* do something with the return value */
3165 ret = gst_base_sink_render_object (basesink, pad, ot, o);
3166 if (ret != GST_FLOW_OK)
3167 goto dequeue_failed;
3170 /* now render the object */
3171 ret = gst_base_sink_render_object (basesink, pad, obj_type, obj);
3172 basesink->preroll_queued = 0;
3179 GST_DEBUG_OBJECT (basesink, "preroll failed, reason %s",
3180 gst_flow_get_name (ret));
3181 gst_mini_object_unref (GST_MINI_OBJECT_CAST (obj));
3186 /* add object to the queue and return */
3187 GST_DEBUG_OBJECT (basesink, "need more preroll data %d <= %d",
3188 length, basesink->preroll_queue_max_len);
3189 g_queue_push_tail (basesink->preroll_queue, obj);
3194 GST_DEBUG_OBJECT (basesink, "rendering queued objects failed, reason %s",
3195 gst_flow_get_name (ret));
3196 gst_mini_object_unref (GST_MINI_OBJECT_CAST (obj));
3203 * This function grabs the PREROLL_LOCK and adds the object to
3206 * This function takes ownership of obj.
3208 * Note: Only GstEvent seem to be passed to this private method
3210 static GstFlowReturn
3211 gst_base_sink_queue_object (GstBaseSink * basesink, GstPad * pad,
3212 GstMiniObject * obj, gboolean prerollable)
3216 GST_BASE_SINK_PREROLL_LOCK (basesink);
3217 if (G_UNLIKELY (basesink->flushing))
3220 if (G_UNLIKELY (basesink->priv->received_eos))
3224 gst_base_sink_queue_object_unlocked (basesink, pad, _PR_IS_EVENT, obj,
3226 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
3233 GST_DEBUG_OBJECT (basesink, "sink is flushing");
3234 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
3235 gst_mini_object_unref (obj);
3236 return GST_FLOW_WRONG_STATE;
3240 GST_DEBUG_OBJECT (basesink,
3241 "we are EOS, dropping object, return UNEXPECTED");
3242 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
3243 gst_mini_object_unref (obj);
3244 return GST_FLOW_UNEXPECTED;
3249 gst_base_sink_flush_start (GstBaseSink * basesink, GstPad * pad)
3251 /* make sure we are not blocked on the clock also clear any pending
3253 gst_base_sink_set_flushing (basesink, pad, TRUE);
3255 /* we grab the stream lock but that is not needed since setting the
3256 * sink to flushing would make sure no state commit is being done
3258 GST_PAD_STREAM_LOCK (pad);
3259 gst_base_sink_reset_qos (basesink);
3260 /* and we need to commit our state again on the next
3261 * prerolled buffer */
3262 basesink->playing_async = TRUE;
3263 if (basesink->priv->async_enabled) {
3264 gst_element_lost_state (GST_ELEMENT_CAST (basesink), TRUE);
3266 basesink->priv->have_latency = TRUE;
3268 gst_base_sink_set_last_buffer (basesink, NULL);
3269 GST_PAD_STREAM_UNLOCK (pad);
3273 gst_base_sink_flush_stop (GstBaseSink * basesink, GstPad * pad)
3275 /* unset flushing so we can accept new data, this also flushes out any EOS
3277 gst_base_sink_set_flushing (basesink, pad, FALSE);
3279 /* for position reporting */
3280 GST_OBJECT_LOCK (basesink);
3281 basesink->priv->current_sstart = GST_CLOCK_TIME_NONE;
3282 basesink->priv->current_sstop = GST_CLOCK_TIME_NONE;
3283 basesink->priv->eos_rtime = GST_CLOCK_TIME_NONE;
3284 basesink->priv->call_preroll = TRUE;
3285 basesink->priv->current_step.valid = FALSE;
3286 basesink->priv->pending_step.valid = FALSE;
3287 if (basesink->pad_mode == GST_ACTIVATE_PUSH) {
3288 /* we need new segment info after the flush. */
3289 basesink->have_newsegment = FALSE;
3290 gst_segment_init (&basesink->segment, GST_FORMAT_UNDEFINED);
3291 gst_segment_init (basesink->clip_segment, GST_FORMAT_UNDEFINED);
3293 GST_OBJECT_UNLOCK (basesink);
3297 gst_base_sink_event (GstPad * pad, GstEvent * event)
3299 GstBaseSink *basesink;
3300 gboolean result = TRUE;
3301 GstBaseSinkClass *bclass;
3303 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
3304 if (G_UNLIKELY (basesink == NULL)) {
3305 gst_event_unref (event);
3309 bclass = GST_BASE_SINK_GET_CLASS (basesink);
3311 GST_DEBUG_OBJECT (basesink, "received event %p %" GST_PTR_FORMAT, event,
3314 switch (GST_EVENT_TYPE (event)) {
3319 GST_BASE_SINK_PREROLL_LOCK (basesink);
3320 if (G_UNLIKELY (basesink->flushing))
3323 if (G_UNLIKELY (basesink->priv->received_eos)) {
3324 /* we can't accept anything when we are EOS */
3326 gst_event_unref (event);
3328 /* we set the received EOS flag here so that we can use it when testing if
3329 * we are prerolled and to refuse more buffers. */
3330 basesink->priv->received_eos = TRUE;
3332 /* EOS is a prerollable object, we call the unlocked version because it
3333 * does not check the received_eos flag. */
3334 ret = gst_base_sink_queue_object_unlocked (basesink, pad,
3335 _PR_IS_EVENT, GST_MINI_OBJECT_CAST (event), TRUE);
3336 if (G_UNLIKELY (ret != GST_FLOW_OK))
3339 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
3342 case GST_EVENT_CAPS:
3346 GST_DEBUG_OBJECT (basesink, "caps %p", event);
3348 gst_event_parse_caps (event, &caps);
3349 if (bclass->set_caps)
3350 result = bclass->set_caps (basesink, caps);
3352 gst_event_unref (event);
3355 case GST_EVENT_SEGMENT:
3359 GST_DEBUG_OBJECT (basesink, "segment %p", event);
3361 GST_BASE_SINK_PREROLL_LOCK (basesink);
3362 if (G_UNLIKELY (basesink->flushing))
3365 /* the new segment is a non prerollable item and does not block anything,
3366 * we need to configure the current clipping segment and insert the event
3367 * in the queue to serialize it with the buffers for rendering. */
3368 gst_base_sink_configure_segment (basesink, pad, event,
3369 basesink->clip_segment);
3372 gst_base_sink_queue_object_unlocked (basesink, pad,
3373 _PR_IS_EVENT, GST_MINI_OBJECT_CAST (event), FALSE);
3374 if (G_UNLIKELY (ret != GST_FLOW_OK))
3377 GST_OBJECT_LOCK (basesink);
3378 basesink->have_newsegment = TRUE;
3379 GST_OBJECT_UNLOCK (basesink);
3381 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
3384 case GST_EVENT_FLUSH_START:
3386 bclass->event (basesink, event);
3388 GST_DEBUG_OBJECT (basesink, "flush-start %p", event);
3390 gst_base_sink_flush_start (basesink, pad);
3392 gst_event_unref (event);
3394 case GST_EVENT_FLUSH_STOP:
3396 bclass->event (basesink, event);
3398 GST_DEBUG_OBJECT (basesink, "flush-stop %p", event);
3400 gst_base_sink_flush_stop (basesink, pad);
3402 gst_event_unref (event);
3405 /* other events are sent to queue or subclass depending on if they
3406 * are serialized. */
3407 if (GST_EVENT_IS_SERIALIZED (event)) {
3408 gst_base_sink_queue_object (basesink, pad,
3409 GST_MINI_OBJECT_CAST (event), FALSE);
3412 bclass->event (basesink, event);
3413 gst_event_unref (event);
3418 gst_object_unref (basesink);
3425 GST_DEBUG_OBJECT (basesink, "we are flushing");
3426 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
3428 gst_event_unref (event);
3433 /* default implementation to calculate the start and end
3434 * timestamps on a buffer, subclasses can override
3437 gst_base_sink_get_times (GstBaseSink * basesink, GstBuffer * buffer,
3438 GstClockTime * start, GstClockTime * end)
3440 GstClockTime timestamp, duration;
3442 timestamp = GST_BUFFER_TIMESTAMP (buffer);
3443 if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
3445 /* get duration to calculate end time */
3446 duration = GST_BUFFER_DURATION (buffer);
3447 if (GST_CLOCK_TIME_IS_VALID (duration)) {
3448 *end = timestamp + duration;
3454 /* must be called with PREROLL_LOCK */
3456 gst_base_sink_needs_preroll (GstBaseSink * basesink)
3458 gboolean is_prerolled, res;
3460 /* we have 2 cases where the PREROLL_LOCK is released:
3461 * 1) we are blocking in the PREROLL_LOCK and thus are prerolled.
3462 * 2) we are syncing on the clock
3464 is_prerolled = basesink->have_preroll || basesink->priv->received_eos;
3465 res = !is_prerolled;
3467 GST_DEBUG_OBJECT (basesink, "have_preroll: %d, EOS: %d => needs preroll: %d",
3468 basesink->have_preroll, basesink->priv->received_eos, res);
3473 /* with STREAM_LOCK, PREROLL_LOCK
3475 * Takes a buffer and compare the timestamps with the last segment.
3476 * If the buffer falls outside of the segment boundaries, drop it.
3477 * Else queue the buffer for preroll and rendering.
3479 * This function takes ownership of the buffer.
3481 static GstFlowReturn
3482 gst_base_sink_chain_unlocked (GstBaseSink * basesink, GstPad * pad,
3483 guint8 obj_type, gpointer obj)
3485 GstBaseSinkClass *bclass;
3486 GstFlowReturn result;
3487 GstClockTime start = GST_CLOCK_TIME_NONE, end = GST_CLOCK_TIME_NONE;
3488 GstSegment *clip_segment;
3489 GstBuffer *time_buf;
3491 if (G_UNLIKELY (basesink->flushing))
3494 if (G_UNLIKELY (basesink->priv->received_eos))
3497 if (OBJ_IS_BUFFERLIST (obj_type)) {
3498 time_buf = gst_buffer_list_get (GST_BUFFER_LIST_CAST (obj), 0);
3499 g_assert (NULL != time_buf);
3501 time_buf = GST_BUFFER_CAST (obj);
3504 /* for code clarity */
3505 clip_segment = basesink->clip_segment;
3507 if (G_UNLIKELY (!basesink->have_newsegment)) {
3510 sync = gst_base_sink_get_sync (basesink);
3512 GST_ELEMENT_WARNING (basesink, STREAM, FAILED,
3513 (_("Internal data flow problem.")),
3514 ("Received buffer without a new-segment. Assuming timestamps start from 0."));
3517 /* this means this sink will assume timestamps start from 0 */
3518 GST_OBJECT_LOCK (basesink);
3519 clip_segment->start = 0;
3520 clip_segment->stop = -1;
3521 basesink->segment.start = 0;
3522 basesink->segment.stop = -1;
3523 basesink->have_newsegment = TRUE;
3524 GST_OBJECT_UNLOCK (basesink);
3527 bclass = GST_BASE_SINK_GET_CLASS (basesink);
3529 /* check if the buffer needs to be dropped, we first ask the subclass for the
3531 if (bclass->get_times)
3532 bclass->get_times (basesink, time_buf, &start, &end);
3534 if (!GST_CLOCK_TIME_IS_VALID (start)) {
3535 /* if the subclass does not want sync, we use our own values so that we at
3536 * least clip the buffer to the segment */
3537 gst_base_sink_get_times (basesink, time_buf, &start, &end);
3540 GST_DEBUG_OBJECT (basesink, "got times start: %" GST_TIME_FORMAT
3541 ", end: %" GST_TIME_FORMAT, GST_TIME_ARGS (start), GST_TIME_ARGS (end));
3543 /* a dropped buffer does not participate in anything */
3544 if (GST_CLOCK_TIME_IS_VALID (start) &&
3545 (clip_segment->format == GST_FORMAT_TIME)) {
3546 if (G_UNLIKELY (!gst_segment_clip (clip_segment,
3547 GST_FORMAT_TIME, start, end, NULL, NULL)))
3548 goto out_of_segment;
3551 /* now we can process the buffer in the queue, this function takes ownership
3553 result = gst_base_sink_queue_object_unlocked (basesink, pad,
3554 obj_type, obj, TRUE);
3560 GST_DEBUG_OBJECT (basesink, "sink is flushing");
3561 gst_mini_object_unref (GST_MINI_OBJECT_CAST (obj));
3562 return GST_FLOW_WRONG_STATE;
3566 GST_DEBUG_OBJECT (basesink,
3567 "we are EOS, dropping object, return UNEXPECTED");
3568 gst_mini_object_unref (GST_MINI_OBJECT_CAST (obj));
3569 return GST_FLOW_UNEXPECTED;
3573 GST_DEBUG_OBJECT (basesink, "dropping buffer, out of clipping segment");
3574 gst_mini_object_unref (GST_MINI_OBJECT_CAST (obj));
3581 static GstFlowReturn
3582 gst_base_sink_chain_main (GstBaseSink * basesink, GstPad * pad,
3583 guint8 obj_type, gpointer obj)
3585 GstFlowReturn result;
3587 if (G_UNLIKELY (basesink->pad_mode != GST_ACTIVATE_PUSH))
3590 GST_BASE_SINK_PREROLL_LOCK (basesink);
3591 result = gst_base_sink_chain_unlocked (basesink, pad, obj_type, obj);
3592 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
3600 GST_OBJECT_LOCK (pad);
3601 GST_WARNING_OBJECT (basesink,
3602 "Push on pad %s:%s, but it was not activated in push mode",
3603 GST_DEBUG_PAD_NAME (pad));
3604 GST_OBJECT_UNLOCK (pad);
3605 gst_mini_object_unref (GST_MINI_OBJECT_CAST (obj));
3606 /* we don't post an error message this will signal to the peer
3607 * pushing that EOS is reached. */
3608 result = GST_FLOW_UNEXPECTED;
3613 static GstFlowReturn
3614 gst_base_sink_chain (GstPad * pad, GstBuffer * buf)
3616 GstBaseSink *basesink;
3618 basesink = GST_BASE_SINK (GST_OBJECT_PARENT (pad));
3620 return gst_base_sink_chain_main (basesink, pad, _PR_IS_BUFFER, buf);
3623 static GstFlowReturn
3624 gst_base_sink_chain_list (GstPad * pad, GstBufferList * list)
3626 GstBaseSink *basesink;
3627 GstBaseSinkClass *bclass;
3628 GstFlowReturn result;
3630 basesink = GST_BASE_SINK (GST_OBJECT_PARENT (pad));
3631 bclass = GST_BASE_SINK_GET_CLASS (basesink);
3633 if (G_LIKELY (bclass->render_list)) {
3634 result = gst_base_sink_chain_main (basesink, pad, _PR_IS_BUFFERLIST, list);
3639 GST_INFO_OBJECT (pad, "chaining each group in list as a merged buffer");
3641 len = gst_buffer_list_len (list);
3643 result = GST_FLOW_OK;
3644 for (i = 0; i < len; i++) {
3645 buffer = gst_buffer_list_get (list, 0);
3646 result = gst_base_sink_chain_main (basesink, pad, _PR_IS_BUFFER,
3647 gst_buffer_ref (buffer));
3648 if (result != GST_FLOW_OK)
3651 gst_buffer_list_unref (list);
3658 gst_base_sink_default_do_seek (GstBaseSink * sink, GstSegment * segment)
3660 gboolean res = TRUE;
3662 /* update our offset if the start/stop position was updated */
3663 if (segment->format == GST_FORMAT_BYTES) {
3664 segment->time = segment->start;
3665 } else if (segment->start == 0) {
3666 /* seek to start, we can implement a default for this. */
3670 GST_INFO_OBJECT (sink, "Can't do a default seek");
3676 #define SEEK_TYPE_IS_RELATIVE(t) (((t) != GST_SEEK_TYPE_NONE) && ((t) != GST_SEEK_TYPE_SET))
3679 gst_base_sink_default_prepare_seek_segment (GstBaseSink * sink,
3680 GstEvent * event, GstSegment * segment)
3682 /* By default, we try one of 2 things:
3683 * - For absolute seek positions, convert the requested position to our
3684 * configured processing format and place it in the output segment \
3685 * - For relative seek positions, convert our current (input) values to the
3686 * seek format, adjust by the relative seek offset and then convert back to
3687 * the processing format
3689 GstSeekType cur_type, stop_type;
3692 GstFormat seek_format, dest_format;
3695 gboolean res = TRUE;
3697 gst_event_parse_seek (event, &rate, &seek_format, &flags,
3698 &cur_type, &cur, &stop_type, &stop);
3699 dest_format = segment->format;
3701 if (seek_format == dest_format) {
3702 gst_segment_do_seek (segment, rate, seek_format, flags,
3703 cur_type, cur, stop_type, stop, &update);
3707 if (cur_type != GST_SEEK_TYPE_NONE) {
3708 /* FIXME: Handle seek_cur & seek_end by converting the input segment vals */
3710 gst_pad_query_convert (sink->sinkpad, seek_format, cur, &dest_format,
3712 cur_type = GST_SEEK_TYPE_SET;
3715 if (res && stop_type != GST_SEEK_TYPE_NONE) {
3716 /* FIXME: Handle seek_cur & seek_end by converting the input segment vals */
3718 gst_pad_query_convert (sink->sinkpad, seek_format, stop, &dest_format,
3720 stop_type = GST_SEEK_TYPE_SET;
3723 /* And finally, configure our output segment in the desired format */
3724 gst_segment_do_seek (segment, rate, dest_format, flags, cur_type, cur,
3725 stop_type, stop, &update);
3734 GST_DEBUG_OBJECT (sink, "undefined format given, seek aborted.");
3739 /* perform a seek, only executed in pull mode */
3741 gst_base_sink_perform_seek (GstBaseSink * sink, GstPad * pad, GstEvent * event)
3745 GstFormat seek_format, dest_format;
3747 GstSeekType cur_type, stop_type;
3748 gboolean seekseg_configured = FALSE;
3750 gboolean update, res = TRUE;
3751 GstSegment seeksegment;
3753 dest_format = sink->segment.format;
3756 GST_DEBUG_OBJECT (sink, "performing seek with event %p", event);
3757 gst_event_parse_seek (event, &rate, &seek_format, &flags,
3758 &cur_type, &cur, &stop_type, &stop);
3760 flush = flags & GST_SEEK_FLAG_FLUSH;
3762 GST_DEBUG_OBJECT (sink, "performing seek without event");
3767 GST_DEBUG_OBJECT (sink, "flushing upstream");
3768 gst_pad_push_event (pad, gst_event_new_flush_start ());
3769 gst_base_sink_flush_start (sink, pad);
3771 GST_DEBUG_OBJECT (sink, "pausing pulling thread");
3774 GST_PAD_STREAM_LOCK (pad);
3776 /* If we configured the seeksegment above, don't overwrite it now. Otherwise
3777 * copy the current segment info into the temp segment that we can actually
3778 * attempt the seek with. We only update the real segment if the seek suceeds. */
3779 if (!seekseg_configured) {
3780 memcpy (&seeksegment, &sink->segment, sizeof (GstSegment));
3782 /* now configure the final seek segment */
3784 if (sink->segment.format != seek_format) {
3785 /* OK, here's where we give the subclass a chance to convert the relative
3786 * seek into an absolute one in the processing format. We set up any
3787 * absolute seek above, before taking the stream lock. */
3788 if (!gst_base_sink_default_prepare_seek_segment (sink, event,
3790 GST_DEBUG_OBJECT (sink,
3791 "Preparing the seek failed after flushing. " "Aborting seek");
3795 /* The seek format matches our processing format, no need to ask the
3796 * the subclass to configure the segment. */
3797 gst_segment_do_seek (&seeksegment, rate, seek_format, flags,
3798 cur_type, cur, stop_type, stop, &update);
3801 /* Else, no seek event passed, so we're just (re)starting the
3806 GST_DEBUG_OBJECT (sink, "segment configured from %" G_GINT64_FORMAT
3807 " to %" G_GINT64_FORMAT ", position %" G_GINT64_FORMAT,
3808 seeksegment.start, seeksegment.stop, seeksegment.position);
3810 /* do the seek, segment.position contains the new position. */
3811 res = gst_base_sink_default_do_seek (sink, &seeksegment);
3816 GST_DEBUG_OBJECT (sink, "stop flushing upstream");
3817 gst_pad_push_event (pad, gst_event_new_flush_stop ());
3818 gst_base_sink_flush_stop (sink, pad);
3819 } else if (res && sink->running) {
3820 /* we are running the current segment and doing a non-flushing seek,
3821 * close the segment first based on the position. */
3822 GST_DEBUG_OBJECT (sink, "closing running segment %" G_GINT64_FORMAT
3823 " to %" G_GINT64_FORMAT, sink->segment.start, sink->segment.position);
3826 /* The subclass must have converted the segment to the processing format
3828 if (res && seeksegment.format != dest_format) {
3829 GST_DEBUG_OBJECT (sink, "Subclass failed to prepare a seek segment "
3830 "in the correct format. Aborting seek.");
3834 /* if successfull seek, we update our real segment and push
3835 * out the new segment. */
3837 gst_segment_copy_into (&seeksegment, &sink->segment);
3839 if (sink->segment.flags & GST_SEEK_FLAG_SEGMENT) {
3840 gst_element_post_message (GST_ELEMENT (sink),
3841 gst_message_new_segment_start (GST_OBJECT (sink),
3842 sink->segment.format, sink->segment.position));
3846 sink->priv->discont = TRUE;
3847 sink->running = TRUE;
3849 GST_PAD_STREAM_UNLOCK (pad);
3855 set_step_info (GstBaseSink * sink, GstStepInfo * current, GstStepInfo * pending,
3856 guint seqnum, GstFormat format, guint64 amount, gdouble rate,
3857 gboolean flush, gboolean intermediate)
3859 GST_OBJECT_LOCK (sink);
3860 pending->seqnum = seqnum;
3861 pending->format = format;
3862 pending->amount = amount;
3863 pending->position = 0;
3864 pending->rate = rate;
3865 pending->flush = flush;
3866 pending->intermediate = intermediate;
3867 pending->valid = TRUE;
3868 /* flush invalidates the current stepping segment */
3870 current->valid = FALSE;
3871 GST_OBJECT_UNLOCK (sink);
3875 gst_base_sink_perform_step (GstBaseSink * sink, GstPad * pad, GstEvent * event)
3877 GstBaseSinkPrivate *priv;
3878 GstBaseSinkClass *bclass;
3879 gboolean flush, intermediate;
3884 GstStepInfo *pending, *current;
3885 GstMessage *message;
3887 bclass = GST_BASE_SINK_GET_CLASS (sink);
3890 GST_DEBUG_OBJECT (sink, "performing step with event %p", event);
3892 gst_event_parse_step (event, &format, &amount, &rate, &flush, &intermediate);
3893 seqnum = gst_event_get_seqnum (event);
3895 pending = &priv->pending_step;
3896 current = &priv->current_step;
3898 /* post message first */
3899 message = gst_message_new_step_start (GST_OBJECT (sink), FALSE, format,
3900 amount, rate, flush, intermediate);
3901 gst_message_set_seqnum (message, seqnum);
3902 gst_element_post_message (GST_ELEMENT (sink), message);
3905 /* we need to call ::unlock before locking PREROLL_LOCK
3906 * since we lock it before going into ::render */
3908 bclass->unlock (sink);
3910 GST_BASE_SINK_PREROLL_LOCK (sink);
3911 /* now that we have the PREROLL lock, clear our unlock request */
3912 if (bclass->unlock_stop)
3913 bclass->unlock_stop (sink);
3915 /* update the stepinfo and make it valid */
3916 set_step_info (sink, current, pending, seqnum, format, amount, rate, flush,
3919 if (sink->priv->async_enabled) {
3920 /* and we need to commit our state again on the next
3921 * prerolled buffer */
3922 sink->playing_async = TRUE;
3923 priv->pending_step.need_preroll = TRUE;
3924 sink->need_preroll = FALSE;
3925 gst_element_lost_state (GST_ELEMENT_CAST (sink), FALSE);
3927 sink->priv->have_latency = TRUE;
3928 sink->need_preroll = FALSE;
3930 priv->current_sstart = GST_CLOCK_TIME_NONE;
3931 priv->current_sstop = GST_CLOCK_TIME_NONE;
3932 priv->eos_rtime = GST_CLOCK_TIME_NONE;
3933 priv->call_preroll = TRUE;
3934 gst_base_sink_set_last_buffer (sink, NULL);
3935 gst_base_sink_reset_qos (sink);
3937 if (sink->clock_id) {
3938 gst_clock_id_unschedule (sink->clock_id);
3941 if (sink->have_preroll) {
3942 GST_DEBUG_OBJECT (sink, "signal waiter");
3943 priv->step_unlock = TRUE;
3944 GST_BASE_SINK_PREROLL_SIGNAL (sink);
3946 GST_BASE_SINK_PREROLL_UNLOCK (sink);
3948 /* update the stepinfo and make it valid */
3949 set_step_info (sink, current, pending, seqnum, format, amount, rate, flush,
3959 gst_base_sink_loop (GstPad * pad)
3961 GstBaseSink *basesink;
3962 GstBuffer *buf = NULL;
3963 GstFlowReturn result;
3967 basesink = GST_BASE_SINK (GST_OBJECT_PARENT (pad));
3969 g_assert (basesink->pad_mode == GST_ACTIVATE_PULL);
3971 if ((blocksize = basesink->priv->blocksize) == 0)
3974 offset = basesink->segment.position;
3976 GST_DEBUG_OBJECT (basesink, "pulling %" G_GUINT64_FORMAT ", %u",
3979 result = gst_pad_pull_range (pad, offset, blocksize, &buf);
3980 if (G_UNLIKELY (result != GST_FLOW_OK))
3983 if (G_UNLIKELY (buf == NULL))
3986 offset += gst_buffer_get_size (buf);
3988 basesink->segment.position = offset;
3990 GST_BASE_SINK_PREROLL_LOCK (basesink);
3991 result = gst_base_sink_chain_unlocked (basesink, pad, _PR_IS_BUFFER, buf);
3992 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
3993 if (G_UNLIKELY (result != GST_FLOW_OK))
4001 GST_LOG_OBJECT (basesink, "pausing task, reason %s",
4002 gst_flow_get_name (result));
4003 gst_pad_pause_task (pad);
4004 if (result == GST_FLOW_UNEXPECTED) {
4005 /* perform EOS logic */
4006 if (basesink->segment.flags & GST_SEEK_FLAG_SEGMENT) {
4007 gst_element_post_message (GST_ELEMENT_CAST (basesink),
4008 gst_message_new_segment_done (GST_OBJECT_CAST (basesink),
4009 basesink->segment.format, basesink->segment.position));
4011 gst_base_sink_event (pad, gst_event_new_eos ());
4013 } else if (result == GST_FLOW_NOT_LINKED || result <= GST_FLOW_UNEXPECTED) {
4014 /* for fatal errors we post an error message, post the error
4015 * first so the app knows about the error first.
4016 * wrong-state is not a fatal error because it happens due to
4017 * flushing and posting an error message in that case is the
4018 * wrong thing to do, e.g. when basesrc is doing a flushing
4020 GST_ELEMENT_ERROR (basesink, STREAM, FAILED,
4021 (_("Internal data stream error.")),
4022 ("stream stopped, reason %s", gst_flow_get_name (result)));
4023 gst_base_sink_event (pad, gst_event_new_eos ());
4029 GST_LOG_OBJECT (basesink, "no buffer, pausing");
4030 GST_ELEMENT_ERROR (basesink, STREAM, FAILED,
4031 (_("Internal data flow error.")), ("element returned NULL buffer"));
4032 result = GST_FLOW_ERROR;
4038 gst_base_sink_set_flushing (GstBaseSink * basesink, GstPad * pad,
4041 GstBaseSinkClass *bclass;
4043 bclass = GST_BASE_SINK_GET_CLASS (basesink);
4046 /* unlock any subclasses, we need to do this before grabbing the
4047 * PREROLL_LOCK since we hold this lock before going into ::render. */
4049 bclass->unlock (basesink);
4052 GST_BASE_SINK_PREROLL_LOCK (basesink);
4053 basesink->flushing = flushing;
4055 /* step 1, now that we have the PREROLL lock, clear our unlock request */
4056 if (bclass->unlock_stop)
4057 bclass->unlock_stop (basesink);
4059 /* set need_preroll before we unblock the clock. If the clock is unblocked
4060 * before timing out, we can reuse the buffer for preroll. */
4061 basesink->need_preroll = TRUE;
4063 /* step 2, unblock clock sync (if any) or any other blocking thing */
4064 if (basesink->clock_id) {
4065 gst_clock_id_unschedule (basesink->clock_id);
4068 /* flush out the data thread if it's locked in finish_preroll, this will
4069 * also flush out the EOS state */
4070 GST_DEBUG_OBJECT (basesink,
4071 "flushing out data thread, need preroll to TRUE");
4072 gst_base_sink_preroll_queue_flush (basesink, pad);
4074 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
4080 gst_base_sink_default_activate_pull (GstBaseSink * basesink, gboolean active)
4086 result = gst_pad_start_task (basesink->sinkpad,
4087 (GstTaskFunction) gst_base_sink_loop, basesink->sinkpad);
4089 /* step 2, make sure streaming finishes */
4090 result = gst_pad_stop_task (basesink->sinkpad);
4097 gst_base_sink_pad_activate (GstPad * pad)
4099 gboolean result = FALSE;
4100 GstBaseSink *basesink;
4102 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
4104 GST_DEBUG_OBJECT (basesink, "Trying pull mode first");
4106 gst_base_sink_set_flushing (basesink, pad, FALSE);
4108 /* we need to have the pull mode enabled */
4109 if (!basesink->can_activate_pull) {
4110 GST_DEBUG_OBJECT (basesink, "pull mode disabled");
4114 /* check if downstreams supports pull mode at all */
4115 if (!gst_pad_check_pull_range (pad)) {
4116 GST_DEBUG_OBJECT (basesink, "pull mode not supported");
4120 /* set the pad mode before starting the task so that it's in the
4121 * correct state for the new thread. also the sink set_caps and get_caps
4122 * function checks this */
4123 basesink->pad_mode = GST_ACTIVATE_PULL;
4125 /* we first try to negotiate a format so that when we try to activate
4126 * downstream, it knows about our format */
4127 if (!gst_base_sink_negotiate_pull (basesink)) {
4128 GST_DEBUG_OBJECT (basesink, "failed to negotiate in pull mode");
4132 /* ok activate now */
4133 if (!gst_pad_activate_pull (pad, TRUE)) {
4134 /* clear any pending caps */
4135 GST_OBJECT_LOCK (basesink);
4136 gst_caps_replace (&basesink->priv->pull_caps, NULL);
4137 GST_OBJECT_UNLOCK (basesink);
4138 GST_DEBUG_OBJECT (basesink, "failed to activate in pull mode");
4142 GST_DEBUG_OBJECT (basesink, "Success activating pull mode");
4146 /* push mode fallback */
4148 GST_DEBUG_OBJECT (basesink, "Falling back to push mode");
4149 if ((result = gst_pad_activate_push (pad, TRUE))) {
4150 GST_DEBUG_OBJECT (basesink, "Success activating push mode");
4155 GST_WARNING_OBJECT (basesink, "Could not activate pad in either mode");
4156 gst_base_sink_set_flushing (basesink, pad, TRUE);
4159 gst_object_unref (basesink);
4165 gst_base_sink_pad_activate_push (GstPad * pad, gboolean active)
4168 GstBaseSink *basesink;
4170 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
4173 if (!basesink->can_activate_push) {
4175 basesink->pad_mode = GST_ACTIVATE_NONE;
4178 basesink->pad_mode = GST_ACTIVATE_PUSH;
4181 if (G_UNLIKELY (basesink->pad_mode != GST_ACTIVATE_PUSH)) {
4182 g_warning ("Internal GStreamer activation error!!!");
4185 gst_base_sink_set_flushing (basesink, pad, TRUE);
4187 basesink->pad_mode = GST_ACTIVATE_NONE;
4191 gst_object_unref (basesink);
4197 gst_base_sink_negotiate_pull (GstBaseSink * basesink)
4204 /* this returns the intersection between our caps and the peer caps. If there
4205 * is no peer, it returns NULL and we can't operate in pull mode so we can
4206 * fail the negotiation. */
4207 caps = gst_pad_get_allowed_caps (GST_BASE_SINK_PAD (basesink));
4208 if (caps == NULL || gst_caps_is_empty (caps))
4209 goto no_caps_possible;
4211 GST_DEBUG_OBJECT (basesink, "allowed caps: %" GST_PTR_FORMAT, caps);
4213 caps = gst_caps_make_writable (caps);
4214 /* get the first (prefered) format */
4215 gst_caps_truncate (caps);
4217 GST_DEBUG_OBJECT (basesink, "have caps: %" GST_PTR_FORMAT, caps);
4219 if (gst_caps_is_any (caps)) {
4220 GST_DEBUG_OBJECT (basesink, "caps were ANY after fixating, "
4222 /* neither side has template caps in this case, so they are prepared for
4223 pull() without setcaps() */
4227 gst_pad_fixate_caps (GST_BASE_SINK_PAD (basesink), caps);
4228 GST_DEBUG_OBJECT (basesink, "fixated to: %" GST_PTR_FORMAT, caps);
4230 if (gst_caps_is_fixed (caps)) {
4231 if (!gst_pad_set_caps (GST_BASE_SINK_PAD (basesink), caps))
4232 goto could_not_set_caps;
4234 GST_OBJECT_LOCK (basesink);
4235 gst_caps_replace (&basesink->priv->pull_caps, caps);
4236 GST_OBJECT_UNLOCK (basesink);
4242 gst_caps_unref (caps);
4248 GST_INFO_OBJECT (basesink, "Pipeline could not agree on caps");
4249 GST_DEBUG_OBJECT (basesink, "get_allowed_caps() returned EMPTY");
4251 gst_caps_unref (caps);
4256 GST_INFO_OBJECT (basesink, "Could not set caps: %" GST_PTR_FORMAT, caps);
4257 gst_caps_unref (caps);
4262 /* this won't get called until we implement an activate function */
4264 gst_base_sink_pad_activate_pull (GstPad * pad, gboolean active)
4266 gboolean result = FALSE;
4267 GstBaseSink *basesink;
4268 GstBaseSinkClass *bclass;
4270 basesink = GST_BASE_SINK (gst_pad_get_parent (pad));
4271 bclass = GST_BASE_SINK_GET_CLASS (basesink);
4277 /* we mark we have a newsegment here because pull based
4278 * mode works just fine without having a newsegment before the
4280 format = GST_FORMAT_BYTES;
4282 gst_segment_init (&basesink->segment, format);
4283 gst_segment_init (basesink->clip_segment, format);
4284 GST_OBJECT_LOCK (basesink);
4285 basesink->have_newsegment = TRUE;
4286 GST_OBJECT_UNLOCK (basesink);
4288 /* get the peer duration in bytes */
4289 result = gst_pad_query_peer_duration (pad, &format, &duration);
4291 GST_DEBUG_OBJECT (basesink,
4292 "setting duration in bytes to %" G_GINT64_FORMAT, duration);
4293 basesink->clip_segment->duration = duration;
4294 basesink->segment.duration = duration;
4296 GST_DEBUG_OBJECT (basesink, "unknown duration");
4299 if (bclass->activate_pull)
4300 result = bclass->activate_pull (basesink, TRUE);
4305 goto activate_failed;
4308 if (G_UNLIKELY (basesink->pad_mode != GST_ACTIVATE_PULL)) {
4309 g_warning ("Internal GStreamer activation error!!!");
4312 result = gst_base_sink_set_flushing (basesink, pad, TRUE);
4313 if (bclass->activate_pull)
4314 result &= bclass->activate_pull (basesink, FALSE);
4315 basesink->pad_mode = GST_ACTIVATE_NONE;
4316 /* clear any pending caps */
4317 GST_OBJECT_LOCK (basesink);
4318 gst_caps_replace (&basesink->priv->pull_caps, NULL);
4319 GST_OBJECT_UNLOCK (basesink);
4322 gst_object_unref (basesink);
4329 /* reset, as starting the thread failed */
4330 basesink->pad_mode = GST_ACTIVATE_NONE;
4332 GST_ERROR_OBJECT (basesink, "subclass failed to activate in pull mode");
4337 /* send an event to our sinkpad peer. */
4339 gst_base_sink_send_event (GstElement * element, GstEvent * event)
4342 GstBaseSink *basesink = GST_BASE_SINK (element);
4343 gboolean forward, result = TRUE;
4344 GstActivateMode mode;
4346 GST_OBJECT_LOCK (element);
4347 /* get the pad and the scheduling mode */
4348 pad = gst_object_ref (basesink->sinkpad);
4349 mode = basesink->pad_mode;
4350 GST_OBJECT_UNLOCK (element);
4352 /* only push UPSTREAM events upstream */
4353 forward = GST_EVENT_IS_UPSTREAM (event);
4355 GST_DEBUG_OBJECT (basesink, "handling event %p %" GST_PTR_FORMAT, event,
4358 switch (GST_EVENT_TYPE (event)) {
4359 case GST_EVENT_LATENCY:
4361 GstClockTime latency;
4363 gst_event_parse_latency (event, &latency);
4365 /* store the latency. We use this to adjust the running_time before syncing
4366 * it to the clock. */
4367 GST_OBJECT_LOCK (element);
4368 basesink->priv->latency = latency;
4369 if (!basesink->priv->have_latency)
4371 GST_OBJECT_UNLOCK (element);
4372 GST_DEBUG_OBJECT (basesink, "latency set to %" GST_TIME_FORMAT,
4373 GST_TIME_ARGS (latency));
4375 /* We forward this event so that all elements know about the global pipeline
4376 * latency. This is interesting for an element when it wants to figure out
4377 * when a particular piece of data will be rendered. */
4380 case GST_EVENT_SEEK:
4381 /* in pull mode we will execute the seek */
4382 if (mode == GST_ACTIVATE_PULL)
4383 result = gst_base_sink_perform_seek (basesink, pad, event);
4385 case GST_EVENT_STEP:
4386 result = gst_base_sink_perform_step (basesink, pad, event);
4394 result = gst_pad_push_event (pad, event);
4396 /* not forwarded, unref the event */
4397 gst_event_unref (event);
4400 gst_object_unref (pad);
4405 gst_base_sink_get_position (GstBaseSink * basesink, GstFormat format,
4406 gint64 * cur, gboolean * upstream)
4408 GstClock *clock = NULL;
4409 gboolean res = FALSE;
4410 GstFormat oformat, tformat;
4411 GstSegment *segment;
4412 GstClockTime now, latency;
4413 GstClockTimeDiff base_time;
4414 gint64 time, base, duration;
4417 gboolean last_seen, with_clock, in_paused;
4419 GST_OBJECT_LOCK (basesink);
4420 /* we can only get the segment when we are not NULL or READY */
4421 if (!basesink->have_newsegment)
4425 /* when not in PLAYING or when we're busy with a state change, we
4426 * cannot read from the clock so we report time based on the
4427 * last seen timestamp. */
4428 if (GST_STATE (basesink) != GST_STATE_PLAYING ||
4429 GST_STATE_PENDING (basesink) != GST_STATE_VOID_PENDING) {
4433 /* we don't use the clip segment in pull mode, when seeking we update the
4434 * main segment directly with the new segment values without it having to be
4435 * activated by the rendering after preroll */
4436 if (basesink->pad_mode == GST_ACTIVATE_PUSH)
4437 segment = basesink->clip_segment;
4439 segment = &basesink->segment;
4441 /* our intermediate time format */
4442 tformat = GST_FORMAT_TIME;
4443 /* get the format in the segment */
4444 oformat = segment->format;
4446 /* report with last seen position when EOS */
4447 last_seen = basesink->eos;
4449 /* assume we will use the clock for getting the current position */
4451 if (basesink->sync == FALSE)
4454 /* and we need a clock */
4455 if (G_UNLIKELY ((clock = GST_ELEMENT_CLOCK (basesink)) == NULL))
4458 gst_object_ref (clock);
4460 /* collect all data we need holding the lock */
4461 if (GST_CLOCK_TIME_IS_VALID (segment->time))
4462 time = segment->time;
4466 if (GST_CLOCK_TIME_IS_VALID (segment->stop))
4467 duration = segment->stop - segment->start;
4471 base = segment->base;
4472 rate = segment->rate * segment->applied_rate;
4473 latency = basesink->priv->latency;
4475 if (oformat == GST_FORMAT_TIME) {
4478 start = basesink->priv->current_sstart;
4479 stop = basesink->priv->current_sstop;
4482 /* in paused we use the last position as a lower bound */
4483 if (stop == -1 || segment->rate > 0.0)
4488 /* in playing, use last stop time as upper bound */
4489 if (start == -1 || segment->rate > 0.0)
4495 /* convert last stop to stream time */
4496 last = gst_segment_to_stream_time (segment, oformat, segment->position);
4500 /* in paused, use start_time */
4501 base_time = GST_ELEMENT_START_TIME (basesink);
4502 GST_DEBUG_OBJECT (basesink, "in paused, using start time %" GST_TIME_FORMAT,
4503 GST_TIME_ARGS (base_time));
4504 } else if (with_clock) {
4505 /* else use clock when needed */
4506 base_time = GST_ELEMENT_CAST (basesink)->base_time;
4507 GST_DEBUG_OBJECT (basesink, "using clock and base time %" GST_TIME_FORMAT,
4508 GST_TIME_ARGS (base_time));
4510 /* else, no sync or clock -> no base time */
4511 GST_DEBUG_OBJECT (basesink, "no sync or no clock");
4515 /* no base_time, we can't calculate running_time, use last seem timestamp to report
4517 if (base_time == -1)
4520 /* need to release the object lock before we can get the time,
4521 * a clock might take the LOCK of the provider, which could be
4522 * a basesink subclass. */
4523 GST_OBJECT_UNLOCK (basesink);
4526 /* in EOS or when no valid stream_time, report the value of last seen
4529 /* no timestamp, we need to ask upstream */
4530 GST_DEBUG_OBJECT (basesink, "no last seen timestamp, asking upstream");
4535 GST_DEBUG_OBJECT (basesink, "using last seen timestamp %" GST_TIME_FORMAT,
4536 GST_TIME_ARGS (last));
4539 if (oformat != tformat) {
4540 /* convert base, time and duration to time */
4541 if (!gst_pad_query_convert (basesink->sinkpad, oformat, base, &tformat,
4543 goto convert_failed;
4544 if (!gst_pad_query_convert (basesink->sinkpad, oformat, duration,
4545 &tformat, &duration))
4546 goto convert_failed;
4547 if (!gst_pad_query_convert (basesink->sinkpad, oformat, time, &tformat,
4549 goto convert_failed;
4550 if (!gst_pad_query_convert (basesink->sinkpad, oformat, last, &tformat,
4552 goto convert_failed;
4554 /* assume time format from now on */
4558 if (!in_paused && with_clock) {
4559 now = gst_clock_get_time (clock);
4565 /* subtract base time and base time from the clock time.
4566 * Make sure we don't go negative. This is the current time in
4567 * the segment which we need to scale with the combined
4568 * rate and applied rate. */
4570 base_time += latency;
4571 if (GST_CLOCK_DIFF (base_time, now) < 0)
4574 /* for negative rates we need to count back from the segment
4579 *cur = time + gst_guint64_to_gdouble (now - base_time) * rate;
4582 /* never report less than segment values in paused */
4584 *cur = MAX (last, *cur);
4586 /* never report more than last seen position in playing */
4588 *cur = MIN (last, *cur);
4591 GST_DEBUG_OBJECT (basesink,
4592 "now %" GST_TIME_FORMAT " - base_time %" GST_TIME_FORMAT " - base %"
4593 GST_TIME_FORMAT " + time %" GST_TIME_FORMAT " last %" GST_TIME_FORMAT,
4594 GST_TIME_ARGS (now), GST_TIME_ARGS (base_time), GST_TIME_ARGS (base),
4595 GST_TIME_ARGS (time), GST_TIME_ARGS (last));
4598 if (oformat != format) {
4599 /* convert to final format */
4600 if (!gst_pad_query_convert (basesink->sinkpad, oformat, *cur, &format, cur))
4601 goto convert_failed;
4607 GST_DEBUG_OBJECT (basesink, "res: %d, POSITION: %" GST_TIME_FORMAT,
4608 res, GST_TIME_ARGS (*cur));
4611 gst_object_unref (clock);
4618 /* in NULL or READY we always return FALSE and -1 */
4619 GST_DEBUG_OBJECT (basesink, "position in wrong state, return -1");
4622 GST_OBJECT_UNLOCK (basesink);
4627 GST_DEBUG_OBJECT (basesink, "convert failed, try upstream");
4635 gst_base_sink_get_duration (GstBaseSink * basesink, GstFormat format,
4636 gint64 * dur, gboolean * upstream)
4638 gboolean res = FALSE;
4640 if (basesink->pad_mode == GST_ACTIVATE_PULL) {
4641 GstFormat uformat = GST_FORMAT_BYTES;
4644 /* get the duration in bytes, in pull mode that's all we are sure to
4645 * know. We have to explicitly get this value from upstream instead of
4646 * using our cached value because it might change. Duration caching
4647 * should be done at a higher level. */
4648 res = gst_pad_query_peer_duration (basesink->sinkpad, &uformat, &uduration);
4650 basesink->segment.duration = uduration;
4651 if (format != uformat) {
4652 /* convert to the requested format */
4653 res = gst_pad_query_convert (basesink->sinkpad, uformat, uduration,
4667 static const GstQueryType *
4668 gst_base_sink_get_query_types (GstElement * element)
4670 static const GstQueryType query_types[] = {
4682 gst_base_sink_query (GstElement * element, GstQuery * query)
4684 gboolean res = FALSE;
4686 GstBaseSink *basesink = GST_BASE_SINK (element);
4688 switch (GST_QUERY_TYPE (query)) {
4689 case GST_QUERY_POSITION:
4693 gboolean upstream = FALSE;
4695 gst_query_parse_position (query, &format, NULL);
4697 GST_DEBUG_OBJECT (basesink, "position query in format %s",
4698 gst_format_get_name (format));
4700 /* first try to get the position based on the clock */
4702 gst_base_sink_get_position (basesink, format, &cur, &upstream))) {
4703 gst_query_set_position (query, format, cur);
4704 } else if (upstream) {
4705 /* fallback to peer query */
4706 res = gst_pad_peer_query (basesink->sinkpad, query);
4709 /* we can handle a few things if upstream failed */
4710 if (format == GST_FORMAT_PERCENT) {
4712 GstFormat uformat = GST_FORMAT_TIME;
4714 res = gst_base_sink_get_position (basesink, GST_FORMAT_TIME, &cur,
4716 if (!res && upstream) {
4717 res = gst_pad_query_peer_position (basesink->sinkpad, &uformat,
4721 res = gst_base_sink_get_duration (basesink, GST_FORMAT_TIME, &dur,
4723 if (!res && upstream) {
4724 res = gst_pad_query_peer_duration (basesink->sinkpad, &uformat,
4731 pos = gst_util_uint64_scale (100 * GST_FORMAT_PERCENT_SCALE, cur,
4733 gst_query_set_position (query, GST_FORMAT_PERCENT, pos);
4739 case GST_QUERY_DURATION:
4743 gboolean upstream = FALSE;
4745 gst_query_parse_duration (query, &format, NULL);
4747 GST_DEBUG_OBJECT (basesink, "duration query in format %s",
4748 gst_format_get_name (format));
4751 gst_base_sink_get_duration (basesink, format, &dur, &upstream))) {
4752 gst_query_set_duration (query, format, dur);
4753 } else if (upstream) {
4754 /* fallback to peer query */
4755 res = gst_pad_peer_query (basesink->sinkpad, query);
4758 /* we can handle a few things if upstream failed */
4759 if (format == GST_FORMAT_PERCENT) {
4760 gst_query_set_duration (query, GST_FORMAT_PERCENT,
4761 GST_FORMAT_PERCENT_MAX);
4767 case GST_QUERY_LATENCY:
4769 gboolean live, us_live;
4770 GstClockTime min, max;
4772 if ((res = gst_base_sink_query_latency (basesink, &live, &us_live, &min,
4774 gst_query_set_latency (query, live, min, max);
4778 case GST_QUERY_JITTER:
4780 case GST_QUERY_RATE:
4781 /* gst_query_set_rate (query, basesink->segment_rate); */
4784 case GST_QUERY_SEGMENT:
4786 if (basesink->pad_mode == GST_ACTIVATE_PULL) {
4787 gst_query_set_segment (query, basesink->segment.rate,
4788 GST_FORMAT_TIME, basesink->segment.start, basesink->segment.stop);
4791 res = gst_pad_peer_query (basesink->sinkpad, query);
4795 case GST_QUERY_SEEKING:
4796 case GST_QUERY_CONVERT:
4797 case GST_QUERY_FORMATS:
4799 res = gst_pad_peer_query (basesink->sinkpad, query);
4802 GST_DEBUG_OBJECT (basesink, "query %s returns %d",
4803 GST_QUERY_TYPE_NAME (query), res);
4807 static GstStateChangeReturn
4808 gst_base_sink_change_state (GstElement * element, GstStateChange transition)
4810 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
4811 GstBaseSink *basesink = GST_BASE_SINK (element);
4812 GstBaseSinkClass *bclass;
4813 GstBaseSinkPrivate *priv;
4815 priv = basesink->priv;
4817 bclass = GST_BASE_SINK_GET_CLASS (basesink);
4819 switch (transition) {
4820 case GST_STATE_CHANGE_NULL_TO_READY:
4822 if (!bclass->start (basesink))
4825 case GST_STATE_CHANGE_READY_TO_PAUSED:
4826 /* need to complete preroll before this state change completes, there
4827 * is no data flow in READY so we can safely assume we need to preroll. */
4828 GST_BASE_SINK_PREROLL_LOCK (basesink);
4829 GST_DEBUG_OBJECT (basesink, "READY to PAUSED");
4830 basesink->have_newsegment = FALSE;
4831 gst_segment_init (&basesink->segment, GST_FORMAT_UNDEFINED);
4832 gst_segment_init (basesink->clip_segment, GST_FORMAT_UNDEFINED);
4833 basesink->offset = 0;
4834 basesink->have_preroll = FALSE;
4835 priv->step_unlock = FALSE;
4836 basesink->need_preroll = TRUE;
4837 basesink->playing_async = TRUE;
4838 priv->current_sstart = GST_CLOCK_TIME_NONE;
4839 priv->current_sstop = GST_CLOCK_TIME_NONE;
4840 priv->eos_rtime = GST_CLOCK_TIME_NONE;
4842 basesink->eos = FALSE;
4843 priv->received_eos = FALSE;
4844 gst_base_sink_reset_qos (basesink);
4845 priv->commited = FALSE;
4846 priv->call_preroll = TRUE;
4847 priv->current_step.valid = FALSE;
4848 priv->pending_step.valid = FALSE;
4849 if (priv->async_enabled) {
4850 GST_DEBUG_OBJECT (basesink, "doing async state change");
4851 /* when async enabled, post async-start message and return ASYNC from
4852 * the state change function */
4853 ret = GST_STATE_CHANGE_ASYNC;
4854 gst_element_post_message (GST_ELEMENT_CAST (basesink),
4855 gst_message_new_async_start (GST_OBJECT_CAST (basesink), FALSE));
4857 priv->have_latency = TRUE;
4859 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
4861 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
4862 GST_BASE_SINK_PREROLL_LOCK (basesink);
4863 if (!gst_base_sink_needs_preroll (basesink)) {
4864 GST_DEBUG_OBJECT (basesink, "PAUSED to PLAYING, don't need preroll");
4865 /* no preroll needed anymore now. */
4866 basesink->playing_async = FALSE;
4867 basesink->need_preroll = FALSE;
4868 if (basesink->eos) {
4869 GstMessage *message;
4871 /* need to post EOS message here */
4872 GST_DEBUG_OBJECT (basesink, "Now posting EOS");
4873 message = gst_message_new_eos (GST_OBJECT_CAST (basesink));
4874 gst_message_set_seqnum (message, basesink->priv->seqnum);
4875 gst_element_post_message (GST_ELEMENT_CAST (basesink), message);
4877 GST_DEBUG_OBJECT (basesink, "signal preroll");
4878 GST_BASE_SINK_PREROLL_SIGNAL (basesink);
4881 GST_DEBUG_OBJECT (basesink, "PAUSED to PLAYING, we are not prerolled");
4882 basesink->need_preroll = TRUE;
4883 basesink->playing_async = TRUE;
4884 priv->call_preroll = TRUE;
4885 priv->commited = FALSE;
4886 if (priv->async_enabled) {
4887 GST_DEBUG_OBJECT (basesink, "doing async state change");
4888 ret = GST_STATE_CHANGE_ASYNC;
4889 gst_element_post_message (GST_ELEMENT_CAST (basesink),
4890 gst_message_new_async_start (GST_OBJECT_CAST (basesink), FALSE));
4893 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
4900 GstStateChangeReturn bret;
4902 bret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
4903 if (G_UNLIKELY (bret == GST_STATE_CHANGE_FAILURE))
4904 goto activate_failed;
4907 switch (transition) {
4908 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
4909 GST_DEBUG_OBJECT (basesink, "PLAYING to PAUSED");
4910 /* FIXME, make sure we cannot enter _render first */
4912 /* we need to call ::unlock before locking PREROLL_LOCK
4913 * since we lock it before going into ::render */
4915 bclass->unlock (basesink);
4917 GST_BASE_SINK_PREROLL_LOCK (basesink);
4918 GST_DEBUG_OBJECT (basesink, "got preroll lock");
4919 /* now that we have the PREROLL lock, clear our unlock request */
4920 if (bclass->unlock_stop)
4921 bclass->unlock_stop (basesink);
4923 /* we need preroll again and we set the flag before unlocking the clockid
4924 * because if the clockid is unlocked before a current buffer expired, we
4925 * can use that buffer to preroll with */
4926 basesink->need_preroll = TRUE;
4928 if (basesink->clock_id) {
4929 GST_DEBUG_OBJECT (basesink, "unschedule clock");
4930 gst_clock_id_unschedule (basesink->clock_id);
4933 /* if we don't have a preroll buffer we need to wait for a preroll and
4935 if (!gst_base_sink_needs_preroll (basesink)) {
4936 GST_DEBUG_OBJECT (basesink, "PLAYING to PAUSED, we are prerolled");
4937 basesink->playing_async = FALSE;
4939 if (GST_STATE_TARGET (GST_ELEMENT (basesink)) <= GST_STATE_READY) {
4940 GST_DEBUG_OBJECT (basesink, "element is <= READY");
4941 ret = GST_STATE_CHANGE_SUCCESS;
4943 GST_DEBUG_OBJECT (basesink,
4944 "PLAYING to PAUSED, we are not prerolled");
4945 basesink->playing_async = TRUE;
4946 priv->commited = FALSE;
4947 priv->call_preroll = TRUE;
4948 if (priv->async_enabled) {
4949 GST_DEBUG_OBJECT (basesink, "doing async state change");
4950 ret = GST_STATE_CHANGE_ASYNC;
4951 gst_element_post_message (GST_ELEMENT_CAST (basesink),
4952 gst_message_new_async_start (GST_OBJECT_CAST (basesink),
4957 GST_DEBUG_OBJECT (basesink, "rendered: %" G_GUINT64_FORMAT
4958 ", dropped: %" G_GUINT64_FORMAT, priv->rendered, priv->dropped);
4960 gst_base_sink_reset_qos (basesink);
4961 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
4963 case GST_STATE_CHANGE_PAUSED_TO_READY:
4964 GST_BASE_SINK_PREROLL_LOCK (basesink);
4965 /* start by reseting our position state with the object lock so that the
4966 * position query gets the right idea. We do this before we post the
4967 * messages so that the message handlers pick this up. */
4968 GST_OBJECT_LOCK (basesink);
4969 basesink->have_newsegment = FALSE;
4970 priv->current_sstart = GST_CLOCK_TIME_NONE;
4971 priv->current_sstop = GST_CLOCK_TIME_NONE;
4972 priv->have_latency = FALSE;
4973 if (priv->cached_clock_id) {
4974 gst_clock_id_unref (priv->cached_clock_id);
4975 priv->cached_clock_id = NULL;
4977 GST_OBJECT_UNLOCK (basesink);
4979 gst_base_sink_set_last_buffer (basesink, NULL);
4980 priv->call_preroll = FALSE;
4982 if (!priv->commited) {
4983 if (priv->async_enabled) {
4984 GST_DEBUG_OBJECT (basesink, "PAUSED to READY, posting async-done");
4986 gst_element_post_message (GST_ELEMENT_CAST (basesink),
4987 gst_message_new_state_changed (GST_OBJECT_CAST (basesink),
4988 GST_STATE_PLAYING, GST_STATE_PAUSED, GST_STATE_READY));
4990 gst_element_post_message (GST_ELEMENT_CAST (basesink),
4991 gst_message_new_async_done (GST_OBJECT_CAST (basesink)));
4993 priv->commited = TRUE;
4995 GST_DEBUG_OBJECT (basesink, "PAUSED to READY, don't need_preroll");
4997 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
4999 case GST_STATE_CHANGE_READY_TO_NULL:
5001 if (!bclass->stop (basesink)) {
5002 GST_WARNING_OBJECT (basesink, "failed to stop");
5005 gst_base_sink_set_last_buffer (basesink, NULL);
5006 priv->call_preroll = FALSE;
5017 GST_DEBUG_OBJECT (basesink, "failed to start");
5018 return GST_STATE_CHANGE_FAILURE;
5022 GST_DEBUG_OBJECT (basesink,
5023 "element failed to change states -- activation problem?");
5024 return GST_STATE_CHANGE_FAILURE;