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 #include "gstbaseaudiosink.h"
39 GST_DEBUG_CATEGORY_STATIC (gst_base_audio_sink_debug);
40 #define GST_CAT_DEFAULT gst_base_audio_sink_debug
42 #define GST_BASE_AUDIO_SINK_GET_PRIVATE(obj) \
43 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_BASE_AUDIO_SINK, GstBaseAudioSinkPrivate))
45 struct _GstBaseAudioSinkPrivate
47 /* upstream latency */
48 GstClockTime us_latency;
49 /* the clock slaving algorithm in use */
50 GstBaseAudioSinkSlaveMethod slave_method;
51 /* running average of clock skew */
52 GstClockTimeDiff avg_skew;
53 /* the number of samples we aligned last time */
56 gboolean sync_latency;
58 GstClockTime eos_time;
60 /* number of microseconds we alow timestamps or clock slaving to drift
62 guint64 drift_tolerance;
65 /* BaseAudioSink signals and args */
72 /* FIXME: 0.11, store the buffer_time and latency_time in nanoseconds */
73 #define DEFAULT_BUFFER_TIME ((200 * GST_MSECOND) / GST_USECOND)
74 #define DEFAULT_LATENCY_TIME ((10 * GST_MSECOND) / GST_USECOND)
75 #define DEFAULT_PROVIDE_CLOCK TRUE
76 #define DEFAULT_SLAVE_METHOD GST_BASE_AUDIO_SINK_SLAVE_SKEW
78 /* FIXME, enable pull mode when clock slaving and trick modes are figured out */
79 #define DEFAULT_CAN_ACTIVATE_PULL FALSE
81 /* when timestamps or clock slaving drift for more than 40ms we resync. This is
82 * a reasonable default */
83 #define DEFAULT_DRIFT_TOLERANCE ((40 * GST_MSECOND) / GST_USECOND)
93 PROP_CAN_ACTIVATE_PULL,
100 gst_base_audio_sink_slave_method_get_type (void)
102 static volatile gsize slave_method_type = 0;
103 static const GEnumValue slave_method[] = {
104 {GST_BASE_AUDIO_SINK_SLAVE_RESAMPLE, "GST_BASE_AUDIO_SINK_SLAVE_RESAMPLE",
106 {GST_BASE_AUDIO_SINK_SLAVE_SKEW, "GST_BASE_AUDIO_SINK_SLAVE_SKEW", "skew"},
107 {GST_BASE_AUDIO_SINK_SLAVE_NONE, "GST_BASE_AUDIO_SINK_SLAVE_NONE", "none"},
111 if (g_once_init_enter (&slave_method_type)) {
113 g_enum_register_static ("GstBaseAudioSinkSlaveMethod", slave_method);
114 g_once_init_leave (&slave_method_type, tmp);
117 return (GType) slave_method_type;
122 GST_DEBUG_CATEGORY_INIT (gst_base_audio_sink_debug, "baseaudiosink", 0, "baseaudiosink element");
123 #define gst_base_audio_sink_parent_class parent_class
124 G_DEFINE_TYPE_WITH_CODE (GstBaseAudioSink, gst_base_audio_sink,
125 GST_TYPE_BASE_SINK, _do_init);
127 static void gst_base_audio_sink_dispose (GObject * object);
129 static void gst_base_audio_sink_set_property (GObject * object, guint prop_id,
130 const GValue * value, GParamSpec * pspec);
131 static void gst_base_audio_sink_get_property (GObject * object, guint prop_id,
132 GValue * value, GParamSpec * pspec);
135 static GstStateChangeReturn gst_base_audio_sink_async_play (GstBaseSink *
138 static GstStateChangeReturn gst_base_audio_sink_change_state (GstElement *
139 element, GstStateChange transition);
140 static gboolean gst_base_audio_sink_activate_pull (GstBaseSink * basesink,
142 static gboolean gst_base_audio_sink_query (GstElement * element, GstQuery *
145 static GstClock *gst_base_audio_sink_provide_clock (GstElement * elem);
146 static GstClockTime gst_base_audio_sink_get_time (GstClock * clock,
147 GstBaseAudioSink * sink);
148 static void gst_base_audio_sink_callback (GstRingBuffer * rbuf, guint8 * data,
149 guint len, gpointer user_data);
151 static GstFlowReturn gst_base_audio_sink_preroll (GstBaseSink * bsink,
153 static GstFlowReturn gst_base_audio_sink_render (GstBaseSink * bsink,
155 static gboolean gst_base_audio_sink_event (GstBaseSink * bsink,
157 static void gst_base_audio_sink_get_times (GstBaseSink * bsink,
158 GstBuffer * buffer, GstClockTime * start, GstClockTime * end);
159 static gboolean gst_base_audio_sink_setcaps (GstBaseSink * bsink,
161 static void gst_base_audio_sink_fixate (GstBaseSink * bsink, GstCaps * caps);
163 static gboolean gst_base_audio_sink_query_pad (GstBaseSink * bsink,
167 /* static guint gst_base_audio_sink_signals[LAST_SIGNAL] = { 0 }; */
170 gst_base_audio_sink_class_init (GstBaseAudioSinkClass * klass)
172 GObjectClass *gobject_class;
173 GstElementClass *gstelement_class;
174 GstBaseSinkClass *gstbasesink_class;
176 gobject_class = (GObjectClass *) klass;
177 gstelement_class = (GstElementClass *) klass;
178 gstbasesink_class = (GstBaseSinkClass *) klass;
180 g_type_class_add_private (klass, sizeof (GstBaseAudioSinkPrivate));
182 gobject_class->set_property = gst_base_audio_sink_set_property;
183 gobject_class->get_property = gst_base_audio_sink_get_property;
184 gobject_class->dispose = gst_base_audio_sink_dispose;
186 g_object_class_install_property (gobject_class, PROP_BUFFER_TIME,
187 g_param_spec_int64 ("buffer-time", "Buffer Time",
188 "Size of audio buffer in microseconds", 1,
189 G_MAXINT64, DEFAULT_BUFFER_TIME,
190 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
192 g_object_class_install_property (gobject_class, PROP_LATENCY_TIME,
193 g_param_spec_int64 ("latency-time", "Latency Time",
194 "Audio latency in microseconds", 1,
195 G_MAXINT64, DEFAULT_LATENCY_TIME,
196 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
198 g_object_class_install_property (gobject_class, PROP_PROVIDE_CLOCK,
199 g_param_spec_boolean ("provide-clock", "Provide Clock",
200 "Provide a clock to be used as the global pipeline clock",
201 DEFAULT_PROVIDE_CLOCK, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
203 g_object_class_install_property (gobject_class, PROP_SLAVE_METHOD,
204 g_param_spec_enum ("slave-method", "Slave Method",
205 "Algorithm to use to match the rate of the masterclock",
206 GST_TYPE_BASE_AUDIO_SINK_SLAVE_METHOD, DEFAULT_SLAVE_METHOD,
207 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
209 g_object_class_install_property (gobject_class, PROP_CAN_ACTIVATE_PULL,
210 g_param_spec_boolean ("can-activate-pull", "Allow Pull Scheduling",
211 "Allow pull-based scheduling", DEFAULT_CAN_ACTIVATE_PULL,
212 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
214 * GstBaseAudioSink:drift-tolerance
216 * Controls the amount of time in milliseconds that timestamps or clocks are allowed
217 * to drift before resynchronisation happens.
221 g_object_class_install_property (gobject_class, PROP_DRIFT_TOLERANCE,
222 g_param_spec_int64 ("drift-tolerance", "Drift Tolerance",
223 "Tolerance for timestamp and clock drift in microseconds", 1,
224 G_MAXINT64, DEFAULT_DRIFT_TOLERANCE,
225 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
227 gstelement_class->change_state =
228 GST_DEBUG_FUNCPTR (gst_base_audio_sink_change_state);
229 gstelement_class->provide_clock =
230 GST_DEBUG_FUNCPTR (gst_base_audio_sink_provide_clock);
231 gstelement_class->query = GST_DEBUG_FUNCPTR (gst_base_audio_sink_query);
233 gstbasesink_class->event = GST_DEBUG_FUNCPTR (gst_base_audio_sink_event);
234 gstbasesink_class->preroll = GST_DEBUG_FUNCPTR (gst_base_audio_sink_preroll);
235 gstbasesink_class->query = GST_DEBUG_FUNCPTR (gst_base_audio_sink_query_pad);
236 gstbasesink_class->render = GST_DEBUG_FUNCPTR (gst_base_audio_sink_render);
237 gstbasesink_class->get_times =
238 GST_DEBUG_FUNCPTR (gst_base_audio_sink_get_times);
239 gstbasesink_class->set_caps = GST_DEBUG_FUNCPTR (gst_base_audio_sink_setcaps);
240 gstbasesink_class->fixate = GST_DEBUG_FUNCPTR (gst_base_audio_sink_fixate);
242 gstbasesink_class->async_play =
243 GST_DEBUG_FUNCPTR (gst_base_audio_sink_async_play);
245 gstbasesink_class->activate_pull =
246 GST_DEBUG_FUNCPTR (gst_base_audio_sink_activate_pull);
248 /* ref class from a thread-safe context to work around missing bit of
249 * thread-safety in GObject */
250 g_type_class_ref (GST_TYPE_AUDIO_CLOCK);
251 g_type_class_ref (GST_TYPE_RING_BUFFER);
256 gst_base_audio_sink_init (GstBaseAudioSink * baseaudiosink)
258 GstBaseSink *basesink;
260 baseaudiosink->priv = GST_BASE_AUDIO_SINK_GET_PRIVATE (baseaudiosink);
262 baseaudiosink->buffer_time = DEFAULT_BUFFER_TIME;
263 baseaudiosink->latency_time = DEFAULT_LATENCY_TIME;
264 baseaudiosink->provide_clock = DEFAULT_PROVIDE_CLOCK;
265 baseaudiosink->priv->slave_method = DEFAULT_SLAVE_METHOD;
266 baseaudiosink->priv->drift_tolerance = DEFAULT_DRIFT_TOLERANCE;
268 baseaudiosink->provided_clock = gst_audio_clock_new ("GstAudioSinkClock",
269 (GstAudioClockGetTimeFunc) gst_base_audio_sink_get_time, baseaudiosink);
271 basesink = GST_BASE_SINK_CAST (baseaudiosink);
272 basesink->can_activate_push = TRUE;
273 basesink->can_activate_pull = DEFAULT_CAN_ACTIVATE_PULL;
275 gst_base_sink_set_last_buffer_enabled (basesink, FALSE);
279 gst_base_audio_sink_dispose (GObject * object)
281 GstBaseAudioSink *sink;
283 sink = GST_BASE_AUDIO_SINK (object);
285 if (sink->provided_clock) {
286 gst_audio_clock_invalidate (sink->provided_clock);
287 gst_object_unref (sink->provided_clock);
288 sink->provided_clock = NULL;
291 if (sink->ringbuffer) {
292 gst_object_unparent (GST_OBJECT_CAST (sink->ringbuffer));
293 sink->ringbuffer = NULL;
296 G_OBJECT_CLASS (parent_class)->dispose (object);
301 gst_base_audio_sink_provide_clock (GstElement * elem)
303 GstBaseAudioSink *sink;
306 sink = GST_BASE_AUDIO_SINK (elem);
308 /* we have no ringbuffer (must be NULL state) */
309 if (sink->ringbuffer == NULL)
312 if (!gst_ring_buffer_is_acquired (sink->ringbuffer))
315 GST_OBJECT_LOCK (sink);
316 if (!sink->provide_clock)
319 clock = GST_CLOCK_CAST (gst_object_ref (sink->provided_clock));
320 GST_OBJECT_UNLOCK (sink);
327 GST_DEBUG_OBJECT (sink, "ringbuffer not acquired");
332 GST_DEBUG_OBJECT (sink, "clock provide disabled");
333 GST_OBJECT_UNLOCK (sink);
339 gst_base_audio_sink_query_pad (GstBaseSink * bsink, GstQuery * query)
341 gboolean res = FALSE;
342 GstBaseAudioSink *basesink;
344 basesink = GST_BASE_AUDIO_SINK (bsink);
346 switch (GST_QUERY_TYPE (query)) {
347 case GST_QUERY_CONVERT:
349 GstFormat src_fmt, dest_fmt;
350 gint64 src_val, dest_val;
352 GST_LOG_OBJECT (basesink, "query convert");
354 if (basesink->ringbuffer) {
355 gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, NULL);
356 res = gst_ring_buffer_convert (basesink->ringbuffer, src_fmt, src_val,
357 dest_fmt, &dest_val);
359 gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
365 res = GST_BASE_SINK_CLASS (parent_class)->query (bsink, query);
372 gst_base_audio_sink_query (GstElement * element, GstQuery * query)
374 gboolean res = FALSE;
375 GstBaseAudioSink *basesink;
377 basesink = GST_BASE_AUDIO_SINK (element);
379 switch (GST_QUERY_TYPE (query)) {
380 case GST_QUERY_LATENCY:
382 gboolean live, us_live;
383 GstClockTime min_l, max_l;
385 GST_DEBUG_OBJECT (basesink, "latency query");
387 /* ask parent first, it will do an upstream query for us. */
389 gst_base_sink_query_latency (GST_BASE_SINK_CAST (basesink), &live,
390 &us_live, &min_l, &max_l))) {
391 GstClockTime base_latency, min_latency, max_latency;
393 /* we and upstream are both live, adjust the min_latency */
394 if (live && us_live) {
395 GstRingBufferSpec *spec;
397 GST_OBJECT_LOCK (basesink);
398 if (!basesink->ringbuffer || !basesink->ringbuffer->spec.info.rate) {
399 GST_OBJECT_UNLOCK (basesink);
401 GST_DEBUG_OBJECT (basesink,
402 "we are not yet negotiated, can't report latency yet");
406 spec = &basesink->ringbuffer->spec;
408 basesink->priv->us_latency = min_l;
411 gst_util_uint64_scale_int (spec->seglatency * spec->segsize,
412 GST_SECOND, spec->info.rate * spec->info.bpf);
413 GST_OBJECT_UNLOCK (basesink);
415 /* we cannot go lower than the buffer size and the min peer latency */
416 min_latency = base_latency + min_l;
417 /* the max latency is the max of the peer, we can delay an infinite
419 max_latency = (max_l == -1) ? -1 : (base_latency + max_l);
421 GST_DEBUG_OBJECT (basesink,
422 "peer min %" GST_TIME_FORMAT ", our min latency: %"
423 GST_TIME_FORMAT, GST_TIME_ARGS (min_l),
424 GST_TIME_ARGS (min_latency));
425 GST_DEBUG_OBJECT (basesink,
426 "peer max %" GST_TIME_FORMAT ", our max latency: %"
427 GST_TIME_FORMAT, GST_TIME_ARGS (max_l),
428 GST_TIME_ARGS (max_latency));
430 GST_DEBUG_OBJECT (basesink,
431 "peer or we are not live, don't care about latency");
435 gst_query_set_latency (query, live, min_latency, max_latency);
439 case GST_QUERY_CONVERT:
441 GstFormat src_fmt, dest_fmt;
442 gint64 src_val, dest_val;
444 GST_LOG_OBJECT (basesink, "query convert");
446 if (basesink->ringbuffer) {
447 gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, NULL);
448 res = gst_ring_buffer_convert (basesink->ringbuffer, src_fmt, src_val,
449 dest_fmt, &dest_val);
451 gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
457 res = GST_ELEMENT_CLASS (parent_class)->query (element, query);
467 gst_base_audio_sink_get_time (GstClock * clock, GstBaseAudioSink * sink)
469 guint64 raw, samples;
473 if (sink->ringbuffer == NULL || sink->ringbuffer->spec.info.rate == 0)
474 return GST_CLOCK_TIME_NONE;
476 /* our processed samples are always increasing */
477 raw = samples = gst_ring_buffer_samples_done (sink->ringbuffer);
479 /* the number of samples not yet processed, this is still queued in the
480 * device (not played for playback). */
481 delay = gst_ring_buffer_delay (sink->ringbuffer);
483 if (G_LIKELY (samples >= delay))
488 result = gst_util_uint64_scale_int (samples, GST_SECOND,
489 sink->ringbuffer->spec.info.rate);
491 GST_DEBUG_OBJECT (sink,
492 "processed samples: raw %" G_GUINT64_FORMAT ", delay %u, real %"
493 G_GUINT64_FORMAT ", time %" GST_TIME_FORMAT,
494 raw, delay, samples, GST_TIME_ARGS (result));
500 * gst_base_audio_sink_set_provide_clock:
501 * @sink: a #GstBaseAudioSink
502 * @provide: new state
504 * Controls whether @sink will provide a clock or not. If @provide is %TRUE,
505 * gst_element_provide_clock() will return a clock that reflects the datarate
506 * of @sink. If @provide is %FALSE, gst_element_provide_clock() will return NULL.
511 gst_base_audio_sink_set_provide_clock (GstBaseAudioSink * sink,
514 g_return_if_fail (GST_IS_BASE_AUDIO_SINK (sink));
516 GST_OBJECT_LOCK (sink);
517 sink->provide_clock = provide;
518 GST_OBJECT_UNLOCK (sink);
522 * gst_base_audio_sink_get_provide_clock:
523 * @sink: a #GstBaseAudioSink
525 * Queries whether @sink will provide a clock or not. See also
526 * gst_base_audio_sink_set_provide_clock.
528 * Returns: %TRUE if @sink will provide a clock.
533 gst_base_audio_sink_get_provide_clock (GstBaseAudioSink * sink)
537 g_return_val_if_fail (GST_IS_BASE_AUDIO_SINK (sink), FALSE);
539 GST_OBJECT_LOCK (sink);
540 result = sink->provide_clock;
541 GST_OBJECT_UNLOCK (sink);
547 * gst_base_audio_sink_set_slave_method:
548 * @sink: a #GstBaseAudioSink
549 * @method: the new slave method
551 * Controls how clock slaving will be performed in @sink.
556 gst_base_audio_sink_set_slave_method (GstBaseAudioSink * sink,
557 GstBaseAudioSinkSlaveMethod method)
559 g_return_if_fail (GST_IS_BASE_AUDIO_SINK (sink));
561 GST_OBJECT_LOCK (sink);
562 sink->priv->slave_method = method;
563 GST_OBJECT_UNLOCK (sink);
567 * gst_base_audio_sink_get_slave_method:
568 * @sink: a #GstBaseAudioSink
570 * Get the current slave method used by @sink.
572 * Returns: The current slave method used by @sink.
576 GstBaseAudioSinkSlaveMethod
577 gst_base_audio_sink_get_slave_method (GstBaseAudioSink * sink)
579 GstBaseAudioSinkSlaveMethod result;
581 g_return_val_if_fail (GST_IS_BASE_AUDIO_SINK (sink), -1);
583 GST_OBJECT_LOCK (sink);
584 result = sink->priv->slave_method;
585 GST_OBJECT_UNLOCK (sink);
592 * gst_base_audio_sink_set_drift_tolerance:
593 * @sink: a #GstBaseAudioSink
594 * @drift_tolerance: the new drift tolerance in microseconds
596 * Controls the sink's drift tolerance.
601 gst_base_audio_sink_set_drift_tolerance (GstBaseAudioSink * sink,
602 gint64 drift_tolerance)
604 g_return_if_fail (GST_IS_BASE_AUDIO_SINK (sink));
606 GST_OBJECT_LOCK (sink);
607 sink->priv->drift_tolerance = drift_tolerance;
608 GST_OBJECT_UNLOCK (sink);
612 * gst_base_audio_sink_get_drift_tolerance
613 * @sink: a #GstBaseAudioSink
615 * Get the current drift tolerance, in microseconds, used by @sink.
617 * Returns: The current drift tolerance used by @sink.
622 gst_base_audio_sink_get_drift_tolerance (GstBaseAudioSink * sink)
626 g_return_val_if_fail (GST_IS_BASE_AUDIO_SINK (sink), -1);
628 GST_OBJECT_LOCK (sink);
629 result = sink->priv->drift_tolerance;
630 GST_OBJECT_UNLOCK (sink);
636 gst_base_audio_sink_set_property (GObject * object, guint prop_id,
637 const GValue * value, GParamSpec * pspec)
639 GstBaseAudioSink *sink;
641 sink = GST_BASE_AUDIO_SINK (object);
644 case PROP_BUFFER_TIME:
645 sink->buffer_time = g_value_get_int64 (value);
647 case PROP_LATENCY_TIME:
648 sink->latency_time = g_value_get_int64 (value);
650 case PROP_PROVIDE_CLOCK:
651 gst_base_audio_sink_set_provide_clock (sink, g_value_get_boolean (value));
653 case PROP_SLAVE_METHOD:
654 gst_base_audio_sink_set_slave_method (sink, g_value_get_enum (value));
656 case PROP_CAN_ACTIVATE_PULL:
657 GST_BASE_SINK (sink)->can_activate_pull = g_value_get_boolean (value);
659 case PROP_DRIFT_TOLERANCE:
660 gst_base_audio_sink_set_drift_tolerance (sink, g_value_get_int64 (value));
663 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
669 gst_base_audio_sink_get_property (GObject * object, guint prop_id,
670 GValue * value, GParamSpec * pspec)
672 GstBaseAudioSink *sink;
674 sink = GST_BASE_AUDIO_SINK (object);
677 case PROP_BUFFER_TIME:
678 g_value_set_int64 (value, sink->buffer_time);
680 case PROP_LATENCY_TIME:
681 g_value_set_int64 (value, sink->latency_time);
683 case PROP_PROVIDE_CLOCK:
684 g_value_set_boolean (value, gst_base_audio_sink_get_provide_clock (sink));
686 case PROP_SLAVE_METHOD:
687 g_value_set_enum (value, gst_base_audio_sink_get_slave_method (sink));
689 case PROP_CAN_ACTIVATE_PULL:
690 g_value_set_boolean (value, GST_BASE_SINK (sink)->can_activate_pull);
692 case PROP_DRIFT_TOLERANCE:
693 g_value_set_int64 (value, gst_base_audio_sink_get_drift_tolerance (sink));
696 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
702 gst_base_audio_sink_setcaps (GstBaseSink * bsink, GstCaps * caps)
704 GstBaseAudioSink *sink = GST_BASE_AUDIO_SINK (bsink);
705 GstRingBufferSpec *spec;
707 GstClockTime crate_num, crate_denom;
709 if (!sink->ringbuffer)
712 spec = &sink->ringbuffer->spec;
714 GST_DEBUG_OBJECT (sink, "release old ringbuffer");
716 /* get current time, updates the last_time. When the subclass has a clock that
717 * restarts from 0 when a new format is negotiated, it will call
718 * gst_audio_clock_reset() which will use this last_time to create an offset
719 * so that time from the clock keeps on increasing monotonically. */
720 now = gst_clock_get_time (sink->provided_clock);
722 GST_DEBUG_OBJECT (sink, "time was %" GST_TIME_FORMAT, GST_TIME_ARGS (now));
724 /* release old ringbuffer */
725 gst_ring_buffer_pause (sink->ringbuffer);
726 gst_ring_buffer_activate (sink->ringbuffer, FALSE);
727 gst_ring_buffer_release (sink->ringbuffer);
729 GST_DEBUG_OBJECT (sink, "parse caps");
731 spec->buffer_time = sink->buffer_time;
732 spec->latency_time = sink->latency_time;
735 if (!gst_ring_buffer_parse_caps (spec, caps))
738 gst_ring_buffer_debug_spec_buff (spec);
740 GST_DEBUG_OBJECT (sink, "acquire ringbuffer");
741 if (!gst_ring_buffer_acquire (sink->ringbuffer, spec))
744 if (bsink->pad_mode == GST_PAD_ACTIVATE_PUSH) {
745 GST_DEBUG_OBJECT (sink, "activate ringbuffer");
746 gst_ring_buffer_activate (sink->ringbuffer, TRUE);
749 /* due to possible changes in the spec file we should recalibrate the clock */
750 gst_clock_get_calibration (sink->provided_clock, NULL, NULL,
751 &crate_num, &crate_denom);
752 gst_clock_set_calibration (sink->provided_clock,
753 gst_clock_get_internal_time (sink->provided_clock), now, crate_num,
756 /* calculate actual latency and buffer times.
757 * FIXME: In 0.11, store the latency_time internally in ns */
758 spec->latency_time = gst_util_uint64_scale (spec->segsize,
759 (GST_SECOND / GST_USECOND), spec->info.rate * spec->info.bpf);
761 spec->buffer_time = spec->segtotal * spec->latency_time;
763 gst_ring_buffer_debug_spec_buff (spec);
770 GST_DEBUG_OBJECT (sink, "could not parse caps");
771 GST_ELEMENT_ERROR (sink, STREAM, FORMAT,
772 (NULL), ("cannot parse audio format."));
777 GST_DEBUG_OBJECT (sink, "could not acquire ringbuffer");
783 gst_base_audio_sink_fixate (GstBaseSink * bsink, GstCaps * caps)
788 s = gst_caps_get_structure (caps, 0);
790 /* fields for all formats */
791 gst_structure_fixate_field_nearest_int (s, "rate", 44100);
792 gst_structure_fixate_field_nearest_int (s, "channels", 2);
793 gst_structure_fixate_field_nearest_int (s, "width", 16);
796 if (gst_structure_has_field (s, "depth")) {
797 gst_structure_get_int (s, "width", &width);
798 /* round width to nearest multiple of 8 for the depth */
799 depth = GST_ROUND_UP_8 (width);
800 gst_structure_fixate_field_nearest_int (s, "depth", depth);
802 if (gst_structure_has_field (s, "signed"))
803 gst_structure_fixate_field_boolean (s, "signed", TRUE);
804 if (gst_structure_has_field (s, "endianness"))
805 gst_structure_fixate_field_nearest_int (s, "endianness", G_BYTE_ORDER);
809 gst_base_audio_sink_get_times (GstBaseSink * bsink, GstBuffer * buffer,
810 GstClockTime * start, GstClockTime * end)
812 /* our clock sync is a bit too much for the base class to handle so
813 * we implement it ourselves. */
814 *start = GST_CLOCK_TIME_NONE;
815 *end = GST_CLOCK_TIME_NONE;
818 /* This waits for the drain to happen and can be canceled */
820 gst_base_audio_sink_drain (GstBaseAudioSink * sink)
822 if (!sink->ringbuffer)
824 if (!sink->ringbuffer->spec.info.rate)
827 /* if PLAYING is interrupted,
828 * arrange to have clock running when going to PLAYING again */
829 g_atomic_int_set (&sink->eos_rendering, 1);
831 /* need to start playback before we can drain, but only when
832 * we have successfully negotiated a format and thus acquired the
834 if (gst_ring_buffer_is_acquired (sink->ringbuffer))
835 gst_ring_buffer_start (sink->ringbuffer);
837 if (sink->priv->eos_time != -1) {
838 GST_DEBUG_OBJECT (sink,
839 "last sample time %" GST_TIME_FORMAT,
840 GST_TIME_ARGS (sink->priv->eos_time));
842 /* wait for the EOS time to be reached, this is the time when the last
843 * sample is played. */
844 gst_base_sink_wait_eos (GST_BASE_SINK (sink), sink->priv->eos_time, NULL);
846 GST_DEBUG_OBJECT (sink, "drained audio");
848 g_atomic_int_set (&sink->eos_rendering, 0);
853 gst_base_audio_sink_event (GstBaseSink * bsink, GstEvent * event)
855 GstBaseAudioSink *sink = GST_BASE_AUDIO_SINK (bsink);
857 switch (GST_EVENT_TYPE (event)) {
858 case GST_EVENT_FLUSH_START:
859 if (sink->ringbuffer)
860 gst_ring_buffer_set_flushing (sink->ringbuffer, TRUE);
862 case GST_EVENT_FLUSH_STOP:
863 /* always resync on sample after a flush */
864 sink->priv->avg_skew = -1;
865 sink->next_sample = -1;
866 sink->priv->eos_time = -1;
867 if (sink->ringbuffer)
868 gst_ring_buffer_set_flushing (sink->ringbuffer, FALSE);
871 /* now wait till we played everything */
872 gst_base_audio_sink_drain (sink);
881 gst_base_audio_sink_preroll (GstBaseSink * bsink, GstBuffer * buffer)
883 GstBaseAudioSink *sink = GST_BASE_AUDIO_SINK (bsink);
885 if (!gst_ring_buffer_is_acquired (sink->ringbuffer))
888 /* we don't really do anything when prerolling. We could make a
889 * property to play this buffer to have some sort of scrubbing
895 GST_DEBUG_OBJECT (sink, "ringbuffer in wrong state");
896 GST_ELEMENT_ERROR (sink, STREAM, FORMAT, (NULL), ("sink not negotiated."));
897 return GST_FLOW_NOT_NEGOTIATED;
902 gst_base_audio_sink_get_offset (GstBaseAudioSink * sink)
905 gint writeseg, segdone, sps;
908 /* assume we can append to the previous sample */
909 sample = sink->next_sample;
910 /* no previous sample, try to insert at position 0 */
914 sps = sink->ringbuffer->samples_per_seg;
916 /* figure out the segment and the offset inside the segment where
917 * the sample should be written. */
918 writeseg = sample / sps;
920 /* get the currently processed segment */
921 segdone = g_atomic_int_get (&sink->ringbuffer->segdone)
922 - sink->ringbuffer->segbase;
924 /* see how far away it is from the write segment */
925 diff = writeseg - segdone;
927 /* sample would be dropped, position to next playable position */
928 sample = (segdone + 1) * sps;
935 clock_convert_external (GstClockTime external, GstClockTime cinternal,
936 GstClockTime cexternal, GstClockTime crate_num, GstClockTime crate_denom)
938 /* adjust for rate and speed */
939 if (external >= cexternal) {
941 gst_util_uint64_scale (external - cexternal, crate_denom, crate_num);
942 external += cinternal;
945 gst_util_uint64_scale (cexternal - external, crate_denom, crate_num);
946 if (cinternal > external)
947 external = cinternal - external;
954 /* algorithm to calculate sample positions that will result in resampling to
955 * match the clock rate of the master */
957 gst_base_audio_sink_resample_slaving (GstBaseAudioSink * sink,
958 GstClockTime render_start, GstClockTime render_stop,
959 GstClockTime * srender_start, GstClockTime * srender_stop)
961 GstClockTime cinternal, cexternal;
962 GstClockTime crate_num, crate_denom;
964 /* FIXME, we can sample and add observations here or use the timeouts on the
965 * clock. No idea which one is better or more stable. The timeout seems more
966 * arbitrary but this one seems more demanding and does not work when there is
967 * no data comming in to the sink. */
969 GstClockTime etime, itime;
972 /* sample clocks and figure out clock skew */
973 etime = gst_clock_get_time (GST_ELEMENT_CLOCK (sink));
974 itime = gst_audio_clock_get_time (sink->provided_clock);
976 /* add new observation */
977 gst_clock_add_observation (sink->provided_clock, itime, etime, &r_squared);
980 /* get calibration parameters to compensate for speed and offset differences
981 * when we are slaved */
982 gst_clock_get_calibration (sink->provided_clock, &cinternal, &cexternal,
983 &crate_num, &crate_denom);
985 GST_DEBUG_OBJECT (sink, "internal %" GST_TIME_FORMAT " external %"
986 GST_TIME_FORMAT " %" G_GUINT64_FORMAT "/%" G_GUINT64_FORMAT " = %f",
987 GST_TIME_ARGS (cinternal), GST_TIME_ARGS (cexternal), crate_num,
988 crate_denom, gst_guint64_to_gdouble (crate_num) /
989 gst_guint64_to_gdouble (crate_denom));
992 crate_denom = crate_num = 1;
994 /* bring external time to internal time */
995 render_start = clock_convert_external (render_start, cinternal, cexternal,
996 crate_num, crate_denom);
997 render_stop = clock_convert_external (render_stop, cinternal, cexternal,
998 crate_num, crate_denom);
1000 GST_DEBUG_OBJECT (sink,
1001 "after slaving: start %" GST_TIME_FORMAT " - stop %" GST_TIME_FORMAT,
1002 GST_TIME_ARGS (render_start), GST_TIME_ARGS (render_stop));
1004 *srender_start = render_start;
1005 *srender_stop = render_stop;
1008 /* algorithm to calculate sample positions that will result in changing the
1009 * playout pointer to match the clock rate of the master */
1011 gst_base_audio_sink_skew_slaving (GstBaseAudioSink * sink,
1012 GstClockTime render_start, GstClockTime render_stop,
1013 GstClockTime * srender_start, GstClockTime * srender_stop)
1015 GstClockTime cinternal, cexternal, crate_num, crate_denom;
1016 GstClockTime etime, itime;
1017 GstClockTimeDiff skew, mdrift, mdrift2;
1021 /* get calibration parameters to compensate for offsets */
1022 gst_clock_get_calibration (sink->provided_clock, &cinternal, &cexternal,
1023 &crate_num, &crate_denom);
1025 /* sample clocks and figure out clock skew */
1026 etime = gst_clock_get_time (GST_ELEMENT_CLOCK (sink));
1027 itime = gst_audio_clock_get_time (sink->provided_clock);
1028 itime = gst_audio_clock_adjust (sink->provided_clock, itime);
1030 GST_DEBUG_OBJECT (sink,
1031 "internal %" GST_TIME_FORMAT " external %" GST_TIME_FORMAT
1032 " cinternal %" GST_TIME_FORMAT " cexternal %" GST_TIME_FORMAT,
1033 GST_TIME_ARGS (itime), GST_TIME_ARGS (etime),
1034 GST_TIME_ARGS (cinternal), GST_TIME_ARGS (cexternal));
1036 /* make sure we never go below 0 */
1037 etime = etime > cexternal ? etime - cexternal : 0;
1038 itime = itime > cinternal ? itime - cinternal : 0;
1040 /* do itime - etime.
1041 * positive value means external clock goes slower
1042 * negative value means external clock goes faster */
1043 skew = GST_CLOCK_DIFF (etime, itime);
1044 if (sink->priv->avg_skew == -1) {
1045 /* first observation */
1046 sink->priv->avg_skew = skew;
1048 /* next observations use a moving average */
1049 sink->priv->avg_skew = (31 * sink->priv->avg_skew + skew) / 32;
1052 GST_DEBUG_OBJECT (sink, "internal %" GST_TIME_FORMAT " external %"
1053 GST_TIME_FORMAT " skew %" G_GINT64_FORMAT " avg %" G_GINT64_FORMAT,
1054 GST_TIME_ARGS (itime), GST_TIME_ARGS (etime), skew, sink->priv->avg_skew);
1056 /* the max drift we allow */
1057 mdrift = sink->priv->drift_tolerance * 1000;
1058 mdrift2 = mdrift / 2;
1060 /* adjust playout pointer based on skew */
1061 if (sink->priv->avg_skew > mdrift2) {
1062 /* master is running slower, move internal time forward */
1063 GST_WARNING_OBJECT (sink,
1064 "correct clock skew %" G_GINT64_FORMAT " > %" G_GINT64_FORMAT,
1065 sink->priv->avg_skew, mdrift2);
1066 cexternal = cexternal > mdrift ? cexternal - mdrift : 0;
1067 sink->priv->avg_skew -= mdrift;
1069 driftsamples = (sink->ringbuffer->spec.info.rate * mdrift) / GST_SECOND;
1070 last_align = sink->priv->last_align;
1072 /* if we were aligning in the wrong direction or we aligned more than what we
1073 * will correct, resync */
1074 if (last_align < 0 || last_align > driftsamples)
1075 sink->next_sample = -1;
1077 GST_DEBUG_OBJECT (sink,
1078 "last_align %" G_GINT64_FORMAT " driftsamples %u, next %"
1079 G_GUINT64_FORMAT, last_align, driftsamples, sink->next_sample);
1081 gst_clock_set_calibration (sink->provided_clock, cinternal, cexternal,
1082 crate_num, crate_denom);
1083 } else if (sink->priv->avg_skew < -mdrift2) {
1084 /* master is running faster, move external time forwards */
1085 GST_WARNING_OBJECT (sink,
1086 "correct clock skew %" G_GINT64_FORMAT " < %" G_GINT64_FORMAT,
1087 sink->priv->avg_skew, -mdrift2);
1088 cexternal += mdrift;
1089 sink->priv->avg_skew += mdrift;
1091 driftsamples = (sink->ringbuffer->spec.info.rate * mdrift) / GST_SECOND;
1092 last_align = sink->priv->last_align;
1094 /* if we were aligning in the wrong direction or we aligned more than what we
1095 * will correct, resync */
1096 if (last_align > 0 || -last_align > driftsamples)
1097 sink->next_sample = -1;
1099 GST_DEBUG_OBJECT (sink,
1100 "last_align %" G_GINT64_FORMAT " driftsamples %u, next %"
1101 G_GUINT64_FORMAT, last_align, driftsamples, sink->next_sample);
1103 gst_clock_set_calibration (sink->provided_clock, cinternal, cexternal,
1104 crate_num, crate_denom);
1107 /* convert, ignoring speed */
1108 render_start = clock_convert_external (render_start, cinternal, cexternal,
1109 crate_num, crate_denom);
1110 render_stop = clock_convert_external (render_stop, cinternal, cexternal,
1111 crate_num, crate_denom);
1113 *srender_start = render_start;
1114 *srender_stop = render_stop;
1117 /* apply the clock offset but do no slaving otherwise */
1119 gst_base_audio_sink_none_slaving (GstBaseAudioSink * sink,
1120 GstClockTime render_start, GstClockTime render_stop,
1121 GstClockTime * srender_start, GstClockTime * srender_stop)
1123 GstClockTime cinternal, cexternal, crate_num, crate_denom;
1125 /* get calibration parameters to compensate for offsets */
1126 gst_clock_get_calibration (sink->provided_clock, &cinternal, &cexternal,
1127 &crate_num, &crate_denom);
1129 /* convert, ignoring speed */
1130 render_start = clock_convert_external (render_start, cinternal, cexternal,
1131 crate_num, crate_denom);
1132 render_stop = clock_convert_external (render_stop, cinternal, cexternal,
1133 crate_num, crate_denom);
1135 *srender_start = render_start;
1136 *srender_stop = render_stop;
1139 /* converts render_start and render_stop to their slaved values */
1141 gst_base_audio_sink_handle_slaving (GstBaseAudioSink * sink,
1142 GstClockTime render_start, GstClockTime render_stop,
1143 GstClockTime * srender_start, GstClockTime * srender_stop)
1145 switch (sink->priv->slave_method) {
1146 case GST_BASE_AUDIO_SINK_SLAVE_RESAMPLE:
1147 gst_base_audio_sink_resample_slaving (sink, render_start, render_stop,
1148 srender_start, srender_stop);
1150 case GST_BASE_AUDIO_SINK_SLAVE_SKEW:
1151 gst_base_audio_sink_skew_slaving (sink, render_start, render_stop,
1152 srender_start, srender_stop);
1154 case GST_BASE_AUDIO_SINK_SLAVE_NONE:
1155 gst_base_audio_sink_none_slaving (sink, render_start, render_stop,
1156 srender_start, srender_stop);
1159 g_warning ("unknown slaving method %d", sink->priv->slave_method);
1164 /* must be called with LOCK */
1165 static GstFlowReturn
1166 gst_base_audio_sink_sync_latency (GstBaseSink * bsink, GstMiniObject * obj)
1169 GstClockReturn status;
1170 GstClockTime time, render_delay;
1172 GstBaseAudioSink *sink;
1173 GstClockTime itime, etime;
1174 GstClockTime rate_num, rate_denom;
1175 GstClockTimeDiff jitter;
1177 sink = GST_BASE_AUDIO_SINK (bsink);
1179 clock = GST_ELEMENT_CLOCK (sink);
1180 if (G_UNLIKELY (clock == NULL))
1183 /* we provided the global clock, don't need to do anything special */
1184 if (clock == sink->provided_clock)
1187 GST_OBJECT_UNLOCK (sink);
1190 GST_DEBUG_OBJECT (sink, "checking preroll");
1192 ret = gst_base_sink_do_preroll (bsink, obj);
1193 if (ret != GST_FLOW_OK)
1196 GST_OBJECT_LOCK (sink);
1197 time = sink->priv->us_latency;
1198 GST_OBJECT_UNLOCK (sink);
1200 /* Renderdelay is added onto our own latency, and needs
1201 * to be subtracted as well */
1202 render_delay = gst_base_sink_get_render_delay (bsink);
1204 if (G_LIKELY (time > render_delay))
1205 time -= render_delay;
1209 /* preroll done, we can sync since we are in PLAYING now. */
1210 GST_DEBUG_OBJECT (sink, "possibly waiting for clock to reach %"
1211 GST_TIME_FORMAT, GST_TIME_ARGS (time));
1213 /* wait for the clock, this can be interrupted because we got shut down or
1215 status = gst_base_sink_wait_clock (bsink, time, &jitter);
1217 GST_DEBUG_OBJECT (sink, "clock returned %d %" GST_TIME_FORMAT, status,
1218 GST_TIME_ARGS (jitter));
1220 /* invalid time, no clock or sync disabled, just continue then */
1221 if (status == GST_CLOCK_BADTIME)
1224 /* waiting could have been interrupted and we can be flushing now */
1225 if (G_UNLIKELY (bsink->flushing))
1228 /* retry if we got unscheduled, which means we did not reach the timeout
1229 * yet. if some other error occures, we continue. */
1230 } while (status == GST_CLOCK_UNSCHEDULED);
1232 GST_OBJECT_LOCK (sink);
1233 GST_DEBUG_OBJECT (sink, "latency synced");
1235 /* when we prerolled in time, we can accurately set the calibration,
1236 * our internal clock should exactly have been the latency (== the running
1237 * time of the external clock) */
1238 etime = GST_ELEMENT_CAST (sink)->base_time + time;
1239 itime = gst_audio_clock_get_time (sink->provided_clock);
1240 itime = gst_audio_clock_adjust (sink->provided_clock, itime);
1242 if (status == GST_CLOCK_EARLY) {
1243 /* when we prerolled late, we have to take into account the lateness */
1244 GST_DEBUG_OBJECT (sink, "late preroll, adding jitter");
1248 /* start ringbuffer so we can start slaving right away when we need to */
1249 gst_ring_buffer_start (sink->ringbuffer);
1251 GST_DEBUG_OBJECT (sink,
1252 "internal time: %" GST_TIME_FORMAT " external time: %" GST_TIME_FORMAT,
1253 GST_TIME_ARGS (itime), GST_TIME_ARGS (etime));
1255 /* copy the original calibrated rate but update the internal and external
1257 gst_clock_get_calibration (sink->provided_clock, NULL, NULL, &rate_num,
1259 gst_clock_set_calibration (sink->provided_clock, itime, etime,
1260 rate_num, rate_denom);
1262 switch (sink->priv->slave_method) {
1263 case GST_BASE_AUDIO_SINK_SLAVE_RESAMPLE:
1264 /* only set as master when we are resampling */
1265 GST_DEBUG_OBJECT (sink, "Setting clock as master");
1266 gst_clock_set_master (sink->provided_clock, clock);
1268 case GST_BASE_AUDIO_SINK_SLAVE_SKEW:
1269 case GST_BASE_AUDIO_SINK_SLAVE_NONE:
1274 sink->priv->avg_skew = -1;
1275 sink->next_sample = -1;
1276 sink->priv->eos_time = -1;
1283 GST_DEBUG_OBJECT (sink, "we have no clock");
1288 GST_DEBUG_OBJECT (sink, "we are not slaved");
1293 GST_DEBUG_OBJECT (sink, "we are flushing");
1294 GST_OBJECT_LOCK (sink);
1295 return GST_FLOW_WRONG_STATE;
1300 gst_base_audio_sink_get_alignment (GstBaseAudioSink * sink,
1301 GstClockTime sample_offset)
1303 GstRingBuffer *ringbuf = sink->ringbuffer;
1307 gint segdone = g_atomic_int_get (&ringbuf->segdone) - ringbuf->segbase;
1308 gint64 samples_done = segdone * ringbuf->samples_per_seg;
1309 gint64 headroom = sample_offset - samples_done;
1310 gboolean allow_align = TRUE;
1313 /* now try to align the sample to the previous one, first see how big the
1315 if (sample_offset >= sink->next_sample)
1316 diff = sample_offset - sink->next_sample;
1318 diff = sink->next_sample - sample_offset;
1320 rate = GST_AUDIO_INFO_RATE (&ringbuf->spec.info);
1322 /* calculate the max allowed drift in units of samples. By default this is
1323 * 20ms and should be anough to compensate for timestamp rounding errors. */
1324 maxdrift = (rate * sink->priv->drift_tolerance) / GST_MSECOND;
1326 /* calc align with previous sample */
1327 align = sink->next_sample - sample_offset;
1329 /* don't align if it means writing behind the read-segment */
1330 if (diff > headroom && align < 0)
1331 allow_align = FALSE;
1333 if (G_LIKELY (diff < maxdrift && allow_align)) {
1334 GST_DEBUG_OBJECT (sink,
1335 "align with prev sample, ABS (%" G_GINT64_FORMAT ") < %"
1336 G_GINT64_FORMAT, align, maxdrift);
1338 gint64 diff_s G_GNUC_UNUSED;
1340 /* calculate sample diff in seconds for error message */
1341 diff_s = gst_util_uint64_scale_int (diff, GST_SECOND, rate);
1343 /* timestamps drifted apart from previous samples too much, we need to
1344 * resync. We log this as an element warning. */
1345 GST_WARNING_OBJECT (sink,
1346 "Unexpected discontinuity in audio timestamps of "
1347 "%s%" GST_TIME_FORMAT ", resyncing",
1348 sample_offset > sink->next_sample ? "+" : "-", GST_TIME_ARGS (diff_s));
1355 static GstFlowReturn
1356 gst_base_audio_sink_render (GstBaseSink * bsink, GstBuffer * buf)
1359 GstClockTime time, stop, render_start, render_stop, sample_offset;
1360 GstClockTimeDiff sync_offset, ts_offset;
1361 GstBaseAudioSinkClass *bclass;
1362 GstBaseAudioSink *sink;
1363 GstRingBuffer *ringbuf;
1365 guint64 ctime, cstop;
1369 guint samples, written;
1373 GstClockTime base_time, render_delay, latency;
1375 gboolean sync, slaved, align_next;
1377 GstSegment clip_seg;
1379 GstBuffer *out = NULL;
1381 sink = GST_BASE_AUDIO_SINK (bsink);
1382 bclass = GST_BASE_AUDIO_SINK_GET_CLASS (sink);
1384 ringbuf = sink->ringbuffer;
1386 /* can't do anything when we don't have the device */
1387 if (G_UNLIKELY (!gst_ring_buffer_is_acquired (ringbuf)))
1390 /* Wait for upstream latency before starting the ringbuffer, we do this so
1391 * that we can align the first sample of the ringbuffer to the base_time +
1393 GST_OBJECT_LOCK (sink);
1394 base_time = GST_ELEMENT_CAST (sink)->base_time;
1395 if (G_UNLIKELY (sink->priv->sync_latency)) {
1396 ret = gst_base_audio_sink_sync_latency (bsink, GST_MINI_OBJECT_CAST (buf));
1397 GST_OBJECT_UNLOCK (sink);
1398 if (G_UNLIKELY (ret != GST_FLOW_OK))
1399 goto sync_latency_failed;
1400 /* only do this once until we are set back to PLAYING */
1401 sink->priv->sync_latency = FALSE;
1403 GST_OBJECT_UNLOCK (sink);
1406 /* Before we go on, let's see if we need to payload the data. If yes, we also
1407 * need to unref the output buffer before leaving. */
1408 if (bclass->payload) {
1409 out = bclass->payload (sink, buf);
1412 goto payload_failed;
1417 bpf = GST_AUDIO_INFO_BPF (&ringbuf->spec.info);
1418 rate = GST_AUDIO_INFO_RATE (&ringbuf->spec.info);
1420 size = gst_buffer_get_size (buf);
1421 if (G_UNLIKELY (size % bpf) != 0)
1424 samples = size / bpf;
1425 out_samples = samples;
1427 in_offset = GST_BUFFER_OFFSET (buf);
1428 time = GST_BUFFER_TIMESTAMP (buf);
1430 GST_DEBUG_OBJECT (sink,
1431 "time %" GST_TIME_FORMAT ", offset %" G_GUINT64_FORMAT ", start %"
1432 GST_TIME_FORMAT ", samples %u", GST_TIME_ARGS (time), in_offset,
1433 GST_TIME_ARGS (bsink->segment.start), samples);
1437 /* if not valid timestamp or we can't clip or sync, try to play
1439 if (!GST_CLOCK_TIME_IS_VALID (time)) {
1440 render_start = gst_base_audio_sink_get_offset (sink);
1441 render_stop = render_start + samples;
1442 GST_DEBUG_OBJECT (sink, "Buffer of size %" G_GSIZE_FORMAT " has no time."
1443 " Using render_start=%" G_GUINT64_FORMAT, size, render_start);
1444 /* we don't have a start so we don't know stop either */
1449 /* let's calc stop based on the number of samples in the buffer instead
1450 * of trusting the DURATION */
1451 stop = time + gst_util_uint64_scale_int (samples, GST_SECOND, rate);
1453 /* prepare the clipping segment. Since we will be subtracting ts-offset and
1454 * device-delay later we scale the start and stop with those values so that we
1455 * can correctly clip them */
1456 clip_seg.format = GST_FORMAT_TIME;
1457 clip_seg.start = bsink->segment.start;
1458 clip_seg.stop = bsink->segment.stop;
1459 clip_seg.duration = -1;
1461 /* the sync offset is the combination of ts-offset and device-delay */
1462 latency = gst_base_sink_get_latency (bsink);
1463 ts_offset = gst_base_sink_get_ts_offset (bsink);
1464 render_delay = gst_base_sink_get_render_delay (bsink);
1465 sync_offset = ts_offset - render_delay + latency;
1467 GST_DEBUG_OBJECT (sink,
1468 "sync-offset %" G_GINT64_FORMAT ", render-delay %" GST_TIME_FORMAT
1469 ", ts-offset %" G_GINT64_FORMAT, sync_offset,
1470 GST_TIME_ARGS (render_delay), ts_offset);
1472 /* compensate for ts-offset and device-delay when negative we need to
1474 if (sync_offset < 0) {
1475 clip_seg.start += -sync_offset;
1476 if (clip_seg.stop != -1)
1477 clip_seg.stop += -sync_offset;
1480 /* samples should be rendered based on their timestamp. All samples
1481 * arriving before the segment.start or after segment.stop are to be
1482 * thrown away. All samples should also be clipped to the segment
1484 if (!gst_segment_clip (&clip_seg, GST_FORMAT_TIME, time, stop, &ctime,
1486 goto out_of_segment;
1488 /* see if some clipping happened */
1489 diff = ctime - time;
1491 /* bring clipped time to samples */
1492 diff = gst_util_uint64_scale_int (diff, rate, GST_SECOND);
1493 GST_DEBUG_OBJECT (sink, "clipping start to %" GST_TIME_FORMAT " %"
1494 G_GUINT64_FORMAT " samples", GST_TIME_ARGS (ctime), diff);
1496 offset += diff * bpf;
1499 diff = stop - cstop;
1501 /* bring clipped time to samples */
1502 diff = gst_util_uint64_scale_int (diff, rate, GST_SECOND);
1503 GST_DEBUG_OBJECT (sink, "clipping stop to %" GST_TIME_FORMAT " %"
1504 G_GUINT64_FORMAT " samples", GST_TIME_ARGS (cstop), diff);
1509 /* figure out how to sync */
1510 if ((clock = GST_ELEMENT_CLOCK (bsink)))
1516 /* no sync needed, play sample ASAP */
1517 render_start = gst_base_audio_sink_get_offset (sink);
1518 render_stop = render_start + samples;
1519 GST_DEBUG_OBJECT (sink,
1520 "no sync needed. Using render_start=%" G_GUINT64_FORMAT, render_start);
1524 /* bring buffer start and stop times to running time */
1526 gst_segment_to_running_time (&bsink->segment, GST_FORMAT_TIME, time);
1528 gst_segment_to_running_time (&bsink->segment, GST_FORMAT_TIME, stop);
1530 GST_DEBUG_OBJECT (sink,
1531 "running: start %" GST_TIME_FORMAT " - stop %" GST_TIME_FORMAT,
1532 GST_TIME_ARGS (render_start), GST_TIME_ARGS (render_stop));
1534 /* store the time of the last sample, we'll use this to perform sync on the
1535 * last sample when draining the buffer */
1536 if (bsink->segment.rate >= 0.0) {
1537 sink->priv->eos_time = render_stop;
1539 sink->priv->eos_time = render_start;
1542 /* compensate for ts-offset and delay we know this will not underflow because we
1544 GST_DEBUG_OBJECT (sink,
1545 "compensating for sync-offset %" GST_TIME_FORMAT,
1546 GST_TIME_ARGS (sync_offset));
1547 render_start += sync_offset;
1548 render_stop += sync_offset;
1550 GST_DEBUG_OBJECT (sink, "adding base_time %" GST_TIME_FORMAT,
1551 GST_TIME_ARGS (base_time));
1553 /* add base time to sync against the clock */
1554 render_start += base_time;
1555 render_stop += base_time;
1557 GST_DEBUG_OBJECT (sink,
1558 "after compensation: start %" GST_TIME_FORMAT " - stop %" GST_TIME_FORMAT,
1559 GST_TIME_ARGS (render_start), GST_TIME_ARGS (render_stop));
1561 if ((slaved = clock != sink->provided_clock)) {
1562 /* handle clock slaving */
1563 gst_base_audio_sink_handle_slaving (sink, render_start, render_stop,
1564 &render_start, &render_stop);
1566 /* no slaving needed but we need to adapt to the clock calibration
1568 gst_base_audio_sink_none_slaving (sink, render_start, render_stop,
1569 &render_start, &render_stop);
1572 GST_DEBUG_OBJECT (sink,
1573 "final timestamps: start %" GST_TIME_FORMAT " - stop %" GST_TIME_FORMAT,
1574 GST_TIME_ARGS (render_start), GST_TIME_ARGS (render_stop));
1576 /* bring to position in the ringbuffer */
1577 time_offset = GST_AUDIO_CLOCK_CAST (sink->provided_clock)->time_offset;
1578 GST_DEBUG_OBJECT (sink,
1579 "time offset %" GST_TIME_FORMAT, GST_TIME_ARGS (time_offset));
1580 if (render_start > time_offset)
1581 render_start -= time_offset;
1584 if (render_stop > time_offset)
1585 render_stop -= time_offset;
1589 /* in some clock slaving cases, all late samples end up at 0 first,
1590 * and subsequent ones align with that until threshold exceeded,
1591 * and then sync back to 0 and so on, so avoid that altogether */
1592 if (G_UNLIKELY (render_start == 0 && render_stop == 0))
1595 /* and bring the time to the rate corrected offset in the buffer */
1596 render_start = gst_util_uint64_scale_int (render_start, rate, GST_SECOND);
1597 render_stop = gst_util_uint64_scale_int (render_stop, rate, GST_SECOND);
1599 /* positive playback rate, first sample is render_start, negative rate, first
1600 * sample is render_stop. When no rate conversion is active, render exactly
1601 * the amount of input samples to avoid aligning to rounding errors. */
1602 if (bsink->segment.rate >= 0.0) {
1603 sample_offset = render_start;
1604 if (bsink->segment.rate == 1.0)
1605 render_stop = sample_offset + samples;
1607 sample_offset = render_stop;
1608 if (bsink->segment.rate == -1.0)
1609 render_start = sample_offset + samples;
1612 /* always resync after a discont */
1613 if (G_UNLIKELY (GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_DISCONT))) {
1614 GST_DEBUG_OBJECT (sink, "resync after discont");
1618 /* resync when we don't know what to align the sample with */
1619 if (G_UNLIKELY (sink->next_sample == -1)) {
1620 GST_DEBUG_OBJECT (sink,
1621 "no align possible: no previous sample position known");
1625 align = gst_base_audio_sink_get_alignment (sink, sample_offset);
1626 sink->priv->last_align = align;
1628 /* apply alignment */
1629 render_start += align;
1631 /* only align stop if we are not slaved to resample */
1632 if (slaved && sink->priv->slave_method == GST_BASE_AUDIO_SINK_SLAVE_RESAMPLE) {
1633 GST_DEBUG_OBJECT (sink, "no stop time align needed: we are slaved");
1636 render_stop += align;
1639 /* number of target samples is difference between start and stop */
1640 out_samples = render_stop - render_start;
1643 /* we render the first or last sample first, depending on the rate */
1644 if (bsink->segment.rate >= 0.0)
1645 sample_offset = render_start;
1647 sample_offset = render_stop;
1649 GST_DEBUG_OBJECT (sink, "rendering at %" G_GUINT64_FORMAT " %d/%d",
1650 sample_offset, samples, out_samples);
1652 /* we need to accumulate over different runs for when we get interrupted */
1655 data = gst_buffer_map (buf, &size, NULL, GST_MAP_READ);
1658 gst_ring_buffer_commit_full (ringbuf, &sample_offset, data + offset,
1659 samples, out_samples, &accum);
1661 GST_DEBUG_OBJECT (sink, "wrote %u of %u", written, samples);
1662 /* if we wrote all, we're done */
1663 if (written == samples)
1666 /* else something interrupted us and we wait for preroll. */
1667 if ((ret = gst_base_sink_wait_preroll (bsink)) != GST_FLOW_OK)
1670 /* if we got interrupted, we cannot assume that the next sample should
1671 * be aligned to this one */
1674 /* update the output samples. FIXME, this will just skip them when pausing
1675 * during trick mode */
1676 if (out_samples > written) {
1677 out_samples -= written;
1683 offset += written * bpf;
1685 gst_buffer_unmap (buf, data, size);
1688 sink->next_sample = sample_offset;
1690 sink->next_sample = -1;
1692 GST_DEBUG_OBJECT (sink, "next sample expected at %" G_GUINT64_FORMAT,
1695 if (GST_CLOCK_TIME_IS_VALID (stop) && stop >= bsink->segment.stop) {
1696 GST_DEBUG_OBJECT (sink,
1697 "start playback because we are at the end of segment");
1698 gst_ring_buffer_start (ringbuf);
1705 gst_buffer_unref (out);
1712 GST_DEBUG_OBJECT (sink,
1713 "dropping sample out of segment time %" GST_TIME_FORMAT ", start %"
1714 GST_TIME_FORMAT, GST_TIME_ARGS (time),
1715 GST_TIME_ARGS (bsink->segment.start));
1721 GST_DEBUG_OBJECT (sink, "dropping late sample");
1727 GST_ELEMENT_ERROR (sink, STREAM, FORMAT, (NULL), ("failed to payload."));
1728 ret = GST_FLOW_ERROR;
1733 GST_DEBUG_OBJECT (sink, "ringbuffer not negotiated");
1734 GST_ELEMENT_ERROR (sink, STREAM, FORMAT, (NULL), ("sink not negotiated."));
1735 ret = GST_FLOW_NOT_NEGOTIATED;
1740 GST_DEBUG_OBJECT (sink, "wrong size");
1741 GST_ELEMENT_ERROR (sink, STREAM, WRONG_TYPE,
1742 (NULL), ("sink received buffer of wrong size."));
1743 ret = GST_FLOW_ERROR;
1748 GST_DEBUG_OBJECT (sink, "preroll got interrupted: %d (%s)", ret,
1749 gst_flow_get_name (ret));
1750 gst_buffer_unmap (buf, data, size);
1753 sync_latency_failed:
1755 GST_DEBUG_OBJECT (sink, "failed waiting for latency");
1761 * gst_base_audio_sink_create_ringbuffer:
1762 * @sink: a #GstBaseAudioSink.
1764 * Create and return the #GstRingBuffer for @sink. This function will call the
1765 * ::create_ringbuffer vmethod and will set @sink as the parent of the returned
1766 * buffer (see gst_object_set_parent()).
1768 * Returns: The new ringbuffer of @sink.
1771 gst_base_audio_sink_create_ringbuffer (GstBaseAudioSink * sink)
1773 GstBaseAudioSinkClass *bclass;
1774 GstRingBuffer *buffer = NULL;
1776 bclass = GST_BASE_AUDIO_SINK_GET_CLASS (sink);
1777 if (bclass->create_ringbuffer)
1778 buffer = bclass->create_ringbuffer (sink);
1781 gst_object_set_parent (GST_OBJECT (buffer), GST_OBJECT (sink));
1787 gst_base_audio_sink_callback (GstRingBuffer * rbuf, guint8 * data, guint len,
1790 GstBaseSink *basesink;
1791 GstBaseAudioSink *sink;
1796 basesink = GST_BASE_SINK (user_data);
1797 sink = GST_BASE_AUDIO_SINK (user_data);
1799 GST_PAD_STREAM_LOCK (basesink->sinkpad);
1801 /* would be nice to arrange for pad_alloc_buffer to return data -- as it is we
1802 will copy twice, once into data, once into DMA */
1803 GST_LOG_OBJECT (basesink, "pulling %u bytes offset %" G_GUINT64_FORMAT
1804 " to fill audio buffer", len, basesink->offset);
1806 gst_pad_pull_range (basesink->sinkpad, basesink->segment.position, len,
1809 if (ret != GST_FLOW_OK) {
1810 if (ret == GST_FLOW_EOS)
1816 GST_BASE_SINK_PREROLL_LOCK (basesink);
1817 if (basesink->flushing)
1820 /* complete preroll and wait for PLAYING */
1821 ret = gst_base_sink_do_preroll (basesink, GST_MINI_OBJECT_CAST (buf));
1822 if (ret != GST_FLOW_OK)
1825 size = gst_buffer_get_size (buf);
1828 GST_INFO_OBJECT (basesink,
1829 "got different size than requested from sink pad: %u"
1830 " != %" G_GSIZE_FORMAT, len, size);
1831 len = MIN (size, len);
1834 basesink->segment.position += len;
1836 gst_buffer_extract (buf, 0, data, len);
1837 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
1839 GST_PAD_STREAM_UNLOCK (basesink->sinkpad);
1845 GST_WARNING_OBJECT (basesink, "Got flow '%s' but can't return it: %d",
1846 gst_flow_get_name (ret), ret);
1847 gst_ring_buffer_pause (rbuf);
1848 GST_PAD_STREAM_UNLOCK (basesink->sinkpad);
1853 /* FIXME: this is not quite correct; we'll be called endlessly until
1854 * the sink gets shut down; maybe we should set a flag somewhere, or
1855 * set segment.stop and segment.duration to the last sample or so */
1856 GST_DEBUG_OBJECT (sink, "EOS");
1857 gst_base_audio_sink_drain (sink);
1858 gst_ring_buffer_pause (rbuf);
1859 gst_element_post_message (GST_ELEMENT_CAST (sink),
1860 gst_message_new_eos (GST_OBJECT_CAST (sink)));
1861 GST_PAD_STREAM_UNLOCK (basesink->sinkpad);
1865 GST_DEBUG_OBJECT (sink, "we are flushing");
1866 gst_ring_buffer_pause (rbuf);
1867 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
1868 GST_PAD_STREAM_UNLOCK (basesink->sinkpad);
1873 GST_DEBUG_OBJECT (sink, "error %s", gst_flow_get_name (ret));
1874 gst_ring_buffer_pause (rbuf);
1875 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
1876 GST_PAD_STREAM_UNLOCK (basesink->sinkpad);
1882 gst_base_audio_sink_activate_pull (GstBaseSink * basesink, gboolean active)
1885 GstBaseAudioSink *sink = GST_BASE_AUDIO_SINK (basesink);
1888 GST_DEBUG_OBJECT (basesink, "activating pull");
1890 gst_ring_buffer_set_callback (sink->ringbuffer,
1891 gst_base_audio_sink_callback, sink);
1893 ret = gst_ring_buffer_activate (sink->ringbuffer, TRUE);
1895 GST_DEBUG_OBJECT (basesink, "deactivating pull");
1896 gst_ring_buffer_set_callback (sink->ringbuffer, NULL, NULL);
1897 ret = gst_ring_buffer_activate (sink->ringbuffer, FALSE);
1904 /* should be called with the LOCK */
1905 static GstStateChangeReturn
1906 gst_base_audio_sink_async_play (GstBaseSink * basesink)
1908 GstBaseAudioSink *sink;
1910 sink = GST_BASE_AUDIO_SINK (basesink);
1912 GST_DEBUG_OBJECT (sink, "ringbuffer may start now");
1913 sink->priv->sync_latency = TRUE;
1914 gst_ring_buffer_may_start (sink->ringbuffer, TRUE);
1915 if (basesink->pad_mode == GST_PAD_ACTIVATE_PULL) {
1916 /* we always start the ringbuffer in pull mode immediatly */
1917 gst_ring_buffer_start (sink->ringbuffer);
1920 return GST_STATE_CHANGE_SUCCESS;
1924 static GstStateChangeReturn
1925 gst_base_audio_sink_change_state (GstElement * element,
1926 GstStateChange transition)
1928 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
1929 GstBaseAudioSink *sink = GST_BASE_AUDIO_SINK (element);
1931 switch (transition) {
1932 case GST_STATE_CHANGE_NULL_TO_READY:
1933 if (sink->ringbuffer == NULL) {
1934 gst_audio_clock_reset (GST_AUDIO_CLOCK (sink->provided_clock), 0);
1935 sink->ringbuffer = gst_base_audio_sink_create_ringbuffer (sink);
1937 if (!gst_ring_buffer_open_device (sink->ringbuffer))
1940 case GST_STATE_CHANGE_READY_TO_PAUSED:
1941 sink->next_sample = -1;
1942 sink->priv->last_align = -1;
1943 sink->priv->eos_time = -1;
1944 gst_ring_buffer_set_flushing (sink->ringbuffer, FALSE);
1945 gst_ring_buffer_may_start (sink->ringbuffer, FALSE);
1947 /* Only post clock-provide messages if this is the clock that
1948 * we've created. If the subclass has overriden it the subclass
1949 * should post this messages whenever necessary */
1950 if (sink->provided_clock && GST_IS_AUDIO_CLOCK (sink->provided_clock) &&
1951 GST_AUDIO_CLOCK_CAST (sink->provided_clock)->func ==
1952 (GstAudioClockGetTimeFunc) gst_base_audio_sink_get_time)
1953 gst_element_post_message (element,
1954 gst_message_new_clock_provide (GST_OBJECT_CAST (element),
1955 sink->provided_clock, TRUE));
1957 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1961 GST_OBJECT_LOCK (sink);
1962 GST_DEBUG_OBJECT (sink, "ringbuffer may start now");
1963 sink->priv->sync_latency = TRUE;
1964 eos = GST_BASE_SINK (sink)->eos;
1965 GST_OBJECT_UNLOCK (sink);
1967 gst_ring_buffer_may_start (sink->ringbuffer, TRUE);
1968 if (GST_BASE_SINK_CAST (sink)->pad_mode == GST_PAD_ACTIVATE_PULL ||
1969 g_atomic_int_get (&sink->eos_rendering) || eos) {
1970 /* we always start the ringbuffer in pull mode immediatly */
1971 /* sync rendering on eos needs running clock,
1972 * and others need running clock when finished rendering eos */
1973 gst_ring_buffer_start (sink->ringbuffer);
1977 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1978 /* ringbuffer cannot start anymore */
1979 gst_ring_buffer_may_start (sink->ringbuffer, FALSE);
1980 gst_ring_buffer_pause (sink->ringbuffer);
1982 GST_OBJECT_LOCK (sink);
1983 sink->priv->sync_latency = FALSE;
1984 GST_OBJECT_UNLOCK (sink);
1986 case GST_STATE_CHANGE_PAUSED_TO_READY:
1987 /* Only post clock-lost messages if this is the clock that
1988 * we've created. If the subclass has overriden it the subclass
1989 * should post this messages whenever necessary */
1990 if (sink->provided_clock && GST_IS_AUDIO_CLOCK (sink->provided_clock) &&
1991 GST_AUDIO_CLOCK_CAST (sink->provided_clock)->func ==
1992 (GstAudioClockGetTimeFunc) gst_base_audio_sink_get_time)
1993 gst_element_post_message (element,
1994 gst_message_new_clock_lost (GST_OBJECT_CAST (element),
1995 sink->provided_clock));
1997 /* make sure we unblock before calling the parent state change
1998 * so it can grab the STREAM_LOCK */
1999 gst_ring_buffer_set_flushing (sink->ringbuffer, TRUE);
2005 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
2007 switch (transition) {
2008 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
2009 /* stop slaving ourselves to the master, if any */
2010 gst_clock_set_master (sink->provided_clock, NULL);
2012 case GST_STATE_CHANGE_PAUSED_TO_READY:
2013 gst_ring_buffer_activate (sink->ringbuffer, FALSE);
2014 gst_ring_buffer_release (sink->ringbuffer);
2016 case GST_STATE_CHANGE_READY_TO_NULL:
2017 /* we release again here because the aqcuire happens when setting the
2018 * caps, which happens before we commit the state to PAUSED and thus the
2019 * PAUSED->READY state change (see above, where we release the ringbuffer)
2020 * might not be called when we get here. */
2021 gst_ring_buffer_activate (sink->ringbuffer, FALSE);
2022 gst_ring_buffer_release (sink->ringbuffer);
2023 gst_ring_buffer_close_device (sink->ringbuffer);
2024 GST_OBJECT_LOCK (sink);
2025 gst_object_unparent (GST_OBJECT_CAST (sink->ringbuffer));
2026 sink->ringbuffer = NULL;
2027 GST_OBJECT_UNLOCK (sink);
2038 /* subclass must post a meaningfull error message */
2039 GST_DEBUG_OBJECT (sink, "open failed");
2040 return GST_STATE_CHANGE_FAILURE;