Debian Codegraph @

libs/gst/base/gstbasesink.c

raw
1 /* GStreamer
2 * Copyright (C) 2005-2007 Wim Taymans <[email protected]>
3 *
4 * gstbasesink.c: Base class for sink elements
5 *
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.
10 *
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.
15 *
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
20 */
21
22 /**
23 * SECTION:gstbasesink
24 * @title: GstBaseSink
25 * @short_description: Base class for sink elements
26 * @see_also: #GstBaseTransform, #GstBaseSrc
27 *
28 * #GstBaseSink is the base class for sink elements in GStreamer, such as
29 * xvimagesink or filesink. It is a layer on top of #GstElement that provides a
30 * simplified interface to plugin writers. #GstBaseSink handles many details
31 * for you, for example: preroll, clock synchronization, state changes,
32 * activation in push or pull mode, and queries.
33 *
34 * In most cases, when writing sink elements, there is no need to implement
35 * class methods from #GstElement or to set functions on pads, because the
36 * #GstBaseSink infrastructure should be sufficient.
37 *
38 * #GstBaseSink provides support for exactly one sink pad, which should be
39 * named "sink". A sink implementation (subclass of #GstBaseSink) should
40 * install a pad template in its class_init function, like so:
41 * |[<!-- language="C" -->
42 * static void
43 * my_element_class_init (GstMyElementClass *klass)
44 * {
45 * GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
46 *
47 * // sinktemplate should be a #GstStaticPadTemplate with direction
48 * // %GST_PAD_SINK and name "sink"
49 * gst_element_class_add_static_pad_template (gstelement_class, &sinktemplate);
50 *
51 * gst_element_class_set_static_metadata (gstelement_class,
52 * "Sink name",
53 * "Sink",
54 * "My Sink element",
55 * "The author <[email protected]>");
56 * }
57 * ]|
58 *
59 * #GstBaseSink will handle the prerolling correctly. This means that it will
60 * return %GST_STATE_CHANGE_ASYNC from a state change to PAUSED until the first
61 * buffer arrives in this element. The base class will call the
62 * #GstBaseSinkClass::preroll vmethod with this preroll buffer and will then
63 * commit the state change to the next asynchronously pending state.
64 *
65 * When the element is set to PLAYING, #GstBaseSink will synchronise on the
66 * clock using the times returned from #GstBaseSinkClass::get_times. If this
67 * function returns %GST_CLOCK_TIME_NONE for the start time, no synchronisation
68 * will be done. Synchronisation can be disabled entirely by setting the object
69 * #GstBaseSink:sync property to %FALSE.
70 *
71 * After synchronisation the virtual method #GstBaseSinkClass::render will be
72 * called. Subclasses should minimally implement this method.
73 *
74 * Subclasses that synchronise on the clock in the #GstBaseSinkClass::render
75 * method are supported as well. These classes typically receive a buffer in
76 * the render method and can then potentially block on the clock while
77 * rendering. A typical example is an audiosink.
78 * These subclasses can use gst_base_sink_wait_preroll() to perform the
79 * blocking wait.
80 *
81 * Upon receiving the EOS event in the PLAYING state, #GstBaseSink will wait
82 * for the clock to reach the time indicated by the stop time of the last
83 * #GstBaseSinkClass::get_times call before posting an EOS message. When the
84 * element receives EOS in PAUSED, preroll completes, the event is queued and an
85 * EOS message is posted when going to PLAYING.
86 *
87 * #GstBaseSink will internally use the %GST_EVENT_SEGMENT events to schedule
88 * synchronisation and clipping of buffers. Buffers that fall completely outside
89 * of the current segment are dropped. Buffers that fall partially in the
90 * segment are rendered (and prerolled). Subclasses should do any subbuffer
91 * clipping themselves when needed.
92 *
93 * #GstBaseSink will by default report the current playback position in
94 * %GST_FORMAT_TIME based on the current clock time and segment information.
95 * If no clock has been set on the element, the query will be forwarded
96 * upstream.
97 *
98 * The #GstBaseSinkClass::set_caps function will be called when the subclass
99 * should configure itself to process a specific media type.
100 *
101 * The #GstBaseSinkClass::start and #GstBaseSinkClass::stop virtual methods
102 * will be called when resources should be allocated. Any
103 * #GstBaseSinkClass::preroll, #GstBaseSinkClass::render and
104 * #GstBaseSinkClass::set_caps function will be called between the
105 * #GstBaseSinkClass::start and #GstBaseSinkClass::stop calls.
106 *
107 * The #GstBaseSinkClass::event virtual method will be called when an event is
108 * received by #GstBaseSink. Normally this method should only be overridden by
109 * very specific elements (such as file sinks) which need to handle the
110 * newsegment event specially.
111 *
112 * The #GstBaseSinkClass::unlock method is called when the elements should
113 * unblock any blocking operations they perform in the
114 * #GstBaseSinkClass::render method. This is mostly useful when the
115 * #GstBaseSinkClass::render method performs a blocking write on a file
116 * descriptor, for example.
117 *
118 * The #GstBaseSink:max-lateness property affects how the sink deals with
119 * buffers that arrive too late in the sink. A buffer arrives too late in the
120 * sink when the presentation time (as a combination of the last segment, buffer
121 * timestamp and element base_time) plus the duration is before the current
122 * time of the clock.
123 * If the frame is later than max-lateness, the sink will drop the buffer
124 * without calling the render method.
125 * This feature is disabled if sync is disabled, the
126 * #GstBaseSinkClass::get_times method does not return a valid start time or
127 * max-lateness is set to -1 (the default).
128 * Subclasses can use gst_base_sink_set_max_lateness() to configure the
129 * max-lateness value.
130 *
131 * The #GstBaseSink:qos property will enable the quality-of-service features of
132 * the basesink which gather statistics about the real-time performance of the
133 * clock synchronisation. For each buffer received in the sink, statistics are
134 * gathered and a QOS event is sent upstream with these numbers. This
135 * information can then be used by upstream elements to reduce their processing
136 * rate, for example.
137 *
138 * The #GstBaseSink:async property can be used to instruct the sink to never
139 * perform an ASYNC state change. This feature is mostly usable when dealing
140 * with non-synchronized streams or sparse streams.
141 */
142
143 #ifdef HAVE_CONFIG_H
144 # include "config.h"
145 #endif
146
147 #include <gst/gst_private.h>
148
149 #include "gstbasesink.h"
150 #include <glib/gi18n-lib.h>
151
152 GST_DEBUG_CATEGORY_STATIC (gst_base_sink_debug);
153 #define GST_CAT_DEFAULT gst_base_sink_debug
154
155 #define GST_FLOW_STEP GST_FLOW_CUSTOM_ERROR
156
157 typedef struct
158 {
159 gboolean valid; /* if this info is valid */
160 guint32 seqnum; /* the seqnum of the STEP event */
161 GstFormat format; /* the format of the amount */
162 guint64 amount; /* the total amount of data to skip */
163 guint64 position; /* the position in the stepped data */
164 guint64 duration; /* the duration in time of the skipped data */
165 guint64 start; /* running_time of the start */
166 gdouble rate; /* rate of skipping */
167 gdouble start_rate; /* rate before skipping */
168 guint64 start_start; /* start position skipping */
169 guint64 start_stop; /* stop position skipping */
170 gboolean flush; /* if this was a flushing step */
171 gboolean intermediate; /* if this is an intermediate step */
172 gboolean need_preroll; /* if we need preroll after this step */
173 } GstStepInfo;
174
175 struct _GstBaseSinkPrivate
176 {
177 gint qos_enabled; /* ATOMIC */
178 gboolean async_enabled;
179 GstClockTimeDiff ts_offset;
180 GstClockTime render_delay;
181 GstClockTime processing_deadline;
182
183 /* start, stop of current buffer, stream time, used to report position */
184 GstClockTime current_sstart;
185 GstClockTime current_sstop;
186
187 /* start, stop and jitter of current buffer, running time */
188 GstClockTime current_rstart;
189 GstClockTime current_rstop;
190 GstClockTimeDiff current_jitter;
191 /* the running time of the previous buffer */
192 GstClockTime prev_rstart;
193
194 /* EOS sync time in running time */
195 GstClockTime eos_rtime;
196
197 /* last buffer that arrived in time, running time */
198 GstClockTime last_render_time;
199 /* when the last buffer left the sink, running time */
200 GstClockTime last_left;
201
202 /* running averages go here these are done on running time */
203 GstClockTime avg_pt, avg_in_diff;
204 gdouble avg_rate; /* average with infinite window */
205
206 /* number of rendered and dropped frames */
207 guint64 rendered;
208 guint64 dropped;
209
210 /* latency stuff */
211 GstClockTime latency;
212
213 /* if we already committed the state */
214 gboolean committed;
215 /* state change to playing ongoing */
216 gboolean to_playing;
217
218 /* when we received EOS */
219 gboolean received_eos;
220
221 /* when we are prerolled and able to report latency */
222 gboolean have_latency;
223
224 /* the last buffer we prerolled or rendered. Useful for making snapshots */
225 gint enable_last_sample; /* atomic */
226 GstBuffer *last_buffer;
227 GstCaps *last_caps;
228 GstBufferList *last_buffer_list;
229
230 /* negotiated caps */
231 GstCaps *caps;
232
233 /* blocksize for pulling */
234 guint blocksize;
235
236 gboolean discont;
237
238 /* seqnum of the stream */
239 guint32 seqnum;
240
241 gboolean call_preroll;
242 gboolean step_unlock;
243
244 /* we have a pending and a current step operation */
245 GstStepInfo current_step;
246 GstStepInfo pending_step;
247
248 /* instant rate change state */
249 /* seqnum of the last instant-rate-sync-time event
250 * received. %GST_SEQNUM_INVALID if there isn't one */
251 guint32 instant_rate_sync_seqnum;
252 /* Active instant-rate multipler. 0.0 if nothing pending */
253 gdouble instant_rate_multiplier;
254 /* seqnum of the last instant-rate event.
255 * %GST_SEQNUM_INVALID if there isn't one */
256 guint32 last_instant_rate_seqnum;
257 GstSegment upstream_segment;
258 /* Running time at the start of the last segment event
259 * or instant-rate switch in *our* segment, not upstream */
260 GstClockTime last_anchor_running_time;
261 /* Difference between upstream running time and our own running time
262 * at the last segment event or instant-rate switch:
263 * upstream + offset = ours */
264 GstClockTimeDiff instant_rate_offset;
265
266 /* Cached GstClockID */
267 GstClockID cached_clock_id;
268
269 /* for throttling and QoS */
270 GstClockTime earliest_in_time;
271 GstClockTime throttle_time;
272
273 /* for rate control */
274 guint64 max_bitrate;
275 GstClockTime rc_time;
276 GstClockTime rc_next;
277 gsize rc_accumulated;
278
279 gboolean drop_out_of_segment;
280 };
281
282 #define DO_RUNNING_AVG(avg,val,size) (((val) + ((size)-1) * (avg)) / (size))
283
284 /* generic running average, this has a neutral window size */
285 #define UPDATE_RUNNING_AVG(avg,val) DO_RUNNING_AVG(avg,val,8)
286
287 /* the windows for these running averages are experimentally obtained.
288 * positive values get averaged more while negative values use a small
289 * window so we can react faster to badness. */
290 #define UPDATE_RUNNING_AVG_P(avg,val) DO_RUNNING_AVG(avg,val,16)
291 #define UPDATE_RUNNING_AVG_N(avg,val) DO_RUNNING_AVG(avg,val,4)
292
293 /* BaseSink properties */
294
295 #define DEFAULT_CAN_ACTIVATE_PULL FALSE /* fixme: enable me */
296 #define DEFAULT_CAN_ACTIVATE_PUSH TRUE
297
298 #define DEFAULT_SYNC TRUE
299 #define DEFAULT_MAX_LATENESS -1
300 #define DEFAULT_QOS FALSE
301 #define DEFAULT_ASYNC TRUE
302 #define DEFAULT_TS_OFFSET 0
303 #define DEFAULT_BLOCKSIZE 4096
304 #define DEFAULT_RENDER_DELAY 0
305 #define DEFAULT_ENABLE_LAST_SAMPLE TRUE
306 #define DEFAULT_THROTTLE_TIME 0
307 #define DEFAULT_MAX_BITRATE 0
308 #define DEFAULT_DROP_OUT_OF_SEGMENT TRUE
309 #define DEFAULT_PROCESSING_DEADLINE (20 * GST_MSECOND)
310
311 enum
312 {
313 PROP_0,
314 PROP_SYNC,
315 PROP_MAX_LATENESS,
316 PROP_QOS,
317 PROP_ASYNC,
318 PROP_TS_OFFSET,
319 PROP_ENABLE_LAST_SAMPLE,
320 PROP_LAST_SAMPLE,
321 PROP_BLOCKSIZE,
322 PROP_RENDER_DELAY,
323 PROP_THROTTLE_TIME,
324 PROP_MAX_BITRATE,
325 PROP_PROCESSING_DEADLINE,
326 PROP_STATS,
327 PROP_LAST
328 };
329
330 static GstElementClass *parent_class = NULL;
331 static gint private_offset = 0;
332
333 static void gst_base_sink_class_init (GstBaseSinkClass * klass);
334 static void gst_base_sink_init (GstBaseSink * trans, gpointer g_class);
335 static void gst_base_sink_finalize (GObject * object);
336
337 GType
338 gst_base_sink_get_type (void)
339 {
340 static gsize base_sink_type = 0;
341
342 if (g_once_init_enter (&base_sink_type)) {
343 GType _type;
344 static const GTypeInfo base_sink_info = {
345 sizeof (GstBaseSinkClass),
346 NULL,
347 NULL,
348 (GClassInitFunc) gst_base_sink_class_init,
349 NULL,
350 NULL,
351 sizeof (GstBaseSink),
352 0,
353 (GInstanceInitFunc) gst_base_sink_init,
354 };
355
356 _type = g_type_register_static (GST_TYPE_ELEMENT,
357 "GstBaseSink", &base_sink_info, G_TYPE_FLAG_ABSTRACT);
358
359 private_offset =
360 g_type_add_instance_private (_type, sizeof (GstBaseSinkPrivate));
361
362 g_once_init_leave (&base_sink_type, _type);
363 }
364 return base_sink_type;
365 }
366
367 static inline GstBaseSinkPrivate *
368 gst_base_sink_get_instance_private (GstBaseSink * self)
369 {
370 return (G_STRUCT_MEMBER_P (self, private_offset));
371 }
372
373 static void gst_base_sink_set_property (GObject * object, guint prop_id,
374 const GValue * value, GParamSpec * pspec);
375 static void gst_base_sink_get_property (GObject * object, guint prop_id,
376 GValue * value, GParamSpec * pspec);
377
378 static gboolean gst_base_sink_send_event (GstElement * element,
379 GstEvent * event);
380 static gboolean default_element_query (GstElement * element, GstQuery * query);
381
382 static GstCaps *gst_base_sink_default_get_caps (GstBaseSink * sink,
383 GstCaps * caps);
384 static gboolean gst_base_sink_default_set_caps (GstBaseSink * sink,
385 GstCaps * caps);
386 static void gst_base_sink_default_get_times (GstBaseSink * basesink,
387 GstBuffer * buffer, GstClockTime * start, GstClockTime * end);
388 static gboolean gst_base_sink_set_flushing (GstBaseSink * basesink,
389 GstPad * pad, gboolean flushing);
390 static gboolean gst_base_sink_default_activate_pull (GstBaseSink * basesink,
391 gboolean active);
392 static gboolean gst_base_sink_default_do_seek (GstBaseSink * sink,
393 GstSegment * segment);
394 static gboolean gst_base_sink_default_prepare_seek_segment (GstBaseSink * sink,
395 GstEvent * event, GstSegment * segment);
396
397 static GstStateChangeReturn gst_base_sink_change_state (GstElement * element,
398 GstStateChange transition);
399
400 static gboolean gst_base_sink_sink_query (GstPad * pad, GstObject * parent,
401 GstQuery * query);
402 static GstFlowReturn gst_base_sink_chain (GstPad * pad, GstObject * parent,
403 GstBuffer * buffer);
404 static GstFlowReturn gst_base_sink_chain_list (GstPad * pad, GstObject * parent,
405 GstBufferList * list);
406
407 static void gst_base_sink_loop (GstPad * pad);
408 static gboolean gst_base_sink_pad_activate (GstPad * pad, GstObject * parent);
409 static gboolean gst_base_sink_pad_activate_mode (GstPad * pad,
410 GstObject * parent, GstPadMode mode, gboolean active);
411 static gboolean gst_base_sink_default_event (GstBaseSink * basesink,
412 GstEvent * event);
413 static GstFlowReturn gst_base_sink_default_wait_event (GstBaseSink * basesink,
414 GstEvent * event);
415 static gboolean gst_base_sink_event (GstPad * pad, GstObject * parent,
416 GstEvent * event);
417
418 static gboolean gst_base_sink_default_query (GstBaseSink * sink,
419 GstQuery * query);
420
421 static gboolean gst_base_sink_negotiate_pull (GstBaseSink * basesink);
422 static GstCaps *gst_base_sink_default_fixate (GstBaseSink * bsink,
423 GstCaps * caps);
424 static GstCaps *gst_base_sink_fixate (GstBaseSink * bsink, GstCaps * caps);
425
426 /* check if an object was too late */
427 static gboolean gst_base_sink_is_too_late (GstBaseSink * basesink,
428 GstMiniObject * obj, GstClockTime rstart, GstClockTime rstop,
429 GstClockReturn status, GstClockTimeDiff jitter, gboolean render);
430
431 static void
432 gst_base_sink_class_init (GstBaseSinkClass * klass)
433 {
434 GObjectClass *gobject_class;
435 GstElementClass *gstelement_class;
436
437 gobject_class = G_OBJECT_CLASS (klass);
438 gstelement_class = GST_ELEMENT_CLASS (klass);
439
440 if (private_offset != 0)
441 g_type_class_adjust_private_offset (klass, &private_offset);
442
443 GST_DEBUG_CATEGORY_INIT (gst_base_sink_debug, "basesink", 0,
444 "basesink element");
445
446 parent_class = g_type_class_peek_parent (klass);
447
448 gobject_class->finalize = gst_base_sink_finalize;
449 gobject_class->set_property = gst_base_sink_set_property;
450 gobject_class->get_property = gst_base_sink_get_property;
451
452 g_object_class_install_property (gobject_class, PROP_SYNC,
453 g_param_spec_boolean ("sync", "Sync", "Sync on the clock", DEFAULT_SYNC,
454 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
455
456 g_object_class_install_property (gobject_class, PROP_MAX_LATENESS,
457 g_param_spec_int64 ("max-lateness", "Max Lateness",
458 "Maximum number of nanoseconds that a buffer can be late before it "
459 "is dropped (-1 unlimited)", -1, G_MAXINT64, DEFAULT_MAX_LATENESS,
460 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
461
462 g_object_class_install_property (gobject_class, PROP_QOS,
463 g_param_spec_boolean ("qos", "Qos",
464 "Generate Quality-of-Service events upstream", DEFAULT_QOS,
465 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
466 /**
467 * GstBaseSink:async:
468 *
469 * If set to %TRUE, the basesink will perform asynchronous state changes.
470 * When set to %FALSE, the sink will not signal the parent when it prerolls.
471 * Use this option when dealing with sparse streams or when synchronisation is
472 * not required.
473 */
474 g_object_class_install_property (gobject_class, PROP_ASYNC,
475 g_param_spec_boolean ("async", "Async",
476 "Go asynchronously to PAUSED", DEFAULT_ASYNC,
477 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
478 /**
479 * GstBaseSink:ts-offset:
480 *
481 * Controls the final synchronisation, a negative value will render the buffer
482 * earlier while a positive value delays playback. This property can be
483 * used to fix synchronisation in bad files.
484 */
485 g_object_class_install_property (gobject_class, PROP_TS_OFFSET,
486 g_param_spec_int64 ("ts-offset", "TS Offset",
487 "Timestamp offset in nanoseconds", G_MININT64, G_MAXINT64,
488 DEFAULT_TS_OFFSET, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
489
490 /**
491 * GstBaseSink:enable-last-sample:
492 *
493 * Enable the last-sample property. If %FALSE, basesink doesn't keep a
494 * reference to the last buffer arrived and the last-sample property is always
495 * set to %NULL. This can be useful if you need buffers to be released as soon
496 * as possible, eg. if you're using a buffer pool.
497 */
498 g_object_class_install_property (gobject_class, PROP_ENABLE_LAST_SAMPLE,
499 g_param_spec_boolean ("enable-last-sample", "Enable Last Buffer",
500 "Enable the last-sample property", DEFAULT_ENABLE_LAST_SAMPLE,
501 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
502
503 /**
504 * GstBaseSink:last-sample:
505 *
506 * The last buffer that arrived in the sink and was used for preroll or for
507 * rendering. This property can be used to generate thumbnails. This property
508 * can be %NULL when the sink has not yet received a buffer.
509 */
510 g_object_class_install_property (gobject_class, PROP_LAST_SAMPLE,
511 g_param_spec_boxed ("last-sample", "Last Sample",
512 "The last sample received in the sink", GST_TYPE_SAMPLE,
513 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
514 /**
515 * GstBaseSink:blocksize:
516 *
517 * The amount of bytes to pull when operating in pull mode.
518 */
519 /* FIXME 2.0: blocksize property should be int, otherwise min>max.. */
520 g_object_class_install_property (gobject_class, PROP_BLOCKSIZE,
521 g_param_spec_uint ("blocksize", "Block size",
522 "Size in bytes to pull per buffer (0 = default)", 0, G_MAXUINT,
523 DEFAULT_BLOCKSIZE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
524 /**
525 * GstBaseSink:render-delay:
526 *
527 * The additional delay between synchronisation and actual rendering of the
528 * media. This property will add additional latency to the device in order to
529 * make other sinks compensate for the delay.
530 */
531 g_object_class_install_property (gobject_class, PROP_RENDER_DELAY,
532 g_param_spec_uint64 ("render-delay", "Render Delay",
533 "Additional render delay of the sink in nanoseconds", 0, G_MAXUINT64,
534 DEFAULT_RENDER_DELAY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
535 /**
536 * GstBaseSink:throttle-time:
537 *
538 * The time to insert between buffers. This property can be used to control
539 * the maximum amount of buffers per second to render. Setting this property
540 * to a value bigger than 0 will make the sink create THROTTLE QoS events.
541 */
542 g_object_class_install_property (gobject_class, PROP_THROTTLE_TIME,
543 g_param_spec_uint64 ("throttle-time", "Throttle time",
544 "The time to keep between rendered buffers (0 = disabled)", 0,
545 G_MAXUINT64, DEFAULT_THROTTLE_TIME,
546 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
547 /**
548 * GstBaseSink:max-bitrate:
549 *
550 * Control the maximum amount of bits that will be rendered per second.
551 * Setting this property to a value bigger than 0 will make the sink delay
552 * rendering of the buffers when it would exceed to max-bitrate.
553 *
554 * Since: 1.2
555 */
556 g_object_class_install_property (gobject_class, PROP_MAX_BITRATE,
557 g_param_spec_uint64 ("max-bitrate", "Max Bitrate",
558 "The maximum bits per second to render (0 = disabled)", 0,
559 G_MAXUINT64, DEFAULT_MAX_BITRATE,
560 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
561 /**
562 * GstBaseSink:processing-deadline:
563 *
564 * Maximum amount of time (in nanoseconds) that the pipeline can take
565 * for processing the buffer. This is added to the latency of live
566 * pipelines.
567 *
568 * Since: 1.16
569 */
570 g_object_class_install_property (gobject_class, PROP_PROCESSING_DEADLINE,
571 g_param_spec_uint64 ("processing-deadline", "Processing deadline",
572 "Maximum processing time for a buffer in nanoseconds", 0,
573 G_MAXUINT64, DEFAULT_PROCESSING_DEADLINE,
574 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
575
576
577 /**
578 * GstBaseSink:stats:
579 *
580 * Various #GstBaseSink statistics. This property returns a #GstStructure
581 * with name `application/x-gst-base-sink-stats` with the following fields:
582 *
583 * - "average-rate" G_TYPE_DOUBLE average frame rate
584 * - "dropped" G_TYPE_UINT64 Number of dropped frames
585 * - "rendered" G_TYPE_UINT64 Number of rendered frames
586 *
587 * Since: 1.18
588 */
589 g_object_class_install_property (gobject_class, PROP_STATS,
590 g_param_spec_boxed ("stats", "Statistics",
591 "Sink Statistics", GST_TYPE_STRUCTURE,
592 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
593
594 gstelement_class->change_state =
595 GST_DEBUG_FUNCPTR (gst_base_sink_change_state);
596 gstelement_class->send_event = GST_DEBUG_FUNCPTR (gst_base_sink_send_event);
597 gstelement_class->query = GST_DEBUG_FUNCPTR (default_element_query);
598
599 klass->get_caps = GST_DEBUG_FUNCPTR (gst_base_sink_default_get_caps);
600 klass->set_caps = GST_DEBUG_FUNCPTR (gst_base_sink_default_set_caps);
601 klass->fixate = GST_DEBUG_FUNCPTR (gst_base_sink_default_fixate);
602 klass->activate_pull =
603 GST_DEBUG_FUNCPTR (gst_base_sink_default_activate_pull);
604 klass->get_times = GST_DEBUG_FUNCPTR (gst_base_sink_default_get_times);
605 klass->query = GST_DEBUG_FUNCPTR (gst_base_sink_default_query);
606 klass->event = GST_DEBUG_FUNCPTR (gst_base_sink_default_event);
607 klass->wait_event = GST_DEBUG_FUNCPTR (gst_base_sink_default_wait_event);
608
609 /* Registering debug symbols for function pointers */
610 GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_fixate);
611 GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_pad_activate);
612 GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_pad_activate_mode);
613 GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_event);
614 GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_chain);
615 GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_chain_list);
616 GST_DEBUG_REGISTER_FUNCPTR (gst_base_sink_sink_query);
617 }
618
619 static GstCaps *
620 gst_base_sink_query_caps (GstBaseSink * bsink, GstPad * pad, GstCaps * filter)
621 {
622 GstBaseSinkClass *bclass;
623 GstCaps *caps = NULL;
624 gboolean fixed;
625
626 bclass = GST_BASE_SINK_GET_CLASS (bsink);
627 fixed = GST_PAD_IS_FIXED_CAPS (pad);
628
629 if (fixed || bsink->pad_mode == GST_PAD_MODE_PULL) {
630 /* if we are operating in pull mode or fixed caps, we only accept the
631 * currently negotiated caps */
632 caps = gst_pad_get_current_caps (pad);
633 }
634 if (caps == NULL) {
635 if (bclass->get_caps)
636 caps = bclass->get_caps (bsink, filter);
637
638 if (caps == NULL) {
639 GstPadTemplate *pad_template;
640
641 pad_template =
642 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (bclass),
643 "sink");
644 if (pad_template != NULL) {
645 caps = gst_pad_template_get_caps (pad_template);
646
647 if (filter) {
648 GstCaps *intersection;
649
650 intersection =
651 gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
652 gst_caps_unref (caps);
653 caps = intersection;
654 }
655 }
656 }
657 }
658
659 return caps;
660 }
661
662 static GstCaps *
663 gst_base_sink_default_fixate (GstBaseSink * bsink, GstCaps * caps)
664 {
665 GST_DEBUG_OBJECT (bsink, "using default caps fixate function");
666 return gst_caps_fixate (caps);
667 }
668
669 static GstCaps *
670 gst_base_sink_fixate (GstBaseSink * bsink, GstCaps * caps)
671 {
672 GstBaseSinkClass *bclass;
673
674 bclass = GST_BASE_SINK_GET_CLASS (bsink);
675
676 if (bclass->fixate)
677 caps = bclass->fixate (bsink, caps);
678
679 return caps;
680 }
681
682 static void
683 gst_base_sink_init (GstBaseSink * basesink, gpointer g_class)
684 {
685 GstPadTemplate *pad_template;
686 GstBaseSinkPrivate *priv;
687
688 basesink->priv = priv = gst_base_sink_get_instance_private (basesink);
689
690 pad_template =
691 gst_element_class_get_pad_template (GST_ELEMENT_CLASS (g_class), "sink");
692 g_return_if_fail (pad_template != NULL);
693
694 basesink->sinkpad = gst_pad_new_from_template (pad_template, "sink");
695
696 gst_pad_set_activate_function (basesink->sinkpad, gst_base_sink_pad_activate);
697 gst_pad_set_activatemode_function (basesink->sinkpad,
698 gst_base_sink_pad_activate_mode);
699 gst_pad_set_query_function (basesink->sinkpad, gst_base_sink_sink_query);
700 gst_pad_set_event_function (basesink->sinkpad, gst_base_sink_event);
701 gst_pad_set_chain_function (basesink->sinkpad, gst_base_sink_chain);
702 gst_pad_set_chain_list_function (basesink->sinkpad, gst_base_sink_chain_list);
703 gst_element_add_pad (GST_ELEMENT_CAST (basesink), basesink->sinkpad);
704
705 basesink->pad_mode = GST_PAD_MODE_NONE;
706 g_mutex_init (&basesink->preroll_lock);
707 g_cond_init (&basesink->preroll_cond);
708 priv->have_latency = FALSE;
709
710 basesink->can_activate_push = DEFAULT_CAN_ACTIVATE_PUSH;
711 basesink->can_activate_pull = DEFAULT_CAN_ACTIVATE_PULL;
712
713 basesink->sync = DEFAULT_SYNC;
714 basesink->max_lateness = DEFAULT_MAX_LATENESS;
715 g_atomic_int_set (&priv->qos_enabled, DEFAULT_QOS);
716 priv->async_enabled = DEFAULT_ASYNC;
717 priv->ts_offset = DEFAULT_TS_OFFSET;
718 priv->render_delay = DEFAULT_RENDER_DELAY;
719 priv->processing_deadline = DEFAULT_PROCESSING_DEADLINE;
720 priv->blocksize = DEFAULT_BLOCKSIZE;
721 priv->cached_clock_id = NULL;
722 g_atomic_int_set (&priv->enable_last_sample, DEFAULT_ENABLE_LAST_SAMPLE);
723 priv->throttle_time = DEFAULT_THROTTLE_TIME;
724 priv->max_bitrate = DEFAULT_MAX_BITRATE;
725
726 priv->drop_out_of_segment = DEFAULT_DROP_OUT_OF_SEGMENT;
727
728 GST_OBJECT_FLAG_SET (basesink, GST_ELEMENT_FLAG_SINK);
729 }
730
731 static void
732 gst_base_sink_finalize (GObject * object)
733 {
734 GstBaseSink *basesink;
735
736 basesink = GST_BASE_SINK (object);
737
738 g_mutex_clear (&basesink->preroll_lock);
739 g_cond_clear (&basesink->preroll_cond);
740
741 G_OBJECT_CLASS (parent_class)->finalize (object);
742 }
743
744 /**
745 * gst_base_sink_set_sync:
746 * @sink: the sink
747 * @sync: the new sync value.
748 *
749 * Configures @sink to synchronize on the clock or not. When
750 * @sync is %FALSE, incoming samples will be played as fast as
751 * possible. If @sync is %TRUE, the timestamps of the incoming
752 * buffers will be used to schedule the exact render time of its
753 * contents.
754 */
755 void
756 gst_base_sink_set_sync (GstBaseSink * sink, gboolean sync)
757 {
758 g_return_if_fail (GST_IS_BASE_SINK (sink));
759
760 GST_OBJECT_LOCK (sink);
761 sink->sync = sync;
762 GST_OBJECT_UNLOCK (sink);
763 }
764
765 /**
766 * gst_base_sink_get_sync:
767 * @sink: the sink
768 *
769 * Checks if @sink is currently configured to synchronize against the
770 * clock.
771 *
772 * Returns: %TRUE if the sink is configured to synchronize against the clock.
773 */
774 gboolean
775 gst_base_sink_get_sync (GstBaseSink * sink)
776 {
777 gboolean res;
778
779 g_return_val_if_fail (GST_IS_BASE_SINK (sink), FALSE);
780
781 GST_OBJECT_LOCK (sink);
782 res = sink->sync;
783 GST_OBJECT_UNLOCK (sink);
784
785 return res;
786 }
787
788 /**
789 * gst_base_sink_set_drop_out_of_segment:
790 * @sink: the sink
791 * @drop_out_of_segment: drop buffers outside the segment
792 *
793 * Configure @sink to drop buffers which are outside the current segment
794 *
795 * Since: 1.12
796 */
797 void
798 gst_base_sink_set_drop_out_of_segment (GstBaseSink * sink,
799 gboolean drop_out_of_segment)
800 {
801 g_return_if_fail (GST_IS_BASE_SINK (sink));
802
803 GST_OBJECT_LOCK (sink);
804 sink->priv->drop_out_of_segment = drop_out_of_segment;
805 GST_OBJECT_UNLOCK (sink);
806
807 }
808
809 /**
810 * gst_base_sink_get_drop_out_of_segment:
811 * @sink: the sink
812 *
813 * Checks if @sink is currently configured to drop buffers which are outside
814 * the current segment
815 *
816 * Returns: %TRUE if the sink is configured to drop buffers outside the
817 * current segment.
818 *
819 * Since: 1.12
820 */
821 gboolean
822 gst_base_sink_get_drop_out_of_segment (GstBaseSink * sink)
823 {
824 gboolean res;
825
826 g_return_val_if_fail (GST_IS_BASE_SINK (sink), FALSE);
827
828 GST_OBJECT_LOCK (sink);
829 res = sink->priv->drop_out_of_segment;
830 GST_OBJECT_UNLOCK (sink);
831
832 return res;
833 }
834
835 /**
836 * gst_base_sink_set_max_lateness:
837 * @sink: the sink
838 * @max_lateness: the new max lateness value.
839 *
840 * Sets the new max lateness value to @max_lateness. This value is
841 * used to decide if a buffer should be dropped or not based on the
842 * buffer timestamp and the current clock time. A value of -1 means
843 * an unlimited time.
844 */
845 void
846 gst_base_sink_set_max_lateness (GstBaseSink * sink, gint64 max_lateness)
847 {
848 g_return_if_fail (GST_IS_BASE_SINK (sink));
849
850 GST_OBJECT_LOCK (sink);
851 sink->max_lateness = max_lateness;
852 GST_OBJECT_UNLOCK (sink);
853 }
854
855 /**
856 * gst_base_sink_get_max_lateness:
857 * @sink: the sink
858 *
859 * Gets the max lateness value. See gst_base_sink_set_max_lateness() for
860 * more details.
861 *
862 * Returns: The maximum time in nanoseconds that a buffer can be late
863 * before it is dropped and not rendered. A value of -1 means an
864 * unlimited time.
865 */
866 gint64
867 gst_base_sink_get_max_lateness (GstBaseSink * sink)
868 {
869 gint64 res;
870
871 g_return_val_if_fail (GST_IS_BASE_SINK (sink), -1);
872
873 GST_OBJECT_LOCK (sink);
874 res = sink->max_lateness;
875 GST_OBJECT_UNLOCK (sink);
876
877 return res;
878 }
879
880 /**
881 * gst_base_sink_set_qos_enabled:
882 * @sink: the sink
883 * @enabled: the new qos value.
884 *
885 * Configures @sink to send Quality-of-Service events upstream.
886 */
887 void
888 gst_base_sink_set_qos_enabled (GstBaseSink * sink, gboolean enabled)
889 {
890 g_return_if_fail (GST_IS_BASE_SINK (sink));
891
892 g_atomic_int_set (&sink->priv->qos_enabled, enabled);
893 }
894
895 /**
896 * gst_base_sink_is_qos_enabled:
897 * @sink: the sink
898 *
899 * Checks if @sink is currently configured to send Quality-of-Service events
900 * upstream.
901 *
902 * Returns: %TRUE if the sink is configured to perform Quality-of-Service.
903 */
904 gboolean
905 gst_base_sink_is_qos_enabled (GstBaseSink * sink)
906 {
907 gboolean res;
908
909 g_return_val_if_fail (GST_IS_BASE_SINK (sink), FALSE);
910
911 res = g_atomic_int_get (&sink->priv->qos_enabled);
912
913 return res;
914 }
915
916 /**
917 * gst_base_sink_set_async_enabled:
918 * @sink: the sink
919 * @enabled: the new async value.
920 *
921 * Configures @sink to perform all state changes asynchronously. When async is
922 * disabled, the sink will immediately go to PAUSED instead of waiting for a
923 * preroll buffer. This feature is useful if the sink does not synchronize
924 * against the clock or when it is dealing with sparse streams.
925 */
926 void
927 gst_base_sink_set_async_enabled (GstBaseSink * sink, gboolean enabled)
928 {
929 g_return_if_fail (GST_IS_BASE_SINK (sink));
930
931 GST_BASE_SINK_PREROLL_LOCK (sink);
932 g_atomic_int_set (&sink->priv->async_enabled, enabled);
933 GST_LOG_OBJECT (sink, "set async enabled to %d", enabled);
934 GST_BASE_SINK_PREROLL_UNLOCK (sink);
935 }
936
937 /**
938 * gst_base_sink_is_async_enabled:
939 * @sink: the sink
940 *
941 * Checks if @sink is currently configured to perform asynchronous state
942 * changes to PAUSED.
943 *
944 * Returns: %TRUE if the sink is configured to perform asynchronous state
945 * changes.
946 */
947 gboolean
948 gst_base_sink_is_async_enabled (GstBaseSink * sink)
949 {
950 gboolean res;
951
952 g_return_val_if_fail (GST_IS_BASE_SINK (sink), FALSE);
953
954 res = g_atomic_int_get (&sink->priv->async_enabled);
955
956 return res;
957 }
958
959 /**
960 * gst_base_sink_set_ts_offset:
961 * @sink: the sink
962 * @offset: the new offset
963 *
964 * Adjust the synchronisation of @sink with @offset. A negative value will
965 * render buffers earlier than their timestamp. A positive value will delay
966 * rendering. This function can be used to fix playback of badly timestamped
967 * buffers.
968 */
969 void
970 gst_base_sink_set_ts_offset (GstBaseSink * sink, GstClockTimeDiff offset)
971 {
972 g_return_if_fail (GST_IS_BASE_SINK (sink));
973
974 GST_OBJECT_LOCK (sink);
975 sink->priv->ts_offset = offset;
976 GST_LOG_OBJECT (sink, "set time offset to %" G_GINT64_FORMAT, offset);
977 GST_OBJECT_UNLOCK (sink);
978 }
979
980 /**
981 * gst_base_sink_get_ts_offset:
982 * @sink: the sink
983 *
984 * Get the synchronisation offset of @sink.
985 *
986 * Returns: The synchronisation offset.
987 */
988 GstClockTimeDiff
989 gst_base_sink_get_ts_offset (GstBaseSink * sink)
990 {
991 GstClockTimeDiff res;
992
993 g_return_val_if_fail (GST_IS_BASE_SINK (sink), 0);
994
995 GST_OBJECT_LOCK (sink);
996 res = sink->priv->ts_offset;
997 GST_OBJECT_UNLOCK (sink);
998
999 return res;
1000 }
1001
1002 /**
1003 * gst_base_sink_get_last_sample:
1004 * @sink: the sink
1005 *
1006 * Get the last sample that arrived in the sink and was used for preroll or for
1007 * rendering. This property can be used to generate thumbnails.
1008 *
1009 * The #GstCaps on the sample can be used to determine the type of the buffer.
1010 *
1011 * Free-function: gst_sample_unref
1012 *
1013 * Returns: (transfer full) (nullable): a #GstSample. gst_sample_unref() after
1014 * usage. This function returns %NULL when no buffer has arrived in the
1015 * sink yet or when the sink is not in PAUSED or PLAYING.
1016 */
1017 GstSample *
1018 gst_base_sink_get_last_sample (GstBaseSink * sink)
1019 {
1020 GstSample *res = NULL;
1021
1022 g_return_val_if_fail (GST_IS_BASE_SINK (sink), NULL);
1023
1024 GST_OBJECT_LOCK (sink);
1025 if (sink->priv->last_buffer_list) {
1026 GstBuffer *first_buffer = NULL;
1027
1028 /* Set the first buffer in the list to last sample's buffer */
1029 first_buffer = gst_buffer_list_get (sink->priv->last_buffer_list, 0);
1030 res =
1031 gst_sample_new (first_buffer, sink->priv->last_caps, &sink->segment,
1032 NULL);
1033 gst_sample_set_buffer_list (res, sink->priv->last_buffer_list);
1034 } else if (sink->priv->last_buffer) {
1035 res = gst_sample_new (sink->priv->last_buffer,
1036 sink->priv->last_caps, &sink->segment, NULL);
1037 }
1038 GST_OBJECT_UNLOCK (sink);
1039
1040 return res;
1041 }
1042
1043 /* with OBJECT_LOCK */
1044 static void
1045 gst_base_sink_set_last_buffer_unlocked (GstBaseSink * sink, GstBuffer * buffer)
1046 {
1047 GstBuffer *old;
1048
1049 old = sink->priv->last_buffer;
1050 if (G_LIKELY (old != buffer)) {
1051 GST_DEBUG_OBJECT (sink, "setting last buffer to %p", buffer);
1052 if (G_LIKELY (buffer))
1053 gst_buffer_ref (buffer);
1054 sink->priv->last_buffer = buffer;
1055 if (buffer)
1056 /* copy over the caps */
1057 gst_caps_replace (&sink->priv->last_caps, sink->priv->caps);
1058 else
1059 gst_caps_replace (&sink->priv->last_caps, NULL);
1060 } else {
1061 old = NULL;
1062 }
1063 /* avoid unreffing with the lock because cleanup code might want to take the
1064 * lock too */
1065 if (G_LIKELY (old)) {
1066 GST_OBJECT_UNLOCK (sink);
1067 gst_buffer_unref (old);
1068 GST_OBJECT_LOCK (sink);
1069 }
1070 }
1071
1072 /* with OBJECT_LOCK */
1073 static void
1074 gst_base_sink_set_last_buffer_list_unlocked (GstBaseSink * sink,
1075 GstBufferList * buffer_list)
1076 {
1077 GstBufferList *old;
1078
1079 old = sink->priv->last_buffer_list;
1080 if (G_LIKELY (old != buffer_list)) {
1081 GST_DEBUG_OBJECT (sink, "setting last buffer list to %p", buffer_list);
1082 if (G_LIKELY (buffer_list))
1083 gst_mini_object_ref (GST_MINI_OBJECT_CAST (buffer_list));
1084 sink->priv->last_buffer_list = buffer_list;
1085 } else {
1086 old = NULL;
1087 }
1088
1089 /* avoid unreffing with the lock because cleanup code might want to take the
1090 * lock too */
1091 if (G_LIKELY (old)) {
1092 GST_OBJECT_UNLOCK (sink);
1093 gst_mini_object_unref (GST_MINI_OBJECT_CAST (old));
1094 GST_OBJECT_LOCK (sink);
1095 }
1096 }
1097
1098 static void
1099 gst_base_sink_set_last_buffer (GstBaseSink * sink, GstBuffer * buffer)
1100 {
1101 if (!g_atomic_int_get (&sink->priv->enable_last_sample))
1102 return;
1103
1104 GST_OBJECT_LOCK (sink);
1105 gst_base_sink_set_last_buffer_unlocked (sink, buffer);
1106 GST_OBJECT_UNLOCK (sink);
1107 }
1108
1109 static void
1110 gst_base_sink_set_last_buffer_list (GstBaseSink * sink,
1111 GstBufferList * buffer_list)
1112 {
1113 if (!g_atomic_int_get (&sink->priv->enable_last_sample))
1114 return;
1115
1116 GST_OBJECT_LOCK (sink);
1117 gst_base_sink_set_last_buffer_list_unlocked (sink, buffer_list);
1118 GST_OBJECT_UNLOCK (sink);
1119 }
1120
1121 /**
1122 * gst_base_sink_set_last_sample_enabled:
1123 * @sink: the sink
1124 * @enabled: the new enable-last-sample value.
1125 *
1126 * Configures @sink to store the last received sample in the last-sample
1127 * property.
1128 */
1129 void
1130 gst_base_sink_set_last_sample_enabled (GstBaseSink * sink, gboolean enabled)
1131 {
1132 g_return_if_fail (GST_IS_BASE_SINK (sink));
1133
1134 /* Only take lock if we change the value */
1135 if (g_atomic_int_compare_and_exchange (&sink->priv->enable_last_sample,
1136 !enabled, enabled) && !enabled) {
1137 GST_OBJECT_LOCK (sink);
1138 gst_base_sink_set_last_buffer_unlocked (sink, NULL);
1139 gst_base_sink_set_last_buffer_list_unlocked (sink, NULL);
1140 GST_OBJECT_UNLOCK (sink);
1141 }
1142 }
1143
1144 /**
1145 * gst_base_sink_is_last_sample_enabled:
1146 * @sink: the sink
1147 *
1148 * Checks if @sink is currently configured to store the last received sample in
1149 * the last-sample property.
1150 *
1151 * Returns: %TRUE if the sink is configured to store the last received sample.
1152 */
1153 gboolean
1154 gst_base_sink_is_last_sample_enabled (GstBaseSink * sink)
1155 {
1156 g_return_val_if_fail (GST_IS_BASE_SINK (sink), FALSE);
1157
1158 return g_atomic_int_get (&sink->priv->enable_last_sample);
1159 }
1160
1161 /**
1162 * gst_base_sink_get_latency:
1163 * @sink: the sink
1164 *
1165 * Get the currently configured latency.
1166 *
1167 * Returns: The configured latency.
1168 */
1169 GstClockTime
1170 gst_base_sink_get_latency (GstBaseSink * sink)
1171 {
1172 GstClockTime res;
1173
1174 GST_OBJECT_LOCK (sink);
1175 res = sink->priv->latency;
1176 GST_OBJECT_UNLOCK (sink);
1177
1178 return res;
1179 }
1180
1181 /**
1182 * gst_base_sink_query_latency:
1183 * @sink: the sink
1184 * @live: (out) (allow-none): if the sink is live
1185 * @upstream_live: (out) (allow-none): if an upstream element is live
1186 * @min_latency: (out) (allow-none): the min latency of the upstream elements
1187 * @max_latency: (out) (allow-none): the max latency of the upstream elements
1188 *
1189 * Query the sink for the latency parameters. The latency will be queried from
1190 * the upstream elements. @live will be %TRUE if @sink is configured to
1191 * synchronize against the clock. @upstream_live will be %TRUE if an upstream
1192 * element is live.
1193 *
1194 * If both @live and @upstream_live are %TRUE, the sink will want to compensate
1195 * for the latency introduced by the upstream elements by setting the
1196 * @min_latency to a strictly positive value.
1197 *
1198 * This function is mostly used by subclasses.
1199 *
1200 * Returns: %TRUE if the query succeeded.
1201 */
1202 gboolean
1203 gst_base_sink_query_latency (GstBaseSink * sink, gboolean * live,
1204 gboolean * upstream_live, GstClockTime * min_latency,
1205 GstClockTime * max_latency)
1206 {
1207 gboolean l, us_live, res, have_latency;
1208 GstClockTime min, max, render_delay, processing_deadline;
1209 GstQuery *query;
1210 GstClockTime us_min, us_max;
1211
1212 /* we are live when we sync to the clock */
1213 GST_OBJECT_LOCK (sink);
1214 l = sink->sync;
1215 have_latency = sink->priv->have_latency;
1216 render_delay = sink->priv->render_delay;
1217 processing_deadline = sink->priv->processing_deadline;
1218 GST_OBJECT_UNLOCK (sink);
1219
1220 /* assume no latency */
1221 min = 0;
1222 max = -1;
1223 us_live = FALSE;
1224 us_min = 0;
1225 us_max = 0;
1226
1227 if (have_latency) {
1228 GST_DEBUG_OBJECT (sink, "we are ready for LATENCY query");
1229 /* we are ready for a latency query this is when we preroll or when we are
1230 * not async. */
1231 query = gst_query_new_latency ();
1232
1233 /* ask the peer for the latency */
1234 if ((res = gst_pad_peer_query (sink->sinkpad, query))) {
1235 /* get upstream min and max latency */
1236 gst_query_parse_latency (query, &us_live, &us_min, &us_max);
1237
1238 if (us_live) {
1239 /* upstream live, use its latency, subclasses should use these
1240 * values to create the complete latency. */
1241 min = us_min;
1242 max = us_max;
1243
1244 if (l) {
1245 if (max == -1 || min + processing_deadline <= max)
1246 min += processing_deadline;
1247 else {
1248 GST_ELEMENT_WARNING (sink, CORE, CLOCK,
1249 (_("Pipeline construction is invalid, please add queues.")),
1250 ("Not enough buffering available for "
1251 " the processing deadline of %" GST_TIME_FORMAT
1252 ", add enough queues to buffer %" GST_TIME_FORMAT
1253 " additional data. Shortening processing latency to %"
1254 GST_TIME_FORMAT ".",
1255 GST_TIME_ARGS (processing_deadline),
1256 GST_TIME_ARGS (min + processing_deadline - max),
1257 GST_TIME_ARGS (max - min)));
1258 min = max;
1259 }
1260 }
1261 }
1262 if (l) {
1263 /* we need to add the render delay if we are live */
1264 min += render_delay;
1265 if (max != -1)
1266 max += render_delay;
1267 }
1268 }
1269 gst_query_unref (query);
1270 } else {
1271 GST_DEBUG_OBJECT (sink, "we are not yet ready for LATENCY query");
1272 res = FALSE;
1273 }
1274
1275 /* not live, we tried to do the query, if it failed we return TRUE anyway */
1276 if (!res) {
1277 if (!l) {
1278 res = TRUE;
1279 GST_DEBUG_OBJECT (sink, "latency query failed but we are not live");
1280 } else {
1281 GST_DEBUG_OBJECT (sink, "latency query failed and we are live");
1282 }
1283 }
1284
1285 if (res) {
1286 GST_DEBUG_OBJECT (sink, "latency query: live: %d, have_latency %d,"
1287 " upstream_live %d, min(%" GST_TIME_FORMAT ")=upstream(%"
1288 GST_TIME_FORMAT ")+processing_deadline(%" GST_TIME_FORMAT
1289 ")+render_delay(%" GST_TIME_FORMAT "), max(%" GST_TIME_FORMAT
1290 ")=upstream(%" GST_TIME_FORMAT ")+render_delay(%" GST_TIME_FORMAT ")",
1291 l, have_latency, us_live, GST_TIME_ARGS (min), GST_TIME_ARGS (us_min),
1292 GST_TIME_ARGS (processing_deadline), GST_TIME_ARGS (render_delay),
1293 GST_TIME_ARGS (max), GST_TIME_ARGS (us_max),
1294 GST_TIME_ARGS (render_delay));
1295
1296 if (live)
1297 *live = l;
1298 if (upstream_live)
1299 *upstream_live = us_live;
1300 if (min_latency)
1301 *min_latency = min;
1302 if (max_latency)
1303 *max_latency = max;
1304 }
1305 return res;
1306 }
1307
1308 /**
1309 * gst_base_sink_set_render_delay:
1310 * @sink: a #GstBaseSink
1311 * @delay: the new delay
1312 *
1313 * Set the render delay in @sink to @delay. The render delay is the time
1314 * between actual rendering of a buffer and its synchronisation time. Some
1315 * devices might delay media rendering which can be compensated for with this
1316 * function.
1317 *
1318 * After calling this function, this sink will report additional latency and
1319 * other sinks will adjust their latency to delay the rendering of their media.
1320 *
1321 * This function is usually called by subclasses.
1322 */
1323 void
1324 gst_base_sink_set_render_delay (GstBaseSink * sink, GstClockTime delay)
1325 {
1326 GstClockTime old_render_delay;
1327
1328 g_return_if_fail (GST_IS_BASE_SINK (sink));
1329 g_return_if_fail (GST_CLOCK_TIME_IS_VALID (delay));
1330
1331 GST_OBJECT_LOCK (sink);
1332 old_render_delay = sink->priv->render_delay;
1333 sink->priv->render_delay = delay;
1334 GST_LOG_OBJECT (sink, "set render delay to %" GST_TIME_FORMAT,
1335 GST_TIME_ARGS (delay));
1336 GST_OBJECT_UNLOCK (sink);
1337
1338 if (delay != old_render_delay) {
1339 GST_DEBUG_OBJECT (sink, "posting latency changed");
1340 gst_element_post_message (GST_ELEMENT_CAST (sink),
1341 gst_message_new_latency (GST_OBJECT_CAST (sink)));
1342 }
1343 }
1344
1345 /**
1346 * gst_base_sink_get_render_delay:
1347 * @sink: a #GstBaseSink
1348 *
1349 * Get the render delay of @sink. see gst_base_sink_set_render_delay() for more
1350 * information about the render delay.
1351 *
1352 * Returns: the render delay of @sink.
1353 */
1354 GstClockTime
1355 gst_base_sink_get_render_delay (GstBaseSink * sink)
1356 {
1357 GstClockTimeDiff res;
1358
1359 g_return_val_if_fail (GST_IS_BASE_SINK (sink), 0);
1360
1361 GST_OBJECT_LOCK (sink);
1362 res = sink->priv->render_delay;
1363 GST_OBJECT_UNLOCK (sink);
1364
1365 return res;
1366 }
1367
1368 /**
1369 * gst_base_sink_set_blocksize:
1370 * @sink: a #GstBaseSink
1371 * @blocksize: the blocksize in bytes
1372 *
1373 * Set the number of bytes that the sink will pull when it is operating in pull
1374 * mode.
1375 */
1376 /* FIXME 2.0: blocksize property should be int, otherwise min>max.. */
1377 void
1378 gst_base_sink_set_blocksize (GstBaseSink * sink, guint blocksize)
1379 {
1380 g_return_if_fail (GST_IS_BASE_SINK (sink));
1381
1382 GST_OBJECT_LOCK (sink);
1383 sink->priv->blocksize = blocksize;
1384 GST_LOG_OBJECT (sink, "set blocksize to %u", blocksize);
1385 GST_OBJECT_UNLOCK (sink);
1386 }
1387
1388 /**
1389 * gst_base_sink_get_blocksize:
1390 * @sink: a #GstBaseSink
1391 *
1392 * Get the number of bytes that the sink will pull when it is operating in pull
1393 * mode.
1394 *
1395 * Returns: the number of bytes @sink will pull in pull mode.
1396 */
1397 /* FIXME 2.0: blocksize property should be int, otherwise min>max.. */
1398 guint
1399 gst_base_sink_get_blocksize (GstBaseSink * sink)
1400 {
1401 guint res;
1402
1403 g_return_val_if_fail (GST_IS_BASE_SINK (sink), 0);
1404
1405 GST_OBJECT_LOCK (sink);
1406 res = sink->priv->blocksize;
1407 GST_OBJECT_UNLOCK (sink);
1408
1409 return res;
1410 }
1411
1412 /**
1413 * gst_base_sink_set_throttle_time:
1414 * @sink: a #GstBaseSink
1415 * @throttle: the throttle time in nanoseconds
1416 *
1417 * Set the time that will be inserted between rendered buffers. This
1418 * can be used to control the maximum buffers per second that the sink
1419 * will render.
1420 */
1421 void
1422 gst_base_sink_set_throttle_time (GstBaseSink * sink, guint64 throttle)
1423 {
1424 g_return_if_fail (GST_IS_BASE_SINK (sink));
1425
1426 GST_OBJECT_LOCK (sink);
1427 sink->priv->throttle_time = throttle;
1428 GST_LOG_OBJECT (sink, "set throttle_time to %" G_GUINT64_FORMAT, throttle);
1429 GST_OBJECT_UNLOCK (sink);
1430 }
1431
1432 /**
1433 * gst_base_sink_get_throttle_time:
1434 * @sink: a #GstBaseSink
1435 *
1436 * Get the time that will be inserted between frames to control the
1437 * maximum buffers per second.
1438 *
1439 * Returns: the number of nanoseconds @sink will put between frames.
1440 */
1441 guint64
1442 gst_base_sink_get_throttle_time (GstBaseSink * sink)
1443 {
1444 guint64 res;
1445
1446 g_return_val_if_fail (GST_IS_BASE_SINK (sink), 0);
1447
1448 GST_OBJECT_LOCK (sink);
1449 res = sink->priv->throttle_time;
1450 GST_OBJECT_UNLOCK (sink);
1451
1452 return res;
1453 }
1454
1455 /**
1456 * gst_base_sink_set_max_bitrate:
1457 * @sink: a #GstBaseSink
1458 * @max_bitrate: the max_bitrate in bits per second
1459 *
1460 * Set the maximum amount of bits per second that the sink will render.
1461 *
1462 * Since: 1.2
1463 */
1464 void
1465 gst_base_sink_set_max_bitrate (GstBaseSink * sink, guint64 max_bitrate)
1466 {
1467 g_return_if_fail (GST_IS_BASE_SINK (sink));
1468
1469 GST_OBJECT_LOCK (sink);
1470 sink->priv->max_bitrate = max_bitrate;
1471 GST_LOG_OBJECT (sink, "set max_bitrate to %" G_GUINT64_FORMAT, max_bitrate);
1472 GST_OBJECT_UNLOCK (sink);
1473 }
1474
1475 /**
1476 * gst_base_sink_get_max_bitrate:
1477 * @sink: a #GstBaseSink
1478 *
1479 * Get the maximum amount of bits per second that the sink will render.
1480 *
1481 * Returns: the maximum number of bits per second @sink will render.
1482 *
1483 * Since: 1.2
1484 */
1485 guint64
1486 gst_base_sink_get_max_bitrate (GstBaseSink * sink)
1487 {
1488 guint64 res;
1489
1490 g_return_val_if_fail (GST_IS_BASE_SINK (sink), 0);
1491
1492 GST_OBJECT_LOCK (sink);
1493 res = sink->priv->max_bitrate;
1494 GST_OBJECT_UNLOCK (sink);
1495
1496 return res;
1497 }
1498
1499 /**
1500 * gst_base_sink_set_processing_deadline:
1501 * @sink: a #GstBaseSink
1502 * @processing_deadline: the new processing deadline in nanoseconds.
1503 *
1504 * Maximum amount of time (in nanoseconds) that the pipeline can take
1505 * for processing the buffer. This is added to the latency of live
1506 * pipelines.
1507 *
1508 * This function is usually called by subclasses.
1509 *
1510 * Since: 1.16
1511 */
1512 void
1513 gst_base_sink_set_processing_deadline (GstBaseSink * sink,
1514 GstClockTime processing_deadline)
1515 {
1516 GstClockTime old_processing_deadline;
1517
1518 g_return_if_fail (GST_IS_BASE_SINK (sink));
1519
1520 GST_OBJECT_LOCK (sink);
1521 old_processing_deadline = sink->priv->processing_deadline;
1522 sink->priv->processing_deadline = processing_deadline;
1523 GST_LOG_OBJECT (sink, "set render processing_deadline to %" GST_TIME_FORMAT,
1524 GST_TIME_ARGS (processing_deadline));
1525 GST_OBJECT_UNLOCK (sink);
1526
1527 if (processing_deadline != old_processing_deadline) {
1528 GST_DEBUG_OBJECT (sink, "posting latency changed");
1529 gst_element_post_message (GST_ELEMENT_CAST (sink),
1530 gst_message_new_latency (GST_OBJECT_CAST (sink)));
1531 }
1532 }
1533
1534 /**
1535 * gst_base_sink_get_processing_deadline:
1536 * @sink: a #GstBaseSink
1537 *
1538 * Get the processing deadline of @sink. see
1539 * gst_base_sink_set_processing_deadline() for more information about
1540 * the processing deadline.
1541 *
1542 * Returns: the processing deadline
1543 *
1544 * Since: 1.16
1545 */
1546 GstClockTime
1547 gst_base_sink_get_processing_deadline (GstBaseSink * sink)
1548 {
1549 GstClockTimeDiff res;
1550
1551 g_return_val_if_fail (GST_IS_BASE_SINK (sink), 0);
1552
1553 GST_OBJECT_LOCK (sink);
1554 res = sink->priv->processing_deadline;
1555 GST_OBJECT_UNLOCK (sink);
1556
1557 return res;
1558 }
1559
1560 static void
1561 gst_base_sink_set_property (GObject * object, guint prop_id,
1562 const GValue * value, GParamSpec * pspec)
1563 {
1564 GstBaseSink *sink = GST_BASE_SINK (object);
1565
1566 switch (prop_id) {
1567 case PROP_SYNC:
1568 gst_base_sink_set_sync (sink, g_value_get_boolean (value));
1569 break;
1570 case PROP_MAX_LATENESS:
1571 gst_base_sink_set_max_lateness (sink, g_value_get_int64 (value));
1572 break;
1573 case PROP_QOS:
1574 gst_base_sink_set_qos_enabled (sink, g_value_get_boolean (value));
1575 break;
1576 case PROP_ASYNC:
1577 gst_base_sink_set_async_enabled (sink, g_value_get_boolean (value));
1578 break;
1579 case PROP_TS_OFFSET:
1580 gst_base_sink_set_ts_offset (sink, g_value_get_int64 (value));
1581 break;
1582 case PROP_BLOCKSIZE:
1583 gst_base_sink_set_blocksize (sink, g_value_get_uint (value));
1584 break;
1585 case PROP_RENDER_DELAY:
1586 gst_base_sink_set_render_delay (sink, g_value_get_uint64 (value));
1587 break;
1588 case PROP_ENABLE_LAST_SAMPLE:
1589 gst_base_sink_set_last_sample_enabled (sink, g_value_get_boolean (value));
1590 break;
1591 case PROP_THROTTLE_TIME:
1592 gst_base_sink_set_throttle_time (sink, g_value_get_uint64 (value));
1593 break;
1594 case PROP_MAX_BITRATE:
1595 gst_base_sink_set_max_bitrate (sink, g_value_get_uint64 (value));
1596 break;
1597 case PROP_PROCESSING_DEADLINE:
1598 gst_base_sink_set_processing_deadline (sink, g_value_get_uint64 (value));
1599 break;
1600 default:
1601 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1602 break;
1603 }
1604 }
1605
1606 static void
1607 gst_base_sink_get_property (GObject * object, guint prop_id, GValue * value,
1608 GParamSpec * pspec)
1609 {
1610 GstBaseSink *sink = GST_BASE_SINK (object);
1611
1612 switch (prop_id) {
1613 case PROP_SYNC:
1614 g_value_set_boolean (value, gst_base_sink_get_sync (sink));
1615 break;
1616 case PROP_MAX_LATENESS:
1617 g_value_set_int64 (value, gst_base_sink_get_max_lateness (sink));
1618 break;
1619 case PROP_QOS:
1620 g_value_set_boolean (value, gst_base_sink_is_qos_enabled (sink));
1621 break;
1622 case PROP_ASYNC:
1623 g_value_set_boolean (value, gst_base_sink_is_async_enabled (sink));
1624 break;
1625 case PROP_TS_OFFSET:
1626 g_value_set_int64 (value, gst_base_sink_get_ts_offset (sink));
1627 break;
1628 case PROP_LAST_SAMPLE:
1629 gst_value_take_sample (value, gst_base_sink_get_last_sample (sink));
1630 break;
1631 case PROP_ENABLE_LAST_SAMPLE:
1632 g_value_set_boolean (value, gst_base_sink_is_last_sample_enabled (sink));
1633 break;
1634 case PROP_BLOCKSIZE:
1635 g_value_set_uint (value, gst_base_sink_get_blocksize (sink));
1636 break;
1637 case PROP_RENDER_DELAY:
1638 g_value_set_uint64 (value, gst_base_sink_get_render_delay (sink));
1639 break;
1640 case PROP_THROTTLE_TIME:
1641 g_value_set_uint64 (value, gst_base_sink_get_throttle_time (sink));
1642 break;
1643 case PROP_MAX_BITRATE:
1644 g_value_set_uint64 (value, gst_base_sink_get_max_bitrate (sink));
1645 break;
1646 case PROP_PROCESSING_DEADLINE:
1647 g_value_set_uint64 (value, gst_base_sink_get_processing_deadline (sink));
1648 break;
1649 case PROP_STATS:
1650 g_value_take_boxed (value, gst_base_sink_get_stats (sink));
1651 break;
1652 default:
1653 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1654 break;
1655 }
1656 }
1657
1658
1659 static GstCaps *
1660 gst_base_sink_default_get_caps (GstBaseSink * sink, GstCaps * filter)
1661 {
1662 return NULL;
1663 }
1664
1665 static gboolean
1666 gst_base_sink_default_set_caps (GstBaseSink * sink, GstCaps * caps)
1667 {
1668 return TRUE;
1669 }
1670
1671 /* with PREROLL_LOCK, STREAM_LOCK */
1672 static gboolean
1673 gst_base_sink_commit_state (GstBaseSink * basesink)
1674 {
1675 /* commit state and proceed to next pending state */
1676 GstState current, next, pending, post_pending;
1677 gboolean post_paused = FALSE;
1678 gboolean post_async_done = FALSE;
1679 gboolean post_playing = FALSE;
1680
1681 /* we are certainly not playing async anymore now */
1682 basesink->playing_async = FALSE;
1683
1684 GST_OBJECT_LOCK (basesink);
1685 current = GST_STATE (basesink);
1686 next = GST_STATE_NEXT (basesink);
1687 pending = GST_STATE_PENDING (basesink);
1688 post_pending = pending;
1689
1690 switch (pending) {
1691 case GST_STATE_PLAYING:
1692 {
1693 GST_DEBUG_OBJECT (basesink, "committing state to PLAYING");
1694
1695 basesink->need_preroll = FALSE;
1696 post_async_done = TRUE;
1697 basesink->priv->committed = TRUE;
1698 post_playing = TRUE;
1699 /* post PAUSED too when we were READY */
1700 if (current == GST_STATE_READY) {
1701 post_paused = TRUE;
1702 }
1703 break;
1704 }
1705 case GST_STATE_PAUSED:
1706 GST_DEBUG_OBJECT (basesink, "committing state to PAUSED");
1707 post_paused = TRUE;
1708 post_async_done = TRUE;
1709 basesink->priv->committed = TRUE;
1710 post_pending = GST_STATE_VOID_PENDING;
1711 break;
1712 case GST_STATE_READY:
1713 case GST_STATE_NULL:
1714 goto stopping;
1715 case GST_STATE_VOID_PENDING:
1716 goto nothing_pending;
1717 default:
1718 break;
1719 }
1720
1721 /* we can report latency queries now */
1722 basesink->priv->have_latency = TRUE;
1723
1724 GST_STATE (basesink) = pending;
1725 GST_STATE_NEXT (basesink) = GST_STATE_VOID_PENDING;
1726 GST_STATE_PENDING (basesink) = GST_STATE_VOID_PENDING;
1727 GST_STATE_RETURN (basesink) = GST_STATE_CHANGE_SUCCESS;
1728 GST_OBJECT_UNLOCK (basesink);
1729
1730 if (post_paused) {
1731 GST_DEBUG_OBJECT (basesink, "posting PAUSED state change message");
1732 gst_element_post_message (GST_ELEMENT_CAST (basesink),
1733 gst_message_new_state_changed (GST_OBJECT_CAST (basesink),
1734 current, next, post_pending));
1735 }
1736 if (post_async_done) {
1737 GST_DEBUG_OBJECT (basesink, "posting async-done message");
1738 gst_element_post_message (GST_ELEMENT_CAST (basesink),
1739 gst_message_new_async_done (GST_OBJECT_CAST (basesink),
1740 GST_CLOCK_TIME_NONE));
1741 }
1742 if (post_playing) {
1743 if (post_paused) {
1744 GstElementClass *klass;
1745
1746 klass = GST_ELEMENT_GET_CLASS (basesink);
1747 basesink->have_preroll = TRUE;
1748 /* after releasing this lock, the state change function
1749 * can execute concurrently with this thread. There is nothing we do to
1750 * prevent this for now. subclasses should be prepared to handle it. */
1751 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
1752
1753 if (klass->change_state)
1754 klass->change_state (GST_ELEMENT_CAST (basesink),
1755 GST_STATE_CHANGE_PAUSED_TO_PLAYING);
1756
1757 GST_BASE_SINK_PREROLL_LOCK (basesink);
1758 /* state change function could have been executed and we could be
1759 * flushing now */
1760 if (G_UNLIKELY (basesink->flushing))
1761 goto stopping_unlocked;
1762 }
1763 GST_DEBUG_OBJECT (basesink, "posting PLAYING state change message");
1764 /* FIXME, we released the PREROLL lock above, it's possible that this
1765 * message is not correct anymore when the element went back to PAUSED */
1766 gst_element_post_message (GST_ELEMENT_CAST (basesink),
1767 gst_message_new_state_changed (GST_OBJECT_CAST (basesink),
1768 next, pending, GST_STATE_VOID_PENDING));
1769 }
1770
1771 gst_element_post_message (GST_ELEMENT_CAST (basesink),
1772 gst_message_new_latency (GST_OBJECT_CAST (basesink)));
1773
1774 GST_STATE_BROADCAST (basesink);
1775
1776 return TRUE;
1777
1778 nothing_pending:
1779 {
1780 /* Depending on the state, set our vars. We get in this situation when the
1781 * state change function got a change to update the state vars before the
1782 * streaming thread did. This is fine but we need to make sure that we
1783 * update the need_preroll var since it was %TRUE when we got here and might
1784 * become %FALSE if we got to PLAYING. */
1785 GST_DEBUG_OBJECT (basesink, "nothing to commit, now in %s",
1786 gst_state_get_name (current));
1787 switch (current) {
1788 case GST_STATE_PLAYING:
1789 basesink->need_preroll = FALSE;
1790 break;
1791 case GST_STATE_PAUSED:
1792 basesink->need_preroll = TRUE;
1793 break;
1794 default:
1795 basesink->need_preroll = FALSE;
1796 basesink->flushing = TRUE;
1797 break;
1798 }
1799 /* we can report latency queries now */
1800 basesink->priv->have_latency = TRUE;
1801 GST_OBJECT_UNLOCK (basesink);
1802
1803 gst_element_post_message (GST_ELEMENT_CAST (basesink),
1804 gst_message_new_latency (GST_OBJECT_CAST (basesink)));
1805 return TRUE;
1806 }
1807 stopping_unlocked:
1808 {
1809 GST_OBJECT_LOCK (basesink);
1810 goto stopping;
1811 }
1812 stopping:
1813 {
1814 /* app is going to READY */
1815 GST_DEBUG_OBJECT (basesink, "stopping");
1816 basesink->need_preroll = FALSE;
1817 basesink->flushing = TRUE;
1818 GST_OBJECT_UNLOCK (basesink);
1819 return FALSE;
1820 }
1821 }
1822
1823 static void
1824 start_stepping (GstBaseSink * sink, GstSegment * segment,
1825 GstStepInfo * pending, GstStepInfo * current)
1826 {
1827 gint64 end;
1828 GstMessage *message;
1829
1830 GST_DEBUG_OBJECT (sink, "update pending step");
1831
1832 GST_OBJECT_LOCK (sink);
1833 memcpy (current, pending, sizeof (GstStepInfo));
1834 pending->valid = FALSE;
1835 GST_OBJECT_UNLOCK (sink);
1836
1837 /* post message first */
1838 message =
1839 gst_message_new_step_start (GST_OBJECT (sink), TRUE, current->format,
1840 current->amount, current->rate, current->flush, current->intermediate);
1841 gst_message_set_seqnum (message, current->seqnum);
1842 gst_element_post_message (GST_ELEMENT (sink), message);
1843
1844 /* get the running time of where we paused and remember it */
1845 current->start = gst_element_get_start_time (GST_ELEMENT_CAST (sink));
1846 gst_segment_set_running_time (segment, GST_FORMAT_TIME, current->start);
1847
1848 /* set the new rate for the remainder of the segment */
1849 current->start_rate = segment->rate;
1850 segment->rate *= current->rate;
1851
1852 /* save values */
1853 if (segment->rate > 0.0)
1854 current->start_stop = segment->stop;
1855 else
1856 current->start_start = segment->start;
1857
1858 if (current->format == GST_FORMAT_TIME) {
1859 /* calculate the running-time when the step operation should stop */
1860 if (current->amount != -1)
1861 end = current->start + current->amount;
1862 else
1863 end = -1;
1864
1865 if (!current->flush) {
1866 gint64 position;
1867
1868 /* update the segment clipping regions for non-flushing seeks */
1869 if (segment->rate > 0.0) {
1870 if (end != -1)
1871 position =
1872 gst_segment_position_from_running_time (segment, GST_FORMAT_TIME,
1873 end);
1874 else
1875 position = segment->stop;
1876
1877 segment->stop = position;
1878 segment->position = position;
1879 } else {
1880 if (end != -1)
1881 position =
1882 gst_segment_position_from_running_time (segment, GST_FORMAT_TIME,
1883 end);
1884 else
1885 position = segment->start;
1886
1887 segment->time = position;
1888 segment->start = position;
1889 segment->position = position;
1890 }
1891 }
1892 }
1893
1894 GST_DEBUG_OBJECT (sink, "segment now %" GST_SEGMENT_FORMAT, segment);
1895 GST_DEBUG_OBJECT (sink, "step started at running_time %" GST_TIME_FORMAT,
1896 GST_TIME_ARGS (current->start));
1897
1898 GST_DEBUG_OBJECT (sink, "step amount: %" G_GUINT64_FORMAT ", format: %s, "
1899 "rate: %f", current->amount, gst_format_get_name (current->format),
1900 current->rate);
1901 }
1902
1903 static void
1904 stop_stepping (GstBaseSink * sink, GstSegment * segment,
1905 GstStepInfo * current, gint64 rstart, gint64 rstop, gboolean eos)
1906 {
1907 gint64 stop, position;
1908 GstMessage *message;
1909
1910 GST_DEBUG_OBJECT (sink, "step complete");
1911
1912 if (segment->rate > 0.0)
1913 stop = rstart;
1914 else
1915 stop = rstop;
1916
1917 GST_DEBUG_OBJECT (sink,
1918 "step stop at running_time %" GST_TIME_FORMAT, GST_TIME_ARGS (stop));
1919
1920 if (stop == -1)
1921 current->duration = current->position;
1922 else
1923 current->duration = stop - current->start;
1924
1925 GST_DEBUG_OBJECT (sink, "step elapsed running_time %" GST_TIME_FORMAT,
1926 GST_TIME_ARGS (current->duration));
1927
1928 position = current->start + current->duration;
1929
1930 /* now move the segment to the new running time */
1931 gst_segment_set_running_time (segment, GST_FORMAT_TIME, position);
1932
1933 if (current->flush) {
1934 /* and remove the time we flushed, start time did not change */
1935 segment->base = current->start;
1936 } else {
1937 /* start time is now the stepped position */
1938 gst_element_set_start_time (GST_ELEMENT_CAST (sink), position);
1939 }
1940
1941 /* restore the previous rate */
1942 segment->rate = current->start_rate;
1943
1944 if (segment->rate > 0.0)
1945 segment->stop = current->start_stop;
1946 else
1947 segment->start = current->start_start;
1948
1949 /* post the step done when we know the stepped duration in TIME */
1950 message =
1951 gst_message_new_step_done (GST_OBJECT_CAST (sink), current->format,
1952 current->amount, current->rate, current->flush, current->intermediate,
1953 current->duration, eos);
1954 gst_message_set_seqnum (message, current->seqnum);
1955 gst_element_post_message (GST_ELEMENT_CAST (sink), message);
1956
1957 if (!current->intermediate)
1958 sink->need_preroll = current->need_preroll;
1959
1960 /* and the current step info finished and becomes invalid */
1961 current->valid = FALSE;
1962 }
1963
1964 static gboolean
1965 handle_stepping (GstBaseSink * sink, GstSegment * segment,
1966 GstStepInfo * current, guint64 * cstart, guint64 * cstop, guint64 * rstart,
1967 guint64 * rstop)
1968 {
1969 gboolean step_end = FALSE;
1970
1971 /* stepping never stops */
1972 if (current->amount == -1)
1973 return FALSE;
1974
1975 /* see if we need to skip this buffer because of stepping */
1976 switch (current->format) {
1977 case GST_FORMAT_TIME:
1978 {
1979 guint64 end;
1980 guint64 first, last;
1981 gdouble abs_rate;
1982
1983 if (segment->rate > 0.0) {
1984 if (segment->stop == *cstop)
1985 *rstop = *rstart + current->amount;
1986
1987 first = *rstart;
1988 last = *rstop;
1989 } else {
1990 if (segment->start == *cstart)
1991 *rstart = *rstop + current->amount;
1992
1993 first = *rstop;
1994 last = *rstart;
1995 }
1996
1997 end = current->start + current->amount;
1998 current->position = first - current->start;
1999
2000 abs_rate = ABS (segment->rate);
2001 if (G_UNLIKELY (abs_rate != 1.0))
2002 current->position /= abs_rate;
2003
2004 GST_DEBUG_OBJECT (sink,
2005 "buffer: %" GST_TIME_FORMAT "-%" GST_TIME_FORMAT,
2006 GST_TIME_ARGS (first), GST_TIME_ARGS (last));
2007 GST_DEBUG_OBJECT (sink,
2008 "got time step %" GST_TIME_FORMAT "-%" GST_TIME_FORMAT "/%"
2009 GST_TIME_FORMAT, GST_TIME_ARGS (current->position),
2010 GST_TIME_ARGS (last - current->start),
2011 GST_TIME_ARGS (current->amount));
2012
2013 if ((current->flush && current->position >= current->amount)
2014 || last >= end) {
2015 GST_DEBUG_OBJECT (sink, "step ended, we need clipping");
2016 step_end = TRUE;
2017 if (segment->rate > 0.0) {
2018 *rstart = end;
2019 *cstart =
2020 gst_segment_position_from_running_time (segment, GST_FORMAT_TIME,
2021 end);
2022 } else {
2023 *rstop = end;
2024 *cstop =
2025 gst_segment_position_from_running_time (segment, GST_FORMAT_TIME,
2026 end);
2027 }
2028 }
2029 GST_DEBUG_OBJECT (sink,
2030 "cstart %" GST_TIME_FORMAT ", rstart %" GST_TIME_FORMAT,
2031 GST_TIME_ARGS (*cstart), GST_TIME_ARGS (*rstart));
2032 GST_DEBUG_OBJECT (sink,
2033 "cstop %" GST_TIME_FORMAT ", rstop %" GST_TIME_FORMAT,
2034 GST_TIME_ARGS (*cstop), GST_TIME_ARGS (*rstop));
2035 break;
2036 }
2037 case GST_FORMAT_BUFFERS:
2038 GST_DEBUG_OBJECT (sink,
2039 "got default step %" G_GUINT64_FORMAT "/%" G_GUINT64_FORMAT,
2040 current->position, current->amount);
2041
2042 if (current->position < current->amount) {
2043 current->position++;
2044 } else {
2045 step_end = TRUE;
2046 }
2047 break;
2048 case GST_FORMAT_DEFAULT:
2049 default:
2050 GST_DEBUG_OBJECT (sink,
2051 "got unknown step %" G_GUINT64_FORMAT "/%" G_GUINT64_FORMAT,
2052 current->position, current->amount);
2053 break;
2054 }
2055 return step_end;
2056 }
2057
2058 /* with STREAM_LOCK, PREROLL_LOCK
2059 *
2060 * Returns %TRUE if the object needs synchronisation and takes therefore
2061 * part in prerolling.
2062 *
2063 * rsstart/rsstop contain the start/stop in stream time.
2064 * rrstart/rrstop contain the start/stop in running time.
2065 */
2066 static gboolean
2067 gst_base_sink_get_sync_times (GstBaseSink * basesink, GstMiniObject * obj,
2068 GstClockTime * rsstart, GstClockTime * rsstop,
2069 GstClockTime * rrstart, GstClockTime * rrstop, GstClockTime * rrnext,
2070 gboolean * do_sync, gboolean * stepped, GstStepInfo * step,
2071 gboolean * step_end)
2072 {
2073 GstBaseSinkClass *bclass;
2074 GstClockTime start, stop; /* raw start/stop timestamps */
2075 guint64 cstart, cstop; /* clipped raw timestamps */
2076 guint64 rstart, rstop, rnext; /* clipped timestamps converted to running time */
2077 GstClockTime sstart, sstop; /* clipped timestamps converted to stream time */
2078 GstFormat format;
2079 GstBaseSinkPrivate *priv;
2080 GstSegment *segment;
2081 gboolean eos;
2082
2083 priv = basesink->priv;
2084 segment = &basesink->segment;
2085
2086 bclass = GST_BASE_SINK_GET_CLASS (basesink);
2087
2088 again:
2089 /* start with nothing */
2090 start = stop = GST_CLOCK_TIME_NONE;
2091 eos = FALSE;
2092
2093 if (G_UNLIKELY (GST_IS_EVENT (obj))) {
2094 GstEvent *event = GST_EVENT_CAST (obj);
2095
2096 switch (GST_EVENT_TYPE (event)) {
2097 /* EOS event needs syncing */
2098 case GST_EVENT_EOS:
2099 {
2100 if (segment->rate >= 0.0) {
2101 sstart = sstop = priv->current_sstop;
2102 if (!GST_CLOCK_TIME_IS_VALID (sstart)) {
2103 /* we have not seen a buffer yet, use the segment values */
2104 sstart = sstop = gst_segment_to_stream_time (segment,
2105 segment->format, segment->stop);
2106 }
2107 } else {
2108 sstart = sstop = priv->current_sstart;
2109 if (!GST_CLOCK_TIME_IS_VALID (sstart)) {
2110 /* we have not seen a buffer yet, use the segment values */
2111 sstart = sstop = gst_segment_to_stream_time (segment,
2112 segment->format, segment->start);
2113 }
2114 }
2115
2116 rstart = rstop = rnext = priv->eos_rtime;
2117 *do_sync = GST_CLOCK_TIME_IS_VALID (rstart);
2118 GST_DEBUG_OBJECT (basesink, "sync times for EOS %" GST_TIME_FORMAT,
2119 GST_TIME_ARGS (rstart));
2120 /* if we are stepping, we end now */
2121 *step_end = step->valid;
2122 eos = TRUE;
2123 goto eos_done;
2124 }
2125 case GST_EVENT_GAP:
2126 {
2127 GstClockTime timestamp, duration;
2128 gst_event_parse_gap (event, &timestamp, &duration);
2129
2130 GST_DEBUG_OBJECT (basesink, "Got Gap time %" GST_TIME_FORMAT
2131 " duration %" GST_TIME_FORMAT,
2132 GST_TIME_ARGS (timestamp), GST_TIME_ARGS (duration));
2133
2134 if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
2135 start = timestamp;
2136 if (GST_CLOCK_TIME_IS_VALID (duration))
2137 stop = start + duration;
2138 }
2139 *do_sync = TRUE;
2140 break;
2141 }
2142 default:
2143 /* other events do not need syncing */
2144 return FALSE;
2145 }
2146 } else {
2147 /* else do buffer sync code */
2148 GstBuffer *buffer = GST_BUFFER_CAST (obj);
2149
2150 /* just get the times to see if we need syncing, if the retuned start is -1
2151 * we don't sync. */
2152 if (bclass->get_times)
2153 bclass->get_times (basesink, buffer, &start, &stop);
2154
2155 if (!GST_CLOCK_TIME_IS_VALID (start)) {
2156 /* we don't need to sync but we still want to get the timestamps for
2157 * tracking the position */
2158 gst_base_sink_default_get_times (basesink, buffer, &start, &stop);
2159 *do_sync = FALSE;
2160 } else {
2161 *do_sync = TRUE;
2162 }
2163 }
2164
2165 GST_DEBUG_OBJECT (basesink, "got times start: %" GST_TIME_FORMAT
2166 ", stop: %" GST_TIME_FORMAT ", do_sync %d", GST_TIME_ARGS (start),
2167 GST_TIME_ARGS (stop), *do_sync);
2168
2169 /* collect segment and format for code clarity */
2170 format = segment->format;
2171
2172 /* clip */
2173 if (G_UNLIKELY (!gst_segment_clip (segment, format,
2174 start, stop, &cstart, &cstop))) {
2175 if (step->valid) {
2176 GST_DEBUG_OBJECT (basesink, "step out of segment");
2177 /* when we are stepping, pretend we're at the end of the segment */
2178 if (segment->rate > 0.0) {
2179 cstart = segment->stop;
2180 cstop = segment->stop;
2181 } else {
2182 cstart = segment->start;
2183 cstop = segment->start;
2184 }
2185 goto do_times;
2186 }
2187 if (basesink->priv->drop_out_of_segment)
2188 goto out_of_segment;
2189
2190 cstart = start;
2191 cstop = stop;
2192 }
2193
2194 if (G_UNLIKELY (start != cstart || stop != cstop)) {
2195 GST_DEBUG_OBJECT (basesink, "clipped to: start %" GST_TIME_FORMAT
2196 ", stop: %" GST_TIME_FORMAT, GST_TIME_ARGS (cstart),
2197 GST_TIME_ARGS (cstop));
2198 }
2199
2200 /* set last stop position */
2201 if (G_LIKELY (stop != GST_CLOCK_TIME_NONE && cstop != GST_CLOCK_TIME_NONE))
2202 segment->position = cstop;
2203 else
2204 segment->position = cstart;
2205
2206 do_times:
2207 rstart = gst_segment_to_running_time (segment, format, cstart);
2208 rstop = gst_segment_to_running_time (segment, format, cstop);
2209
2210 /* In reverse playback, play from stop to start */
2211 if (segment->rate < 0.0 && GST_CLOCK_TIME_IS_VALID (rstop)
2212 /* FIXME: Current stepping implemenation expects unmodified rstart/rstop
2213 * for reverse playback. Don't swap those values when stepping
2214 * unless stepping code is updated as such */
2215 && !step->valid) {
2216 GstClockTime tmp = rstart;
2217 rstart = rstop;
2218 rstop = tmp;
2219 }
2220
2221 if (GST_CLOCK_TIME_IS_VALID (stop))
2222 rnext = rstop;
2223 else
2224 rnext = rstart;
2225
2226 if (G_UNLIKELY (step->valid)) {
2227 if (!(*step_end = handle_stepping (basesink, segment, step, &cstart, &cstop,
2228 &rstart, &rstop))) {
2229 /* step is still busy, we discard data when we are flushing */
2230 *stepped = step->flush;
2231 GST_DEBUG_OBJECT (basesink, "stepping busy");
2232 }
2233 }
2234 /* this can produce wrong values if we accumulated non-TIME segments. If this happens,
2235 * upstream is behaving very badly */
2236 sstart = gst_segment_to_stream_time (segment, format, cstart);
2237 sstop = gst_segment_to_stream_time (segment, format, cstop);
2238
2239 eos_done:
2240 /* eos_done label only called when doing EOS, we also stop stepping then */
2241 if (*step_end && step->flush) {
2242 GST_DEBUG_OBJECT (basesink, "flushing step ended");
2243 stop_stepping (basesink, segment, step, rstart, rstop, eos);
2244 *step_end = FALSE;
2245 /* re-determine running start times for adjusted segment
2246 * (which has a flushed amount of running/accumulated time removed) */
2247 if (!GST_IS_EVENT (obj)) {
2248 GST_DEBUG_OBJECT (basesink, "refresh sync times");
2249 goto again;
2250 }
2251 }
2252
2253 /* save times */
2254 *rsstart = sstart;
2255 *rsstop = sstop;
2256 *rrstart = rstart;
2257 *rrstop = rstop;
2258 *rrnext = rnext;
2259
2260 /* buffers and EOS always need syncing and preroll */
2261 return TRUE;
2262
2263 /* special cases */
2264 out_of_segment:
2265 {
2266 /* we usually clip in the chain function already but stepping could cause
2267 * the segment to be updated later. we return %FALSE so that we don't try
2268 * to sync on it. */
2269 GST_LOG_OBJECT (basesink, "buffer skipped, not in segment");
2270 return FALSE;
2271 }
2272 }
2273
2274 /* with STREAM_LOCK, PREROLL_LOCK, LOCK
2275 * adjust a timestamp with the latency and timestamp offset. This function does
2276 * not adjust for the render delay. */
2277 static GstClockTime
2278 gst_base_sink_adjust_time (GstBaseSink * basesink, GstClockTime time)
2279 {
2280 GstClockTimeDiff ts_offset;
2281
2282 /* don't do anything funny with invalid timestamps */
2283 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (time)))
2284 return time;
2285
2286 time += basesink->priv->latency;
2287
2288 /* apply offset, be careful for underflows */
2289 ts_offset = basesink->priv->ts_offset;
2290 if (ts_offset < 0) {
2291 ts_offset = -ts_offset;
2292 if (ts_offset < time)
2293 time -= ts_offset;
2294 else
2295 time = 0;
2296 } else
2297 time += ts_offset;
2298
2299 /* subtract the render delay again, which was included in the latency */
2300 if (time > basesink->priv->render_delay)
2301 time -= basesink->priv->render_delay;
2302 else
2303 time = 0;
2304
2305 return time;
2306 }
2307
2308 /**
2309 * gst_base_sink_wait_clock:
2310 * @sink: the sink
2311 * @time: the running_time to be reached
2312 * @jitter: (out) (allow-none): the jitter to be filled with time diff, or %NULL
2313 *
2314 * This function will block until @time is reached. It is usually called by
2315 * subclasses that use their own internal synchronisation.
2316 *
2317 * If @time is not valid, no synchronisation is done and %GST_CLOCK_BADTIME is
2318 * returned. Likewise, if synchronisation is disabled in the element or there
2319 * is no clock, no synchronisation is done and %GST_CLOCK_BADTIME is returned.
2320 *
2321 * This function should only be called with the PREROLL_LOCK held, like when
2322 * receiving an EOS event in the #GstBaseSinkClass::event vmethod or when
2323 * receiving a buffer in
2324 * the #GstBaseSinkClass::render vmethod.
2325 *
2326 * The @time argument should be the running_time of when this method should
2327 * return and is not adjusted with any latency or offset configured in the
2328 * sink.
2329 *
2330 * Returns: #GstClockReturn
2331 */
2332 GstClockReturn
2333 gst_base_sink_wait_clock (GstBaseSink * sink, GstClockTime time,
2334 GstClockTimeDiff * jitter)
2335 {
2336 GstClockReturn ret;
2337 GstClock *clock;
2338 GstClockTime base_time;
2339
2340 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (time)))
2341 goto invalid_time;
2342
2343 GST_OBJECT_LOCK (sink);
2344 if (G_UNLIKELY (!sink->sync))
2345 goto no_sync;
2346
2347 if (G_UNLIKELY ((clock = GST_ELEMENT_CLOCK (sink)) == NULL))
2348 goto no_clock;
2349
2350 base_time = GST_ELEMENT_CAST (sink)->base_time;
2351 GST_LOG_OBJECT (sink,
2352 "time %" GST_TIME_FORMAT ", base_time %" GST_TIME_FORMAT,
2353 GST_TIME_ARGS (time), GST_TIME_ARGS (base_time));
2354
2355 /* add base_time to running_time to get the time against the clock */
2356 time += base_time;
2357
2358 /* Re-use existing clockid if available */
2359 if (G_LIKELY (sink->priv->cached_clock_id != NULL
2360 && gst_clock_id_uses_clock (sink->priv->cached_clock_id, clock))) {
2361 if (!gst_clock_single_shot_id_reinit (clock, sink->priv->cached_clock_id,
2362 time)) {
2363 gst_clock_id_unref (sink->priv->cached_clock_id);
2364 sink->priv->cached_clock_id = gst_clock_new_single_shot_id (clock, time);
2365 }
2366 } else {
2367 if (sink->priv->cached_clock_id != NULL)
2368 gst_clock_id_unref (sink->priv->cached_clock_id);
2369 sink->priv->cached_clock_id = gst_clock_new_single_shot_id (clock, time);
2370 }
2371 GST_OBJECT_UNLOCK (sink);
2372
2373 /* A blocking wait is performed on the clock. We save the ClockID
2374 * so we can unlock the entry at any time. While we are blocking, we
2375 * release the PREROLL_LOCK so that other threads can interrupt the
2376 * entry. */
2377 sink->clock_id = sink->priv->cached_clock_id;
2378 /* release the preroll lock while waiting */
2379 GST_BASE_SINK_PREROLL_UNLOCK (sink);
2380
2381 ret = gst_clock_id_wait (sink->priv->cached_clock_id, jitter);
2382
2383 GST_BASE_SINK_PREROLL_LOCK (sink);
2384 sink->clock_id = NULL;
2385
2386 return ret;
2387
2388 /* no syncing needed */
2389 invalid_time:
2390 {
2391 GST_DEBUG_OBJECT (sink, "time not valid, no sync needed");
2392 return GST_CLOCK_BADTIME;
2393 }
2394 no_sync:
2395 {
2396 GST_DEBUG_OBJECT (sink, "sync disabled");
2397 GST_OBJECT_UNLOCK (sink);
2398 return GST_CLOCK_BADTIME;
2399 }
2400 no_clock:
2401 {
2402 GST_DEBUG_OBJECT (sink, "no clock, can't sync");
2403 GST_OBJECT_UNLOCK (sink);
2404 return GST_CLOCK_BADTIME;
2405 }
2406 }
2407
2408 /**
2409 * gst_base_sink_wait_preroll:
2410 * @sink: the sink
2411 *
2412 * If the #GstBaseSinkClass::render method performs its own synchronisation
2413 * against the clock it must unblock when going from PLAYING to the PAUSED state
2414 * and call this method before continuing to render the remaining data.
2415 *
2416 * If the #GstBaseSinkClass::render method can block on something else than
2417 * the clock, it must also be ready to unblock immediately on
2418 * the #GstBaseSinkClass::unlock method and cause the
2419 * #GstBaseSinkClass::render method to immediately call this function.
2420 * In this case, the subclass must be prepared to continue rendering where it
2421 * left off if this function returns %GST_FLOW_OK.
2422 *
2423 * This function will block until a state change to PLAYING happens (in which
2424 * case this function returns %GST_FLOW_OK) or the processing must be stopped due
2425 * to a state change to READY or a FLUSH event (in which case this function
2426 * returns %GST_FLOW_FLUSHING).
2427 *
2428 * This function should only be called with the PREROLL_LOCK held, like in the
2429 * render function.
2430 *
2431 * Returns: %GST_FLOW_OK if the preroll completed and processing can
2432 * continue. Any other return value should be returned from the render vmethod.
2433 */
2434 GstFlowReturn
2435 gst_base_sink_wait_preroll (GstBaseSink * sink)
2436 {
2437 sink->have_preroll = TRUE;
2438 GST_DEBUG_OBJECT (sink, "waiting in preroll for flush or PLAYING");
2439 /* block until the state changes, or we get a flush, or something */
2440 GST_BASE_SINK_PREROLL_WAIT (sink);
2441 sink->have_preroll = FALSE;
2442 if (G_UNLIKELY (sink->flushing))
2443 goto stopping;
2444 if (G_UNLIKELY (sink->priv->step_unlock))
2445 goto step_unlocked;
2446 GST_DEBUG_OBJECT (sink, "continue after preroll");
2447
2448 return GST_FLOW_OK;
2449
2450 /* ERRORS */
2451 stopping:
2452 {
2453 GST_DEBUG_OBJECT (sink, "preroll interrupted because of flush");
2454 return GST_FLOW_FLUSHING;
2455 }
2456 step_unlocked:
2457 {
2458 sink->priv->step_unlock = FALSE;
2459 GST_DEBUG_OBJECT (sink, "preroll interrupted because of step");
2460 return GST_FLOW_STEP;
2461 }
2462 }
2463
2464 /**
2465 * gst_base_sink_do_preroll:
2466 * @sink: the sink
2467 * @obj: (transfer none): the mini object that caused the preroll
2468 *
2469 * If the @sink spawns its own thread for pulling buffers from upstream it
2470 * should call this method after it has pulled a buffer. If the element needed
2471 * to preroll, this function will perform the preroll and will then block
2472 * until the element state is changed.
2473 *
2474 * This function should be called with the PREROLL_LOCK held.
2475 *
2476 * Returns: %GST_FLOW_OK if the preroll completed and processing can
2477 * continue. Any other return value should be returned from the render vmethod.
2478 */
2479 GstFlowReturn
2480 gst_base_sink_do_preroll (GstBaseSink * sink, GstMiniObject * obj)
2481 {
2482 GstFlowReturn ret;
2483
2484 while (G_UNLIKELY (sink->need_preroll)) {
2485 GST_DEBUG_OBJECT (sink, "prerolling object %p", obj);
2486
2487 /* if it's a buffer, we need to call the preroll method */
2488 if (sink->priv->call_preroll) {
2489 GstBaseSinkClass *bclass;
2490 GstBuffer *buf;
2491
2492 if (GST_IS_BUFFER_LIST (obj)) {
2493 buf = gst_buffer_list_get (GST_BUFFER_LIST_CAST (obj), 0);
2494 gst_base_sink_set_last_buffer (sink, buf);
2495 gst_base_sink_set_last_buffer_list (sink, GST_BUFFER_LIST_CAST (obj));
2496 g_assert (NULL != buf);
2497 } else if (GST_IS_BUFFER (obj)) {
2498 buf = GST_BUFFER_CAST (obj);
2499 /* For buffer lists do not set last buffer for now */
2500 gst_base_sink_set_last_buffer (sink, buf);
2501 gst_base_sink_set_last_buffer_list (sink, NULL);
2502 } else {
2503 buf = NULL;
2504 }
2505
2506 if (buf) {
2507 GST_DEBUG_OBJECT (sink, "preroll buffer %" GST_TIME_FORMAT,
2508 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)));
2509
2510 bclass = GST_BASE_SINK_GET_CLASS (sink);
2511
2512 if (bclass->prepare)
2513 if ((ret = bclass->prepare (sink, buf)) != GST_FLOW_OK)
2514 goto prepare_canceled;
2515
2516 if (bclass->preroll)
2517 if ((ret = bclass->preroll (sink, buf)) != GST_FLOW_OK)
2518 goto preroll_canceled;
2519
2520 sink->priv->call_preroll = FALSE;
2521 }
2522 }
2523
2524 /* commit state */
2525 if (G_LIKELY (sink->playing_async)) {
2526 if (G_UNLIKELY (!gst_base_sink_commit_state (sink)))
2527 goto stopping;
2528 }
2529
2530 /* need to recheck here because the commit state could have
2531 * made us not need the preroll anymore */
2532 if (G_LIKELY (sink->need_preroll)) {
2533 /* block until the state changes, or we get a flush, or something */
2534 ret = gst_base_sink_wait_preroll (sink);
2535 if ((ret != GST_FLOW_OK) && (ret != GST_FLOW_STEP))
2536 goto preroll_failed;
2537 }
2538 }
2539 return GST_FLOW_OK;
2540
2541 /* ERRORS */
2542 prepare_canceled:
2543 {
2544 GST_DEBUG_OBJECT (sink, "prepare failed, abort state");
2545 gst_element_abort_state (GST_ELEMENT_CAST (sink));
2546 return ret;
2547 }
2548 preroll_canceled:
2549 {
2550 GST_DEBUG_OBJECT (sink, "preroll failed, abort state");
2551 gst_element_abort_state (GST_ELEMENT_CAST (sink));
2552 return ret;
2553 }
2554 stopping:
2555 {
2556 GST_DEBUG_OBJECT (sink, "stopping while committing state");
2557 return GST_FLOW_FLUSHING;
2558 }
2559 preroll_failed:
2560 {
2561 GST_DEBUG_OBJECT (sink, "preroll failed: %s", gst_flow_get_name (ret));
2562 return ret;
2563 }
2564 }
2565
2566 /**
2567 * gst_base_sink_wait:
2568 * @sink: the sink
2569 * @time: the running_time to be reached
2570 * @jitter: (out) (allow-none): the jitter to be filled with time diff, or %NULL
2571 *
2572 * This function will wait for preroll to complete and will then block until @time
2573 * is reached. It is usually called by subclasses that use their own internal
2574 * synchronisation but want to let some synchronization (like EOS) be handled
2575 * by the base class.
2576 *
2577 * This function should only be called with the PREROLL_LOCK held (like when
2578 * receiving an EOS event in the ::event vmethod or when handling buffers in
2579 * ::render).
2580 *
2581 * The @time argument should be the running_time of when the timeout should happen
2582 * and will be adjusted with any latency and offset configured in the sink.
2583 *
2584 * Returns: #GstFlowReturn
2585 */
2586 GstFlowReturn
2587 gst_base_sink_wait (GstBaseSink * sink, GstClockTime time,
2588 GstClockTimeDiff * jitter)
2589 {
2590 GstClockReturn status;
2591 GstFlowReturn ret;
2592
2593 do {
2594 GstClockTime stime;
2595
2596 GST_DEBUG_OBJECT (sink, "checking preroll");
2597
2598 /* first wait for the playing state before we can continue */
2599 while (G_UNLIKELY (sink->need_preroll)) {
2600 ret = gst_base_sink_wait_preroll (sink);
2601 if ((ret != GST_FLOW_OK) && (ret != GST_FLOW_STEP))
2602 goto flushing;
2603 }
2604
2605 /* preroll done, we can sync since we are in PLAYING now. */
2606 GST_DEBUG_OBJECT (sink, "possibly waiting for clock to reach %"
2607 GST_TIME_FORMAT, GST_TIME_ARGS (time));
2608
2609 /* compensate for latency, ts_offset and render delay */
2610 stime = gst_base_sink_adjust_time (sink, time);
2611
2612 /* wait for the clock, this can be interrupted because we got shut down or
2613 * we PAUSED. */
2614 status = gst_base_sink_wait_clock (sink, stime, jitter);
2615
2616 GST_DEBUG_OBJECT (sink, "clock returned %d", status);
2617
2618 /* invalid time, no clock or sync disabled, just continue then */
2619 if (status == GST_CLOCK_BADTIME)
2620 break;
2621
2622 /* waiting could have been interrupted and we can be flushing now */
2623 if (G_UNLIKELY (sink->flushing))
2624 goto flushing;
2625
2626 /* retry if we got unscheduled, which means we did not reach the timeout
2627 * yet. if some other error occurs, we continue. */
2628 } while (status == GST_CLOCK_UNSCHEDULED);
2629
2630 GST_DEBUG_OBJECT (sink, "end of stream");
2631
2632 return GST_FLOW_OK;
2633
2634 /* ERRORS */
2635 flushing:
2636 {
2637 GST_DEBUG_OBJECT (sink, "we are flushing");
2638 return GST_FLOW_FLUSHING;
2639 }
2640 }
2641
2642 /* with STREAM_LOCK, PREROLL_LOCK
2643 *
2644 * Make sure we are in PLAYING and synchronize an object to the clock.
2645 *
2646 * If we need preroll, we are not in PLAYING. We try to commit the state
2647 * if needed and then block if we still are not PLAYING.
2648 *
2649 * We start waiting on the clock in PLAYING. If we got interrupted, we
2650 * immediately try to re-preroll.
2651 *
2652 * Some objects do not need synchronisation (most events) and so this function
2653 * immediately returns GST_FLOW_OK.
2654 *
2655 * for objects that arrive later than max-lateness to be synchronized to the
2656 * clock have the @late boolean set to %TRUE.
2657 *
2658 * This function keeps a running average of the jitter (the diff between the
2659 * clock time and the requested sync time). The jitter is negative for
2660 * objects that arrive in time and positive for late buffers.
2661 *
2662 * does not take ownership of obj.
2663 */
2664 static GstFlowReturn
2665 gst_base_sink_do_sync (GstBaseSink * basesink,
2666 GstMiniObject * obj, gboolean * late, gboolean * step_end)
2667 {
2668 GstClockTimeDiff jitter = 0;
2669 gboolean syncable;
2670 GstClockReturn status = GST_CLOCK_OK;
2671 GstClockTime rstart, rstop, rnext, sstart, sstop, stime;
2672 gboolean do_sync;
2673 GstBaseSinkPrivate *priv;
2674 GstFlowReturn ret;
2675 GstStepInfo *current, *pending;
2676 gboolean stepped;
2677 guint32 current_instant_rate_seqnum;
2678
2679 priv = basesink->priv;
2680
2681 /* remember the currently handled instant-rate sequence number. If this
2682 * changes after pre-rolling, we need to goto do_step again for updating
2683 * the timing information of the current buffer */
2684 current_instant_rate_seqnum = priv->instant_rate_sync_seqnum;
2685 do_step:
2686 sstart = sstop = rstart = rstop = rnext = GST_CLOCK_TIME_NONE;
2687 do_sync = TRUE;
2688 stepped = FALSE;
2689
2690 priv->current_rstart = GST_CLOCK_TIME_NONE;
2691
2692 /* get stepping info */
2693 current = &priv->current_step;
2694 pending = &priv->pending_step;
2695
2696 /* get timing information for this object against the render segment */
2697 syncable = gst_base_sink_get_sync_times (basesink, obj,
2698 &sstart, &sstop, &rstart, &rstop, &rnext, &do_sync, &stepped, current,
2699 step_end);
2700
2701 if (G_UNLIKELY (stepped))
2702 goto step_skipped;
2703
2704 /* a syncable object needs to participate in preroll and
2705 * clocking. All buffers and EOS are syncable. */
2706 if (G_UNLIKELY (!syncable))
2707 goto not_syncable;
2708
2709 /* store timing info for current object */
2710 priv->current_rstart = rstart;
2711 priv->current_rstop = (GST_CLOCK_TIME_IS_VALID (rstop) ? rstop : rstart);
2712
2713 /* save sync time for eos when the previous object needed sync */
2714 priv->eos_rtime = (do_sync ? rnext : GST_CLOCK_TIME_NONE);
2715
2716 /* calculate inter frame spacing */
2717 if (G_UNLIKELY (GST_CLOCK_TIME_IS_VALID (priv->prev_rstart) &&
2718 priv->prev_rstart < rstart)) {
2719 GstClockTime in_diff;
2720
2721 in_diff = rstart - priv->prev_rstart;
2722
2723 if (priv->avg_in_diff == -1)
2724 priv->avg_in_diff = in_diff;
2725 else
2726 priv->avg_in_diff = UPDATE_RUNNING_AVG (priv->avg_in_diff, in_diff);
2727
2728 GST_LOG_OBJECT (basesink, "avg frame diff %" GST_TIME_FORMAT,
2729 GST_TIME_ARGS (priv->avg_in_diff));
2730
2731 }
2732 priv->prev_rstart = rstart;
2733
2734 if (G_UNLIKELY (GST_CLOCK_TIME_IS_VALID (priv->earliest_in_time) &&
2735 rstart < priv->earliest_in_time))
2736 goto qos_dropped;
2737
2738 again:
2739 /* first do preroll, this makes sure we commit our state
2740 * to PAUSED and can continue to PLAYING. We cannot perform
2741 * any clock sync in PAUSED because there is no clock. */
2742 ret = gst_base_sink_do_preroll (basesink, obj);
2743 if (G_UNLIKELY (ret != GST_FLOW_OK))
2744 goto preroll_failed;
2745
2746 /* update the segment with a pending step if the current one is invalid and we
2747 * have a new pending one. We only accept new step updates after a preroll */
2748 if (G_UNLIKELY (pending->valid && !current->valid)) {
2749 start_stepping (basesink, &basesink->segment, pending, current);
2750 goto do_step;
2751 }
2752
2753 if (G_UNLIKELY (priv->instant_rate_sync_seqnum !=
2754 current_instant_rate_seqnum)) {
2755 current_instant_rate_seqnum = priv->instant_rate_sync_seqnum;
2756 // TODO rename the goto label - it does more these days.
2757 goto do_step;
2758 }
2759
2760 /* After rendering we store the position of the last buffer so that we can use
2761 * it to report the position. We need to take the lock here. */
2762 GST_OBJECT_LOCK (basesink);
2763 priv->current_sstart = sstart;
2764 priv->current_sstop = (GST_CLOCK_TIME_IS_VALID (sstop) ? sstop : sstart);
2765 GST_OBJECT_UNLOCK (basesink);
2766
2767 if (!do_sync)
2768 goto done;
2769
2770 /* adjust for latency */
2771 stime = gst_base_sink_adjust_time (basesink, rstart);
2772
2773 /* adjust for rate control */
2774 if (priv->rc_next == -1 || (stime != -1 && stime >= priv->rc_next)) {
2775 GST_DEBUG_OBJECT (basesink, "reset rc_time to time %" GST_TIME_FORMAT,
2776 GST_TIME_ARGS (stime));
2777 priv->rc_time = stime;
2778 priv->rc_accumulated = 0;
2779 } else {
2780 GST_DEBUG_OBJECT (basesink, "rate control next %" GST_TIME_FORMAT,
2781 GST_TIME_ARGS (priv->rc_next));
2782 stime = priv->rc_next;
2783 }
2784
2785 /* preroll done, we can sync since we are in PLAYING now. */
2786 GST_DEBUG_OBJECT (basesink, "possibly waiting for clock to reach %"
2787 GST_TIME_FORMAT ", adjusted %" GST_TIME_FORMAT,
2788 GST_TIME_ARGS (rstart), GST_TIME_ARGS (stime));
2789
2790 /* This function will return immediately if start == -1, no clock
2791 * or sync is disabled with GST_CLOCK_BADTIME. */
2792 status = gst_base_sink_wait_clock (basesink, stime, &jitter);
2793
2794 GST_DEBUG_OBJECT (basesink, "clock returned %d, jitter %c%" GST_TIME_FORMAT,
2795 status, (jitter < 0 ? '-' : ' '), GST_TIME_ARGS (ABS (jitter)));
2796
2797 /* invalid time, no clock or sync disabled, just render */
2798 if (status == GST_CLOCK_BADTIME)
2799 goto done;
2800
2801 /* waiting could have been interrupted and we can be flushing now */
2802 if (G_UNLIKELY (basesink->flushing))
2803 goto flushing;
2804
2805 /* check for unlocked by a state change, we are not flushing so
2806 * we can try to preroll on the current buffer. */
2807 if (G_UNLIKELY (status == GST_CLOCK_UNSCHEDULED)) {
2808 if (G_UNLIKELY (priv->instant_rate_sync_seqnum !=
2809 current_instant_rate_seqnum)) {
2810 current_instant_rate_seqnum = priv->instant_rate_sync_seqnum;
2811 // TODO rename
2812 goto do_step;
2813 }
2814
2815 GST_DEBUG_OBJECT (basesink, "unscheduled, waiting some more");
2816 priv->call_preroll = TRUE;
2817 goto again;
2818 }
2819
2820 /* successful syncing done, record observation */
2821 priv->current_jitter = jitter;
2822
2823 /* check if the object should be dropped */
2824 *late = gst_base_sink_is_too_late (basesink, obj, rstart, rstop,
2825 status, jitter, TRUE);
2826
2827 done:
2828 return GST_FLOW_OK;
2829
2830 /* ERRORS */
2831 step_skipped:
2832 {
2833 GST_DEBUG_OBJECT (basesink, "skipped stepped object %p", obj);
2834 *late = TRUE;
2835 return GST_FLOW_OK;
2836 }
2837 not_syncable:
2838 {
2839 GST_DEBUG_OBJECT (basesink, "non syncable object %p", obj);
2840 return GST_FLOW_OK;
2841 }
2842 qos_dropped:
2843 {
2844 GST_DEBUG_OBJECT (basesink, "dropped because of QoS %p", obj);
2845 *late = TRUE;
2846 return GST_FLOW_OK;
2847 }
2848 flushing:
2849 {
2850 GST_DEBUG_OBJECT (basesink, "we are flushing");
2851 return GST_FLOW_FLUSHING;
2852 }
2853 preroll_failed:
2854 {
2855 GST_DEBUG_OBJECT (basesink, "preroll failed");
2856 *step_end = FALSE;
2857 return ret;
2858 }
2859 }
2860
2861 static gboolean
2862 gst_base_sink_send_qos (GstBaseSink * basesink, GstQOSType type,
2863 gdouble proportion, GstClockTime time, GstClockTimeDiff diff)
2864 {
2865 GstEvent *event;
2866 gboolean res;
2867
2868 /* generate Quality-of-Service event */
2869 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, basesink,
2870 "qos: type %d, proportion: %lf, diff %" G_GINT64_FORMAT ", timestamp %"
2871 GST_TIME_FORMAT, type, proportion, diff, GST_TIME_ARGS (time));
2872
2873 /* Compensate for any instant-rate-change related running time offset
2874 * between upstream and the internal running time of the sink */
2875 if (basesink->priv->instant_rate_sync_seqnum != GST_SEQNUM_INVALID) {
2876 GstClockTime actual_duration;
2877 GstClockTime upstream_duration;
2878 GstClockTimeDiff difference;
2879 gboolean negative_duration;
2880
2881 GST_DEBUG_OBJECT (basesink,
2882 "Current internal running time %" GST_TIME_FORMAT
2883 ", last internal running time %" GST_TIME_FORMAT, GST_TIME_ARGS (time),
2884 GST_TIME_ARGS (basesink->priv->last_anchor_running_time));
2885
2886 /* Calculate how much running time was spent since the last switch/segment
2887 * in the "corrected upstream segment", our segment */
2888 /* Due to rounding errors and other inaccuracies, it can happen
2889 * that our calculated internal running time is before the upstream
2890 * running time. We need to compensate for that */
2891 if (time < basesink->priv->last_anchor_running_time) {
2892 actual_duration = basesink->priv->last_anchor_running_time - time;
2893 negative_duration = TRUE;
2894 } else {
2895 actual_duration = time - basesink->priv->last_anchor_running_time;
2896 negative_duration = FALSE;
2897 }
2898
2899 /* Transpose that duration (i.e. what upstream beliefs) */
2900 upstream_duration =
2901 (actual_duration * basesink->segment.rate) /
2902 basesink->priv->upstream_segment.rate;
2903
2904 /* Add the difference to the previously accumulated correction */
2905 if (negative_duration)
2906 difference = upstream_duration - actual_duration;
2907 else
2908 difference = actual_duration - upstream_duration;
2909
2910 GST_DEBUG_OBJECT (basesink,
2911 "Current instant rate correction offset. Actual duration %"
2912 GST_TIME_FORMAT ", upstream duration %" GST_TIME_FORMAT
2913 ", negative %d, difference %" GST_STIME_FORMAT ", current offset %"
2914 GST_STIME_FORMAT, GST_TIME_ARGS (actual_duration),
2915 GST_TIME_ARGS (upstream_duration), negative_duration,
2916 GST_STIME_ARGS (difference),
2917 GST_STIME_ARGS (basesink->priv->instant_rate_offset + difference));
2918
2919 difference = basesink->priv->instant_rate_offset + difference;
2920
2921 if (difference > 0 && time < difference)
2922 time = 0;
2923 else
2924 time -= difference;
2925 }
2926
2927 event = gst_event_new_qos (type, proportion, diff, time);
2928
2929 /* send upstream */
2930 res = gst_pad_push_event (basesink->sinkpad, event);
2931
2932 return res;
2933 }
2934
2935 static void
2936 gst_base_sink_perform_qos (GstBaseSink * sink, gboolean dropped)
2937 {
2938 GstBaseSinkPrivate *priv;
2939 GstClockTime start, stop;
2940 GstClockTimeDiff jitter;
2941 GstClockTime pt, entered, left;
2942 GstClockTime duration;
2943 gdouble rate;
2944
2945 priv = sink->priv;
2946
2947 start = priv->current_rstart;
2948
2949 if (priv->current_step.valid)
2950 return;
2951
2952 /* if Quality-of-Service disabled, do nothing */
2953 if (!g_atomic_int_get (&priv->qos_enabled) ||
2954 !GST_CLOCK_TIME_IS_VALID (start))
2955 return;
2956
2957 stop = priv->current_rstop;
2958 jitter = priv->current_jitter;
2959
2960 if (jitter < 0) {
2961 /* this is the time the buffer entered the sink */
2962 if (start < -jitter)
2963 entered = 0;
2964 else
2965 entered = start + jitter;
2966 left = start;
2967 } else {
2968 /* this is the time the buffer entered the sink */
2969 entered = start + jitter;
2970 /* this is the time the buffer left the sink */
2971 left = start + jitter;
2972 }
2973
2974 /* calculate duration of the buffer, only use buffer durations if not in
2975 * trick mode or key-unit mode. Otherwise the buffer durations will be
2976 * meaningless as frames are being dropped in-between without updating the
2977 * durations. */
2978 duration = priv->avg_in_diff;
2979
2980 /* if we have the time when the last buffer left us, calculate
2981 * processing time */
2982 if (GST_CLOCK_TIME_IS_VALID (priv->last_left)) {
2983 if (entered > priv->last_left) {
2984 pt = entered - priv->last_left;
2985 } else {
2986 pt = 0;
2987 }
2988 } else {
2989 pt = priv->avg_pt;
2990 }
2991
2992 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, sink, "start: %" GST_TIME_FORMAT
2993 ", stop %" GST_TIME_FORMAT ", entered %" GST_TIME_FORMAT ", left %"
2994 GST_TIME_FORMAT ", pt: %" GST_TIME_FORMAT ", duration %" GST_TIME_FORMAT
2995 ",jitter %" G_GINT64_FORMAT, GST_TIME_ARGS (start), GST_TIME_ARGS (stop),
2996 GST_TIME_ARGS (entered), GST_TIME_ARGS (left), GST_TIME_ARGS (pt),
2997 GST_TIME_ARGS (duration), jitter);
2998
2999 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, sink,
3000 "avg_pt: %" GST_TIME_FORMAT ", avg_rate: %g",
3001 GST_TIME_ARGS (priv->avg_pt), priv->avg_rate);
3002
3003 /* collect running averages. for first observations, we copy the
3004 * values */
3005 if (!GST_CLOCK_TIME_IS_VALID (priv->avg_pt))
3006 priv->avg_pt = pt;
3007 else
3008 priv->avg_pt = UPDATE_RUNNING_AVG (priv->avg_pt, pt);
3009
3010 if (duration != -1 && duration != 0) {
3011 rate =
3012 gst_guint64_to_gdouble (priv->avg_pt) /
3013 gst_guint64_to_gdouble (duration);
3014 } else {
3015 rate = 1.0;
3016 }
3017
3018 if (GST_CLOCK_TIME_IS_VALID (priv->last_left)) {
3019 if (dropped || priv->avg_rate < 0.0) {
3020 priv->avg_rate = rate;
3021 } else {
3022 if (rate > 1.0)
3023 priv->avg_rate = UPDATE_RUNNING_AVG_N (priv->avg_rate, rate);
3024 else
3025 priv->avg_rate = UPDATE_RUNNING_AVG_P (priv->avg_rate, rate);
3026 }
3027 }
3028
3029 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, sink,
3030 "updated: avg_pt: %" GST_TIME_FORMAT
3031 ", avg_rate: %g", GST_TIME_ARGS (priv->avg_pt), priv->avg_rate);
3032
3033
3034 if (priv->avg_rate >= 0.0) {
3035 GstQOSType type;
3036 GstClockTimeDiff diff;
3037
3038 /* if we have a valid rate, start sending QoS messages */
3039 if (priv->current_jitter < 0) {
3040 /* make sure we never go below 0 when adding the jitter to the
3041 * timestamp. */
3042 if (priv->current_rstart < -priv->current_jitter)
3043 priv->current_jitter = -priv->current_rstart;
3044 }
3045
3046 if (priv->throttle_time > 0) {
3047 diff = priv->throttle_time;
3048 type = GST_QOS_TYPE_THROTTLE;
3049 } else {
3050 diff = priv->current_jitter;
3051 if (diff <= 0)
3052 type = GST_QOS_TYPE_OVERFLOW;
3053 else
3054 type = GST_QOS_TYPE_UNDERFLOW;
3055 }
3056
3057 gst_base_sink_send_qos (sink, type, priv->avg_rate, priv->current_rstart,
3058 diff);
3059 }
3060
3061 /* record when this buffer will leave us */
3062 priv->last_left = left;
3063 }
3064
3065 /* reset all qos measuring */
3066 static void
3067 gst_base_sink_reset_qos (GstBaseSink * sink)
3068 {
3069 GstBaseSinkPrivate *priv;
3070
3071 priv = sink->priv;
3072
3073 priv->last_render_time = GST_CLOCK_TIME_NONE;
3074 priv->prev_rstart = GST_CLOCK_TIME_NONE;
3075 priv->earliest_in_time = GST_CLOCK_TIME_NONE;
3076 priv->last_left = GST_CLOCK_TIME_NONE;
3077 priv->avg_pt = GST_CLOCK_TIME_NONE;
3078 priv->avg_rate = -1.0;
3079 priv->avg_in_diff = GST_CLOCK_TIME_NONE;
3080 priv->rendered = 0;
3081 priv->dropped = 0;
3082
3083 }
3084
3085 /* Checks if the object was scheduled too late.
3086 *
3087 * rstart/rstop contain the running_time start and stop values
3088 * of the object.
3089 *
3090 * status and jitter contain the return values from the clock wait.
3091 *
3092 * returns %TRUE if the buffer was too late.
3093 */
3094 static gboolean
3095 gst_base_sink_is_too_late (GstBaseSink * basesink, GstMiniObject * obj,
3096 GstClockTime rstart, GstClockTime rstop,
3097 GstClockReturn status, GstClockTimeDiff jitter, gboolean render)
3098 {
3099 gboolean late;
3100 guint64 max_lateness;
3101 GstBaseSinkPrivate *priv;
3102
3103 priv = basesink->priv;
3104
3105 late = FALSE;
3106
3107 /* only for objects that were too late */
3108 if (G_LIKELY (status != GST_CLOCK_EARLY))
3109 goto in_time;
3110
3111 max_lateness = basesink->max_lateness;
3112
3113 /* check if frame dropping is enabled */
3114 if (max_lateness == -1)
3115 goto no_drop;
3116
3117 /* only check for buffers */
3118 if (G_UNLIKELY (!GST_IS_BUFFER (obj)))
3119 goto not_buffer;
3120
3121 /* can't do check if we don't have a timestamp */
3122 if (G_UNLIKELY (!GST_CLOCK_TIME_IS_VALID (rstart)))
3123 goto no_timestamp;
3124
3125 /* we can add a valid stop time */
3126 if (GST_CLOCK_TIME_IS_VALID (rstop))
3127 max_lateness += rstop;
3128 else {
3129 max_lateness += rstart;
3130 /* no stop time, use avg frame diff */
3131 if (priv->avg_in_diff != -1)
3132 max_lateness += priv->avg_in_diff;
3133 }
3134
3135 /* if the jitter bigger than duration and lateness we are too late */
3136 if ((late = rstart + jitter > max_lateness)) {
3137 GST_CAT_DEBUG_OBJECT (GST_CAT_PERFORMANCE, basesink,
3138 "buffer is too late %" GST_TIME_FORMAT
3139 " > %" GST_TIME_FORMAT, GST_TIME_ARGS (rstart + jitter),
3140 GST_TIME_ARGS (max_lateness));
3141 /* !!emergency!!, if we did not receive anything valid for more than a
3142 * second, render it anyway so the user sees something */
3143 if (GST_CLOCK_TIME_IS_VALID (priv->last_render_time) &&
3144 rstart - priv->last_render_time > GST_SECOND) {
3145 late = FALSE;
3146 GST_ELEMENT_WARNING (basesink, CORE, CLOCK,
3147 (_("A lot of buffers are being dropped.")),
3148 ("There may be a timestamping problem, or this computer is too slow."));
3149 GST_CAT_DEBUG_OBJECT (GST_CAT_PERFORMANCE, basesink,
3150 "**emergency** last buffer at %" GST_TIME_FORMAT " > GST_SECOND",
3151 GST_TIME_ARGS (priv->last_render_time));
3152 }
3153 }
3154
3155 done:
3156 if (render && (!late || !GST_CLOCK_TIME_IS_VALID (priv->last_render_time))) {
3157 priv->last_render_time = rstart;
3158 /* the next allowed input timestamp */
3159 if (priv->throttle_time > 0)
3160 priv->earliest_in_time = rstart + priv->throttle_time;
3161 }
3162 return late;
3163
3164 /* all is fine */
3165 in_time:
3166 {
3167 GST_DEBUG_OBJECT (basesink, "object was scheduled in time");
3168 goto done;
3169 }
3170 no_drop:
3171 {
3172 GST_DEBUG_OBJECT (basesink, "frame dropping disabled");
3173 goto done;
3174 }
3175 not_buffer:
3176 {
3177 GST_DEBUG_OBJECT (basesink, "object is not a buffer");
3178 return FALSE;
3179 }
3180 no_timestamp:
3181 {
3182 GST_DEBUG_OBJECT (basesink, "buffer has no timestamp");
3183 return FALSE;
3184 }
3185 }
3186
3187 static void
3188 gst_base_sink_update_start_time (GstBaseSink * basesink)
3189 {
3190 GstClock *clock;
3191
3192 GST_OBJECT_LOCK (basesink);
3193 if (GST_STATE (basesink) == GST_STATE_PLAYING
3194 && (clock = GST_ELEMENT_CLOCK (basesink))) {
3195 GstClockTime now;
3196
3197 gst_object_ref (clock);
3198 GST_OBJECT_UNLOCK (basesink);
3199
3200 /* calculate the time when we stopped */
3201 now = gst_clock_get_time (clock);
3202 gst_object_unref (clock);
3203
3204 GST_OBJECT_LOCK (basesink);
3205 /* store the current running time */
3206 if (GST_ELEMENT_START_TIME (basesink) != GST_CLOCK_TIME_NONE) {
3207 if (now != GST_CLOCK_TIME_NONE)
3208 GST_ELEMENT_START_TIME (basesink) =
3209 now - GST_ELEMENT_CAST (basesink)->base_time;
3210 else
3211 GST_WARNING_OBJECT (basesink,
3212 "Clock %s returned invalid time, can't calculate "
3213 "running_time when going to the PAUSED state",
3214 GST_OBJECT_NAME (clock));
3215 }
3216 GST_DEBUG_OBJECT (basesink,
3217 "start_time=%" GST_TIME_FORMAT ", now=%" GST_TIME_FORMAT
3218 ", base_time %" GST_TIME_FORMAT,
3219 GST_TIME_ARGS (GST_ELEMENT_START_TIME (basesink)),
3220 GST_TIME_ARGS (now),
3221 GST_TIME_ARGS (GST_ELEMENT_CAST (basesink)->base_time));
3222 }
3223 GST_OBJECT_UNLOCK (basesink);
3224 }
3225
3226 static void
3227 gst_base_sink_flush_start (GstBaseSink * basesink, GstPad * pad)
3228 {
3229 /* make sure we are not blocked on the clock also clear any pending
3230 * eos state. */
3231 gst_base_sink_set_flushing (basesink, pad, TRUE);
3232
3233 /* we grab the stream lock but that is not needed since setting the
3234 * sink to flushing would make sure no state commit is being done
3235 * anymore */
3236 GST_PAD_STREAM_LOCK (pad);
3237 gst_base_sink_reset_qos (basesink);
3238 /* and we need to commit our state again on the next
3239 * prerolled buffer */
3240 basesink->playing_async = TRUE;
3241 if (basesink->priv->async_enabled) {
3242 gst_base_sink_update_start_time (basesink);
3243 gst_element_lost_state (GST_ELEMENT_CAST (basesink));
3244 } else {
3245 /* start time reset in above case as well;
3246 * arranges for a.o. proper position reporting when flushing in PAUSED */
3247 gst_element_set_start_time (GST_ELEMENT_CAST (basesink), 0);
3248 basesink->priv->have_latency = TRUE;
3249 }
3250 gst_base_sink_set_last_buffer (basesink, NULL);
3251 gst_base_sink_set_last_buffer_list (basesink, NULL);
3252 GST_PAD_STREAM_UNLOCK (pad);
3253 }
3254
3255 static void
3256 gst_base_sink_flush_stop (GstBaseSink * basesink, GstPad * pad,
3257 gboolean reset_time)
3258 {
3259 /* unset flushing so we can accept new data, this also flushes out any EOS
3260 * event. */
3261 gst_base_sink_set_flushing (basesink, pad, FALSE);
3262
3263 /* for position reporting */
3264 GST_OBJECT_LOCK (basesink);
3265 basesink->priv->current_sstart = GST_CLOCK_TIME_NONE;
3266 basesink->priv->current_sstop = GST_CLOCK_TIME_NONE;
3267 basesink->priv->eos_rtime = GST_CLOCK_TIME_NONE;
3268 basesink->priv->call_preroll = TRUE;
3269 basesink->priv->current_step.valid = FALSE;
3270 basesink->priv->pending_step.valid = FALSE;
3271 if (basesink->pad_mode == GST_PAD_MODE_PUSH) {
3272 /* we need new segment info after the flush. */
3273 basesink->have_newsegment = FALSE;
3274 if (reset_time) {
3275 gst_segment_init (&basesink->priv->upstream_segment,
3276 GST_FORMAT_UNDEFINED);
3277 gst_segment_init (&basesink->segment, GST_FORMAT_UNDEFINED);
3278 GST_ELEMENT_START_TIME (basesink) = 0;
3279
3280 basesink->priv->last_instant_rate_seqnum = GST_SEQNUM_INVALID;
3281 basesink->priv->instant_rate_sync_seqnum = GST_SEQNUM_INVALID;
3282 basesink->priv->instant_rate_multiplier = 0;
3283 basesink->priv->instant_rate_offset = 0;
3284 basesink->priv->last_anchor_running_time = 0;
3285 }
3286 }
3287 GST_OBJECT_UNLOCK (basesink);
3288
3289 if (reset_time) {
3290 GST_DEBUG_OBJECT (basesink, "posting reset-time message");
3291 gst_element_post_message (GST_ELEMENT_CAST (basesink),
3292 gst_message_new_reset_time (GST_OBJECT_CAST (basesink), 0));
3293 }
3294 }
3295
3296 static GstFlowReturn
3297 gst_base_sink_default_wait_event (GstBaseSink * basesink, GstEvent * event)
3298 {
3299 GstFlowReturn ret;
3300 gboolean late, step_end = FALSE;
3301
3302 ret = gst_base_sink_do_sync (basesink, GST_MINI_OBJECT_CAST (event),
3303 &late, &step_end);
3304
3305 return ret;
3306 }
3307
3308 static GstFlowReturn
3309 gst_base_sink_wait_event (GstBaseSink * basesink, GstEvent * event)
3310 {
3311 GstFlowReturn ret;
3312 GstBaseSinkClass *bclass;
3313
3314 bclass = GST_BASE_SINK_GET_CLASS (basesink);
3315
3316 if (G_LIKELY (bclass->wait_event))
3317 ret = bclass->wait_event (basesink, event);
3318 else
3319 ret = GST_FLOW_NOT_SUPPORTED;
3320
3321 return ret;
3322 }
3323
3324 static gboolean
3325 gst_base_sink_default_event (GstBaseSink * basesink, GstEvent * event)
3326 {
3327 gboolean result = TRUE;
3328 GstBaseSinkClass *bclass;
3329
3330 bclass = GST_BASE_SINK_GET_CLASS (basesink);
3331
3332 switch (GST_EVENT_TYPE (event)) {
3333 case GST_EVENT_FLUSH_START:
3334 {
3335 GST_DEBUG_OBJECT (basesink, "flush-start %p", event);
3336 gst_base_sink_flush_start (basesink, basesink->sinkpad);
3337 break;
3338 }
3339 case GST_EVENT_FLUSH_STOP:
3340 {
3341 gboolean reset_time;
3342
3343 gst_event_parse_flush_stop (event, &reset_time);
3344 GST_DEBUG_OBJECT (basesink, "flush-stop %p, reset_time: %d", event,
3345 reset_time);
3346 gst_base_sink_flush_stop (basesink, basesink->sinkpad, reset_time);
3347 break;
3348 }
3349 case GST_EVENT_EOS:
3350 {
3351 GstMessage *message;
3352 guint32 seqnum;
3353
3354 /* we set the received EOS flag here so that we can use it when testing if
3355 * we are prerolled and to refuse more buffers. */
3356 basesink->priv->received_eos = TRUE;
3357
3358 /* wait for EOS */
3359 if (G_UNLIKELY (gst_base_sink_wait_event (basesink,
3360 event) != GST_FLOW_OK)) {
3361 result = FALSE;
3362 goto done;
3363 }
3364
3365 /* the EOS event is completely handled so we mark
3366 * ourselves as being in the EOS state. eos is also
3367 * protected by the object lock so we can read it when
3368 * answering the POSITION query. */
3369 GST_OBJECT_LOCK (basesink);
3370 basesink->eos = TRUE;
3371 GST_OBJECT_UNLOCK (basesink);
3372
3373 /* ok, now we can post the message */
3374 GST_DEBUG_OBJECT (basesink, "Now posting EOS");
3375
3376 seqnum = basesink->priv->seqnum = gst_event_get_seqnum (event);
3377 GST_DEBUG_OBJECT (basesink, "Got seqnum #%" G_GUINT32_FORMAT, seqnum);
3378
3379 message = gst_message_new_eos (GST_OBJECT_CAST (basesink));
3380 gst_message_set_seqnum (message, seqnum);
3381 gst_element_post_message (GST_ELEMENT_CAST (basesink), message);
3382 break;
3383 }
3384 case GST_EVENT_STREAM_START:
3385 {
3386 GstMessage *message;
3387 guint32 seqnum;
3388 guint group_id;
3389
3390 seqnum = gst_event_get_seqnum (event);
3391 GST_DEBUG_OBJECT (basesink, "Now posting STREAM_START (seqnum:%d)",
3392 seqnum);
3393 message = gst_message_new_stream_start (GST_OBJECT_CAST (basesink));
3394 if (gst_event_parse_group_id (event, &group_id)) {
3395 gst_message_set_group_id (message, group_id);
3396 } else {
3397 GST_FIXME_OBJECT (basesink, "stream-start event without group-id. "
3398 "Consider implementing group-id handling in the upstream "
3399 "elements");
3400 }
3401 gst_message_set_seqnum (message, seqnum);
3402 gst_element_post_message (GST_ELEMENT_CAST (basesink), message);
3403 break;
3404 }
3405 case GST_EVENT_CAPS:
3406 {
3407 GstCaps *caps, *current_caps;
3408
3409 GST_DEBUG_OBJECT (basesink, "caps %p", event);
3410
3411 gst_event_parse_caps (event, &caps);
3412 current_caps = gst_pad_get_current_caps (GST_BASE_SINK_PAD (basesink));
3413
3414 if (current_caps && gst_caps_is_equal (current_caps, caps)) {
3415 GST_DEBUG_OBJECT (basesink,
3416 "New caps equal to old ones: %" GST_PTR_FORMAT, caps);
3417 } else {
3418 if (bclass->set_caps)
3419 result = bclass->set_caps (basesink, caps);
3420
3421 if (result) {
3422 GST_OBJECT_LOCK (basesink);
3423 gst_caps_replace (&basesink->priv->caps, caps);
3424 GST_OBJECT_UNLOCK (basesink);
3425 }
3426 }
3427 if (current_caps)
3428 gst_caps_unref (current_caps);
3429 break;
3430 }
3431 case GST_EVENT_SEGMENT:{
3432 guint32 seqnum = gst_event_get_seqnum (event);
3433 GstSegment new_segment;
3434
3435 /* configure the segment */
3436 /* The segment is protected with both the STREAM_LOCK and the OBJECT_LOCK.
3437 * We protect with the OBJECT_LOCK so that we can use the values to
3438 * safely answer a POSITION query. */
3439 GST_OBJECT_LOCK (basesink);
3440 /* the new segment event is needed to bring the buffer timestamps to the
3441 * stream time and to drop samples outside of the playback segment. */
3442
3443 gst_event_copy_segment (event, &new_segment);
3444
3445 GST_DEBUG_OBJECT (basesink,
3446 "received upstream segment %u %" GST_SEGMENT_FORMAT, seqnum,
3447 &new_segment);
3448
3449 /* Make sure that the position stays between start and stop */
3450 new_segment.position =
3451 CLAMP (new_segment.position, new_segment.start, new_segment.stop);
3452
3453 if (basesink->priv->instant_rate_sync_seqnum != GST_SEQNUM_INVALID) {
3454 GstClockTime upstream_duration;
3455 GstClockTime actual_duration;
3456 GstClockTime new_segment_running_time;
3457 GstClockTimeDiff difference;
3458 gboolean negative_duration;
3459
3460 /* Calculate how much running time upstream believes has passed since
3461 * the last switch/segment */
3462 new_segment_running_time =
3463 gst_segment_to_running_time (&new_segment, GST_FORMAT_TIME,
3464 new_segment.position);
3465
3466 GST_DEBUG_OBJECT (basesink,
3467 "Current upstream running time %" GST_TIME_FORMAT
3468 ", last upstream running time %" GST_TIME_FORMAT,
3469 GST_TIME_ARGS (new_segment_running_time),
3470 GST_TIME_ARGS (basesink->priv->last_anchor_running_time -
3471 basesink->priv->instant_rate_offset));
3472
3473 /* Due to rounding errors and other inaccuracies, it can happen
3474 * that our calculated internal running time is before the upstream
3475 * running time. We need to compensate for that */
3476 if (new_segment_running_time <
3477 basesink->priv->last_anchor_running_time -
3478 basesink->priv->instant_rate_offset) {
3479 upstream_duration =
3480 basesink->priv->last_anchor_running_time -
3481 basesink->priv->instant_rate_offset - new_segment_running_time;
3482 negative_duration = TRUE;
3483 } else {
3484 upstream_duration =
3485 new_segment_running_time -
3486 basesink->priv->last_anchor_running_time +
3487 basesink->priv->instant_rate_offset;
3488 negative_duration = FALSE;
3489 }
3490
3491 /* Calculate the actual running-time duration of the previous segment */
3492 actual_duration =
3493 (upstream_duration * basesink->priv->instant_rate_multiplier);
3494
3495 if (negative_duration)
3496 difference = upstream_duration - actual_duration;
3497 else
3498 difference = actual_duration - upstream_duration;
3499
3500 GST_DEBUG_OBJECT (basesink,
3501 "Current internal running time %" GST_TIME_FORMAT
3502 ", last internal running time %" GST_TIME_FORMAT,
3503 GST_TIME_ARGS (new_segment_running_time +
3504 basesink->priv->instant_rate_offset + difference),
3505 GST_TIME_ARGS (basesink->priv->last_anchor_running_time));
3506
3507 /* Add the difference to the previously accumulated correction. */
3508 basesink->priv->instant_rate_offset += difference;
3509
3510 GST_DEBUG_OBJECT (basesink,
3511 "Updating instant rate correction offset. Actual duration %"
3512 GST_TIME_FORMAT ", upstream duration %" GST_TIME_FORMAT
3513 ", negative %d, difference %" GST_STIME_FORMAT ", new offset %"
3514 GST_STIME_FORMAT, GST_TIME_ARGS (actual_duration),
3515 GST_TIME_ARGS (upstream_duration),
3516 negative_duration,
3517 GST_STIME_ARGS (difference),
3518 GST_STIME_ARGS (basesink->priv->instant_rate_offset));
3519
3520 if (basesink->priv->instant_rate_offset < 0 &&
3521 new_segment_running_time < -basesink->priv->instant_rate_offset) {
3522 GST_WARNING_OBJECT (basesink,
3523 "Upstream current running time %" GST_TIME_FORMAT
3524 " is smaller than calculated offset %" GST_STIME_FORMAT,
3525 GST_TIME_ARGS (new_segment_running_time),
3526 GST_STIME_ARGS (basesink->priv->instant_rate_offset));
3527
3528 basesink->priv->last_anchor_running_time = 0;
3529 basesink->priv->instant_rate_offset = 0;
3530 } else {
3531 basesink->priv->last_anchor_running_time =
3532 new_segment_running_time + basesink->priv->instant_rate_offset;
3533 }
3534
3535 /* Update the segments from the event and with the newly calculated
3536 * correction offset */
3537 basesink->priv->upstream_segment = new_segment;
3538 basesink->segment = new_segment;
3539
3540 basesink->segment.rate *= basesink->priv->instant_rate_multiplier;
3541
3542 gst_segment_offset_running_time (&basesink->segment, GST_FORMAT_TIME,
3543 basesink->priv->instant_rate_offset);
3544
3545 GST_DEBUG_OBJECT (basesink,
3546 "Adjusted segment is now %" GST_SEGMENT_FORMAT, &basesink->segment);
3547 } else {
3548 /* otherwise both segments are simply the same, no correction needed */
3549 basesink->priv->upstream_segment = new_segment;
3550 basesink->segment = new_segment;
3551 basesink->priv->last_anchor_running_time =
3552 gst_segment_to_running_time (&new_segment, new_segment.format,
3553 new_segment.position);
3554 basesink->priv->instant_rate_offset = 0; /* Should already be 0, but to be sure */
3555 }
3556
3557 GST_DEBUG_OBJECT (basesink, "configured segment %" GST_SEGMENT_FORMAT,
3558 &basesink->segment);
3559 basesink->have_newsegment = TRUE;
3560 gst_base_sink_reset_qos (basesink);
3561 GST_OBJECT_UNLOCK (basesink);
3562 break;
3563 }
3564 case GST_EVENT_GAP:
3565 {
3566 if (G_UNLIKELY (gst_base_sink_wait_event (basesink,
3567 event) != GST_FLOW_OK))
3568 result = FALSE;
3569 break;
3570 }
3571 case GST_EVENT_TAG:
3572 {
3573 GstTagList *taglist;
3574
3575 gst_event_parse_tag (event, &taglist);
3576
3577 gst_element_post_message (GST_ELEMENT_CAST (basesink),
3578 gst_message_new_tag (GST_OBJECT_CAST (basesink),
3579 gst_tag_list_copy (taglist)));
3580 break;
3581 }
3582 case GST_EVENT_TOC:
3583 {
3584 GstToc *toc;
3585 gboolean updated;
3586
3587 gst_event_parse_toc (event, &toc, &updated);
3588
3589 gst_element_post_message (GST_ELEMENT_CAST (basesink),
3590 gst_message_new_toc (GST_OBJECT_CAST (basesink), toc, updated));
3591
3592 gst_toc_unref (toc);
3593 break;
3594 }
3595 case GST_EVENT_SINK_MESSAGE:
3596 {
3597 GstMessage *msg = NULL;
3598
3599 gst_event_parse_sink_message (event, &msg);
3600 if (msg)
3601 gst_element_post_message (GST_ELEMENT_CAST (basesink), msg);
3602 break;
3603 }
3604 case GST_EVENT_INSTANT_RATE_CHANGE:
3605 {
3606 GstMessage *msg;
3607 gdouble rate_multiplier;
3608 guint32 seqnum = gst_event_get_seqnum (event);
3609
3610 GST_OBJECT_LOCK (basesink);
3611 if (G_UNLIKELY (basesink->priv->last_instant_rate_seqnum == seqnum)) {
3612 /* Ignore repeated event */
3613 GST_LOG_OBJECT (basesink,
3614 "Ignoring repeated instant-rate-change event");
3615 GST_OBJECT_UNLOCK (basesink);
3616 break;
3617 }
3618 if (basesink->priv->instant_rate_sync_seqnum == seqnum) {
3619 /* Ignore if we already received the instant-rate-sync-time event from the pipeline */
3620 GST_LOG_OBJECT (basesink,
3621 "Ignoring instant-rate-change event for which we already received instant-rate-sync-time");
3622 GST_OBJECT_UNLOCK (basesink);
3623 break;
3624 }
3625
3626 basesink->priv->last_instant_rate_seqnum = seqnum;
3627 GST_OBJECT_UNLOCK (basesink);
3628
3629 gst_event_parse_instant_rate_change (event, &rate_multiplier, NULL);
3630
3631 msg =
3632 gst_message_new_instant_rate_request (GST_OBJECT_CAST (basesink),
3633 rate_multiplier);
3634 gst_message_set_seqnum (msg, seqnum);
3635 gst_element_post_message (GST_ELEMENT_CAST (basesink), msg);
3636
3637 break;
3638 }
3639 default:
3640 break;
3641 }
3642 done:
3643 gst_event_unref (event);
3644
3645 return result;
3646 }
3647
3648 static gboolean
3649 gst_base_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
3650 {
3651 GstBaseSink *basesink;
3652 gboolean result = TRUE;
3653 GstBaseSinkClass *bclass;
3654
3655 basesink = GST_BASE_SINK_CAST (parent);
3656 bclass = GST_BASE_SINK_GET_CLASS (basesink);
3657
3658 GST_DEBUG_OBJECT (basesink, "received event %p %" GST_PTR_FORMAT, event,
3659 event);
3660
3661 switch (GST_EVENT_TYPE (event)) {
3662 case GST_EVENT_FLUSH_STOP:
3663 /* special case for this serialized event because we don't want to grab
3664 * the PREROLL lock or check if we were flushing */
3665 if (bclass->event)
3666 result = bclass->event (basesink, event);
3667 break;
3668 case GST_EVENT_STREAM_START:
3669 basesink->priv->received_eos = FALSE;
3670 /* fallthrough */
3671 default:
3672 if (GST_EVENT_IS_SERIALIZED (event)) {
3673 GST_BASE_SINK_PREROLL_LOCK (basesink);
3674 if (G_UNLIKELY (basesink->flushing))
3675 goto flushing;
3676
3677 if (G_UNLIKELY (basesink->priv->received_eos))
3678 goto after_eos;
3679
3680 if (bclass->event)
3681 result = bclass->event (basesink, event);
3682
3683 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
3684 } else {
3685 if (bclass->event)
3686 result = bclass->event (basesink, event);
3687 }
3688 break;
3689 }
3690 done:
3691 return result;
3692
3693 /* ERRORS */
3694 flushing:
3695 {
3696 GST_DEBUG_OBJECT (basesink, "we are flushing");
3697 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
3698 gst_event_unref (event);
3699 result = FALSE;
3700 goto done;
3701 }
3702
3703 after_eos:
3704 {
3705 GST_DEBUG_OBJECT (basesink, "Event received after EOS, dropping");
3706 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
3707 gst_event_unref (event);
3708 result = FALSE;
3709 goto done;
3710 }
3711 }
3712
3713 /* default implementation to calculate the start and end
3714 * timestamps on a buffer, subclasses can override
3715 */
3716 static void
3717 gst_base_sink_default_get_times (GstBaseSink * basesink, GstBuffer * buffer,
3718 GstClockTime * start, GstClockTime * end)
3719 {
3720 GstClockTime timestamp, duration;
3721
3722 /* first sync on DTS, else use PTS */
3723 timestamp = GST_BUFFER_DTS (buffer);
3724 if (!GST_CLOCK_TIME_IS_VALID (timestamp))
3725 timestamp = GST_BUFFER_PTS (buffer);
3726
3727 if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
3728 /* get duration to calculate end time */
3729 duration = GST_BUFFER_DURATION (buffer);
3730 if (GST_CLOCK_TIME_IS_VALID (duration)) {
3731 *end = timestamp + duration;
3732 }
3733 *start = timestamp;
3734 }
3735 }
3736
3737 /* must be called with PREROLL_LOCK */
3738 static gboolean
3739 gst_base_sink_needs_preroll (GstBaseSink * basesink)
3740 {
3741 gboolean is_prerolled, res;
3742
3743 /* we have 2 cases where the PREROLL_LOCK is released:
3744 * 1) we are blocking in the PREROLL_LOCK and thus are prerolled.
3745 * 2) we are syncing on the clock
3746 */
3747 is_prerolled = basesink->have_preroll || basesink->priv->received_eos;
3748 res = !is_prerolled;
3749
3750 GST_DEBUG_OBJECT (basesink, "have_preroll: %d, EOS: %d => needs preroll: %d",
3751 basesink->have_preroll, basesink->priv->received_eos, res);
3752
3753 return res;
3754 }
3755
3756 /* with STREAM_LOCK, PREROLL_LOCK
3757 *
3758 * Takes a buffer and compare the timestamps with the last segment.
3759 * If the buffer falls outside of the segment boundaries, drop it.
3760 * Else send the buffer for preroll and rendering.
3761 *
3762 * This function takes ownership of the buffer.
3763 */
3764 static GstFlowReturn
3765 gst_base_sink_chain_unlocked (GstBaseSink * basesink, GstPad * pad,
3766 gpointer obj, gboolean is_list)
3767 {
3768 GstBaseSinkClass *bclass;
3769 GstBaseSinkPrivate *priv = basesink->priv;
3770 GstFlowReturn ret = GST_FLOW_OK;
3771 GstClockTime start = GST_CLOCK_TIME_NONE, end = GST_CLOCK_TIME_NONE;
3772 GstSegment *segment;
3773 GstBuffer *sync_buf;
3774 gboolean late, step_end, prepared = FALSE;
3775
3776 if (G_UNLIKELY (basesink->flushing))
3777 goto flushing;
3778
3779 if (G_UNLIKELY (priv->received_eos))
3780 goto was_eos;
3781
3782 if (is_list) {
3783 GstBufferList *buffer_list = GST_BUFFER_LIST_CAST (obj);
3784
3785 if (gst_buffer_list_length (buffer_list) == 0)
3786 goto empty_list;
3787
3788 sync_buf = gst_buffer_list_get (buffer_list, 0);
3789 g_assert (NULL != sync_buf);
3790 } else {
3791 sync_buf = GST_BUFFER_CAST (obj);
3792 }
3793
3794 /* for code clarity */
3795 segment = &basesink->segment;
3796
3797 if (G_UNLIKELY (!basesink->have_newsegment)) {
3798 gboolean sync;
3799
3800 sync = gst_base_sink_get_sync (basesink);
3801 if (sync) {
3802 GST_ELEMENT_WARNING (basesink, STREAM, FAILED,
3803 (_("Internal data flow problem.")),
3804 ("Received buffer without a new-segment. Assuming timestamps start from 0."));
3805 }
3806
3807 /* this means this sink will assume timestamps start from 0 */
3808 GST_OBJECT_LOCK (basesink);
3809 segment->start = 0;
3810 segment->stop = -1;
3811 basesink->segment.start = 0;
3812 basesink->segment.stop = -1;
3813 basesink->have_newsegment = TRUE;
3814 GST_OBJECT_UNLOCK (basesink);
3815 }
3816
3817 bclass = GST_BASE_SINK_GET_CLASS (basesink);
3818
3819 /* check if the buffer needs to be dropped, we first ask the subclass for the
3820 * start and end */
3821 if (bclass->get_times)
3822 bclass->get_times (basesink, sync_buf, &start, &end);
3823
3824 if (!GST_CLOCK_TIME_IS_VALID (start)) {
3825 /* if the subclass does not want sync, we use our own values so that we at
3826 * least clip the buffer to the segment */
3827 gst_base_sink_default_get_times (basesink, sync_buf, &start, &end);
3828 }
3829
3830 GST_DEBUG_OBJECT (basesink, "got times start: %" GST_TIME_FORMAT
3831 ", end: %" GST_TIME_FORMAT, GST_TIME_ARGS (start), GST_TIME_ARGS (end));
3832
3833 /* a dropped buffer does not participate in anything. Buffer can only be
3834 * dropped if their PTS falls completely outside the segment, while we sync
3835 * preferably on DTS */
3836 if (GST_CLOCK_TIME_IS_VALID (start) && (segment->format == GST_FORMAT_TIME)) {
3837 GstClockTime pts = GST_BUFFER_PTS (sync_buf);
3838 GstClockTime pts_end = GST_CLOCK_TIME_NONE;
3839
3840 if (!GST_CLOCK_TIME_IS_VALID (pts))
3841 pts = start;
3842
3843 if (GST_CLOCK_TIME_IS_VALID (end))
3844 pts_end = pts + (end - start);
3845
3846 if (G_UNLIKELY (!gst_segment_clip (segment,
3847 GST_FORMAT_TIME, pts, pts_end, NULL, NULL)
3848 && priv->drop_out_of_segment))
3849 goto out_of_segment;
3850 }
3851
3852 if (bclass->prepare || bclass->prepare_list) {
3853 gboolean do_sync = TRUE, stepped = FALSE, syncable = TRUE;
3854 GstClockTime sstart, sstop, rstart, rstop, rnext;
3855 GstStepInfo *current;
3856
3857 late = FALSE;
3858 step_end = FALSE;
3859
3860 current = &priv->current_step;
3861 syncable =
3862 gst_base_sink_get_sync_times (basesink, obj, &sstart, &sstop, &rstart,
3863 &rstop, &rnext, &do_sync, &stepped, current, &step_end);
3864
3865 if (G_UNLIKELY (stepped))
3866 goto dropped;
3867
3868 if (syncable && do_sync && gst_base_sink_get_sync (basesink)) {
3869 GstClock *clock;
3870
3871 GST_OBJECT_LOCK (basesink);
3872 clock = GST_ELEMENT_CLOCK (basesink);
3873 if (clock && GST_STATE (basesink) == GST_STATE_PLAYING) {
3874 GstClockTime base_time;
3875 GstClockTime stime;
3876 GstClockTime now;
3877
3878 base_time = GST_ELEMENT_CAST (basesink)->base_time;
3879 stime = base_time + gst_base_sink_adjust_time (basesink, rstart);
3880 now = gst_clock_get_time (clock);
3881 GST_OBJECT_UNLOCK (basesink);
3882
3883 late =
3884 gst_base_sink_is_too_late (basesink, obj, rstart, rstop,
3885 GST_CLOCK_EARLY, GST_CLOCK_DIFF (stime, now), FALSE);
3886 } else {
3887 GST_OBJECT_UNLOCK (basesink);
3888 }
3889 }
3890
3891 /* We are about to prepare the first frame, make sure we have prerolled
3892 * already. This prevent nesting prepare/render calls. */
3893 ret = gst_base_sink_do_preroll (basesink, obj);
3894 if (G_UNLIKELY (ret != GST_FLOW_OK))
3895 goto preroll_failed;
3896
3897 if (G_UNLIKELY (late))
3898 goto dropped;
3899
3900 if (!is_list) {
3901 if (bclass->prepare) {
3902 ret = bclass->prepare (basesink, GST_BUFFER_CAST (obj));
3903 if (G_UNLIKELY (ret != GST_FLOW_OK))
3904 goto prepare_failed;
3905 }
3906 } else {
3907 if (bclass->prepare_list) {
3908 ret = bclass->prepare_list (basesink, GST_BUFFER_LIST_CAST (obj));
3909 if (G_UNLIKELY (ret != GST_FLOW_OK))
3910 goto prepare_failed;
3911 }
3912 }
3913
3914 prepared = TRUE;
3915 }
3916
3917 again:
3918 late = FALSE;
3919 step_end = FALSE;
3920
3921 /* synchronize this object, non syncable objects return OK
3922 * immediately. */
3923 ret = gst_base_sink_do_sync (basesink, GST_MINI_OBJECT_CAST (sync_buf),
3924 &late, &step_end);
3925 if (G_UNLIKELY (ret != GST_FLOW_OK))
3926 goto sync_failed;
3927
3928 /* Don't skip if prepare() was called on time */
3929 late = late && !prepared;
3930
3931 /* drop late buffers unconditionally, let's hope it's unlikely */
3932 if (G_UNLIKELY (late))
3933 goto dropped;
3934
3935 if (priv->max_bitrate) {
3936 gsize size;
3937
3938 if (is_list)
3939 size = gst_buffer_list_calculate_size (GST_BUFFER_LIST_CAST (obj));
3940 else
3941 size = gst_buffer_get_size (GST_BUFFER_CAST (obj));
3942
3943 priv->rc_accumulated += size;
3944 priv->rc_next = priv->rc_time + gst_util_uint64_scale (priv->rc_accumulated,
3945 8 * GST_SECOND, priv->max_bitrate);
3946 }
3947
3948 GST_DEBUG_OBJECT (basesink, "rendering object %p", obj);
3949
3950 if (!is_list) {
3951 /* For buffer lists do not set last buffer for now. */
3952 gst_base_sink_set_last_buffer (basesink, GST_BUFFER_CAST (obj));
3953 gst_base_sink_set_last_buffer_list (basesink, NULL);
3954
3955 if (bclass->render)
3956 ret = bclass->render (basesink, GST_BUFFER_CAST (obj));
3957
3958 if (ret == GST_BASE_SINK_FLOW_DROPPED) {
3959 ret = GST_FLOW_OK;
3960 goto dropped;
3961 }
3962 } else {
3963 GstBufferList *buffer_list = GST_BUFFER_LIST_CAST (obj);
3964
3965 if (bclass->render_list)
3966 ret = bclass->render_list (basesink, buffer_list);
3967
3968 /* Set the first buffer and buffer list to be included in last sample */
3969 gst_base_sink_set_last_buffer (basesink, sync_buf);
3970 gst_base_sink_set_last_buffer_list (basesink, buffer_list);
3971
3972 /* Not currently supported */
3973 g_assert (ret != GST_BASE_SINK_FLOW_DROPPED);
3974 }
3975
3976 if (ret == GST_FLOW_STEP)
3977 goto again;
3978
3979 if (G_UNLIKELY (basesink->flushing))
3980 goto flushing;
3981
3982 priv->rendered++;
3983
3984 done:
3985 if (step_end) {
3986 /* the step ended, check if we need to activate a new step */
3987 GST_DEBUG_OBJECT (basesink, "step ended");
3988 stop_stepping (basesink, &basesink->segment, &priv->current_step,
3989 priv->current_rstart, priv->current_rstop, basesink->eos);
3990 goto again;
3991 }
3992
3993 gst_base_sink_perform_qos (basesink, late);
3994
3995 GST_DEBUG_OBJECT (basesink, "object unref after render %p", obj);
3996 gst_mini_object_unref (GST_MINI_OBJECT_CAST (obj));
3997
3998 return ret;
3999
4000 /* ERRORS */
4001 flushing:
4002 {
4003 GST_DEBUG_OBJECT (basesink, "sink is flushing");
4004 gst_mini_object_unref (GST_MINI_OBJECT_CAST (obj));
4005 return GST_FLOW_FLUSHING;
4006 }
4007 was_eos:
4008 {
4009 GST_DEBUG_OBJECT (basesink, "we are EOS, dropping object, return EOS");
4010 gst_mini_object_unref (GST_MINI_OBJECT_CAST (obj));
4011 return GST_FLOW_EOS;
4012 }
4013 empty_list:
4014 {
4015 GST_DEBUG_OBJECT (basesink, "buffer list with no buffers");
4016 gst_mini_object_unref (GST_MINI_OBJECT_CAST (obj));
4017 return GST_FLOW_OK;
4018 }
4019 out_of_segment:
4020 {
4021 GST_DEBUG_OBJECT (basesink, "dropping buffer, out of clipping segment");
4022 gst_mini_object_unref (GST_MINI_OBJECT_CAST (obj));
4023 return GST_FLOW_OK;
4024 }
4025 prepare_failed:
4026 {
4027 GST_DEBUG_OBJECT (basesink, "prepare buffer failed %s",
4028 gst_flow_get_name (ret));
4029 gst_mini_object_unref (GST_MINI_OBJECT_CAST (obj));
4030 return ret;
4031 }
4032 sync_failed:
4033 {
4034 GST_DEBUG_OBJECT (basesink, "do_sync returned %s", gst_flow_get_name (ret));
4035 goto done;
4036 }
4037 dropped:
4038 {
4039 priv->dropped++;
4040 GST_DEBUG_OBJECT (basesink, "buffer late, dropping");
4041
4042 if (g_atomic_int_get (&priv->qos_enabled)) {
4043 GstMessage *qos_msg;
4044 GstClockTime timestamp, duration;
4045
4046 timestamp = GST_BUFFER_TIMESTAMP (GST_BUFFER_CAST (sync_buf));
4047 duration = GST_BUFFER_DURATION (GST_BUFFER_CAST (sync_buf));
4048
4049 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, basesink,
4050 "qos: dropped buffer rt %" GST_TIME_FORMAT ", st %" GST_TIME_FORMAT
4051 ", ts %" GST_TIME_FORMAT ", dur %" GST_TIME_FORMAT,
4052 GST_TIME_ARGS (priv->current_rstart),
4053 GST_TIME_ARGS (priv->current_sstart), GST_TIME_ARGS (timestamp),
4054 GST_TIME_ARGS (duration));
4055 GST_CAT_DEBUG_OBJECT (GST_CAT_QOS, basesink,
4056 "qos: rendered %" G_GUINT64_FORMAT ", dropped %" G_GUINT64_FORMAT,
4057 priv->rendered, priv->dropped);
4058
4059 qos_msg =
4060 gst_message_new_qos (GST_OBJECT_CAST (basesink), basesink->sync,
4061 priv->current_rstart, priv->current_sstart, timestamp, duration);
4062 gst_message_set_qos_values (qos_msg, priv->current_jitter, priv->avg_rate,
4063 1000000);
4064 gst_message_set_qos_stats (qos_msg, GST_FORMAT_BUFFERS, priv->rendered,
4065 priv->dropped);
4066 gst_element_post_message (GST_ELEMENT_CAST (basesink), qos_msg);
4067 }
4068 goto done;
4069 }
4070 preroll_failed:
4071 {
4072 GST_DEBUG_OBJECT (basesink, "preroll failed: %s", gst_flow_get_name (ret));
4073 gst_mini_object_unref (GST_MINI_OBJECT_CAST (obj));
4074 return ret;
4075 }
4076 }
4077
4078 /* with STREAM_LOCK
4079 */
4080 static GstFlowReturn
4081 gst_base_sink_chain_main (GstBaseSink * basesink, GstPad * pad, gpointer obj,
4082 gboolean is_list)
4083 {
4084 GstFlowReturn result;
4085
4086 if (G_UNLIKELY (basesink->pad_mode != GST_PAD_MODE_PUSH))
4087 goto wrong_mode;
4088
4089 GST_BASE_SINK_PREROLL_LOCK (basesink);
4090 result = gst_base_sink_chain_unlocked (basesink, pad, obj, is_list);
4091 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
4092
4093 done:
4094 return result;
4095
4096 /* ERRORS */
4097 wrong_mode:
4098 {
4099 GST_OBJECT_LOCK (pad);
4100 GST_WARNING_OBJECT (basesink,
4101 "Push on pad %s:%s, but it was not activated in push mode",
4102 GST_DEBUG_PAD_NAME (pad));
4103 GST_OBJECT_UNLOCK (pad);
4104 gst_mini_object_unref (GST_MINI_OBJECT_CAST (obj));
4105 /* we don't post an error message this will signal to the peer
4106 * pushing that EOS is reached. */
4107 result = GST_FLOW_EOS;
4108 goto done;
4109 }
4110 }
4111
4112 static GstFlowReturn
4113 gst_base_sink_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
4114 {
4115 GstBaseSink *basesink;
4116
4117 basesink = GST_BASE_SINK (parent);
4118
4119 return gst_base_sink_chain_main (basesink, pad, buf, FALSE);
4120 }
4121
4122 static GstFlowReturn
4123 gst_base_sink_chain_list (GstPad * pad, GstObject * parent,
4124 GstBufferList * list)
4125 {
4126 GstBaseSink *basesink;
4127 GstBaseSinkClass *bclass;
4128 GstFlowReturn result;
4129
4130 basesink = GST_BASE_SINK (parent);
4131 bclass = GST_BASE_SINK_GET_CLASS (basesink);
4132
4133 if (G_LIKELY (bclass->render_list)) {
4134 result = gst_base_sink_chain_main (basesink, pad, list, TRUE);
4135 } else {
4136 guint i, len;
4137 GstBuffer *buffer;
4138
4139 GST_LOG_OBJECT (pad, "chaining each buffer in list");
4140
4141 len = gst_buffer_list_length (list);
4142
4143 result = GST_FLOW_OK;
4144 for (i = 0; i < len; i++) {
4145 buffer = gst_buffer_list_get (list, i);
4146 result = gst_base_sink_chain_main (basesink, pad,
4147 gst_buffer_ref (buffer), FALSE);
4148 if (result != GST_FLOW_OK)
4149 break;
4150 }
4151 gst_buffer_list_unref (list);
4152 }
4153 return result;
4154 }
4155
4156
4157 static gboolean
4158 gst_base_sink_default_do_seek (GstBaseSink * sink, GstSegment * segment)
4159 {
4160 gboolean res = TRUE;
4161
4162 /* update our offset if the start/stop position was updated */
4163 if (segment->format == GST_FORMAT_BYTES) {
4164 segment->time = segment->start;
4165 } else if (segment->start == 0) {
4166 /* seek to start, we can implement a default for this. */
4167 segment->time = 0;
4168 } else {
4169 res = FALSE;
4170 GST_INFO_OBJECT (sink, "Can't do a default seek");
4171 }
4172
4173 return res;
4174 }
4175
4176 #define SEEK_TYPE_IS_RELATIVE(t) (((t) != GST_SEEK_TYPE_NONE) && ((t) != GST_SEEK_TYPE_SET))
4177
4178 static gboolean
4179 gst_base_sink_default_prepare_seek_segment (GstBaseSink * sink,
4180 GstEvent * event, GstSegment * segment)
4181 {
4182 /* By default, we try one of 2 things:
4183 * - For absolute seek positions, convert the requested position to our
4184 * configured processing format and place it in the output segment \
4185 * - For relative seek positions, convert our current (input) values to the
4186 * seek format, adjust by the relative seek offset and then convert back to
4187 * the processing format
4188 */
4189 GstSeekType start_type, stop_type;
4190 gint64 start, stop;
4191 GstSeekFlags flags;
4192 GstFormat seek_format;
4193 gdouble rate;
4194 gboolean update;
4195 gboolean res = TRUE;
4196
4197 gst_event_parse_seek (event, &rate, &seek_format, &flags,
4198 &start_type, &start, &stop_type, &stop);
4199
4200 if (seek_format == segment->format) {
4201 gst_segment_do_seek (segment, rate, seek_format, flags,
4202 start_type, start, stop_type, stop, &update);
4203 return TRUE;
4204 }
4205
4206 if (start_type != GST_SEEK_TYPE_NONE) {
4207 /* FIXME: Handle seek_end by converting the input segment vals */
4208 res =
4209 gst_pad_query_convert (sink->sinkpad, seek_format, start,
4210 segment->format, &start);
4211 start_type = GST_SEEK_TYPE_SET;
4212 }
4213
4214 if (res && stop_type != GST_SEEK_TYPE_NONE) {
4215 /* FIXME: Handle seek_end by converting the input segment vals */
4216 res =
4217 gst_pad_query_convert (sink->sinkpad, seek_format, stop,
4218 segment->format, &stop);
4219 stop_type = GST_SEEK_TYPE_SET;
4220 }
4221
4222 /* And finally, configure our output segment in the desired format */
4223 gst_segment_do_seek (segment, rate, segment->format, flags, start_type, start,
4224 stop_type, stop, &update);
4225
4226 if (!res)
4227 goto no_format;
4228
4229 return res;
4230
4231 no_format:
4232 {
4233 GST_DEBUG_OBJECT (sink, "undefined format given, seek aborted.");
4234 return FALSE;
4235 }
4236 }
4237
4238 /* perform a seek, only executed in pull mode */
4239 static gboolean
4240 gst_base_sink_perform_seek (GstBaseSink * sink, GstPad * pad, GstEvent * event)
4241 {
4242 gboolean flush;
4243 gdouble rate;
4244 GstFormat seek_format, dest_format;
4245 GstSeekFlags flags;
4246 GstSeekType start_type, stop_type;
4247 gboolean seekseg_configured = FALSE;
4248 gint64 start, stop;
4249 gboolean update, res = TRUE;
4250 GstSegment seeksegment;
4251
4252 dest_format = sink->segment.format;
4253
4254 if (event) {
4255 GST_DEBUG_OBJECT (sink, "performing seek with event %p", event);
4256 gst_event_parse_seek (event, &rate, &seek_format, &flags,
4257 &start_type, &start, &stop_type, &stop);
4258
4259 flush = flags & GST_SEEK_FLAG_FLUSH;
4260 } else {
4261 GST_DEBUG_OBJECT (sink, "performing seek without event");
4262 flush = FALSE;
4263 }
4264
4265 if (flush) {
4266 GST_DEBUG_OBJECT (sink, "flushing upstream");
4267 gst_pad_push_event (pad, gst_event_new_flush_start ());
4268 gst_base_sink_flush_start (sink, pad);
4269 } else {
4270 GST_DEBUG_OBJECT (sink, "pausing pulling thread");
4271 }
4272
4273 GST_PAD_STREAM_LOCK (pad);
4274
4275 /* If we configured the seeksegment above, don't overwrite it now. Otherwise
4276 * copy the current segment info into the temp segment that we can actually
4277 * attempt the seek with. We only update the real segment if the seek succeeds. */
4278 if (!seekseg_configured) {
4279 memcpy (&seeksegment, &sink->segment, sizeof (GstSegment));
4280
4281 /* now configure the final seek segment */
4282 if (event) {
4283 if (sink->segment.format != seek_format) {
4284 /* OK, here's where we give the subclass a chance to convert the relative
4285 * seek into an absolute one in the processing format. We set up any
4286 * absolute seek above, before taking the stream lock. */
4287 if (!gst_base_sink_default_prepare_seek_segment (sink, event,
4288 &seeksegment)) {
4289 GST_DEBUG_OBJECT (sink,
4290 "Preparing the seek failed after flushing. " "Aborting seek");
4291 res = FALSE;
4292 }
4293 } else {
4294 /* The seek format matches our processing format, no need to ask the
4295 * the subclass to configure the segment. */
4296 gst_segment_do_seek (&seeksegment, rate, seek_format, flags,
4297 start_type, start, stop_type, stop, &update);
4298 }
4299 }
4300 /* Else, no seek event passed, so we're just (re)starting the
4301 current segment. */
4302 }
4303
4304 if (res) {
4305 GST_DEBUG_OBJECT (sink, "segment configured from %" G_GINT64_FORMAT
4306 " to %" G_GINT64_FORMAT ", position %" G_GINT64_FORMAT,
4307 seeksegment.start, seeksegment.stop, seeksegment.position);
4308
4309 /* do the seek, segment.position contains the new position. */
4310 res = gst_base_sink_default_do_seek (sink, &seeksegment);
4311 }
4312
4313 if (flush) {
4314 GST_DEBUG_OBJECT (sink, "stop flushing upstream");
4315 gst_pad_push_event (pad, gst_event_new_flush_stop (TRUE));
4316 gst_base_sink_flush_stop (sink, pad, TRUE);
4317 } else if (res && sink->running) {
4318 /* we are running the current segment and doing a non-flushing seek,
4319 * close the segment first based on the position. */
4320 GST_DEBUG_OBJECT (sink, "closing running segment %" G_GINT64_FORMAT
4321 " to %" G_GINT64_FORMAT, sink->segment.start, sink->segment.position);
4322 }
4323
4324 /* The subclass must have converted the segment to the processing format
4325 * by now */
4326 if (res && seeksegment.format != dest_format) {
4327 GST_DEBUG_OBJECT (sink, "Subclass failed to prepare a seek segment "
4328 "in the correct format. Aborting seek.");
4329 res = FALSE;
4330 }
4331
4332 GST_INFO_OBJECT (sink, "seeking done %d: %" GST_SEGMENT_FORMAT, res,
4333 &seeksegment);
4334
4335 /* if successful seek, we update our real segment and push
4336 * out the new segment. */
4337 if (res) {
4338 gst_segment_copy_into (&seeksegment, &sink->segment);
4339
4340 if (sink->segment.flags & GST_SEGMENT_FLAG_SEGMENT) {
4341 gst_element_post_message (GST_ELEMENT (sink),
4342 gst_message_new_segment_start (GST_OBJECT (sink),
4343 sink->segment.format, sink->segment.position));
4344 }
4345 }
4346
4347 sink->priv->discont = TRUE;
4348 sink->running = TRUE;
4349
4350 GST_PAD_STREAM_UNLOCK (pad);
4351
4352 return res;
4353 }
4354
4355 static void
4356 set_step_info (GstBaseSink * sink, GstStepInfo * current, GstStepInfo * pending,
4357 guint seqnum, GstFormat format, guint64 amount, gdouble rate,
4358 gboolean flush, gboolean intermediate)
4359 {
4360 GST_OBJECT_LOCK (sink);
4361 pending->seqnum = seqnum;
4362 pending->format = format;
4363 pending->amount = amount;
4364 pending->position = 0;
4365 pending->rate = rate;
4366 pending->flush = flush;
4367 pending->intermediate = intermediate;
4368 pending->valid = TRUE;
4369 /* flush invalidates the current stepping segment */
4370 if (flush)
4371 current->valid = FALSE;
4372 GST_OBJECT_UNLOCK (sink);
4373 }
4374
4375 static gboolean
4376 gst_base_sink_perform_step (GstBaseSink * sink, GstPad * pad, GstEvent * event)
4377 {
4378 GstBaseSinkPrivate *priv;
4379 GstBaseSinkClass *bclass;
4380 gboolean flush, intermediate;
4381 gdouble rate;
4382 GstFormat format;
4383 guint64 amount;
4384 guint seqnum;
4385 GstStepInfo *pending, *current;
4386 GstMessage *message;
4387
4388 bclass = GST_BASE_SINK_GET_CLASS (sink);
4389 priv = sink->priv;
4390
4391 GST_DEBUG_OBJECT (sink, "performing step with event %p", event);
4392
4393 gst_event_parse_step (event, &format, &amount, &rate, &flush, &intermediate);
4394 seqnum = gst_event_get_seqnum (event);
4395
4396 pending = &priv->pending_step;
4397 current = &priv->current_step;
4398
4399 /* post message first */
4400 message = gst_message_new_step_start (GST_OBJECT (sink), FALSE, format,
4401 amount, rate, flush, intermediate);
4402 gst_message_set_seqnum (message, seqnum);
4403 gst_element_post_message (GST_ELEMENT (sink), message);
4404
4405 if (flush) {
4406 /* we need to call ::unlock before locking PREROLL_LOCK
4407 * since we lock it before going into ::render */
4408 if (bclass->unlock)
4409 bclass->unlock (sink);
4410
4411 GST_BASE_SINK_PREROLL_LOCK (sink);
4412 /* now that we have the PREROLL lock, clear our unlock request */
4413 if (bclass->unlock_stop)
4414 bclass->unlock_stop (sink);
4415
4416 /* update the stepinfo and make it valid */
4417 set_step_info (sink, current, pending, seqnum, format, amount, rate, flush,
4418 intermediate);
4419
4420 if (sink->priv->async_enabled) {
4421 /* and we need to commit our state again on the next
4422 * prerolled buffer */
4423 sink->playing_async = TRUE;
4424 priv->pending_step.need_preroll = TRUE;
4425 sink->need_preroll = FALSE;
4426 gst_base_sink_update_start_time (sink);
4427 gst_element_lost_state (GST_ELEMENT_CAST (sink));
4428 } else {
4429 sink->priv->have_latency = TRUE;
4430 sink->need_preroll = FALSE;
4431 }
4432 priv->current_sstart = GST_CLOCK_TIME_NONE;
4433 priv->current_sstop = GST_CLOCK_TIME_NONE;
4434 priv->eos_rtime = GST_CLOCK_TIME_NONE;
4435 priv->call_preroll = TRUE;
4436 gst_base_sink_set_last_buffer (sink, NULL);
4437 gst_base_sink_set_last_buffer_list (sink, NULL);
4438 gst_base_sink_reset_qos (sink);
4439
4440 if (sink->clock_id) {
4441 gst_clock_id_unschedule (sink->clock_id);
4442 }
4443
4444 if (sink->have_preroll) {
4445 GST_DEBUG_OBJECT (sink, "signal waiter");
4446 priv->step_unlock = TRUE;
4447 GST_BASE_SINK_PREROLL_SIGNAL (sink);
4448 }
4449 GST_BASE_SINK_PREROLL_UNLOCK (sink);
4450 } else {
4451 /* update the stepinfo and make it valid */
4452 set_step_info (sink, current, pending, seqnum, format, amount, rate, flush,
4453 intermediate);
4454 }
4455
4456 return TRUE;
4457 }
4458
4459 static gboolean
4460 gst_base_sink_perform_instant_rate_change (GstBaseSink * sink, GstPad * pad,
4461 GstEvent * event)
4462 {
4463 GstBaseSinkPrivate *priv;
4464 guint32 seqnum;
4465 gdouble rate;
4466 GstClockTime running_time, upstream_running_time;
4467
4468 GstClockTime switch_time;
4469 gint res;
4470
4471 priv = sink->priv;
4472
4473 GST_DEBUG_OBJECT (sink, "performing instant-rate-change with event %p",
4474 event);
4475
4476 seqnum = gst_event_get_seqnum (event);
4477 gst_event_parse_instant_rate_sync_time (event, &rate, &running_time,
4478 &upstream_running_time);
4479
4480 GST_DEBUG_OBJECT (sink, "instant-rate-change %u %lf at %" GST_TIME_FORMAT
4481 ", upstream %" GST_TIME_FORMAT,
4482 seqnum, rate, GST_TIME_ARGS (running_time),
4483 GST_TIME_ARGS (upstream_running_time));
4484
4485 /* Take the preroll lock so we can change the segment. We do not call unlock
4486 * like for stepping as that would cause the PLAYING state to be lost and
4487 * would get us into prerolling again first
4488 *
4489 * FIXME: The below potentially blocks until the chain function returns, but
4490 * the lock is not taken during all waiting operations inside the chain
4491 * function (clock, preroll) so this should be fine in most cases. Only
4492 * problem is if the render() or prepare() functions are waiting themselves!
4493 *
4494 * FIXME: If the subclass is calling gst_base_sink_wait() it will be woken
4495 * up but there is no way for it to update the timestamps, or to report back
4496 * to the base class that it should recalculate the values. The current
4497 * change would not be instantaneous in that case but would wait until the
4498 * next buffer.
4499 */
4500 GST_BASE_SINK_PREROLL_LOCK (sink);
4501
4502 /* We can safely change the segment and everything here as we hold the
4503 * PREROLL_LOCK and it is taken for the whole chain function */
4504 sink->priv->instant_rate_sync_seqnum = seqnum;
4505 sink->priv->instant_rate_multiplier = rate;
4506 sink->priv->instant_rate_offset = running_time - upstream_running_time;
4507 sink->priv->last_anchor_running_time = running_time;
4508
4509 GST_DEBUG_OBJECT (sink, "Current internal running time %" GST_TIME_FORMAT
4510 ", last internal running time %" GST_TIME_FORMAT,
4511 GST_TIME_ARGS (running_time),
4512 GST_TIME_ARGS (sink->priv->last_anchor_running_time));
4513
4514 /* Calculate the current position in the segment and do a seek with the
4515 * new rate. This updates rate, base and offset accordingly */
4516 res =
4517 gst_segment_position_from_running_time_full (&sink->segment,
4518 GST_FORMAT_TIME, running_time, &switch_time);
4519
4520 GST_DEBUG_OBJECT (sink, "Before adjustment seg is %" GST_SEGMENT_FORMAT
4521 " new running_time %" GST_TIME_FORMAT
4522 " position %" GST_STIME_FORMAT " res %d", &sink->segment,
4523 GST_TIME_ARGS (running_time),
4524 GST_STIME_ARGS ((GstClockTimeDiff) switch_time), res);
4525
4526 if (res < 0) {
4527 GST_WARNING_OBJECT (sink,
4528 "Negative position calculated. Can't instant-rate change to there");
4529 GST_BASE_SINK_PREROLL_UNLOCK (sink);
4530 return TRUE;
4531 }
4532
4533 sink->segment.position = switch_time;
4534
4535 /* Calculate new output rate based on upstream value */
4536 rate *= sink->priv->upstream_segment.rate;
4537
4538 gst_segment_do_seek (&sink->segment, rate, GST_FORMAT_TIME,
4539 sink->segment.flags & (~GST_SEEK_FLAG_FLUSH) &
4540 GST_SEEK_FLAG_INSTANT_RATE_CHANGE, GST_SEEK_TYPE_NONE, -1,
4541 GST_SEEK_TYPE_NONE, -1, NULL);
4542
4543 GST_DEBUG_OBJECT (sink, "Adjusted segment is now %" GST_SEGMENT_FORMAT,
4544 &sink->segment);
4545
4546 priv->current_sstart = GST_CLOCK_TIME_NONE;
4547 priv->current_sstop = GST_CLOCK_TIME_NONE;
4548 priv->eos_rtime = GST_CLOCK_TIME_NONE;
4549 gst_base_sink_reset_qos (sink);
4550
4551 if (sink->clock_id) {
4552 gst_clock_id_unschedule (sink->clock_id);
4553 }
4554
4555 if (sink->have_preroll) {
4556 GST_DEBUG_OBJECT (sink, "signal waiter");
4557 /* TODO: Rename this, and GST_FLOW_STEP */
4558 priv->step_unlock = TRUE;
4559 GST_BASE_SINK_PREROLL_SIGNAL (sink);
4560 }
4561
4562 GST_BASE_SINK_PREROLL_UNLOCK (sink);
4563
4564 return TRUE;
4565 }
4566
4567 /* with STREAM_LOCK
4568 */
4569 static void
4570 gst_base_sink_loop (GstPad * pad)
4571 {
4572 GstObject *parent;
4573 GstBaseSink *basesink;
4574 GstBuffer *buf = NULL;
4575 GstFlowReturn result;
4576 guint blocksize;
4577 guint64 offset;
4578
4579 parent = GST_OBJECT_PARENT (pad);
4580 basesink = GST_BASE_SINK (parent);
4581
4582 g_assert (basesink->pad_mode == GST_PAD_MODE_PULL);
4583
4584 if ((blocksize = basesink->priv->blocksize) == 0)
4585 blocksize = -1;
4586
4587 offset = basesink->segment.position;
4588
4589 GST_DEBUG_OBJECT (basesink, "pulling %" G_GUINT64_FORMAT ", %u",
4590 offset, blocksize);
4591
4592 result = gst_pad_pull_range (pad, offset, blocksize, &buf);
4593 if (G_UNLIKELY (result != GST_FLOW_OK))
4594 goto paused;
4595
4596 if (G_UNLIKELY (buf == NULL))
4597 goto no_buffer;
4598
4599 offset += gst_buffer_get_size (buf);
4600
4601 basesink->segment.position = offset;
4602
4603 GST_BASE_SINK_PREROLL_LOCK (basesink);
4604 result = gst_base_sink_chain_unlocked (basesink, pad, buf, FALSE);
4605 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
4606 if (G_UNLIKELY (result != GST_FLOW_OK))
4607 goto paused;
4608
4609 return;
4610
4611 /* ERRORS */
4612 paused:
4613 {
4614 GST_LOG_OBJECT (basesink, "pausing task, reason %s",
4615 gst_flow_get_name (result));
4616 gst_pad_pause_task (pad);
4617 if (result == GST_FLOW_EOS) {
4618 /* perform EOS logic */
4619 if (basesink->segment.flags & GST_SEGMENT_FLAG_SEGMENT) {
4620 gst_element_post_message (GST_ELEMENT_CAST (basesink),
4621 gst_message_new_segment_done (GST_OBJECT_CAST (basesink),
4622 basesink->segment.format, basesink->segment.position));
4623 gst_base_sink_event (pad, parent,
4624 gst_event_new_segment_done (basesink->segment.format,
4625 basesink->segment.position));
4626 } else {
4627 gst_base_sink_event (pad, parent, gst_event_new_eos ());
4628 }
4629 } else if (result == GST_FLOW_NOT_LINKED || result <= GST_FLOW_EOS) {
4630 /* for fatal errors we post an error message, post the error
4631 * first so the app knows about the error first.
4632 * wrong-state is not a fatal error because it happens due to
4633 * flushing and posting an error message in that case is the
4634 * wrong thing to do, e.g. when basesrc is doing a flushing
4635 * seek. */
4636 GST_ELEMENT_FLOW_ERROR (basesink, result);
4637 gst_base_sink_event (pad, parent, gst_event_new_eos ());
4638 }
4639 return;
4640 }
4641 no_buffer:
4642 {
4643 GST_LOG_OBJECT (basesink, "no buffer, pausing");
4644 GST_ELEMENT_ERROR (basesink, STREAM, FAILED,
4645 (_("Internal data flow error.")), ("element returned NULL buffer"));
4646 result = GST_FLOW_ERROR;
4647 goto paused;
4648 }
4649 }
4650
4651 static gboolean
4652 gst_base_sink_set_flushing (GstBaseSink * basesink, GstPad * pad,
4653 gboolean flushing)
4654 {
4655 GstBaseSinkClass *bclass;
4656
4657 bclass = GST_BASE_SINK_GET_CLASS (basesink);
4658
4659 if (flushing) {
4660 /* unlock any subclasses, we need to do this before grabbing the
4661 * PREROLL_LOCK since we hold this lock before going into ::render. */
4662 if (bclass->unlock)
4663 bclass->unlock (basesink);
4664 }
4665
4666 GST_BASE_SINK_PREROLL_LOCK (basesink);
4667 basesink->flushing = flushing;
4668 if (flushing) {
4669 /* step 1, now that we have the PREROLL lock, clear our unlock request */
4670 if (bclass->unlock_stop)
4671 bclass->unlock_stop (basesink);
4672
4673 /* set need_preroll before we unblock the clock. If the clock is unblocked
4674 * before timing out, we can reuse the buffer for preroll. */
4675 basesink->need_preroll = TRUE;
4676
4677 /* step 2, unblock clock sync (if any) or any other blocking thing */
4678 if (basesink->clock_id) {
4679 gst_clock_id_unschedule (basesink->clock_id);
4680 }
4681
4682 /* flush out the data thread if it's locked in finish_preroll, this will
4683 * also flush out the EOS state */
4684 GST_DEBUG_OBJECT (basesink,
4685 "flushing out data thread, need preroll to TRUE");
4686
4687 /* we can't have EOS anymore now */
4688 basesink->eos = FALSE;
4689 basesink->priv->received_eos = FALSE;
4690 basesink->have_preroll = FALSE;
4691 basesink->priv->step_unlock = FALSE;
4692 /* can't report latency anymore until we preroll again */
4693 if (basesink->priv->async_enabled) {
4694 GST_OBJECT_LOCK (basesink);
4695 basesink->priv->have_latency = FALSE;
4696 GST_OBJECT_UNLOCK (basesink);
4697 }
4698 /* and signal any waiters now */
4699 GST_BASE_SINK_PREROLL_SIGNAL (basesink);
4700 }
4701 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
4702
4703 return TRUE;
4704 }
4705
4706 static gboolean
4707 gst_base_sink_default_activate_pull (GstBaseSink * basesink, gboolean active)
4708 {
4709 gboolean result;
4710
4711 if (active) {
4712 /* start task */
4713 result = gst_pad_start_task (basesink->sinkpad,
4714 (GstTaskFunction) gst_base_sink_loop, basesink->sinkpad, NULL);
4715 } else {
4716 /* step 2, make sure streaming finishes */
4717 result = gst_pad_stop_task (basesink->sinkpad);
4718 }
4719
4720 return result;
4721 }
4722
4723 static gboolean
4724 gst_base_sink_pad_activate (GstPad * pad, GstObject * parent)
4725 {
4726 gboolean result = FALSE;
4727 GstBaseSink *basesink;
4728 GstQuery *query;
4729 gboolean pull_mode;
4730
4731 basesink = GST_BASE_SINK (parent);
4732
4733 GST_DEBUG_OBJECT (basesink, "Trying pull mode first");
4734
4735 gst_base_sink_set_flushing (basesink, pad, FALSE);
4736
4737 /* we need to have the pull mode enabled */
4738 if (!basesink->can_activate_pull) {
4739 GST_DEBUG_OBJECT (basesink, "pull mode disabled");
4740 goto fallback;
4741 }
4742
4743 /* check if downstreams supports pull mode at all */
4744 query = gst_query_new_scheduling ();
4745
4746 if (!gst_pad_peer_query (pad, query)) {
4747 gst_query_unref (query);
4748 GST_DEBUG_OBJECT (basesink, "peer query failed, no pull mode");
4749 goto fallback;
4750 }
4751
4752 /* parse result of the query */
4753 pull_mode = gst_query_has_scheduling_mode (query, GST_PAD_MODE_PULL);
4754 gst_query_unref (query);
4755
4756 if (!pull_mode) {
4757 GST_DEBUG_OBJECT (basesink, "pull mode not supported");
4758 goto fallback;
4759 }
4760
4761 /* set the pad mode before starting the task so that it's in the
4762 * correct state for the new thread. also the sink set_caps and get_caps
4763 * function checks this */
4764 basesink->pad_mode = GST_PAD_MODE_PULL;
4765
4766 /* we first try to negotiate a format so that when we try to activate
4767 * downstream, it knows about our format */
4768 if (!gst_base_sink_negotiate_pull (basesink)) {
4769 GST_DEBUG_OBJECT (basesink, "failed to negotiate in pull mode");
4770 goto fallback;
4771 }
4772
4773 /* ok activate now */
4774 if (!gst_pad_activate_mode (pad, GST_PAD_MODE_PULL, TRUE)) {
4775 /* clear any pending caps */
4776 GST_OBJECT_LOCK (basesink);
4777 gst_caps_replace (&basesink->priv->caps, NULL);
4778 GST_OBJECT_UNLOCK (basesink);
4779 GST_DEBUG_OBJECT (basesink, "failed to activate in pull mode");
4780 goto fallback;
4781 }
4782
4783 GST_DEBUG_OBJECT (basesink, "Success activating pull mode");
4784 result = TRUE;
4785 goto done;
4786
4787 /* push mode fallback */
4788 fallback:
4789 GST_DEBUG_OBJECT (basesink, "Falling back to push mode");
4790 if ((result = gst_pad_activate_mode (pad, GST_PAD_MODE_PUSH, TRUE))) {
4791 GST_DEBUG_OBJECT (basesink, "Success activating push mode");
4792 }
4793
4794 done:
4795 if (!result) {
4796 GST_WARNING_OBJECT (basesink, "Could not activate pad in either mode");
4797 gst_base_sink_set_flushing (basesink, pad, TRUE);
4798 }
4799
4800 return result;
4801 }
4802
4803 static gboolean
4804 gst_base_sink_pad_activate_push (GstPad * pad, GstObject * parent,
4805 gboolean active)
4806 {
4807 gboolean result;
4808 GstBaseSink *basesink;
4809
4810 basesink = GST_BASE_SINK (parent);
4811
4812 if (active) {
4813 if (!basesink->can_activate_push) {
4814 result = FALSE;
4815 basesink->pad_mode = GST_PAD_MODE_NONE;
4816 } else {
4817 result = TRUE;
4818 basesink->pad_mode = GST_PAD_MODE_PUSH;
4819 }
4820 } else {
4821 if (G_UNLIKELY (basesink->pad_mode != GST_PAD_MODE_PUSH)) {
4822 g_warning ("Internal GStreamer activation error!!!");
4823 result = FALSE;
4824 } else {
4825 gst_base_sink_set_flushing (basesink, pad, TRUE);
4826 result = TRUE;
4827 basesink->pad_mode = GST_PAD_MODE_NONE;
4828 }
4829 }
4830
4831 return result;
4832 }
4833
4834 static gboolean
4835 gst_base_sink_negotiate_pull (GstBaseSink * basesink)
4836 {
4837 GstCaps *caps;
4838 gboolean result;
4839
4840 result = FALSE;
4841
4842 /* this returns the intersection between our caps and the peer caps. If there
4843 * is no peer, it returns %NULL and we can't operate in pull mode so we can
4844 * fail the negotiation. */
4845 caps = gst_pad_get_allowed_caps (GST_BASE_SINK_PAD (basesink));
4846 if (caps == NULL || gst_caps_is_empty (caps))
4847 goto no_caps_possible;
4848
4849 GST_DEBUG_OBJECT (basesink, "allowed caps: %" GST_PTR_FORMAT, caps);
4850
4851 if (gst_caps_is_any (caps)) {
4852 GST_DEBUG_OBJECT (basesink, "caps were ANY after fixating, "
4853 "allowing pull()");
4854 /* neither side has template caps in this case, so they are prepared for
4855 pull() without setcaps() */
4856 result = TRUE;
4857 } else {
4858 /* try to fixate */
4859 caps = gst_base_sink_fixate (basesink, caps);
4860 GST_DEBUG_OBJECT (basesink, "fixated to: %" GST_PTR_FORMAT, caps);
4861
4862 if (gst_caps_is_fixed (caps)) {
4863 if (!gst_pad_set_caps (GST_BASE_SINK_PAD (basesink), caps))
4864 goto could_not_set_caps;
4865
4866 result = TRUE;
4867 }
4868 }
4869
4870 gst_caps_unref (caps);
4871
4872 return result;
4873
4874 no_caps_possible:
4875 {
4876 GST_INFO_OBJECT (basesink, "Pipeline could not agree on caps");
4877 GST_DEBUG_OBJECT (basesink, "get_allowed_caps() returned EMPTY");
4878 if (caps)
4879 gst_caps_unref (caps);
4880 return FALSE;
4881 }
4882 could_not_set_caps:
4883 {
4884 GST_INFO_OBJECT (basesink, "Could not set caps: %" GST_PTR_FORMAT, caps);
4885 gst_caps_unref (caps);
4886 return FALSE;
4887 }
4888 }
4889
4890 /* this won't get called until we implement an activate function */
4891 static gboolean
4892 gst_base_sink_pad_activate_pull (GstPad * pad, GstObject * parent,
4893 gboolean active)
4894 {
4895 gboolean result = FALSE;
4896 GstBaseSink *basesink;
4897 GstBaseSinkClass *bclass;
4898
4899 basesink = GST_BASE_SINK (parent);
4900 bclass = GST_BASE_SINK_GET_CLASS (basesink);
4901
4902 if (active) {
4903 gint64 duration;
4904
4905 /* we mark we have a newsegment here because pull based
4906 * mode works just fine without having a newsegment before the
4907 * first buffer */
4908 gst_segment_init (&basesink->segment, GST_FORMAT_BYTES);
4909 GST_OBJECT_LOCK (basesink);
4910 basesink->have_newsegment = TRUE;
4911 GST_OBJECT_UNLOCK (basesink);
4912
4913 /* get the peer duration in bytes */
4914 result = gst_pad_peer_query_duration (pad, GST_FORMAT_BYTES, &duration);
4915 if (result) {
4916 GST_DEBUG_OBJECT (basesink,
4917 "setting duration in bytes to %" G_GINT64_FORMAT, duration);
4918 basesink->segment.duration = duration;
4919 } else {
4920 GST_DEBUG_OBJECT (basesink, "unknown duration");
4921 }
4922
4923 if (bclass->activate_pull)
4924 result = bclass->activate_pull (basesink, TRUE);
4925 else
4926 result = FALSE;
4927
4928 if (!result)
4929 goto activate_failed;
4930
4931 } else {
4932 if (G_UNLIKELY (basesink->pad_mode != GST_PAD_MODE_PULL)) {
4933 g_warning ("Internal GStreamer activation error!!!");
4934 result = FALSE;
4935 } else {
4936 result = gst_base_sink_set_flushing (basesink, pad, TRUE);
4937 if (bclass->activate_pull)
4938 result &= bclass->activate_pull (basesink, FALSE);
4939 basesink->pad_mode = GST_PAD_MODE_NONE;
4940 }
4941 }
4942
4943 return result;
4944
4945 /* ERRORS */
4946 activate_failed:
4947 {
4948 /* reset, as starting the thread failed */
4949 basesink->pad_mode = GST_PAD_MODE_NONE;
4950
4951 GST_ERROR_OBJECT (basesink, "subclass failed to activate in pull mode");
4952 return FALSE;
4953 }
4954 }
4955
4956 static gboolean
4957 gst_base_sink_pad_activate_mode (GstPad * pad, GstObject * parent,
4958 GstPadMode mode, gboolean active)
4959 {
4960 gboolean res;
4961
4962 switch (mode) {
4963 case GST_PAD_MODE_PULL:
4964 res = gst_base_sink_pad_activate_pull (pad, parent, active);
4965 break;
4966 case GST_PAD_MODE_PUSH:
4967 res = gst_base_sink_pad_activate_push (pad, parent, active);
4968 break;
4969 default:
4970 GST_LOG_OBJECT (pad, "unknown activation mode %d", mode);
4971 res = FALSE;
4972 break;
4973 }
4974 return res;
4975 }
4976
4977 /* send an event to our sinkpad peer. */
4978 static gboolean
4979 gst_base_sink_send_event (GstElement * element, GstEvent * event)
4980 {
4981 GstPad *pad;
4982 GstBaseSink *basesink = GST_BASE_SINK (element);
4983 gboolean forward, result = TRUE;
4984 GstPadMode mode;
4985
4986 GST_OBJECT_LOCK (element);
4987 /* get the pad and the scheduling mode */
4988 pad = gst_object_ref (basesink->sinkpad);
4989 mode = basesink->pad_mode;
4990 GST_OBJECT_UNLOCK (element);
4991
4992 /* only push UPSTREAM events upstream */
4993 forward = GST_EVENT_IS_UPSTREAM (event);
4994
4995 GST_DEBUG_OBJECT (basesink, "handling event %p %" GST_PTR_FORMAT, event,
4996 event);
4997
4998 switch (GST_EVENT_TYPE (event)) {
4999 case GST_EVENT_LATENCY:
5000 {
5001 GstClockTime latency;
5002
5003 gst_event_parse_latency (event, &latency);
5004
5005 /* store the latency. We use this to adjust the running_time before syncing
5006 * it to the clock. */
5007 GST_OBJECT_LOCK (element);
5008 basesink->priv->latency = latency;
5009 if (!basesink->priv->have_latency)
5010 forward = FALSE;
5011 GST_OBJECT_UNLOCK (element);
5012 GST_DEBUG_OBJECT (basesink, "latency set to %" GST_TIME_FORMAT,
5013 GST_TIME_ARGS (latency));
5014
5015 /* We forward this event so that all elements know about the global pipeline
5016 * latency. This is interesting for an element when it wants to figure out
5017 * when a particular piece of data will be rendered. */
5018 break;
5019 }
5020 case GST_EVENT_INSTANT_RATE_SYNC_TIME:
5021 {
5022 gst_base_sink_perform_instant_rate_change (basesink, pad, event);
5023
5024 /* Forward the event. If upstream handles it already, it is supposed to
5025 * send a SEGMENT event with the same seqnum and the final rate before
5026 * the next buffer
5027 */
5028 forward = TRUE;
5029
5030 break;
5031 }
5032 case GST_EVENT_SEEK:
5033 /* in pull mode we will execute the seek */
5034 if (mode == GST_PAD_MODE_PULL)
5035 result = gst_base_sink_perform_seek (basesink, pad, event);
5036 break;
5037 case GST_EVENT_STEP:
5038 result = gst_base_sink_perform_step (basesink, pad, event);
5039 forward = FALSE;
5040 break;
5041 default:
5042 break;
5043 }
5044
5045 if (forward) {
5046 GST_DEBUG_OBJECT (basesink, "sending event %p %" GST_PTR_FORMAT, event,
5047 event);
5048
5049 /* Compensate for any instant-rate-change related running time offset
5050 * between upstream and the internal running time of the sink */
5051 if (basesink->priv->instant_rate_sync_seqnum != GST_SEQNUM_INVALID) {
5052 GstClockTime now = GST_CLOCK_TIME_NONE;
5053 GstClockTime actual_duration;
5054 GstClockTime upstream_duration;
5055 GstClockTimeDiff difference;
5056 gboolean is_playing, negative_duration;
5057
5058 GST_OBJECT_LOCK (basesink);
5059 is_playing = GST_STATE (basesink) == GST_STATE_PLAYING
5060 && (GST_STATE_PENDING (basesink) == GST_STATE_VOID_PENDING ||
5061 GST_STATE_PENDING (basesink) == GST_STATE_PLAYING);
5062
5063 if (is_playing) {
5064 GstClockTime base_time, clock_time;
5065 GstClock *clock;
5066
5067 base_time = GST_ELEMENT_CAST (basesink)->base_time;
5068 clock = GST_ELEMENT_CLOCK (basesink);
5069 GST_OBJECT_UNLOCK (basesink);
5070
5071 if (clock) {
5072 clock_time = gst_clock_get_time (clock);
5073 now = clock_time - base_time;
5074 }
5075 } else {
5076 now = GST_ELEMENT_START_TIME (basesink);
5077 GST_OBJECT_UNLOCK (basesink);
5078 }
5079
5080 GST_DEBUG_OBJECT (basesink,
5081 "Current internal running time %" GST_TIME_FORMAT
5082 ", last internal running time %" GST_TIME_FORMAT, GST_TIME_ARGS (now),
5083 GST_TIME_ARGS (basesink->priv->last_anchor_running_time));
5084
5085 if (now != GST_CLOCK_TIME_NONE) {
5086 /* Calculate how much running time was spent since the last switch/segment
5087 * in the "corrected upstream segment", our segment */
5088 /* Due to rounding errors and other inaccuracies, it can happen
5089 * that our calculated internal running time is before the upstream
5090 * running time. We need to compensate for that */
5091 if (now < basesink->priv->last_anchor_running_time) {
5092 actual_duration = basesink->priv->last_anchor_running_time - now;
5093 negative_duration = TRUE;
5094 } else {
5095 actual_duration = now - basesink->priv->last_anchor_running_time;
5096 negative_duration = FALSE;
5097 }
5098
5099 /* Transpose that duration (i.e. what upstream beliefs) */
5100 upstream_duration =
5101 (actual_duration * basesink->segment.rate) /
5102 basesink->priv->upstream_segment.rate;
5103
5104 /* Add the difference to the previously accumulated correction */
5105 if (negative_duration)
5106 difference = upstream_duration - actual_duration;
5107 else
5108 difference = actual_duration - upstream_duration;
5109
5110 GST_DEBUG_OBJECT (basesink,
5111 "Current instant rate correction offset. Actual duration %"
5112 GST_TIME_FORMAT ", upstream duration %" GST_TIME_FORMAT
5113 ", negative %d, difference %" GST_STIME_FORMAT ", current offset %"
5114 GST_STIME_FORMAT, GST_TIME_ARGS (actual_duration),
5115 GST_TIME_ARGS (upstream_duration), negative_duration,
5116 GST_STIME_ARGS (difference),
5117 GST_STIME_ARGS (basesink->priv->instant_rate_offset + difference));
5118
5119 difference = basesink->priv->instant_rate_offset + difference;
5120
5121 event = gst_event_make_writable (event);
5122 gst_event_set_running_time_offset (event, -difference);
5123 }
5124 }
5125
5126 result = gst_pad_push_event (pad, event);
5127 } else {
5128 /* not forwarded, unref the event */
5129 gst_event_unref (event);
5130 }
5131
5132 gst_object_unref (pad);
5133
5134 GST_DEBUG_OBJECT (basesink, "handled event: %d", result);
5135
5136 return result;
5137 }
5138
5139 static gboolean
5140 gst_base_sink_get_position (GstBaseSink * basesink, GstFormat format,
5141 gint64 * cur, gboolean * upstream)
5142 {
5143 GstClock *clock = NULL;
5144 gboolean res = FALSE;
5145 GstFormat oformat;
5146 GstSegment *segment;
5147 GstClockTime now, latency;
5148 GstClockTimeDiff base_time;
5149 gint64 time, base, offset, duration;
5150 gdouble rate;
5151 gint64 last;
5152 gboolean last_seen, with_clock, in_paused;
5153
5154 GST_OBJECT_LOCK (basesink);
5155 /* we can only get the segment when we are not NULL or READY */
5156 if (GST_STATE (basesink) <= GST_STATE_READY &&
5157 GST_STATE_PENDING (basesink) <= GST_STATE_READY) {
5158 goto wrong_state;
5159 }
5160
5161 segment = &basesink->segment;
5162 /* get the format in the segment */
5163 oformat = segment->format;
5164
5165 if (oformat == GST_FORMAT_UNDEFINED)
5166 goto no_segment;
5167
5168 in_paused = FALSE;
5169 /* when not in PLAYING or when we're busy with a state change, we
5170 * cannot read from the clock so we report time based on the
5171 * last seen timestamp. */
5172 if (GST_STATE (basesink) != GST_STATE_PLAYING ||
5173 GST_STATE_PENDING (basesink) != GST_STATE_VOID_PENDING) {
5174 in_paused = TRUE;
5175 }
5176
5177 /* report with last seen position when EOS */
5178 last_seen = basesink->eos;
5179
5180 /* assume we will use the clock for getting the current position */
5181 with_clock = TRUE;
5182 if (!basesink->sync)
5183 with_clock = FALSE;
5184
5185 /* and we need a clock */
5186 if (G_UNLIKELY ((clock = GST_ELEMENT_CLOCK (basesink)) == NULL))
5187 with_clock = FALSE;
5188 else
5189 gst_object_ref (clock);
5190
5191 /* mainloop might be querying position when going to playing async,
5192 * while (audio) rendering might be quickly advancing stream position,
5193 * so use clock asap rather than last reported position */
5194 if (in_paused && with_clock && g_atomic_int_get (&basesink->priv->to_playing)) {
5195 GST_DEBUG_OBJECT (basesink, "going to PLAYING, so not PAUSED");
5196 in_paused = FALSE;
5197 }
5198
5199 /* collect all data we need holding the lock */
5200 if (GST_CLOCK_TIME_IS_VALID (segment->time))
5201 time = segment->time;
5202 else
5203 time = 0;
5204
5205 if (GST_CLOCK_TIME_IS_VALID (segment->offset))
5206 offset = segment->offset;
5207 else
5208 offset = 0;
5209
5210 if (GST_CLOCK_TIME_IS_VALID (segment->stop))
5211 duration = segment->stop - segment->start;
5212 else
5213 duration = 0;
5214
5215 base = segment->base;
5216 rate = segment->rate * segment->applied_rate;
5217 latency = basesink->priv->latency;
5218
5219 if (in_paused) {
5220 /* in paused, use start_time */
5221 base_time = GST_ELEMENT_START_TIME (basesink);
5222 GST_DEBUG_OBJECT (basesink, "in paused, using start time %" GST_TIME_FORMAT,
5223 GST_TIME_ARGS (base_time));
5224 } else if (with_clock) {
5225 /* else use clock when needed */
5226 base_time = GST_ELEMENT_CAST (basesink)->base_time;
5227 GST_DEBUG_OBJECT (basesink, "using clock and base time %" GST_TIME_FORMAT,
5228 GST_TIME_ARGS (base_time));
5229 } else {
5230 /* else, no sync or clock -> no base time */
5231 GST_DEBUG_OBJECT (basesink, "no sync or no clock");
5232 base_time = -1;
5233 }
5234
5235 /* no base_time, we can't calculate running_time, use last seem timestamp to report
5236 * time */
5237 if (base_time == -1)
5238 last_seen = TRUE;
5239
5240 if (oformat == GST_FORMAT_TIME) {
5241 gint64 start, stop;
5242
5243 start = basesink->priv->current_sstart;
5244 stop = basesink->priv->current_sstop;
5245
5246 if (last_seen) {
5247 /* when we don't use the clock, we use the last position as a lower bound */
5248 if (stop == -1 || segment->rate > 0.0)
5249 last = start;
5250 else
5251 last = stop;
5252
5253 GST_DEBUG_OBJECT (basesink, "in PAUSED using last %" GST_TIME_FORMAT,
5254 GST_TIME_ARGS (last));
5255 } else {
5256 /* in playing and paused, use last stop time as upper bound */
5257 if (start == -1 || segment->rate > 0.0)
5258 last = stop;
5259 else
5260 last = start;
5261
5262 GST_DEBUG_OBJECT (basesink, "in PLAYING using last %" GST_TIME_FORMAT,
5263 GST_TIME_ARGS (last));
5264 }
5265 } else {
5266 /* convert position to stream time */
5267 last = gst_segment_to_stream_time (segment, oformat, segment->position);
5268
5269 GST_DEBUG_OBJECT (basesink, "in using last %" G_GINT64_FORMAT, last);
5270 }
5271
5272 /* need to release the object lock before we can get the time,
5273 * a clock might take the LOCK of the provider, which could be
5274 * a basesink subclass. */
5275 GST_OBJECT_UNLOCK (basesink);
5276
5277 if (last_seen) {
5278 /* in EOS or when no valid stream_time, report the value of last seen
5279 * timestamp */
5280 if (last == -1) {
5281 /* no timestamp, we need to ask upstream */
5282 GST_DEBUG_OBJECT (basesink, "no last seen timestamp, asking upstream");
5283 res = FALSE;
5284 *upstream = TRUE;
5285 goto done;
5286 }
5287 GST_DEBUG_OBJECT (basesink, "using last seen timestamp %" GST_TIME_FORMAT,
5288 GST_TIME_ARGS (last));
5289 *cur = last;
5290 } else {
5291 if (oformat != GST_FORMAT_TIME) {
5292 /* convert base, time and duration to time */
5293 if (!gst_pad_query_convert (basesink->sinkpad, oformat, base,
5294 GST_FORMAT_TIME, &base))
5295 goto convert_failed;
5296 if (!gst_pad_query_convert (basesink->sinkpad, oformat, duration,
5297 GST_FORMAT_TIME, &duration))
5298 goto convert_failed;
5299 if (!gst_pad_query_convert (basesink->sinkpad, oformat, time,
5300 GST_FORMAT_TIME, &time))
5301 goto convert_failed;
5302 if (!gst_pad_query_convert (basesink->sinkpad, oformat, last,
5303 GST_FORMAT_TIME, &last))
5304 goto convert_failed;
5305
5306 /* assume time format from now on */
5307 oformat = GST_FORMAT_TIME;
5308 }
5309
5310 if (!in_paused && with_clock) {
5311 now = gst_clock_get_time (clock);
5312 } else {
5313 now = base_time;
5314 base_time = 0;
5315 }
5316
5317 /* subtract base time and base time from the clock time.
5318 * Make sure we don't go negative. This is the current time in
5319 * the segment which we need to scale with the combined
5320 * rate and applied rate. */
5321 base_time += base;
5322 base_time += latency;
5323 if (GST_CLOCK_DIFF (base_time, now) < 0)
5324 base_time = now;
5325
5326 /* for negative rates we need to count back from the segment
5327 * duration. */
5328 if (rate < 0.0)
5329 time += duration;
5330
5331 *cur = time + offset + gst_guint64_to_gdouble (now - base_time) * rate;
5332
5333 /* never report more than last seen position */
5334 if (last != -1) {
5335 if (rate > 0.0)
5336 *cur = MIN (last, *cur);
5337 else
5338 *cur = MAX (last, *cur);
5339 }
5340
5341 GST_DEBUG_OBJECT (basesink,
5342 "now %" GST_TIME_FORMAT " - base_time %" GST_TIME_FORMAT " - base %"
5343 GST_TIME_FORMAT " + time %" GST_TIME_FORMAT " last %" GST_TIME_FORMAT,
5344 GST_TIME_ARGS (now), GST_TIME_ARGS (base_time), GST_TIME_ARGS (base),
5345 GST_TIME_ARGS (time), GST_TIME_ARGS (last));
5346 }
5347
5348 if (oformat != format) {
5349 /* convert to final format */
5350 if (!gst_pad_query_convert (basesink->sinkpad, oformat, *cur, format, cur))
5351 goto convert_failed;
5352 }
5353
5354 res = TRUE;
5355
5356 done:
5357 GST_DEBUG_OBJECT (basesink, "res: %d, POSITION: %" GST_TIME_FORMAT,
5358 res, GST_TIME_ARGS (*cur));
5359
5360 if (clock)
5361 gst_object_unref (clock);
5362
5363 return res;
5364
5365 /* special cases */
5366 wrong_state:
5367 {
5368 /* in NULL or READY we always return FALSE and -1 */
5369 GST_DEBUG_OBJECT (basesink, "position in wrong state, return -1");
5370 res = FALSE;
5371 *cur = -1;
5372 GST_OBJECT_UNLOCK (basesink);
5373 goto done;
5374 }
5375 no_segment:
5376 {
5377 GST_DEBUG_OBJECT (basesink,
5378 "haven't received a segment yet, can't anwser position, return -1");
5379 res = FALSE;
5380 *cur = -1;
5381 GST_OBJECT_UNLOCK (basesink);
5382 goto done;
5383 }
5384 convert_failed:
5385 {
5386 GST_DEBUG_OBJECT (basesink, "convert failed, try upstream");
5387 *upstream = TRUE;
5388 res = FALSE;
5389 goto done;
5390 }
5391 }
5392
5393 static gboolean
5394 gst_base_sink_get_duration (GstBaseSink * basesink, GstFormat format,
5395 gint64 * dur, gboolean * upstream)
5396 {
5397 gboolean res = FALSE;
5398
5399 if (basesink->pad_mode == GST_PAD_MODE_PULL) {
5400 gint64 uduration;
5401
5402 /* get the duration in bytes, in pull mode that's all we are sure to
5403 * know. We have to explicitly get this value from upstream instead of
5404 * using our cached value because it might change. Duration caching
5405 * should be done at a higher level. */
5406 res =
5407 gst_pad_peer_query_duration (basesink->sinkpad, GST_FORMAT_BYTES,
5408 &uduration);
5409 if (res) {
5410 basesink->segment.duration = uduration;
5411 if (format != GST_FORMAT_BYTES) {
5412 /* convert to the requested format */
5413 res =
5414 gst_pad_query_convert (basesink->sinkpad, GST_FORMAT_BYTES,
5415 uduration, format, dur);
5416 } else {
5417 *dur = uduration;
5418 }
5419 }
5420 *upstream = FALSE;
5421 } else {
5422 *upstream = TRUE;
5423 }
5424
5425 return res;
5426 }
5427
5428 static gboolean
5429 default_element_query (GstElement * element, GstQuery * query)
5430 {
5431 gboolean res = FALSE;
5432
5433 GstBaseSink *basesink = GST_BASE_SINK (element);
5434
5435 switch (GST_QUERY_TYPE (query)) {
5436 case GST_QUERY_POSITION:
5437 {
5438 gint64 cur = 0;
5439 GstFormat format;
5440 gboolean upstream = FALSE;
5441
5442 gst_query_parse_position (query, &format, NULL);
5443
5444 GST_DEBUG_OBJECT (basesink, "position query in format %s",
5445 gst_format_get_name (format));
5446
5447 /* first try to get the position based on the clock */
5448 if ((res =
5449 gst_base_sink_get_position (basesink, format, &cur, &upstream))) {
5450 gst_query_set_position (query, format, cur);
5451 } else if (upstream) {
5452 /* fallback to peer query */
5453 res = gst_pad_peer_query (basesink->sinkpad, query);
5454 }
5455 if (!res) {
5456 /* we can handle a few things if upstream failed */
5457 if (format == GST_FORMAT_PERCENT) {
5458 gint64 dur = 0;
5459
5460 res = gst_base_sink_get_position (basesink, GST_FORMAT_TIME, &cur,
5461 &upstream);
5462 if (!res && upstream) {
5463 res =
5464 gst_pad_peer_query_position (basesink->sinkpad, GST_FORMAT_TIME,
5465 &cur);
5466 }
5467 if (res) {
5468 res = gst_base_sink_get_duration (basesink, GST_FORMAT_TIME, &dur,
5469 &upstream);
5470 if (!res && upstream) {
5471 res =
5472 gst_pad_peer_query_duration (basesink->sinkpad,
5473 GST_FORMAT_TIME, &dur);
5474 }
5475 }
5476 if (res) {
5477 gint64 pos;
5478
5479 pos = gst_util_uint64_scale (100 * GST_FORMAT_PERCENT_SCALE, cur,
5480 dur);
5481 gst_query_set_position (query, GST_FORMAT_PERCENT, pos);
5482 }
5483 }
5484 }
5485 break;
5486 }
5487 case GST_QUERY_DURATION:
5488 {
5489 gint64 dur = 0;
5490 GstFormat format;
5491 gboolean upstream = FALSE;
5492
5493 gst_query_parse_duration (query, &format, NULL);
5494
5495 GST_DEBUG_OBJECT (basesink, "duration query in format %s",
5496 gst_format_get_name (format));
5497
5498 if ((res =
5499 gst_base_sink_get_duration (basesink, format, &dur, &upstream))) {
5500 gst_query_set_duration (query, format, dur);
5501 } else if (upstream) {
5502 /* fallback to peer query */
5503 res = gst_pad_peer_query (basesink->sinkpad, query);
5504 }
5505 if (!res) {
5506 /* we can handle a few things if upstream failed */
5507 if (format == GST_FORMAT_PERCENT) {
5508 gst_query_set_duration (query, GST_FORMAT_PERCENT,
5509 GST_FORMAT_PERCENT_MAX);
5510 res = TRUE;
5511 }
5512 }
5513 break;
5514 }
5515 case GST_QUERY_LATENCY:
5516 {
5517 gboolean live, us_live;
5518 GstClockTime min, max;
5519
5520 if ((res = gst_base_sink_query_latency (basesink, &live, &us_live, &min,
5521 &max))) {
5522 gst_query_set_latency (query, live, min, max);
5523 }
5524 break;
5525 }
5526 case GST_QUERY_JITTER:
5527 break;
5528 case GST_QUERY_RATE:
5529 /* gst_query_set_rate (query, basesink->segment_rate); */
5530 res = TRUE;
5531 break;
5532 case GST_QUERY_SEGMENT:
5533 {
5534 if (basesink->pad_mode == GST_PAD_MODE_PULL) {
5535 GstFormat format;
5536 gint64 start, stop;
5537
5538 format = basesink->segment.format;
5539
5540 start =
5541 gst_segment_to_stream_time (&basesink->segment, format,
5542 basesink->segment.start);
5543 if ((stop = basesink->segment.stop) == -1)
5544 stop = basesink->segment.duration;
5545 else
5546 stop = gst_segment_to_stream_time (&basesink->segment, format, stop);
5547
5548 gst_query_set_segment (query, basesink->segment.rate, format, start,
5549 stop);
5550 res = TRUE;
5551 } else {
5552 res = gst_pad_peer_query (basesink->sinkpad, query);
5553 }
5554 break;
5555 }
5556 case GST_QUERY_SEEKING:
5557 case GST_QUERY_CONVERT:
5558 case GST_QUERY_FORMATS:
5559 default:
5560 res = gst_pad_peer_query (basesink->sinkpad, query);
5561 break;
5562 }
5563 GST_DEBUG_OBJECT (basesink, "query %s returns %d",
5564 GST_QUERY_TYPE_NAME (query), res);
5565 return res;
5566 }
5567
5568 static void
5569 gst_base_sink_drain (GstBaseSink * basesink)
5570 {
5571 GstBuffer *old;
5572 GstBufferList *old_list;
5573
5574 GST_OBJECT_LOCK (basesink);
5575 if ((old = basesink->priv->last_buffer))
5576 basesink->priv->last_buffer = gst_buffer_copy_deep (old);
5577
5578 if ((old_list = basesink->priv->last_buffer_list))
5579 basesink->priv->last_buffer_list = gst_buffer_list_copy_deep (old_list);
5580 GST_OBJECT_UNLOCK (basesink);
5581
5582 if (old)
5583 gst_buffer_unref (old);
5584 if (old_list)
5585 gst_mini_object_unref (GST_MINI_OBJECT_CAST (old_list));
5586 }
5587
5588 static gboolean
5589 gst_base_sink_default_query (GstBaseSink * basesink, GstQuery * query)
5590 {
5591 gboolean res;
5592 GstBaseSinkClass *bclass;
5593
5594 bclass = GST_BASE_SINK_GET_CLASS (basesink);
5595
5596 switch (GST_QUERY_TYPE (query)) {
5597 case GST_QUERY_ALLOCATION:
5598 {
5599 gst_base_sink_drain (basesink);
5600 if (bclass->propose_allocation)
5601 res = bclass->propose_allocation (basesink, query);
5602 else
5603 res = FALSE;
5604 break;
5605 }
5606 case GST_QUERY_CAPS:
5607 {
5608 GstCaps *caps, *filter;
5609
5610 gst_query_parse_caps (query, &filter);
5611 caps = gst_base_sink_query_caps (basesink, basesink->sinkpad, filter);
5612 gst_query_set_caps_result (query, caps);
5613 gst_caps_unref (caps);
5614 res = TRUE;
5615 break;
5616 }
5617 case GST_QUERY_ACCEPT_CAPS:
5618 {
5619 GstCaps *caps, *allowed;
5620 gboolean subset;
5621
5622 /* slightly faster than the default implementation */
5623 gst_query_parse_accept_caps (query, &caps);
5624 allowed = gst_base_sink_query_caps (basesink, basesink->sinkpad, NULL);
5625 subset = gst_caps_is_subset (caps, allowed);
5626 GST_DEBUG_OBJECT (basesink, "Checking if requested caps %" GST_PTR_FORMAT
5627 " are a subset of pad caps %" GST_PTR_FORMAT " result %d", caps,
5628 allowed, subset);
5629 gst_caps_unref (allowed);
5630 gst_query_set_accept_caps_result (query, subset);
5631 res = TRUE;
5632 break;
5633 }
5634 case GST_QUERY_DRAIN:
5635 {
5636 gst_base_sink_drain (basesink);
5637 res = TRUE;
5638 break;
5639 }
5640 case GST_QUERY_POSITION:
5641 {
5642 res = default_element_query (GST_ELEMENT (basesink), query);
5643 break;
5644 }
5645 default:
5646 res =
5647 gst_pad_query_default (basesink->sinkpad, GST_OBJECT_CAST (basesink),
5648 query);
5649 break;
5650 }
5651 return res;
5652 }
5653
5654 static gboolean
5655 gst_base_sink_sink_query (GstPad * pad, GstObject * parent, GstQuery * query)
5656 {
5657 GstBaseSink *basesink;
5658 GstBaseSinkClass *bclass;
5659 gboolean res;
5660
5661 basesink = GST_BASE_SINK_CAST (parent);
5662 bclass = GST_BASE_SINK_GET_CLASS (basesink);
5663
5664 if (bclass->query)
5665 res = bclass->query (basesink, query);
5666 else
5667 res = FALSE;
5668
5669 return res;
5670 }
5671
5672 static GstStateChangeReturn
5673 gst_base_sink_change_state (GstElement * element, GstStateChange transition)
5674 {
5675 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
5676 GstBaseSink *basesink = GST_BASE_SINK (element);
5677 GstBaseSinkClass *bclass;
5678 GstBaseSinkPrivate *priv;
5679
5680 priv = basesink->priv;
5681
5682 bclass = GST_BASE_SINK_GET_CLASS (basesink);
5683
5684 switch (transition) {
5685 case GST_STATE_CHANGE_NULL_TO_READY:
5686 if (bclass->start)
5687 if (!bclass->start (basesink))
5688 goto start_failed;
5689 break;
5690 case GST_STATE_CHANGE_READY_TO_PAUSED:
5691 /* need to complete preroll before this state change completes, there
5692 * is no data flow in READY so we can safely assume we need to preroll. */
5693 GST_BASE_SINK_PREROLL_LOCK (basesink);
5694 GST_DEBUG_OBJECT (basesink, "READY to PAUSED");
5695 basesink->have_newsegment = FALSE;
5696 gst_segment_init (&basesink->segment, GST_FORMAT_UNDEFINED);
5697 gst_segment_init (&basesink->priv->upstream_segment,
5698 GST_FORMAT_UNDEFINED);
5699 basesink->offset = 0;
5700 basesink->have_preroll = FALSE;
5701 priv->step_unlock = FALSE;
5702 basesink->need_preroll = TRUE;
5703 basesink->playing_async = TRUE;
5704 priv->current_sstart = GST_CLOCK_TIME_NONE;
5705 priv->current_sstop = GST_CLOCK_TIME_NONE;
5706 priv->eos_rtime = GST_CLOCK_TIME_NONE;
5707 priv->latency = 0;
5708 basesink->eos = FALSE;
5709 priv->received_eos = FALSE;
5710 gst_base_sink_reset_qos (basesink);
5711 priv->rc_next = -1;
5712 priv->committed = FALSE;
5713 priv->call_preroll = TRUE;
5714 priv->current_step.valid = FALSE;
5715 priv->pending_step.valid = FALSE;
5716 priv->instant_rate_sync_seqnum = GST_SEQNUM_INVALID;
5717 priv->instant_rate_multiplier = 0;
5718 priv->last_instant_rate_seqnum = GST_SEQNUM_INVALID;
5719 priv->instant_rate_offset = 0;
5720 priv->last_anchor_running_time = 0;
5721 if (priv->async_enabled) {
5722 GST_DEBUG_OBJECT (basesink, "doing async state change");
5723 /* when async enabled, post async-start message and return ASYNC from
5724 * the state change function */
5725 ret = GST_STATE_CHANGE_ASYNC;
5726 gst_element_post_message (GST_ELEMENT_CAST (basesink),
5727 gst_message_new_async_start (GST_OBJECT_CAST (basesink)));
5728 } else {
5729 priv->have_latency = TRUE;
5730 gst_element_post_message (GST_ELEMENT_CAST (basesink),
5731 gst_message_new_latency (GST_OBJECT_CAST (basesink)));
5732 }
5733 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
5734 break;
5735 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
5736 GST_BASE_SINK_PREROLL_LOCK (basesink);
5737 g_atomic_int_set (&basesink->priv->to_playing, TRUE);
5738 if (!gst_base_sink_needs_preroll (basesink)) {
5739 GST_DEBUG_OBJECT (basesink, "PAUSED to PLAYING, don't need preroll");
5740 /* no preroll needed anymore now. */
5741 basesink->playing_async = FALSE;
5742 basesink->need_preroll = FALSE;
5743 if (basesink->eos) {
5744 GstMessage *message;
5745
5746 /* need to post EOS message here */
5747 GST_DEBUG_OBJECT (basesink, "Now posting EOS");
5748 message = gst_message_new_eos (GST_OBJECT_CAST (basesink));
5749 gst_message_set_seqnum (message, basesink->priv->seqnum);
5750 gst_element_post_message (GST_ELEMENT_CAST (basesink), message);
5751 } else {
5752 GST_DEBUG_OBJECT (basesink, "signal preroll");
5753 GST_BASE_SINK_PREROLL_SIGNAL (basesink);
5754 }
5755 } else {
5756 GST_DEBUG_OBJECT (basesink, "PAUSED to PLAYING, we are not prerolled");
5757 basesink->need_preroll = TRUE;
5758 basesink->playing_async = TRUE;
5759 priv->call_preroll = TRUE;
5760 priv->committed = FALSE;
5761 if (priv->async_enabled) {
5762 GST_DEBUG_OBJECT (basesink, "doing async state change");
5763 ret = GST_STATE_CHANGE_ASYNC;
5764 gst_element_post_message (GST_ELEMENT_CAST (basesink),
5765 gst_message_new_async_start (GST_OBJECT_CAST (basesink)));
5766 }
5767 }
5768 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
5769 break;
5770 default:
5771 break;
5772 }
5773
5774 {
5775 GstStateChangeReturn bret;
5776
5777 bret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
5778 if (G_UNLIKELY (bret == GST_STATE_CHANGE_FAILURE))
5779 goto activate_failed;
5780 }
5781
5782 switch (transition) {
5783 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
5784 /* completed transition, so need not be marked any longer
5785 * And it should be unmarked, since e.g. losing our position upon flush
5786 * does not really change state to PAUSED ... */
5787 g_atomic_int_set (&basesink->priv->to_playing, FALSE);
5788 break;
5789 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
5790 g_atomic_int_set (&basesink->priv->to_playing, FALSE);
5791 GST_DEBUG_OBJECT (basesink, "PLAYING to PAUSED");
5792 /* FIXME, make sure we cannot enter _render first */
5793
5794 /* we need to call ::unlock before locking PREROLL_LOCK
5795 * since we lock it before going into ::render */
5796 if (bclass->unlock)
5797 bclass->unlock (basesink);
5798
5799 GST_BASE_SINK_PREROLL_LOCK (basesink);
5800 GST_DEBUG_OBJECT (basesink, "got preroll lock");
5801 /* now that we have the PREROLL lock, clear our unlock request */
5802 if (bclass->unlock_stop)
5803 bclass->unlock_stop (basesink);
5804
5805 if (basesink->clock_id) {
5806 GST_DEBUG_OBJECT (basesink, "unschedule clock");
5807 gst_clock_id_unschedule (basesink->clock_id);
5808 }
5809
5810 /* if we don't have a preroll buffer we need to wait for a preroll and
5811 * return ASYNC. */
5812 if (!gst_base_sink_needs_preroll (basesink)) {
5813 GST_DEBUG_OBJECT (basesink, "PLAYING to PAUSED, we are prerolled");
5814 basesink->playing_async = FALSE;
5815 basesink->need_preroll = FALSE;
5816 } else {
5817 if (GST_STATE_TARGET (GST_ELEMENT (basesink)) <= GST_STATE_READY) {
5818 GST_DEBUG_OBJECT (basesink, "element is <= READY");
5819 ret = GST_STATE_CHANGE_SUCCESS;
5820 } else {
5821 GST_DEBUG_OBJECT (basesink,
5822 "PLAYING to PAUSED, we are not prerolled");
5823 basesink->playing_async = TRUE;
5824 basesink->need_preroll = TRUE;
5825 priv->committed = FALSE;
5826 priv->call_preroll = TRUE;
5827 if (priv->async_enabled) {
5828 GST_DEBUG_OBJECT (basesink, "doing async state change");
5829 ret = GST_STATE_CHANGE_ASYNC;
5830 gst_element_post_message (GST_ELEMENT_CAST (basesink),
5831 gst_message_new_async_start (GST_OBJECT_CAST (basesink)));
5832 }
5833 }
5834 }
5835 GST_DEBUG_OBJECT (basesink, "rendered: %" G_GUINT64_FORMAT
5836 ", dropped: %" G_GUINT64_FORMAT, priv->rendered, priv->dropped);
5837
5838 gst_base_sink_reset_qos (basesink);
5839 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
5840 break;
5841 case GST_STATE_CHANGE_PAUSED_TO_READY:
5842 GST_BASE_SINK_PREROLL_LOCK (basesink);
5843 /* start by resetting our position state with the object lock so that the
5844 * position query gets the right idea. We do this before we post the
5845 * messages so that the message handlers pick this up. */
5846 GST_OBJECT_LOCK (basesink);
5847 basesink->have_newsegment = FALSE;
5848 priv->current_sstart = GST_CLOCK_TIME_NONE;
5849 priv->current_sstop = GST_CLOCK_TIME_NONE;
5850 priv->have_latency = FALSE;
5851 if (priv->cached_clock_id) {
5852 gst_clock_id_unref (priv->cached_clock_id);
5853 priv->cached_clock_id = NULL;
5854 }
5855 gst_caps_replace (&basesink->priv->caps, NULL);
5856 GST_OBJECT_UNLOCK (basesink);
5857
5858 gst_base_sink_set_last_buffer (basesink, NULL);
5859 gst_base_sink_set_last_buffer_list (basesink, NULL);
5860 priv->call_preroll = FALSE;
5861
5862 if (!priv->committed) {
5863 if (priv->async_enabled) {
5864 GST_DEBUG_OBJECT (basesink, "PAUSED to READY, posting async-done");
5865
5866 gst_element_post_message (GST_ELEMENT_CAST (basesink),
5867 gst_message_new_state_changed (GST_OBJECT_CAST (basesink),
5868 GST_STATE_PLAYING, GST_STATE_PAUSED, GST_STATE_READY));
5869
5870 gst_element_post_message (GST_ELEMENT_CAST (basesink),
5871 gst_message_new_async_done (GST_OBJECT_CAST (basesink),
5872 GST_CLOCK_TIME_NONE));
5873 }
5874 priv->committed = TRUE;
5875 } else {
5876 GST_DEBUG_OBJECT (basesink, "PAUSED to READY, don't need_preroll");
5877 }
5878 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
5879 break;
5880 case GST_STATE_CHANGE_READY_TO_NULL:
5881 if (bclass->stop) {
5882 if (!bclass->stop (basesink)) {
5883 GST_WARNING_OBJECT (basesink, "failed to stop");
5884 }
5885 }
5886 gst_base_sink_set_last_buffer (basesink, NULL);
5887 gst_base_sink_set_last_buffer_list (basesink, NULL);
5888 priv->call_preroll = FALSE;
5889 break;
5890 default:
5891 break;
5892 }
5893
5894 return ret;
5895
5896 /* ERRORS */
5897 start_failed:
5898 {
5899 GST_DEBUG_OBJECT (basesink, "failed to start");
5900 /* subclass is supposed to post a message but we post one as a fallback
5901 * just in case */
5902 GST_ELEMENT_ERROR (basesink, CORE, STATE_CHANGE, (NULL),
5903 ("Failed to start"));
5904 return GST_STATE_CHANGE_FAILURE;
5905 }
5906 activate_failed:
5907 {
5908 GST_DEBUG_OBJECT (basesink,
5909 "element failed to change states -- activation problem?");
5910 return GST_STATE_CHANGE_FAILURE;
5911 }
5912 }
5913
5914 /**
5915 * gst_base_sink_get_stats:
5916 * @sink: #GstBaseSink
5917 *
5918 * Return various #GstBaseSink statistics. This function returns a #GstStructure
5919 * with name `application/x-gst-base-sink-stats` with the following fields:
5920 *
5921 * - "average-rate" G_TYPE_DOUBLE average frame rate
5922 * - "dropped" G_TYPE_UINT64 Number of dropped frames
5923 * - "rendered" G_TYPE_UINT64 Number of rendered frames
5924 *
5925 * Returns: (transfer full): pointer to #GstStructure
5926 *
5927 * Since: 1.18
5928 */
5929 GstStructure *
5930 gst_base_sink_get_stats (GstBaseSink * sink)
5931 {
5932 GstBaseSinkPrivate *priv = NULL;
5933
5934 g_return_val_if_fail (sink != NULL, NULL);
5935 priv = sink->priv;
5936 return gst_structure_new ("application/x-gst-base-sink-stats",
5937 "average-rate", G_TYPE_DOUBLE, priv->avg_rate,
5938 "dropped", G_TYPE_UINT64, priv->dropped,
5939 "rendered", G_TYPE_UINT64, priv->rendered, NULL);
5940 }