2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2005 Wim Taymans <wim@fluendo.com>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
24 * SECTION:gstbaseaudiosink
25 * @short_description: Base class for audio sinks
26 * @see_also: #GstAudioSink, #GstRingBuffer.
28 * This is the base class for audio sinks. Subclasses need to implement the
29 * ::create_ringbuffer vmethod. This base class will then take care of
30 * writing samples to the ringbuffer, synchronisation, clipping and flushing.
32 * Last reviewed on 2006-09-27 (0.10.12)
37 /* FIXME 0.11: suppress warnings for deprecated API such as GStaticRecMutex
38 * with newer GLib versions (>= 2.31.0) */
39 #define GLIB_DISABLE_DEPRECATION_WARNINGS
41 #include "gstbaseaudiosink.h"
43 GST_DEBUG_CATEGORY_STATIC (gst_base_audio_sink_debug);
44 #define GST_CAT_DEFAULT gst_base_audio_sink_debug
46 #define GST_BASE_AUDIO_SINK_GET_PRIVATE(obj) \
47 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_BASE_AUDIO_SINK, GstBaseAudioSinkPrivate))
49 struct _GstBaseAudioSinkPrivate
51 /* upstream latency */
52 GstClockTime us_latency;
53 /* the clock slaving algorithm in use */
54 GstBaseAudioSinkSlaveMethod slave_method;
55 /* running average of clock skew */
56 GstClockTimeDiff avg_skew;
57 /* the number of samples we aligned last time */
60 gboolean sync_latency;
62 GstClockTime eos_time;
64 gboolean do_time_offset;
65 /* number of microseconds we allow clock slaving to drift
67 guint64 drift_tolerance;
69 /* number of nanoseconds we allow timestamps to drift
71 GstClockTime alignment_threshold;
73 /* time of the previous detected discont candidate */
74 GstClockTime discont_time;
76 /* number of nanoseconds to wait until creating a discontinuity */
77 GstClockTime discont_wait;
80 /* BaseAudioSink signals and args */
87 /* FIXME: 0.11, store the buffer_time and latency_time in nanoseconds */
88 #define DEFAULT_BUFFER_TIME ((200 * GST_MSECOND) / GST_USECOND)
89 #define DEFAULT_LATENCY_TIME ((10 * GST_MSECOND) / GST_USECOND)
90 #define DEFAULT_PROVIDE_CLOCK TRUE
91 #define DEFAULT_SLAVE_METHOD GST_BASE_AUDIO_SINK_SLAVE_SKEW
93 /* FIXME, enable pull mode when clock slaving and trick modes are figured out */
94 #define DEFAULT_CAN_ACTIVATE_PULL FALSE
96 /* when timestamps drift for more than 40ms we resync. This should
97 * be anough to compensate for timestamp rounding errors. */
98 #define DEFAULT_ALIGNMENT_THRESHOLD (40 * GST_MSECOND)
100 /* when clock slaving drift for more than 40ms we resync. This is
101 * a reasonable default */
102 #define DEFAULT_DRIFT_TOLERANCE ((40 * GST_MSECOND) / GST_USECOND)
104 /* allow for one second before resyncing to see if the timestamps drift will
105 * fix itself, or is a permanent offset */
106 #define DEFAULT_DISCONT_WAIT (1 * GST_SECOND)
116 PROP_CAN_ACTIVATE_PULL,
117 PROP_ALIGNMENT_THRESHOLD,
118 PROP_DRIFT_TOLERANCE,
125 gst_base_audio_sink_slave_method_get_type (void)
127 static volatile gsize slave_method_type = 0;
128 static const GEnumValue slave_method[] = {
129 {GST_BASE_AUDIO_SINK_SLAVE_RESAMPLE, "GST_BASE_AUDIO_SINK_SLAVE_RESAMPLE",
131 {GST_BASE_AUDIO_SINK_SLAVE_SKEW, "GST_BASE_AUDIO_SINK_SLAVE_SKEW", "skew"},
132 {GST_BASE_AUDIO_SINK_SLAVE_NONE, "GST_BASE_AUDIO_SINK_SLAVE_NONE", "none"},
136 if (g_once_init_enter (&slave_method_type)) {
138 g_enum_register_static ("GstBaseAudioSinkSlaveMethod", slave_method);
139 g_once_init_leave (&slave_method_type, tmp);
142 return (GType) slave_method_type;
146 #define _do_init(bla) \
147 GST_DEBUG_CATEGORY_INIT (gst_base_audio_sink_debug, "baseaudiosink", 0, "baseaudiosink element");
149 GST_BOILERPLATE_FULL (GstBaseAudioSink, gst_base_audio_sink, GstBaseSink,
150 GST_TYPE_BASE_SINK, _do_init);
152 static void gst_base_audio_sink_dispose (GObject * object);
154 static void gst_base_audio_sink_set_property (GObject * object, guint prop_id,
155 const GValue * value, GParamSpec * pspec);
156 static void gst_base_audio_sink_get_property (GObject * object, guint prop_id,
157 GValue * value, GParamSpec * pspec);
159 static GstStateChangeReturn gst_base_audio_sink_async_play (GstBaseSink *
161 static GstStateChangeReturn gst_base_audio_sink_change_state (GstElement *
162 element, GstStateChange transition);
163 static gboolean gst_base_audio_sink_activate_pull (GstBaseSink * basesink,
165 static gboolean gst_base_audio_sink_query (GstElement * element, GstQuery *
168 static GstClock *gst_base_audio_sink_provide_clock (GstElement * elem);
169 static GstClockTime gst_base_audio_sink_get_time (GstClock * clock,
170 GstBaseAudioSink * sink);
171 static void gst_base_audio_sink_callback (GstRingBuffer * rbuf, guint8 * data,
172 guint len, gpointer user_data);
174 static GstFlowReturn gst_base_audio_sink_preroll (GstBaseSink * bsink,
176 static GstFlowReturn gst_base_audio_sink_render (GstBaseSink * bsink,
178 static gboolean gst_base_audio_sink_event (GstBaseSink * bsink,
180 static void gst_base_audio_sink_get_times (GstBaseSink * bsink,
181 GstBuffer * buffer, GstClockTime * start, GstClockTime * end);
182 static gboolean gst_base_audio_sink_setcaps (GstBaseSink * bsink,
184 static void gst_base_audio_sink_fixate (GstBaseSink * bsink, GstCaps * caps);
186 static gboolean gst_base_audio_sink_query_pad (GstPad * pad, GstQuery * query);
189 /* static guint gst_base_audio_sink_signals[LAST_SIGNAL] = { 0 }; */
192 gst_base_audio_sink_base_init (gpointer g_class)
197 gst_base_audio_sink_class_init (GstBaseAudioSinkClass * klass)
199 GObjectClass *gobject_class;
200 GstElementClass *gstelement_class;
201 GstBaseSinkClass *gstbasesink_class;
203 gobject_class = (GObjectClass *) klass;
204 gstelement_class = (GstElementClass *) klass;
205 gstbasesink_class = (GstBaseSinkClass *) klass;
207 g_type_class_add_private (klass, sizeof (GstBaseAudioSinkPrivate));
209 gobject_class->set_property = gst_base_audio_sink_set_property;
210 gobject_class->get_property = gst_base_audio_sink_get_property;
211 gobject_class->dispose = gst_base_audio_sink_dispose;
213 g_object_class_install_property (gobject_class, PROP_BUFFER_TIME,
214 g_param_spec_int64 ("buffer-time", "Buffer Time",
215 "Size of audio buffer in microseconds", 1,
216 G_MAXINT64, DEFAULT_BUFFER_TIME,
217 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
219 g_object_class_install_property (gobject_class, PROP_LATENCY_TIME,
220 g_param_spec_int64 ("latency-time", "Latency Time",
221 "Audio latency in microseconds", 1,
222 G_MAXINT64, DEFAULT_LATENCY_TIME,
223 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
225 g_object_class_install_property (gobject_class, PROP_PROVIDE_CLOCK,
226 g_param_spec_boolean ("provide-clock", "Provide Clock",
227 "Provide a clock to be used as the global pipeline clock",
228 DEFAULT_PROVIDE_CLOCK, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
230 g_object_class_install_property (gobject_class, PROP_SLAVE_METHOD,
231 g_param_spec_enum ("slave-method", "Slave Method",
232 "Algorithm to use to match the rate of the masterclock",
233 GST_TYPE_BASE_AUDIO_SINK_SLAVE_METHOD, DEFAULT_SLAVE_METHOD,
234 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
236 g_object_class_install_property (gobject_class, PROP_CAN_ACTIVATE_PULL,
237 g_param_spec_boolean ("can-activate-pull", "Allow Pull Scheduling",
238 "Allow pull-based scheduling", DEFAULT_CAN_ACTIVATE_PULL,
239 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
241 * GstBaseAudioSink:drift-tolerance
243 * Controls the amount of time in microseconds that clocks are allowed
244 * to drift before resynchronisation happens.
248 g_object_class_install_property (gobject_class, PROP_DRIFT_TOLERANCE,
249 g_param_spec_int64 ("drift-tolerance", "Drift Tolerance",
250 "Tolerance for clock drift in microseconds", 1,
251 G_MAXINT64, DEFAULT_DRIFT_TOLERANCE,
252 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
254 * GstBaseAudioSink:alignment_threshold
256 * Controls the amount of time in nanoseconds that timestamps are allowed
257 * to drift from their ideal time before choosing not to align them.
261 g_object_class_install_property (gobject_class, PROP_ALIGNMENT_THRESHOLD,
262 g_param_spec_uint64 ("alignment-threshold", "Alignment Threshold",
263 "Timestamp alignment threshold in nanoseconds", 1,
264 G_MAXUINT64 - 1, DEFAULT_ALIGNMENT_THRESHOLD,
265 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
268 * GstBaseAudioSink:discont-wait
270 * A window of time in nanoseconds to wait before creating a discontinuity as
271 * a result of breaching the drift-tolerance.
275 g_object_class_install_property (gobject_class, PROP_DISCONT_WAIT,
276 g_param_spec_uint64 ("discont-wait", "Discont Wait",
277 "Window of time in nanoseconds to wait before "
278 "creating a discontinuity", 0,
279 G_MAXUINT64 - 1, DEFAULT_DISCONT_WAIT,
280 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
282 gstelement_class->change_state =
283 GST_DEBUG_FUNCPTR (gst_base_audio_sink_change_state);
284 gstelement_class->provide_clock =
285 GST_DEBUG_FUNCPTR (gst_base_audio_sink_provide_clock);
286 gstelement_class->query = GST_DEBUG_FUNCPTR (gst_base_audio_sink_query);
288 gstbasesink_class->event = GST_DEBUG_FUNCPTR (gst_base_audio_sink_event);
289 gstbasesink_class->preroll = GST_DEBUG_FUNCPTR (gst_base_audio_sink_preroll);
290 gstbasesink_class->render = GST_DEBUG_FUNCPTR (gst_base_audio_sink_render);
291 gstbasesink_class->get_times =
292 GST_DEBUG_FUNCPTR (gst_base_audio_sink_get_times);
293 gstbasesink_class->set_caps = GST_DEBUG_FUNCPTR (gst_base_audio_sink_setcaps);
294 gstbasesink_class->fixate = GST_DEBUG_FUNCPTR (gst_base_audio_sink_fixate);
295 gstbasesink_class->async_play =
296 GST_DEBUG_FUNCPTR (gst_base_audio_sink_async_play);
297 gstbasesink_class->activate_pull =
298 GST_DEBUG_FUNCPTR (gst_base_audio_sink_activate_pull);
300 /* ref class from a thread-safe context to work around missing bit of
301 * thread-safety in GObject */
302 g_type_class_ref (GST_TYPE_AUDIO_CLOCK);
303 g_type_class_ref (GST_TYPE_RING_BUFFER);
308 gst_base_audio_sink_init (GstBaseAudioSink * baseaudiosink,
309 GstBaseAudioSinkClass * g_class)
311 GstPluginFeature *feature;
312 GstBaseSink *basesink;
314 baseaudiosink->priv = GST_BASE_AUDIO_SINK_GET_PRIVATE (baseaudiosink);
316 baseaudiosink->buffer_time = DEFAULT_BUFFER_TIME;
317 baseaudiosink->latency_time = DEFAULT_LATENCY_TIME;
318 baseaudiosink->provide_clock = DEFAULT_PROVIDE_CLOCK;
319 baseaudiosink->priv->slave_method = DEFAULT_SLAVE_METHOD;
320 baseaudiosink->priv->drift_tolerance = DEFAULT_DRIFT_TOLERANCE;
321 baseaudiosink->priv->alignment_threshold = DEFAULT_ALIGNMENT_THRESHOLD;
322 baseaudiosink->priv->discont_wait = DEFAULT_DISCONT_WAIT;
324 baseaudiosink->provided_clock = gst_audio_clock_new ("GstAudioSinkClock",
325 (GstAudioClockGetTimeFunc) gst_base_audio_sink_get_time, baseaudiosink);
327 basesink = GST_BASE_SINK_CAST (baseaudiosink);
328 basesink->can_activate_push = TRUE;
329 basesink->can_activate_pull = DEFAULT_CAN_ACTIVATE_PULL;
331 gst_base_sink_set_last_buffer_enabled (basesink, FALSE);
333 /* install some custom pad_query functions */
334 gst_pad_set_query_function (GST_BASE_SINK_PAD (baseaudiosink),
335 GST_DEBUG_FUNCPTR (gst_base_audio_sink_query_pad));
337 baseaudiosink->priv->do_time_offset = TRUE;
339 /* check the factory, pulsesink < 0.10.17 does the timestamp offset itself so
340 * we should not do ourselves */
342 GST_PLUGIN_FEATURE_CAST (GST_ELEMENT_CLASS (g_class)->elementfactory);
343 GST_DEBUG ("created from factory %p", feature);
345 /* HACK for old pulsesink that did the time_offset themselves */
347 if (strcmp (gst_plugin_feature_get_name (feature), "pulsesink") == 0) {
348 if (!gst_plugin_feature_check_version (feature, 0, 10, 17)) {
349 /* we're dealing with an old pulsesink, we need to disable time correction */
350 GST_DEBUG ("disable time offset");
351 baseaudiosink->priv->do_time_offset = FALSE;
358 gst_base_audio_sink_dispose (GObject * object)
360 GstBaseAudioSink *sink;
362 sink = GST_BASE_AUDIO_SINK (object);
364 if (sink->provided_clock) {
365 gst_audio_clock_invalidate (sink->provided_clock);
366 gst_object_unref (sink->provided_clock);
367 sink->provided_clock = NULL;
370 if (sink->ringbuffer) {
371 gst_object_unparent (GST_OBJECT_CAST (sink->ringbuffer));
372 sink->ringbuffer = NULL;
375 G_OBJECT_CLASS (parent_class)->dispose (object);
380 gst_base_audio_sink_provide_clock (GstElement * elem)
382 GstBaseAudioSink *sink;
385 sink = GST_BASE_AUDIO_SINK (elem);
387 /* we have no ringbuffer (must be NULL state) */
388 if (sink->ringbuffer == NULL)
391 if (!gst_ring_buffer_is_acquired (sink->ringbuffer))
394 GST_OBJECT_LOCK (sink);
395 if (!sink->provide_clock)
398 clock = GST_CLOCK_CAST (gst_object_ref (sink->provided_clock));
399 GST_OBJECT_UNLOCK (sink);
406 GST_DEBUG_OBJECT (sink, "ringbuffer not acquired");
411 GST_DEBUG_OBJECT (sink, "clock provide disabled");
412 GST_OBJECT_UNLOCK (sink);
418 gst_base_audio_sink_query_pad (GstPad * pad, GstQuery * query)
420 gboolean res = FALSE;
421 GstBaseAudioSink *basesink;
423 basesink = GST_BASE_AUDIO_SINK (gst_pad_get_parent (pad));
425 switch (GST_QUERY_TYPE (query)) {
426 case GST_QUERY_CONVERT:
428 GstFormat src_fmt, dest_fmt;
429 gint64 src_val, dest_val;
431 GST_LOG_OBJECT (pad, "query convert");
433 if (basesink->ringbuffer) {
434 gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, NULL);
435 res = gst_ring_buffer_convert (basesink->ringbuffer, src_fmt, src_val,
436 dest_fmt, &dest_val);
438 gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
447 gst_object_unref (basesink);
453 gst_base_audio_sink_query (GstElement * element, GstQuery * query)
455 gboolean res = FALSE;
456 GstBaseAudioSink *basesink;
458 basesink = GST_BASE_AUDIO_SINK (element);
460 switch (GST_QUERY_TYPE (query)) {
461 case GST_QUERY_LATENCY:
463 gboolean live, us_live;
464 GstClockTime min_l, max_l;
466 GST_DEBUG_OBJECT (basesink, "latency query");
468 /* ask parent first, it will do an upstream query for us. */
470 gst_base_sink_query_latency (GST_BASE_SINK_CAST (basesink), &live,
471 &us_live, &min_l, &max_l))) {
472 GstClockTime base_latency, min_latency, max_latency;
474 /* we and upstream are both live, adjust the min_latency */
475 if (live && us_live) {
476 GstRingBufferSpec *spec;
478 GST_OBJECT_LOCK (basesink);
479 if (!basesink->ringbuffer || !basesink->ringbuffer->spec.rate) {
480 GST_OBJECT_UNLOCK (basesink);
482 GST_DEBUG_OBJECT (basesink,
483 "we are not yet negotiated, can't report latency yet");
487 spec = &basesink->ringbuffer->spec;
489 basesink->priv->us_latency = min_l;
492 gst_util_uint64_scale_int (spec->seglatency * spec->segsize,
493 GST_SECOND, spec->rate * spec->bytes_per_sample);
494 GST_OBJECT_UNLOCK (basesink);
496 /* we cannot go lower than the buffer size and the min peer latency */
497 min_latency = base_latency + min_l;
498 /* the max latency is the max of the peer, we can delay an infinite
500 max_latency = (max_l == -1) ? -1 : (base_latency + max_l);
502 GST_DEBUG_OBJECT (basesink,
503 "peer min %" GST_TIME_FORMAT ", our min latency: %"
504 GST_TIME_FORMAT, GST_TIME_ARGS (min_l),
505 GST_TIME_ARGS (min_latency));
506 GST_DEBUG_OBJECT (basesink,
507 "peer max %" GST_TIME_FORMAT ", our max latency: %"
508 GST_TIME_FORMAT, GST_TIME_ARGS (max_l),
509 GST_TIME_ARGS (max_latency));
511 GST_DEBUG_OBJECT (basesink,
512 "peer or we are not live, don't care about latency");
516 gst_query_set_latency (query, live, min_latency, max_latency);
520 case GST_QUERY_CONVERT:
522 GstFormat src_fmt, dest_fmt;
523 gint64 src_val, dest_val;
525 GST_LOG_OBJECT (basesink, "query convert");
527 if (basesink->ringbuffer) {
528 gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, NULL);
529 res = gst_ring_buffer_convert (basesink->ringbuffer, src_fmt, src_val,
530 dest_fmt, &dest_val);
532 gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
538 res = GST_ELEMENT_CLASS (parent_class)->query (element, query);
548 gst_base_audio_sink_get_time (GstClock * clock, GstBaseAudioSink * sink)
550 guint64 raw, samples;
554 if (sink->ringbuffer == NULL || sink->ringbuffer->spec.rate == 0)
555 return GST_CLOCK_TIME_NONE;
557 /* our processed samples are always increasing */
558 raw = samples = gst_ring_buffer_samples_done (sink->ringbuffer);
560 /* the number of samples not yet processed, this is still queued in the
561 * device (not played for playback). */
562 delay = gst_ring_buffer_delay (sink->ringbuffer);
564 if (G_LIKELY (samples >= delay))
569 result = gst_util_uint64_scale_int (samples, GST_SECOND,
570 sink->ringbuffer->spec.rate);
572 GST_DEBUG_OBJECT (sink,
573 "processed samples: raw %" G_GUINT64_FORMAT ", delay %u, real %"
574 G_GUINT64_FORMAT ", time %" GST_TIME_FORMAT,
575 raw, delay, samples, GST_TIME_ARGS (result));
581 * gst_base_audio_sink_set_provide_clock:
582 * @sink: a #GstBaseAudioSink
583 * @provide: new state
585 * Controls whether @sink will provide a clock or not. If @provide is %TRUE,
586 * gst_element_provide_clock() will return a clock that reflects the datarate
587 * of @sink. If @provide is %FALSE, gst_element_provide_clock() will return NULL.
592 gst_base_audio_sink_set_provide_clock (GstBaseAudioSink * sink,
595 g_return_if_fail (GST_IS_BASE_AUDIO_SINK (sink));
597 GST_OBJECT_LOCK (sink);
598 sink->provide_clock = provide;
599 GST_OBJECT_UNLOCK (sink);
603 * gst_base_audio_sink_get_provide_clock:
604 * @sink: a #GstBaseAudioSink
606 * Queries whether @sink will provide a clock or not. See also
607 * gst_base_audio_sink_set_provide_clock.
609 * Returns: %TRUE if @sink will provide a clock.
614 gst_base_audio_sink_get_provide_clock (GstBaseAudioSink * sink)
618 g_return_val_if_fail (GST_IS_BASE_AUDIO_SINK (sink), FALSE);
620 GST_OBJECT_LOCK (sink);
621 result = sink->provide_clock;
622 GST_OBJECT_UNLOCK (sink);
628 * gst_base_audio_sink_set_slave_method:
629 * @sink: a #GstBaseAudioSink
630 * @method: the new slave method
632 * Controls how clock slaving will be performed in @sink.
637 gst_base_audio_sink_set_slave_method (GstBaseAudioSink * sink,
638 GstBaseAudioSinkSlaveMethod method)
640 g_return_if_fail (GST_IS_BASE_AUDIO_SINK (sink));
642 GST_OBJECT_LOCK (sink);
643 sink->priv->slave_method = method;
644 GST_OBJECT_UNLOCK (sink);
648 * gst_base_audio_sink_get_slave_method:
649 * @sink: a #GstBaseAudioSink
651 * Get the current slave method used by @sink.
653 * Returns: The current slave method used by @sink.
657 GstBaseAudioSinkSlaveMethod
658 gst_base_audio_sink_get_slave_method (GstBaseAudioSink * sink)
660 GstBaseAudioSinkSlaveMethod result;
662 g_return_val_if_fail (GST_IS_BASE_AUDIO_SINK (sink), -1);
664 GST_OBJECT_LOCK (sink);
665 result = sink->priv->slave_method;
666 GST_OBJECT_UNLOCK (sink);
673 * gst_base_audio_sink_set_drift_tolerance:
674 * @sink: a #GstBaseAudioSink
675 * @drift_tolerance: the new drift tolerance in microseconds
677 * Controls the sink's drift tolerance.
682 gst_base_audio_sink_set_drift_tolerance (GstBaseAudioSink * sink,
683 gint64 drift_tolerance)
685 g_return_if_fail (GST_IS_BASE_AUDIO_SINK (sink));
687 GST_OBJECT_LOCK (sink);
688 sink->priv->drift_tolerance = drift_tolerance;
689 GST_OBJECT_UNLOCK (sink);
693 * gst_base_audio_sink_get_drift_tolerance
694 * @sink: a #GstBaseAudioSink
696 * Get the current drift tolerance, in microseconds, used by @sink.
698 * Returns: The current drift tolerance used by @sink.
703 gst_base_audio_sink_get_drift_tolerance (GstBaseAudioSink * sink)
707 g_return_val_if_fail (GST_IS_BASE_AUDIO_SINK (sink), -1);
709 GST_OBJECT_LOCK (sink);
710 result = sink->priv->drift_tolerance;
711 GST_OBJECT_UNLOCK (sink);
717 * gst_base_audio_sink_set_alignment_threshold:
718 * @sink: a #GstBaseAudioSink
719 * @alignment_threshold: the new alignment threshold in nanoseconds
721 * Controls the sink's alignment threshold.
726 gst_base_audio_sink_set_alignment_threshold (GstBaseAudioSink * sink,
727 GstClockTime alignment_threshold)
729 g_return_if_fail (GST_IS_BASE_AUDIO_SINK (sink));
731 GST_OBJECT_LOCK (sink);
732 sink->priv->alignment_threshold = alignment_threshold;
733 GST_OBJECT_UNLOCK (sink);
737 * gst_base_audio_sink_get_alignment_threshold
738 * @sink: a #GstBaseAudioSink
740 * Get the current alignment threshold, in nanoseconds, used by @sink.
742 * Returns: The current alignment threshold used by @sink.
747 gst_base_audio_sink_get_alignment_threshold (GstBaseAudioSink * sink)
751 g_return_val_if_fail (GST_IS_BASE_AUDIO_SINK (sink), -1);
753 GST_OBJECT_LOCK (sink);
754 result = sink->priv->alignment_threshold;
755 GST_OBJECT_UNLOCK (sink);
761 * gst_base_audio_sink_set_discont_wait:
762 * @sink: a #GstBaseAudioSink
763 * @discont_wait: the new discont wait in nanoseconds
765 * Controls how long the sink will wait before creating a discontinuity.
770 gst_base_audio_sink_set_discont_wait (GstBaseAudioSink * sink,
771 GstClockTime discont_wait)
773 g_return_if_fail (GST_IS_BASE_AUDIO_SINK (sink));
775 GST_OBJECT_LOCK (sink);
776 sink->priv->discont_wait = discont_wait;
777 GST_OBJECT_UNLOCK (sink);
781 * gst_base_audio_sink_get_discont_wait
782 * @sink: a #GstBaseAudioSink
784 * Get the current discont wait, in nanoseconds, used by @sink.
786 * Returns: The current discont wait used by @sink.
791 gst_base_audio_sink_get_discont_wait (GstBaseAudioSink * sink)
795 g_return_val_if_fail (GST_IS_BASE_AUDIO_SINK (sink), -1);
797 GST_OBJECT_LOCK (sink);
798 result = sink->priv->discont_wait;
799 GST_OBJECT_UNLOCK (sink);
805 gst_base_audio_sink_set_property (GObject * object, guint prop_id,
806 const GValue * value, GParamSpec * pspec)
808 GstBaseAudioSink *sink;
810 sink = GST_BASE_AUDIO_SINK (object);
813 case PROP_BUFFER_TIME:
814 sink->buffer_time = g_value_get_int64 (value);
816 case PROP_LATENCY_TIME:
817 sink->latency_time = g_value_get_int64 (value);
819 case PROP_PROVIDE_CLOCK:
820 gst_base_audio_sink_set_provide_clock (sink, g_value_get_boolean (value));
822 case PROP_SLAVE_METHOD:
823 gst_base_audio_sink_set_slave_method (sink, g_value_get_enum (value));
825 case PROP_CAN_ACTIVATE_PULL:
826 GST_BASE_SINK (sink)->can_activate_pull = g_value_get_boolean (value);
828 case PROP_DRIFT_TOLERANCE:
829 gst_base_audio_sink_set_drift_tolerance (sink, g_value_get_int64 (value));
831 case PROP_ALIGNMENT_THRESHOLD:
832 gst_base_audio_sink_set_alignment_threshold (sink,
833 g_value_get_uint64 (value));
835 case PROP_DISCONT_WAIT:
836 gst_base_audio_sink_set_discont_wait (sink, g_value_get_uint64 (value));
839 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
845 gst_base_audio_sink_get_property (GObject * object, guint prop_id,
846 GValue * value, GParamSpec * pspec)
848 GstBaseAudioSink *sink;
850 sink = GST_BASE_AUDIO_SINK (object);
853 case PROP_BUFFER_TIME:
854 g_value_set_int64 (value, sink->buffer_time);
856 case PROP_LATENCY_TIME:
857 g_value_set_int64 (value, sink->latency_time);
859 case PROP_PROVIDE_CLOCK:
860 g_value_set_boolean (value, gst_base_audio_sink_get_provide_clock (sink));
862 case PROP_SLAVE_METHOD:
863 g_value_set_enum (value, gst_base_audio_sink_get_slave_method (sink));
865 case PROP_CAN_ACTIVATE_PULL:
866 g_value_set_boolean (value, GST_BASE_SINK (sink)->can_activate_pull);
868 case PROP_DRIFT_TOLERANCE:
869 g_value_set_int64 (value, gst_base_audio_sink_get_drift_tolerance (sink));
871 case PROP_ALIGNMENT_THRESHOLD:
872 g_value_set_uint64 (value,
873 gst_base_audio_sink_get_alignment_threshold (sink));
875 case PROP_DISCONT_WAIT:
876 g_value_set_uint64 (value, gst_base_audio_sink_get_discont_wait (sink));
879 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
885 gst_base_audio_sink_setcaps (GstBaseSink * bsink, GstCaps * caps)
887 GstBaseAudioSink *sink = GST_BASE_AUDIO_SINK (bsink);
888 GstRingBufferSpec *spec;
890 GstClockTime crate_num, crate_denom;
892 if (!sink->ringbuffer)
895 spec = &sink->ringbuffer->spec;
897 GST_DEBUG_OBJECT (sink, "release old ringbuffer");
899 /* get current time, updates the last_time */
900 now = gst_clock_get_time (sink->provided_clock);
902 GST_DEBUG_OBJECT (sink, "time was %" GST_TIME_FORMAT, GST_TIME_ARGS (now));
904 /* release old ringbuffer */
905 gst_ring_buffer_pause (sink->ringbuffer);
906 gst_ring_buffer_activate (sink->ringbuffer, FALSE);
907 gst_ring_buffer_release (sink->ringbuffer);
909 GST_DEBUG_OBJECT (sink, "parse caps");
911 spec->buffer_time = sink->buffer_time;
912 spec->latency_time = sink->latency_time;
915 if (!gst_ring_buffer_parse_caps (spec, caps))
918 gst_ring_buffer_debug_spec_buff (spec);
920 GST_DEBUG_OBJECT (sink, "acquire ringbuffer");
921 if (!gst_ring_buffer_acquire (sink->ringbuffer, spec))
924 if (bsink->pad_mode == GST_ACTIVATE_PUSH) {
925 GST_DEBUG_OBJECT (sink, "activate ringbuffer");
926 gst_ring_buffer_activate (sink->ringbuffer, TRUE);
929 /* due to possible changes in the spec file we should recalibrate the clock */
930 gst_clock_get_calibration (sink->provided_clock, NULL, NULL,
931 &crate_num, &crate_denom);
932 gst_clock_set_calibration (sink->provided_clock,
933 gst_clock_get_internal_time (sink->provided_clock), now, crate_num,
936 /* calculate actual latency and buffer times.
937 * FIXME: In 0.11, store the latency_time internally in ns */
938 spec->latency_time = gst_util_uint64_scale (spec->segsize,
939 (GST_SECOND / GST_USECOND), spec->rate * spec->bytes_per_sample);
941 spec->buffer_time = spec->segtotal * spec->latency_time;
943 gst_ring_buffer_debug_spec_buff (spec);
950 GST_DEBUG_OBJECT (sink, "could not parse caps");
951 GST_ELEMENT_ERROR (sink, STREAM, FORMAT,
952 (NULL), ("cannot parse audio format."));
957 GST_DEBUG_OBJECT (sink, "could not acquire ringbuffer");
963 gst_base_audio_sink_fixate (GstBaseSink * bsink, GstCaps * caps)
968 s = gst_caps_get_structure (caps, 0);
970 /* fields for all formats */
971 gst_structure_fixate_field_nearest_int (s, "rate", 44100);
972 gst_structure_fixate_field_nearest_int (s, "channels", 2);
973 gst_structure_fixate_field_nearest_int (s, "width", 16);
976 if (gst_structure_has_field (s, "depth")) {
977 gst_structure_get_int (s, "width", &width);
978 /* round width to nearest multiple of 8 for the depth */
979 depth = GST_ROUND_UP_8 (width);
980 gst_structure_fixate_field_nearest_int (s, "depth", depth);
982 if (gst_structure_has_field (s, "signed"))
983 gst_structure_fixate_field_boolean (s, "signed", TRUE);
984 if (gst_structure_has_field (s, "endianness"))
985 gst_structure_fixate_field_nearest_int (s, "endianness", G_BYTE_ORDER);
989 gst_base_audio_sink_get_times (GstBaseSink * bsink, GstBuffer * buffer,
990 GstClockTime * start, GstClockTime * end)
992 /* our clock sync is a bit too much for the base class to handle so
993 * we implement it ourselves. */
994 *start = GST_CLOCK_TIME_NONE;
995 *end = GST_CLOCK_TIME_NONE;
998 /* This waits for the drain to happen and can be canceled */
1000 gst_base_audio_sink_drain (GstBaseAudioSink * sink)
1002 if (!sink->ringbuffer)
1004 if (!sink->ringbuffer->spec.rate)
1007 /* if PLAYING is interrupted,
1008 * arrange to have clock running when going to PLAYING again */
1009 g_atomic_int_set (&sink->abidata.ABI.eos_rendering, 1);
1011 /* need to start playback before we can drain, but only when
1012 * we have successfully negotiated a format and thus acquired the
1014 if (gst_ring_buffer_is_acquired (sink->ringbuffer))
1015 gst_ring_buffer_start (sink->ringbuffer);
1017 if (sink->priv->eos_time != -1) {
1018 GST_DEBUG_OBJECT (sink,
1019 "last sample time %" GST_TIME_FORMAT,
1020 GST_TIME_ARGS (sink->priv->eos_time));
1022 /* wait for the EOS time to be reached, this is the time when the last
1023 * sample is played. */
1024 gst_base_sink_wait_eos (GST_BASE_SINK (sink), sink->priv->eos_time, NULL);
1026 GST_DEBUG_OBJECT (sink, "drained audio");
1028 g_atomic_int_set (&sink->abidata.ABI.eos_rendering, 0);
1033 gst_base_audio_sink_event (GstBaseSink * bsink, GstEvent * event)
1035 GstBaseAudioSink *sink = GST_BASE_AUDIO_SINK (bsink);
1037 switch (GST_EVENT_TYPE (event)) {
1038 case GST_EVENT_FLUSH_START:
1039 if (sink->ringbuffer)
1040 gst_ring_buffer_set_flushing (sink->ringbuffer, TRUE);
1042 case GST_EVENT_FLUSH_STOP:
1043 /* always resync on sample after a flush */
1044 sink->priv->avg_skew = -1;
1045 sink->next_sample = -1;
1046 sink->priv->eos_time = -1;
1047 sink->priv->discont_time = -1;
1048 if (sink->ringbuffer)
1049 gst_ring_buffer_set_flushing (sink->ringbuffer, FALSE);
1052 /* now wait till we played everything */
1053 gst_base_audio_sink_drain (sink);
1055 case GST_EVENT_NEWSEGMENT:
1059 /* we only need the rate */
1060 gst_event_parse_new_segment_full (event, NULL, &rate, NULL, NULL,
1063 GST_DEBUG_OBJECT (sink, "new segment rate of %f", rate);
1072 static GstFlowReturn
1073 gst_base_audio_sink_preroll (GstBaseSink * bsink, GstBuffer * buffer)
1075 GstBaseAudioSink *sink = GST_BASE_AUDIO_SINK (bsink);
1077 if (!gst_ring_buffer_is_acquired (sink->ringbuffer))
1080 /* we don't really do anything when prerolling. We could make a
1081 * property to play this buffer to have some sort of scrubbing
1087 GST_DEBUG_OBJECT (sink, "ringbuffer in wrong state");
1088 GST_ELEMENT_ERROR (sink, STREAM, FORMAT, (NULL), ("sink not negotiated."));
1089 return GST_FLOW_NOT_NEGOTIATED;
1094 gst_base_audio_sink_get_offset (GstBaseAudioSink * sink)
1097 gint writeseg, segdone, sps;
1100 /* assume we can append to the previous sample */
1101 sample = sink->next_sample;
1102 /* no previous sample, try to insert at position 0 */
1106 sps = sink->ringbuffer->samples_per_seg;
1108 /* figure out the segment and the offset inside the segment where
1109 * the sample should be written. */
1110 writeseg = sample / sps;
1112 /* get the currently processed segment */
1113 segdone = g_atomic_int_get (&sink->ringbuffer->segdone)
1114 - sink->ringbuffer->segbase;
1116 /* see how far away it is from the write segment */
1117 diff = writeseg - segdone;
1119 /* sample would be dropped, position to next playable position */
1120 sample = (segdone + 1) * sps;
1127 clock_convert_external (GstClockTime external, GstClockTime cinternal,
1128 GstClockTime cexternal, GstClockTime crate_num, GstClockTime crate_denom)
1130 /* adjust for rate and speed */
1131 if (external >= cexternal) {
1133 gst_util_uint64_scale (external - cexternal, crate_denom, crate_num);
1134 external += cinternal;
1137 gst_util_uint64_scale (cexternal - external, crate_denom, crate_num);
1138 if (cinternal > external)
1139 external = cinternal - external;
1146 /* algorithm to calculate sample positions that will result in resampling to
1147 * match the clock rate of the master */
1149 gst_base_audio_sink_resample_slaving (GstBaseAudioSink * sink,
1150 GstClockTime render_start, GstClockTime render_stop,
1151 GstClockTime * srender_start, GstClockTime * srender_stop)
1153 GstClockTime cinternal, cexternal;
1154 GstClockTime crate_num, crate_denom;
1156 /* FIXME, we can sample and add observations here or use the timeouts on the
1157 * clock. No idea which one is better or more stable. The timeout seems more
1158 * arbitrary but this one seems more demanding and does not work when there is
1159 * no data comming in to the sink. */
1161 GstClockTime etime, itime;
1164 /* sample clocks and figure out clock skew */
1165 etime = gst_clock_get_time (GST_ELEMENT_CLOCK (sink));
1166 itime = gst_audio_clock_get_time (sink->provided_clock);
1168 /* add new observation */
1169 gst_clock_add_observation (sink->provided_clock, itime, etime, &r_squared);
1172 /* get calibration parameters to compensate for speed and offset differences
1173 * when we are slaved */
1174 gst_clock_get_calibration (sink->provided_clock, &cinternal, &cexternal,
1175 &crate_num, &crate_denom);
1177 GST_DEBUG_OBJECT (sink, "internal %" GST_TIME_FORMAT " external %"
1178 GST_TIME_FORMAT " %" G_GUINT64_FORMAT "/%" G_GUINT64_FORMAT " = %f",
1179 GST_TIME_ARGS (cinternal), GST_TIME_ARGS (cexternal), crate_num,
1180 crate_denom, gst_guint64_to_gdouble (crate_num) /
1181 gst_guint64_to_gdouble (crate_denom));
1184 crate_denom = crate_num = 1;
1186 /* bring external time to internal time */
1187 render_start = clock_convert_external (render_start, cinternal, cexternal,
1188 crate_num, crate_denom);
1189 render_stop = clock_convert_external (render_stop, cinternal, cexternal,
1190 crate_num, crate_denom);
1192 GST_DEBUG_OBJECT (sink,
1193 "after slaving: start %" GST_TIME_FORMAT " - stop %" GST_TIME_FORMAT,
1194 GST_TIME_ARGS (render_start), GST_TIME_ARGS (render_stop));
1196 *srender_start = render_start;
1197 *srender_stop = render_stop;
1200 /* algorithm to calculate sample positions that will result in changing the
1201 * playout pointer to match the clock rate of the master */
1203 gst_base_audio_sink_skew_slaving (GstBaseAudioSink * sink,
1204 GstClockTime render_start, GstClockTime render_stop,
1205 GstClockTime * srender_start, GstClockTime * srender_stop)
1207 GstClockTime cinternal, cexternal, crate_num, crate_denom;
1208 GstClockTime etime, itime;
1209 GstClockTimeDiff skew, mdrift, mdrift2;
1213 /* get calibration parameters to compensate for offsets */
1214 gst_clock_get_calibration (sink->provided_clock, &cinternal, &cexternal,
1215 &crate_num, &crate_denom);
1217 /* sample clocks and figure out clock skew */
1218 etime = gst_clock_get_time (GST_ELEMENT_CLOCK (sink));
1219 itime = gst_audio_clock_get_time (sink->provided_clock);
1220 itime = gst_audio_clock_adjust (sink->provided_clock, itime);
1222 GST_DEBUG_OBJECT (sink,
1223 "internal %" GST_TIME_FORMAT " external %" GST_TIME_FORMAT
1224 " cinternal %" GST_TIME_FORMAT " cexternal %" GST_TIME_FORMAT,
1225 GST_TIME_ARGS (itime), GST_TIME_ARGS (etime),
1226 GST_TIME_ARGS (cinternal), GST_TIME_ARGS (cexternal));
1228 /* make sure we never go below 0 */
1229 etime = etime > cexternal ? etime - cexternal : 0;
1230 itime = itime > cinternal ? itime - cinternal : 0;
1232 /* do itime - etime.
1233 * positive value means external clock goes slower
1234 * negative value means external clock goes faster */
1235 skew = GST_CLOCK_DIFF (etime, itime);
1236 if (sink->priv->avg_skew == -1) {
1237 /* first observation */
1238 sink->priv->avg_skew = skew;
1240 /* next observations use a moving average */
1241 sink->priv->avg_skew = (31 * sink->priv->avg_skew + skew) / 32;
1244 GST_DEBUG_OBJECT (sink, "internal %" GST_TIME_FORMAT " external %"
1245 GST_TIME_FORMAT " skew %" G_GINT64_FORMAT " avg %" G_GINT64_FORMAT,
1246 GST_TIME_ARGS (itime), GST_TIME_ARGS (etime), skew, sink->priv->avg_skew);
1248 /* the max drift we allow */
1249 mdrift = sink->priv->drift_tolerance * 1000;
1250 mdrift2 = mdrift / 2;
1252 /* adjust playout pointer based on skew */
1253 if (sink->priv->avg_skew > mdrift2) {
1254 /* master is running slower, move internal time forward */
1255 GST_WARNING_OBJECT (sink,
1256 "correct clock skew %" G_GINT64_FORMAT " > %" G_GINT64_FORMAT,
1257 sink->priv->avg_skew, mdrift2);
1258 cexternal = cexternal > mdrift ? cexternal - mdrift : 0;
1259 sink->priv->avg_skew -= mdrift;
1261 driftsamples = (sink->ringbuffer->spec.rate * mdrift) / GST_SECOND;
1262 last_align = sink->priv->last_align;
1264 /* if we were aligning in the wrong direction or we aligned more than what we
1265 * will correct, resync */
1266 if (last_align < 0 || last_align > driftsamples)
1267 sink->next_sample = -1;
1269 GST_DEBUG_OBJECT (sink,
1270 "last_align %" G_GINT64_FORMAT " driftsamples %u, next %"
1271 G_GUINT64_FORMAT, last_align, driftsamples, sink->next_sample);
1273 gst_clock_set_calibration (sink->provided_clock, cinternal, cexternal,
1274 crate_num, crate_denom);
1275 } else if (sink->priv->avg_skew < -mdrift2) {
1276 /* master is running faster, move external time forwards */
1277 GST_WARNING_OBJECT (sink,
1278 "correct clock skew %" G_GINT64_FORMAT " < %" G_GINT64_FORMAT,
1279 sink->priv->avg_skew, -mdrift2);
1280 cexternal += mdrift;
1281 sink->priv->avg_skew += mdrift;
1283 driftsamples = (sink->ringbuffer->spec.rate * mdrift) / GST_SECOND;
1284 last_align = sink->priv->last_align;
1286 /* if we were aligning in the wrong direction or we aligned more than what we
1287 * will correct, resync */
1288 if (last_align > 0 || -last_align > driftsamples)
1289 sink->next_sample = -1;
1291 GST_DEBUG_OBJECT (sink,
1292 "last_align %" G_GINT64_FORMAT " driftsamples %u, next %"
1293 G_GUINT64_FORMAT, last_align, driftsamples, sink->next_sample);
1295 gst_clock_set_calibration (sink->provided_clock, cinternal, cexternal,
1296 crate_num, crate_denom);
1299 /* convert, ignoring speed */
1300 render_start = clock_convert_external (render_start, cinternal, cexternal,
1301 crate_num, crate_denom);
1302 render_stop = clock_convert_external (render_stop, cinternal, cexternal,
1303 crate_num, crate_denom);
1305 *srender_start = render_start;
1306 *srender_stop = render_stop;
1309 /* apply the clock offset but do no slaving otherwise */
1311 gst_base_audio_sink_none_slaving (GstBaseAudioSink * sink,
1312 GstClockTime render_start, GstClockTime render_stop,
1313 GstClockTime * srender_start, GstClockTime * srender_stop)
1315 GstClockTime cinternal, cexternal, crate_num, crate_denom;
1317 /* get calibration parameters to compensate for offsets */
1318 gst_clock_get_calibration (sink->provided_clock, &cinternal, &cexternal,
1319 &crate_num, &crate_denom);
1321 /* convert, ignoring speed */
1322 render_start = clock_convert_external (render_start, cinternal, cexternal,
1323 crate_num, crate_denom);
1324 render_stop = clock_convert_external (render_stop, cinternal, cexternal,
1325 crate_num, crate_denom);
1327 *srender_start = render_start;
1328 *srender_stop = render_stop;
1331 /* converts render_start and render_stop to their slaved values */
1333 gst_base_audio_sink_handle_slaving (GstBaseAudioSink * sink,
1334 GstClockTime render_start, GstClockTime render_stop,
1335 GstClockTime * srender_start, GstClockTime * srender_stop)
1337 switch (sink->priv->slave_method) {
1338 case GST_BASE_AUDIO_SINK_SLAVE_RESAMPLE:
1339 gst_base_audio_sink_resample_slaving (sink, render_start, render_stop,
1340 srender_start, srender_stop);
1342 case GST_BASE_AUDIO_SINK_SLAVE_SKEW:
1343 gst_base_audio_sink_skew_slaving (sink, render_start, render_stop,
1344 srender_start, srender_stop);
1346 case GST_BASE_AUDIO_SINK_SLAVE_NONE:
1347 gst_base_audio_sink_none_slaving (sink, render_start, render_stop,
1348 srender_start, srender_stop);
1351 g_warning ("unknown slaving method %d", sink->priv->slave_method);
1356 /* must be called with LOCK */
1357 static GstFlowReturn
1358 gst_base_audio_sink_sync_latency (GstBaseSink * bsink, GstMiniObject * obj)
1361 GstClockReturn status;
1362 GstClockTime time, render_delay;
1364 GstBaseAudioSink *sink;
1365 GstClockTime itime, etime;
1366 GstClockTime rate_num, rate_denom;
1367 GstClockTimeDiff jitter;
1369 sink = GST_BASE_AUDIO_SINK (bsink);
1371 clock = GST_ELEMENT_CLOCK (sink);
1372 if (G_UNLIKELY (clock == NULL))
1375 /* we provided the global clock, don't need to do anything special */
1376 if (clock == sink->provided_clock)
1379 GST_OBJECT_UNLOCK (sink);
1382 GST_DEBUG_OBJECT (sink, "checking preroll");
1384 ret = gst_base_sink_do_preroll (bsink, obj);
1385 if (ret != GST_FLOW_OK)
1388 GST_OBJECT_LOCK (sink);
1389 time = sink->priv->us_latency;
1390 GST_OBJECT_UNLOCK (sink);
1392 /* Renderdelay is added onto our own latency, and needs
1393 * to be subtracted as well */
1394 render_delay = gst_base_sink_get_render_delay (bsink);
1396 if (G_LIKELY (time > render_delay))
1397 time -= render_delay;
1401 /* preroll done, we can sync since we are in PLAYING now. */
1402 GST_DEBUG_OBJECT (sink, "possibly waiting for clock to reach %"
1403 GST_TIME_FORMAT, GST_TIME_ARGS (time));
1405 /* wait for the clock, this can be interrupted because we got shut down or
1407 status = gst_base_sink_wait_clock (bsink, time, &jitter);
1409 GST_DEBUG_OBJECT (sink, "clock returned %d %" GST_TIME_FORMAT, status,
1410 GST_TIME_ARGS (jitter));
1412 /* invalid time, no clock or sync disabled, just continue then */
1413 if (status == GST_CLOCK_BADTIME)
1416 /* waiting could have been interrupted and we can be flushing now */
1417 if (G_UNLIKELY (bsink->flushing))
1420 /* retry if we got unscheduled, which means we did not reach the timeout
1421 * yet. if some other error occures, we continue. */
1422 } while (status == GST_CLOCK_UNSCHEDULED);
1424 GST_OBJECT_LOCK (sink);
1425 GST_DEBUG_OBJECT (sink, "latency synced");
1427 /* when we prerolled in time, we can accurately set the calibration,
1428 * our internal clock should exactly have been the latency (== the running
1429 * time of the external clock) */
1430 etime = GST_ELEMENT_CAST (sink)->base_time + time;
1431 itime = gst_audio_clock_get_time (sink->provided_clock);
1432 itime = gst_audio_clock_adjust (sink->provided_clock, itime);
1434 if (status == GST_CLOCK_EARLY) {
1435 /* when we prerolled late, we have to take into account the lateness */
1436 GST_DEBUG_OBJECT (sink, "late preroll, adding jitter");
1440 /* start ringbuffer so we can start slaving right away when we need to */
1441 gst_ring_buffer_start (sink->ringbuffer);
1443 GST_DEBUG_OBJECT (sink,
1444 "internal time: %" GST_TIME_FORMAT " external time: %" GST_TIME_FORMAT,
1445 GST_TIME_ARGS (itime), GST_TIME_ARGS (etime));
1447 /* copy the original calibrated rate but update the internal and external
1449 gst_clock_get_calibration (sink->provided_clock, NULL, NULL, &rate_num,
1451 gst_clock_set_calibration (sink->provided_clock, itime, etime,
1452 rate_num, rate_denom);
1454 switch (sink->priv->slave_method) {
1455 case GST_BASE_AUDIO_SINK_SLAVE_RESAMPLE:
1456 /* only set as master when we are resampling */
1457 GST_DEBUG_OBJECT (sink, "Setting clock as master");
1458 gst_clock_set_master (sink->provided_clock, clock);
1460 case GST_BASE_AUDIO_SINK_SLAVE_SKEW:
1461 case GST_BASE_AUDIO_SINK_SLAVE_NONE:
1466 sink->priv->avg_skew = -1;
1467 sink->next_sample = -1;
1468 sink->priv->eos_time = -1;
1469 sink->priv->discont_time = -1;
1476 GST_DEBUG_OBJECT (sink, "we have no clock");
1481 GST_DEBUG_OBJECT (sink, "we are not slaved");
1486 GST_DEBUG_OBJECT (sink, "we are flushing");
1487 GST_OBJECT_LOCK (sink);
1488 return GST_FLOW_WRONG_STATE;
1493 gst_base_audio_sink_get_alignment (GstBaseAudioSink * sink,
1494 GstClockTime sample_offset)
1496 GstRingBuffer *ringbuf = sink->ringbuffer;
1499 gint64 max_sample_diff;
1500 gint segdone = g_atomic_int_get (&ringbuf->segdone) - ringbuf->segbase;
1501 gint64 samples_done = segdone * ringbuf->samples_per_seg;
1502 gint64 headroom = sample_offset - samples_done;
1503 gboolean allow_align = TRUE;
1504 gboolean discont = FALSE;
1506 /* now try to align the sample to the previous one, first see how big the
1508 if (sample_offset >= sink->next_sample)
1509 sample_diff = sample_offset - sink->next_sample;
1511 sample_diff = sink->next_sample - sample_offset;
1513 /* calculate the max allowed drift in units of samples. */
1514 max_sample_diff = gst_util_uint64_scale_int (sink->priv->alignment_threshold,
1515 ringbuf->spec.rate, GST_SECOND);
1517 /* calc align with previous sample */
1518 align = sink->next_sample - sample_offset;
1520 /* don't align if it means writing behind the read-segment */
1521 if (sample_diff > headroom && align < 0)
1522 allow_align = FALSE;
1524 if (G_UNLIKELY (sample_diff >= max_sample_diff)) {
1525 /* wait before deciding to make a discontinuity */
1526 if (sink->priv->discont_wait > 0) {
1527 GstClockTime time = gst_util_uint64_scale_int (sample_offset,
1528 GST_SECOND, ringbuf->spec.rate);
1529 if (sink->priv->discont_time == -1) {
1530 /* discont candidate */
1531 sink->priv->discont_time = time;
1532 } else if (time - sink->priv->discont_time >= sink->priv->discont_wait) {
1533 /* discont_wait expired, discontinuity detected */
1535 sink->priv->discont_time = -1;
1540 } else if (G_UNLIKELY (sink->priv->discont_time != -1)) {
1541 /* we have had a discont, but are now back on track! */
1542 sink->priv->discont_time = -1;
1545 if (G_LIKELY (!discont && allow_align)) {
1546 GST_DEBUG_OBJECT (sink,
1547 "align with prev sample, ABS (%" G_GINT64_FORMAT ") < %"
1548 G_GINT64_FORMAT, align, max_sample_diff);
1550 gint64 diff_s G_GNUC_UNUSED;
1552 /* calculate sample diff in seconds for error message */
1554 gst_util_uint64_scale_int (sample_diff, GST_SECOND, ringbuf->spec.rate);
1556 /* timestamps drifted apart from previous samples too much, we need to
1557 * resync. We log this as an element warning. */
1558 GST_WARNING_OBJECT (sink,
1559 "Unexpected discontinuity in audio timestamps of "
1560 "%s%" GST_TIME_FORMAT ", resyncing",
1561 sample_offset > sink->next_sample ? "+" : "-", GST_TIME_ARGS (diff_s));
1568 static GstFlowReturn
1569 gst_base_audio_sink_render (GstBaseSink * bsink, GstBuffer * buf)
1572 GstClockTime time, stop, render_start, render_stop, sample_offset;
1573 GstClockTimeDiff sync_offset, ts_offset;
1574 GstBaseAudioSinkClass *bclass;
1575 GstBaseAudioSink *sink;
1576 GstRingBuffer *ringbuf;
1577 gint64 diff, align, ctime, cstop;
1580 guint samples, written;
1584 GstClockTime base_time, render_delay, latency;
1586 gboolean sync, slaved, align_next;
1588 GstSegment clip_seg;
1590 GstBuffer *out = NULL;
1592 sink = GST_BASE_AUDIO_SINK (bsink);
1593 bclass = GST_BASE_AUDIO_SINK_GET_CLASS (sink);
1595 ringbuf = sink->ringbuffer;
1597 /* can't do anything when we don't have the device */
1598 if (G_UNLIKELY (!gst_ring_buffer_is_acquired (ringbuf)))
1601 /* Wait for upstream latency before starting the ringbuffer, we do this so
1602 * that we can align the first sample of the ringbuffer to the base_time +
1604 GST_OBJECT_LOCK (sink);
1605 base_time = GST_ELEMENT_CAST (sink)->base_time;
1606 if (G_UNLIKELY (sink->priv->sync_latency)) {
1607 ret = gst_base_audio_sink_sync_latency (bsink, GST_MINI_OBJECT_CAST (buf));
1608 GST_OBJECT_UNLOCK (sink);
1609 if (G_UNLIKELY (ret != GST_FLOW_OK))
1610 goto sync_latency_failed;
1611 /* only do this once until we are set back to PLAYING */
1612 sink->priv->sync_latency = FALSE;
1614 GST_OBJECT_UNLOCK (sink);
1617 /* Before we go on, let's see if we need to payload the data. If yes, we also
1618 * need to unref the output buffer before leaving. */
1619 if (bclass->payload) {
1620 out = bclass->payload (sink, buf);
1623 goto payload_failed;
1628 bps = ringbuf->spec.bytes_per_sample;
1630 size = GST_BUFFER_SIZE (buf);
1631 if (G_UNLIKELY (size % bps) != 0)
1634 samples = size / bps;
1635 out_samples = samples;
1637 in_offset = GST_BUFFER_OFFSET (buf);
1638 time = GST_BUFFER_TIMESTAMP (buf);
1640 GST_DEBUG_OBJECT (sink,
1641 "time %" GST_TIME_FORMAT ", offset %" G_GUINT64_FORMAT ", start %"
1642 GST_TIME_FORMAT ", samples %u", GST_TIME_ARGS (time), in_offset,
1643 GST_TIME_ARGS (bsink->segment.start), samples);
1645 data = GST_BUFFER_DATA (buf);
1647 /* if not valid timestamp or we can't clip or sync, try to play
1649 if (!GST_CLOCK_TIME_IS_VALID (time)) {
1650 render_start = gst_base_audio_sink_get_offset (sink);
1651 render_stop = render_start + samples;
1652 GST_DEBUG_OBJECT (sink,
1653 "Buffer of size %u has no time. Using render_start=%" G_GUINT64_FORMAT,
1654 GST_BUFFER_SIZE (buf), render_start);
1655 /* we don't have a start so we don't know stop either */
1660 /* let's calc stop based on the number of samples in the buffer instead
1661 * of trusting the DURATION */
1662 stop = time + gst_util_uint64_scale_int (samples, GST_SECOND,
1663 ringbuf->spec.rate);
1665 /* prepare the clipping segment. Since we will be subtracting ts-offset and
1666 * device-delay later we scale the start and stop with those values so that we
1667 * can correctly clip them */
1668 clip_seg.format = GST_FORMAT_TIME;
1669 clip_seg.start = bsink->segment.start;
1670 clip_seg.stop = bsink->segment.stop;
1671 clip_seg.duration = -1;
1673 /* the sync offset is the combination of ts-offset and device-delay */
1674 latency = gst_base_sink_get_latency (bsink);
1675 ts_offset = gst_base_sink_get_ts_offset (bsink);
1676 render_delay = gst_base_sink_get_render_delay (bsink);
1677 sync_offset = ts_offset - render_delay + latency;
1679 GST_DEBUG_OBJECT (sink,
1680 "sync-offset %" G_GINT64_FORMAT ", render-delay %" GST_TIME_FORMAT
1681 ", ts-offset %" G_GINT64_FORMAT, sync_offset,
1682 GST_TIME_ARGS (render_delay), ts_offset);
1684 /* compensate for ts-offset and device-delay when negative we need to
1686 if (sync_offset < 0) {
1687 clip_seg.start += -sync_offset;
1688 if (clip_seg.stop != -1)
1689 clip_seg.stop += -sync_offset;
1692 /* samples should be rendered based on their timestamp. All samples
1693 * arriving before the segment.start or after segment.stop are to be
1694 * thrown away. All samples should also be clipped to the segment
1696 if (!gst_segment_clip (&clip_seg, GST_FORMAT_TIME, time, stop, &ctime,
1698 goto out_of_segment;
1700 /* see if some clipping happened */
1701 diff = ctime - time;
1703 /* bring clipped time to samples */
1704 diff = gst_util_uint64_scale_int (diff, ringbuf->spec.rate, GST_SECOND);
1705 GST_DEBUG_OBJECT (sink, "clipping start to %" GST_TIME_FORMAT " %"
1706 G_GUINT64_FORMAT " samples", GST_TIME_ARGS (ctime), diff);
1711 diff = stop - cstop;
1713 /* bring clipped time to samples */
1714 diff = gst_util_uint64_scale_int (diff, ringbuf->spec.rate, GST_SECOND);
1715 GST_DEBUG_OBJECT (sink, "clipping stop to %" GST_TIME_FORMAT " %"
1716 G_GUINT64_FORMAT " samples", GST_TIME_ARGS (cstop), diff);
1721 /* figure out how to sync */
1722 if ((clock = GST_ELEMENT_CLOCK (bsink)))
1728 /* no sync needed, play sample ASAP */
1729 render_start = gst_base_audio_sink_get_offset (sink);
1730 render_stop = render_start + samples;
1731 GST_DEBUG_OBJECT (sink,
1732 "no sync needed. Using render_start=%" G_GUINT64_FORMAT, render_start);
1736 /* bring buffer start and stop times to running time */
1738 gst_segment_to_running_time (&bsink->segment, GST_FORMAT_TIME, time);
1740 gst_segment_to_running_time (&bsink->segment, GST_FORMAT_TIME, stop);
1742 GST_DEBUG_OBJECT (sink,
1743 "running: start %" GST_TIME_FORMAT " - stop %" GST_TIME_FORMAT,
1744 GST_TIME_ARGS (render_start), GST_TIME_ARGS (render_stop));
1746 /* store the time of the last sample, we'll use this to perform sync on the
1747 * last sample when draining the buffer */
1748 if (bsink->segment.rate >= 0.0) {
1749 sink->priv->eos_time = render_stop;
1751 sink->priv->eos_time = render_start;
1754 /* compensate for ts-offset and delay we know this will not underflow because we
1756 GST_DEBUG_OBJECT (sink,
1757 "compensating for sync-offset %" GST_TIME_FORMAT,
1758 GST_TIME_ARGS (sync_offset));
1759 render_start += sync_offset;
1760 render_stop += sync_offset;
1762 GST_DEBUG_OBJECT (sink, "adding base_time %" GST_TIME_FORMAT,
1763 GST_TIME_ARGS (base_time));
1765 /* add base time to sync against the clock */
1766 render_start += base_time;
1767 render_stop += base_time;
1769 GST_DEBUG_OBJECT (sink,
1770 "after compensation: start %" GST_TIME_FORMAT " - stop %" GST_TIME_FORMAT,
1771 GST_TIME_ARGS (render_start), GST_TIME_ARGS (render_stop));
1773 if ((slaved = clock != sink->provided_clock)) {
1774 /* handle clock slaving */
1775 gst_base_audio_sink_handle_slaving (sink, render_start, render_stop,
1776 &render_start, &render_stop);
1778 /* no slaving needed but we need to adapt to the clock calibration
1780 gst_base_audio_sink_none_slaving (sink, render_start, render_stop,
1781 &render_start, &render_stop);
1784 GST_DEBUG_OBJECT (sink,
1785 "final timestamps: start %" GST_TIME_FORMAT " - stop %" GST_TIME_FORMAT,
1786 GST_TIME_ARGS (render_start), GST_TIME_ARGS (render_stop));
1788 /* bring to position in the ringbuffer */
1789 if (sink->priv->do_time_offset) {
1791 GST_AUDIO_CLOCK_CAST (sink->provided_clock)->abidata.ABI.time_offset;
1792 GST_DEBUG_OBJECT (sink,
1793 "time offset %" GST_TIME_FORMAT, GST_TIME_ARGS (time_offset));
1794 if (render_start > time_offset)
1795 render_start -= time_offset;
1798 if (render_stop > time_offset)
1799 render_stop -= time_offset;
1804 /* in some clock slaving cases, all late samples end up at 0 first,
1805 * and subsequent ones align with that until threshold exceeded,
1806 * and then sync back to 0 and so on, so avoid that altogether */
1807 if (G_UNLIKELY (render_start == 0 && render_stop == 0))
1810 /* and bring the time to the rate corrected offset in the buffer */
1811 render_start = gst_util_uint64_scale_int (render_start,
1812 ringbuf->spec.rate, GST_SECOND);
1813 render_stop = gst_util_uint64_scale_int (render_stop,
1814 ringbuf->spec.rate, GST_SECOND);
1816 /* positive playback rate, first sample is render_start, negative rate, first
1817 * sample is render_stop. When no rate conversion is active, render exactly
1818 * the amount of input samples to avoid aligning to rounding errors. */
1819 if (bsink->segment.rate >= 0.0) {
1820 sample_offset = render_start;
1821 if (bsink->segment.rate == 1.0)
1822 render_stop = sample_offset + samples;
1824 sample_offset = render_stop;
1825 if (bsink->segment.rate == -1.0)
1826 render_start = sample_offset + samples;
1829 /* always resync after a discont */
1830 if (G_UNLIKELY (GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_DISCONT))) {
1831 GST_DEBUG_OBJECT (sink, "resync after discont");
1835 /* resync when we don't know what to align the sample with */
1836 if (G_UNLIKELY (sink->next_sample == -1)) {
1837 GST_DEBUG_OBJECT (sink,
1838 "no align possible: no previous sample position known");
1842 align = gst_base_audio_sink_get_alignment (sink, sample_offset);
1843 sink->priv->last_align = align;
1845 /* apply alignment */
1846 render_start += align;
1848 /* only align stop if we are not slaved to resample */
1849 if (slaved && sink->priv->slave_method == GST_BASE_AUDIO_SINK_SLAVE_RESAMPLE) {
1850 GST_DEBUG_OBJECT (sink, "no stop time align needed: we are slaved");
1853 render_stop += align;
1856 /* number of target samples is difference between start and stop */
1857 out_samples = render_stop - render_start;
1860 /* we render the first or last sample first, depending on the rate */
1861 if (bsink->segment.rate >= 0.0)
1862 sample_offset = render_start;
1864 sample_offset = render_stop;
1866 GST_DEBUG_OBJECT (sink, "rendering at %" G_GUINT64_FORMAT " %d/%d",
1867 sample_offset, samples, out_samples);
1869 /* we need to accumulate over different runs for when we get interrupted */
1874 gst_ring_buffer_commit_full (ringbuf, &sample_offset, data, samples,
1875 out_samples, &accum);
1877 GST_DEBUG_OBJECT (sink, "wrote %u of %u", written, samples);
1878 /* if we wrote all, we're done */
1879 if (written == samples)
1882 /* else something interrupted us and we wait for preroll. */
1883 if ((ret = gst_base_sink_wait_preroll (bsink)) != GST_FLOW_OK)
1886 /* if we got interrupted, we cannot assume that the next sample should
1887 * be aligned to this one */
1890 /* update the output samples. FIXME, this will just skip them when pausing
1891 * during trick mode */
1892 if (out_samples > written) {
1893 out_samples -= written;
1899 data += written * bps;
1903 sink->next_sample = sample_offset;
1905 sink->next_sample = -1;
1907 GST_DEBUG_OBJECT (sink, "next sample expected at %" G_GUINT64_FORMAT,
1910 if (GST_CLOCK_TIME_IS_VALID (stop) && stop >= bsink->segment.stop) {
1911 GST_DEBUG_OBJECT (sink,
1912 "start playback because we are at the end of segment");
1913 gst_ring_buffer_start (ringbuf);
1920 gst_buffer_unref (out);
1927 GST_DEBUG_OBJECT (sink,
1928 "dropping sample out of segment time %" GST_TIME_FORMAT ", start %"
1929 GST_TIME_FORMAT, GST_TIME_ARGS (time),
1930 GST_TIME_ARGS (bsink->segment.start));
1936 GST_DEBUG_OBJECT (sink, "dropping late sample");
1943 GST_ELEMENT_ERROR (sink, STREAM, FORMAT, (NULL), ("failed to payload."));
1944 ret = GST_FLOW_ERROR;
1949 GST_DEBUG_OBJECT (sink, "ringbuffer not negotiated");
1950 GST_ELEMENT_ERROR (sink, STREAM, FORMAT, (NULL), ("sink not negotiated."));
1951 ret = GST_FLOW_NOT_NEGOTIATED;
1956 GST_DEBUG_OBJECT (sink, "wrong size");
1957 GST_ELEMENT_ERROR (sink, STREAM, WRONG_TYPE,
1958 (NULL), ("sink received buffer of wrong size."));
1959 ret = GST_FLOW_ERROR;
1964 GST_DEBUG_OBJECT (sink, "preroll got interrupted: %d (%s)", ret,
1965 gst_flow_get_name (ret));
1968 sync_latency_failed:
1970 GST_DEBUG_OBJECT (sink, "failed waiting for latency");
1976 * gst_base_audio_sink_create_ringbuffer:
1977 * @sink: a #GstBaseAudioSink.
1979 * Create and return the #GstRingBuffer for @sink. This function will call the
1980 * ::create_ringbuffer vmethod and will set @sink as the parent of the returned
1981 * buffer (see gst_object_set_parent()).
1983 * Returns: The new ringbuffer of @sink.
1986 gst_base_audio_sink_create_ringbuffer (GstBaseAudioSink * sink)
1988 GstBaseAudioSinkClass *bclass;
1989 GstRingBuffer *buffer = NULL;
1991 bclass = GST_BASE_AUDIO_SINK_GET_CLASS (sink);
1992 if (bclass->create_ringbuffer)
1993 buffer = bclass->create_ringbuffer (sink);
1996 gst_object_set_parent (GST_OBJECT (buffer), GST_OBJECT (sink));
2002 gst_base_audio_sink_callback (GstRingBuffer * rbuf, guint8 * data, guint len,
2005 GstBaseSink *basesink;
2006 GstBaseAudioSink *sink;
2010 basesink = GST_BASE_SINK (user_data);
2011 sink = GST_BASE_AUDIO_SINK (user_data);
2013 GST_PAD_STREAM_LOCK (basesink->sinkpad);
2015 /* would be nice to arrange for pad_alloc_buffer to return data -- as it is we
2016 will copy twice, once into data, once into DMA */
2017 GST_LOG_OBJECT (basesink, "pulling %d bytes offset %" G_GUINT64_FORMAT
2018 " to fill audio buffer", len, basesink->offset);
2020 gst_pad_pull_range (basesink->sinkpad, basesink->segment.last_stop, len,
2023 if (ret != GST_FLOW_OK) {
2024 if (ret == GST_FLOW_UNEXPECTED)
2030 GST_PAD_PREROLL_LOCK (basesink->sinkpad);
2031 if (basesink->flushing)
2034 /* complete preroll and wait for PLAYING */
2035 ret = gst_base_sink_do_preroll (basesink, GST_MINI_OBJECT_CAST (buf));
2036 if (ret != GST_FLOW_OK)
2039 if (len != GST_BUFFER_SIZE (buf)) {
2040 GST_INFO_OBJECT (basesink,
2041 "got different size than requested from sink pad: %u != %u", len,
2042 GST_BUFFER_SIZE (buf));
2043 len = MIN (GST_BUFFER_SIZE (buf), len);
2046 basesink->segment.last_stop += len;
2048 memcpy (data, GST_BUFFER_DATA (buf), len);
2049 GST_PAD_PREROLL_UNLOCK (basesink->sinkpad);
2051 GST_PAD_STREAM_UNLOCK (basesink->sinkpad);
2057 GST_WARNING_OBJECT (basesink, "Got flow '%s' but can't return it: %d",
2058 gst_flow_get_name (ret), ret);
2059 gst_ring_buffer_pause (rbuf);
2060 GST_PAD_STREAM_UNLOCK (basesink->sinkpad);
2065 /* FIXME: this is not quite correct; we'll be called endlessly until
2066 * the sink gets shut down; maybe we should set a flag somewhere, or
2067 * set segment.stop and segment.duration to the last sample or so */
2068 GST_DEBUG_OBJECT (sink, "EOS");
2069 gst_base_audio_sink_drain (sink);
2070 gst_ring_buffer_pause (rbuf);
2071 gst_element_post_message (GST_ELEMENT_CAST (sink),
2072 gst_message_new_eos (GST_OBJECT_CAST (sink)));
2073 GST_PAD_STREAM_UNLOCK (basesink->sinkpad);
2077 GST_DEBUG_OBJECT (sink, "we are flushing");
2078 gst_ring_buffer_pause (rbuf);
2079 GST_PAD_PREROLL_UNLOCK (basesink->sinkpad);
2080 GST_PAD_STREAM_UNLOCK (basesink->sinkpad);
2085 GST_DEBUG_OBJECT (sink, "error %s", gst_flow_get_name (ret));
2086 gst_ring_buffer_pause (rbuf);
2087 GST_PAD_PREROLL_UNLOCK (basesink->sinkpad);
2088 GST_PAD_STREAM_UNLOCK (basesink->sinkpad);
2094 gst_base_audio_sink_activate_pull (GstBaseSink * basesink, gboolean active)
2097 GstBaseAudioSink *sink = GST_BASE_AUDIO_SINK (basesink);
2100 GST_DEBUG_OBJECT (basesink, "activating pull");
2102 gst_ring_buffer_set_callback (sink->ringbuffer,
2103 gst_base_audio_sink_callback, sink);
2105 ret = gst_ring_buffer_activate (sink->ringbuffer, TRUE);
2107 GST_DEBUG_OBJECT (basesink, "deactivating pull");
2108 gst_ring_buffer_set_callback (sink->ringbuffer, NULL, NULL);
2109 ret = gst_ring_buffer_activate (sink->ringbuffer, FALSE);
2115 /* should be called with the LOCK */
2116 static GstStateChangeReturn
2117 gst_base_audio_sink_async_play (GstBaseSink * basesink)
2119 GstBaseAudioSink *sink;
2121 sink = GST_BASE_AUDIO_SINK (basesink);
2123 GST_DEBUG_OBJECT (sink, "ringbuffer may start now");
2124 sink->priv->sync_latency = TRUE;
2125 gst_ring_buffer_may_start (sink->ringbuffer, TRUE);
2126 if (basesink->pad_mode == GST_ACTIVATE_PULL) {
2127 /* we always start the ringbuffer in pull mode immediately */
2128 gst_ring_buffer_start (sink->ringbuffer);
2131 return GST_STATE_CHANGE_SUCCESS;
2134 static GstStateChangeReturn
2135 gst_base_audio_sink_change_state (GstElement * element,
2136 GstStateChange transition)
2138 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
2139 GstBaseAudioSink *sink = GST_BASE_AUDIO_SINK (element);
2141 switch (transition) {
2142 case GST_STATE_CHANGE_NULL_TO_READY:
2143 if (sink->ringbuffer == NULL) {
2144 gst_audio_clock_reset (GST_AUDIO_CLOCK (sink->provided_clock), 0);
2145 sink->ringbuffer = gst_base_audio_sink_create_ringbuffer (sink);
2147 if (!gst_ring_buffer_open_device (sink->ringbuffer))
2150 case GST_STATE_CHANGE_READY_TO_PAUSED:
2151 sink->next_sample = -1;
2152 sink->priv->last_align = -1;
2153 sink->priv->eos_time = -1;
2154 sink->priv->discont_time = -1;
2155 gst_ring_buffer_set_flushing (sink->ringbuffer, FALSE);
2156 gst_ring_buffer_may_start (sink->ringbuffer, FALSE);
2158 /* Only post clock-provide messages if this is the clock that
2159 * we've created. If the subclass has overriden it the subclass
2160 * should post this messages whenever necessary */
2161 if (sink->provided_clock && GST_IS_AUDIO_CLOCK (sink->provided_clock) &&
2162 GST_AUDIO_CLOCK_CAST (sink->provided_clock)->func ==
2163 (GstAudioClockGetTimeFunc) gst_base_audio_sink_get_time)
2164 gst_element_post_message (element,
2165 gst_message_new_clock_provide (GST_OBJECT_CAST (element),
2166 sink->provided_clock, TRUE));
2168 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
2172 GST_OBJECT_LOCK (sink);
2173 GST_DEBUG_OBJECT (sink, "ringbuffer may start now");
2174 sink->priv->sync_latency = TRUE;
2175 eos = GST_BASE_SINK (sink)->eos;
2176 GST_OBJECT_UNLOCK (sink);
2178 gst_ring_buffer_may_start (sink->ringbuffer, TRUE);
2179 if (GST_BASE_SINK_CAST (sink)->pad_mode == GST_ACTIVATE_PULL ||
2180 g_atomic_int_get (&sink->abidata.ABI.eos_rendering) || eos) {
2181 /* we always start the ringbuffer in pull mode immediately */
2182 /* sync rendering on eos needs running clock,
2183 * and others need running clock when finished rendering eos */
2184 gst_ring_buffer_start (sink->ringbuffer);
2188 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
2189 /* ringbuffer cannot start anymore */
2190 gst_ring_buffer_may_start (sink->ringbuffer, FALSE);
2191 gst_ring_buffer_pause (sink->ringbuffer);
2193 GST_OBJECT_LOCK (sink);
2194 sink->priv->sync_latency = FALSE;
2195 GST_OBJECT_UNLOCK (sink);
2197 case GST_STATE_CHANGE_PAUSED_TO_READY:
2198 /* Only post clock-lost messages if this is the clock that
2199 * we've created. If the subclass has overriden it the subclass
2200 * should post this messages whenever necessary */
2201 if (sink->provided_clock && GST_IS_AUDIO_CLOCK (sink->provided_clock) &&
2202 GST_AUDIO_CLOCK_CAST (sink->provided_clock)->func ==
2203 (GstAudioClockGetTimeFunc) gst_base_audio_sink_get_time)
2204 gst_element_post_message (element,
2205 gst_message_new_clock_lost (GST_OBJECT_CAST (element),
2206 sink->provided_clock));
2208 /* make sure we unblock before calling the parent state change
2209 * so it can grab the STREAM_LOCK */
2210 gst_ring_buffer_set_flushing (sink->ringbuffer, TRUE);
2216 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
2218 switch (transition) {
2219 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
2220 /* stop slaving ourselves to the master, if any */
2221 gst_clock_set_master (sink->provided_clock, NULL);
2223 case GST_STATE_CHANGE_PAUSED_TO_READY:
2224 gst_ring_buffer_activate (sink->ringbuffer, FALSE);
2225 gst_ring_buffer_release (sink->ringbuffer);
2227 case GST_STATE_CHANGE_READY_TO_NULL:
2228 /* we release again here because the aqcuire happens when setting the
2229 * caps, which happens before we commit the state to PAUSED and thus the
2230 * PAUSED->READY state change (see above, where we release the ringbuffer)
2231 * might not be called when we get here. */
2232 gst_ring_buffer_activate (sink->ringbuffer, FALSE);
2233 gst_ring_buffer_release (sink->ringbuffer);
2234 gst_ring_buffer_close_device (sink->ringbuffer);
2235 GST_OBJECT_LOCK (sink);
2236 gst_object_unparent (GST_OBJECT_CAST (sink->ringbuffer));
2237 sink->ringbuffer = NULL;
2238 GST_OBJECT_UNLOCK (sink);
2249 /* subclass must post a meaningful error message */
2250 GST_DEBUG_OBJECT (sink, "open failed");
2251 return GST_STATE_CHANGE_FAILURE;