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;
61 /* BaseAudioSink signals and args */
68 /* we tollerate half a second diff before we start resyncing. This
69 * should be enough to compensate for various rounding errors in the timestamp
70 * and sample offset position.
71 * This is an emergency resync fallback since buffers marked as DISCONT will
72 * always lock to the correct timestamp immediatly and buffers not marked as
73 * DISCONT are contiguous by definition.
75 #define DIFF_TOLERANCE 2
77 /* FIXME: 0.11, store the buffer_time and latency_time in nanoseconds */
78 #define DEFAULT_BUFFER_TIME ((200 * GST_MSECOND) / GST_USECOND)
79 #define DEFAULT_LATENCY_TIME ((10 * GST_MSECOND) / GST_USECOND)
80 #define DEFAULT_PROVIDE_CLOCK TRUE
81 #define DEFAULT_SLAVE_METHOD GST_BASE_AUDIO_SINK_SLAVE_SKEW
93 gst_base_audio_sink_slave_method_get_type (void)
95 static GType slave_method_type = 0;
96 static const GEnumValue slave_method[] = {
97 {GST_BASE_AUDIO_SINK_SLAVE_RESAMPLE, "Resampling slaving", "resample"},
98 {GST_BASE_AUDIO_SINK_SLAVE_SKEW, "Skew slaving", "skew"},
99 {GST_BASE_AUDIO_SINK_SLAVE_NONE, "No slaving", "none"},
103 if (!slave_method_type) {
105 g_enum_register_static ("GstBaseAudioSinkSlaveMethod", slave_method);
107 return slave_method_type;
111 #define _do_init(bla) \
112 GST_DEBUG_CATEGORY_INIT (gst_base_audio_sink_debug, "baseaudiosink", 0, "baseaudiosink element");
114 GST_BOILERPLATE_FULL (GstBaseAudioSink, gst_base_audio_sink, GstBaseSink,
115 GST_TYPE_BASE_SINK, _do_init);
117 static void gst_base_audio_sink_dispose (GObject * object);
119 static void gst_base_audio_sink_set_property (GObject * object, guint prop_id,
120 const GValue * value, GParamSpec * pspec);
121 static void gst_base_audio_sink_get_property (GObject * object, guint prop_id,
122 GValue * value, GParamSpec * pspec);
124 static GstStateChangeReturn gst_base_audio_sink_async_play (GstBaseSink *
126 static GstStateChangeReturn gst_base_audio_sink_change_state (GstElement *
127 element, GstStateChange transition);
128 static gboolean gst_base_audio_sink_activate_pull (GstBaseSink * basesink,
130 static gboolean gst_base_audio_sink_query (GstElement * element, GstQuery *
133 static GstClock *gst_base_audio_sink_provide_clock (GstElement * elem);
134 static GstClockTime gst_base_audio_sink_get_time (GstClock * clock,
135 GstBaseAudioSink * sink);
136 static void gst_base_audio_sink_callback (GstRingBuffer * rbuf, guint8 * data,
137 guint len, gpointer user_data);
139 static GstFlowReturn gst_base_audio_sink_preroll (GstBaseSink * bsink,
141 static GstFlowReturn gst_base_audio_sink_render (GstBaseSink * bsink,
143 static gboolean gst_base_audio_sink_event (GstBaseSink * bsink,
145 static void gst_base_audio_sink_get_times (GstBaseSink * bsink,
146 GstBuffer * buffer, GstClockTime * start, GstClockTime * end);
147 static gboolean gst_base_audio_sink_setcaps (GstBaseSink * bsink,
149 static void gst_base_audio_sink_fixate (GstBaseSink * bsink, GstCaps * caps);
151 static gboolean gst_base_audio_sink_query_pad (GstPad * pad, GstQuery * query);
154 /* static guint gst_base_audio_sink_signals[LAST_SIGNAL] = { 0 }; */
157 gst_base_audio_sink_base_init (gpointer g_class)
162 gst_base_audio_sink_class_init (GstBaseAudioSinkClass * klass)
164 GObjectClass *gobject_class;
165 GstElementClass *gstelement_class;
166 GstBaseSinkClass *gstbasesink_class;
168 gobject_class = (GObjectClass *) klass;
169 gstelement_class = (GstElementClass *) klass;
170 gstbasesink_class = (GstBaseSinkClass *) klass;
172 g_type_class_add_private (klass, sizeof (GstBaseAudioSinkPrivate));
174 gobject_class->set_property =
175 GST_DEBUG_FUNCPTR (gst_base_audio_sink_set_property);
176 gobject_class->get_property =
177 GST_DEBUG_FUNCPTR (gst_base_audio_sink_get_property);
178 gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_base_audio_sink_dispose);
180 g_object_class_install_property (gobject_class, PROP_BUFFER_TIME,
181 g_param_spec_int64 ("buffer-time", "Buffer Time",
182 "Size of audio buffer in microseconds", 1,
183 G_MAXINT64, DEFAULT_BUFFER_TIME,
184 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
186 g_object_class_install_property (gobject_class, PROP_LATENCY_TIME,
187 g_param_spec_int64 ("latency-time", "Latency Time",
188 "Audio latency in microseconds", 1,
189 G_MAXINT64, DEFAULT_LATENCY_TIME,
190 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
192 g_object_class_install_property (gobject_class, PROP_PROVIDE_CLOCK,
193 g_param_spec_boolean ("provide-clock", "Provide Clock",
194 "Provide a clock to be used as the global pipeline clock",
195 DEFAULT_PROVIDE_CLOCK, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
197 g_object_class_install_property (gobject_class, PROP_SLAVE_METHOD,
198 g_param_spec_enum ("slave-method", "Slave Method",
199 "Algorithm to use to match the rate of the masterclock",
200 GST_TYPE_BASE_AUDIO_SINK_SLAVE_METHOD, DEFAULT_SLAVE_METHOD,
201 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
203 gstelement_class->change_state =
204 GST_DEBUG_FUNCPTR (gst_base_audio_sink_change_state);
205 gstelement_class->provide_clock =
206 GST_DEBUG_FUNCPTR (gst_base_audio_sink_provide_clock);
207 gstelement_class->query = GST_DEBUG_FUNCPTR (gst_base_audio_sink_query);
209 gstbasesink_class->event = GST_DEBUG_FUNCPTR (gst_base_audio_sink_event);
210 gstbasesink_class->preroll = GST_DEBUG_FUNCPTR (gst_base_audio_sink_preroll);
211 gstbasesink_class->render = GST_DEBUG_FUNCPTR (gst_base_audio_sink_render);
212 gstbasesink_class->get_times =
213 GST_DEBUG_FUNCPTR (gst_base_audio_sink_get_times);
214 gstbasesink_class->set_caps = GST_DEBUG_FUNCPTR (gst_base_audio_sink_setcaps);
215 gstbasesink_class->fixate = GST_DEBUG_FUNCPTR (gst_base_audio_sink_fixate);
216 gstbasesink_class->async_play =
217 GST_DEBUG_FUNCPTR (gst_base_audio_sink_async_play);
218 gstbasesink_class->activate_pull =
219 GST_DEBUG_FUNCPTR (gst_base_audio_sink_activate_pull);
221 /* ref class from a thread-safe context to work around missing bit of
222 * thread-safety in GObject */
223 g_type_class_ref (GST_TYPE_AUDIO_CLOCK);
224 g_type_class_ref (GST_TYPE_RING_BUFFER);
228 gst_base_audio_sink_init (GstBaseAudioSink * baseaudiosink,
229 GstBaseAudioSinkClass * g_class)
231 baseaudiosink->priv = GST_BASE_AUDIO_SINK_GET_PRIVATE (baseaudiosink);
233 baseaudiosink->buffer_time = DEFAULT_BUFFER_TIME;
234 baseaudiosink->latency_time = DEFAULT_LATENCY_TIME;
235 baseaudiosink->provide_clock = DEFAULT_PROVIDE_CLOCK;
236 baseaudiosink->priv->slave_method = DEFAULT_SLAVE_METHOD;
238 baseaudiosink->provided_clock = gst_audio_clock_new ("GstAudioSinkClock",
239 (GstAudioClockGetTimeFunc) gst_base_audio_sink_get_time, baseaudiosink);
241 GST_BASE_SINK (baseaudiosink)->can_activate_push = TRUE;
242 /* FIXME, enable pull mode when segments, latency, state changes, negotiation
243 * and clock slaving are figured out */
244 GST_BASE_SINK (baseaudiosink)->can_activate_pull = FALSE;
246 /* install some custom pad_query functions */
247 gst_pad_set_query_function (GST_BASE_SINK_PAD (baseaudiosink),
248 GST_DEBUG_FUNCPTR (gst_base_audio_sink_query_pad));
252 gst_base_audio_sink_dispose (GObject * object)
254 GstBaseAudioSink *sink;
256 sink = GST_BASE_AUDIO_SINK (object);
258 if (sink->provided_clock)
259 gst_object_unref (sink->provided_clock);
260 sink->provided_clock = NULL;
262 if (sink->ringbuffer) {
263 gst_object_unparent (GST_OBJECT_CAST (sink->ringbuffer));
264 sink->ringbuffer = NULL;
267 G_OBJECT_CLASS (parent_class)->dispose (object);
272 gst_base_audio_sink_provide_clock (GstElement * elem)
274 GstBaseAudioSink *sink;
277 sink = GST_BASE_AUDIO_SINK (elem);
279 /* we have no ringbuffer (must be NULL state) */
280 if (sink->ringbuffer == NULL)
283 if (!gst_ring_buffer_is_acquired (sink->ringbuffer))
286 GST_OBJECT_LOCK (sink);
287 if (!sink->provide_clock)
290 clock = GST_CLOCK_CAST (gst_object_ref (sink->provided_clock));
291 GST_OBJECT_UNLOCK (sink);
298 GST_DEBUG_OBJECT (sink, "ringbuffer not acquired");
303 GST_DEBUG_OBJECT (sink, "clock provide disabled");
304 GST_OBJECT_UNLOCK (sink);
310 gst_base_audio_sink_query_pad (GstPad * pad, GstQuery * query)
312 gboolean res = FALSE;
313 GstBaseAudioSink *basesink;
315 basesink = GST_BASE_AUDIO_SINK (gst_pad_get_parent (pad));
317 switch (GST_QUERY_TYPE (query)) {
318 case GST_QUERY_CONVERT:
320 GstFormat src_fmt, dest_fmt;
321 gint64 src_val, dest_val;
323 GST_LOG_OBJECT (pad, "query convert");
325 if (basesink->ringbuffer) {
326 gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, NULL);
327 res = gst_ring_buffer_convert (basesink->ringbuffer, src_fmt, src_val,
328 dest_fmt, &dest_val);
330 gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
339 gst_object_unref (basesink);
345 gst_base_audio_sink_query (GstElement * element, GstQuery * query)
347 gboolean res = FALSE;
348 GstBaseAudioSink *basesink;
350 basesink = GST_BASE_AUDIO_SINK (element);
352 switch (GST_QUERY_TYPE (query)) {
353 case GST_QUERY_LATENCY:
355 gboolean live, us_live;
356 GstClockTime min_l, max_l;
358 GST_DEBUG_OBJECT (basesink, "latency query");
360 if (!basesink->ringbuffer || !basesink->ringbuffer->spec.rate) {
361 GST_DEBUG_OBJECT (basesink,
362 "we are not yet negotiated, can't report latency yet");
367 /* ask parent first, it will do an upstream query for us. */
369 gst_base_sink_query_latency (GST_BASE_SINK_CAST (basesink), &live,
370 &us_live, &min_l, &max_l))) {
371 GstClockTime min_latency, max_latency;
373 /* we and upstream are both live, adjust the min_latency */
374 if (live && us_live) {
375 GstRingBufferSpec *spec;
377 spec = &basesink->ringbuffer->spec;
379 basesink->priv->us_latency = min_l;
382 gst_util_uint64_scale_int (spec->seglatency * spec->segsize,
383 GST_SECOND, spec->rate * spec->bytes_per_sample);
385 /* we cannot go lower than the buffer size and the min peer latency */
386 min_latency = min_latency + min_l;
387 /* the max latency is the max of the peer, we can delay an infinite
389 max_latency = min_latency + (max_l == -1 ? 0 : max_l);
391 GST_DEBUG_OBJECT (basesink,
392 "peer min %" GST_TIME_FORMAT ", our min latency: %"
393 GST_TIME_FORMAT, GST_TIME_ARGS (min_l),
394 GST_TIME_ARGS (min_latency));
396 GST_DEBUG_OBJECT (basesink,
397 "peer or we are not live, don't care about latency");
401 gst_query_set_latency (query, live, min_latency, max_latency);
405 case GST_QUERY_CONVERT:
407 GstFormat src_fmt, dest_fmt;
408 gint64 src_val, dest_val;
410 GST_LOG_OBJECT (basesink, "query convert");
412 if (basesink->ringbuffer) {
413 gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, NULL);
414 res = gst_ring_buffer_convert (basesink->ringbuffer, src_fmt, src_val,
415 dest_fmt, &dest_val);
417 gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
423 res = GST_ELEMENT_CLASS (parent_class)->query (element, query);
433 gst_base_audio_sink_get_time (GstClock * clock, GstBaseAudioSink * sink)
435 guint64 raw, samples;
439 if (sink->ringbuffer == NULL || sink->ringbuffer->spec.rate == 0)
440 return GST_CLOCK_TIME_NONE;
442 /* our processed samples are always increasing */
443 raw = samples = gst_ring_buffer_samples_done (sink->ringbuffer);
445 /* the number of samples not yet processed, this is still queued in the
446 * device (not played for playback). */
447 delay = gst_ring_buffer_delay (sink->ringbuffer);
449 if (G_LIKELY (samples >= delay))
454 result = gst_util_uint64_scale_int (samples, GST_SECOND,
455 sink->ringbuffer->spec.rate);
457 GST_DEBUG_OBJECT (sink,
458 "processed samples: raw %llu, delay %u, real %llu, time %"
459 GST_TIME_FORMAT, raw, delay, samples, GST_TIME_ARGS (result));
465 * gst_base_audio_sink_set_provide_clock:
466 * @sink: a #GstBaseAudioSink
467 * @provide: new state
469 * Controls whether @sink will provide a clock or not. If @provide is %TRUE,
470 * gst_element_provide_clock() will return a clock that reflects the datarate
471 * of @sink. If @provide is %FALSE, gst_element_provide_clock() will return NULL.
476 gst_base_audio_sink_set_provide_clock (GstBaseAudioSink * sink,
479 g_return_if_fail (GST_IS_BASE_AUDIO_SINK (sink));
481 GST_OBJECT_LOCK (sink);
482 sink->provide_clock = provide;
483 GST_OBJECT_UNLOCK (sink);
487 * gst_base_audio_sink_get_provide_clock:
488 * @sink: a #GstBaseAudioSink
490 * Queries whether @sink will provide a clock or not. See also
491 * gst_base_audio_sink_set_provide_clock.
493 * Returns: %TRUE if @sink will provide a clock.
498 gst_base_audio_sink_get_provide_clock (GstBaseAudioSink * sink)
502 g_return_val_if_fail (GST_IS_BASE_AUDIO_SINK (sink), FALSE);
504 GST_OBJECT_LOCK (sink);
505 result = sink->provide_clock;
506 GST_OBJECT_UNLOCK (sink);
512 * gst_base_audio_sink_set_slave_method:
513 * @sink: a #GstBaseAudioSink
514 * @method: the new slave method
516 * Controls how clock slaving will be performed in @sink.
521 gst_base_audio_sink_set_slave_method (GstBaseAudioSink * sink,
522 GstBaseAudioSinkSlaveMethod method)
524 g_return_if_fail (GST_IS_BASE_AUDIO_SINK (sink));
526 GST_OBJECT_LOCK (sink);
527 sink->priv->slave_method = method;
528 GST_OBJECT_UNLOCK (sink);
532 * gst_base_audio_sink_get_slave_method:
533 * @sink: a #GstBaseAudioSink
535 * Get the current slave method used by @sink.
537 * Returns: The current slave method used by @sink.
541 GstBaseAudioSinkSlaveMethod
542 gst_base_audio_sink_get_slave_method (GstBaseAudioSink * sink)
544 GstBaseAudioSinkSlaveMethod result;
546 g_return_val_if_fail (GST_IS_BASE_AUDIO_SINK (sink), -1);
548 GST_OBJECT_LOCK (sink);
549 result = sink->priv->slave_method;
550 GST_OBJECT_UNLOCK (sink);
556 gst_base_audio_sink_set_property (GObject * object, guint prop_id,
557 const GValue * value, GParamSpec * pspec)
559 GstBaseAudioSink *sink;
561 sink = GST_BASE_AUDIO_SINK (object);
564 case PROP_BUFFER_TIME:
565 sink->buffer_time = g_value_get_int64 (value);
567 case PROP_LATENCY_TIME:
568 sink->latency_time = g_value_get_int64 (value);
570 case PROP_PROVIDE_CLOCK:
571 gst_base_audio_sink_set_provide_clock (sink, g_value_get_boolean (value));
573 case PROP_SLAVE_METHOD:
574 gst_base_audio_sink_set_slave_method (sink, g_value_get_enum (value));
577 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
583 gst_base_audio_sink_get_property (GObject * object, guint prop_id,
584 GValue * value, GParamSpec * pspec)
586 GstBaseAudioSink *sink;
588 sink = GST_BASE_AUDIO_SINK (object);
591 case PROP_BUFFER_TIME:
592 g_value_set_int64 (value, sink->buffer_time);
594 case PROP_LATENCY_TIME:
595 g_value_set_int64 (value, sink->latency_time);
597 case PROP_PROVIDE_CLOCK:
598 g_value_set_boolean (value, gst_base_audio_sink_get_provide_clock (sink));
600 case PROP_SLAVE_METHOD:
601 g_value_set_enum (value, gst_base_audio_sink_get_slave_method (sink));
604 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
610 gst_base_audio_sink_setcaps (GstBaseSink * bsink, GstCaps * caps)
612 GstBaseAudioSink *sink = GST_BASE_AUDIO_SINK (bsink);
613 GstRingBufferSpec *spec;
615 if (!sink->ringbuffer)
618 spec = &sink->ringbuffer->spec;
620 GST_DEBUG_OBJECT (sink, "release old ringbuffer");
622 /* release old ringbuffer */
623 gst_ring_buffer_activate (sink->ringbuffer, FALSE);
624 gst_ring_buffer_release (sink->ringbuffer);
626 GST_DEBUG_OBJECT (sink, "parse caps");
628 spec->buffer_time = sink->buffer_time;
629 spec->latency_time = sink->latency_time;
632 if (!gst_ring_buffer_parse_caps (spec, caps))
635 gst_ring_buffer_debug_spec_buff (spec);
637 GST_DEBUG_OBJECT (sink, "acquire ringbuffer");
638 if (!gst_ring_buffer_acquire (sink->ringbuffer, spec))
641 if (bsink->pad_mode == GST_ACTIVATE_PUSH) {
642 GST_DEBUG_OBJECT (sink, "activate ringbuffer");
643 gst_ring_buffer_activate (sink->ringbuffer, TRUE);
646 /* calculate actual latency and buffer times.
647 * FIXME: In 0.11, store the latency_time internally in ns */
648 spec->latency_time = gst_util_uint64_scale (spec->segsize,
649 (GST_SECOND / GST_USECOND), spec->rate * spec->bytes_per_sample);
651 spec->buffer_time = spec->segtotal * spec->latency_time;
653 gst_ring_buffer_debug_spec_buff (spec);
660 GST_DEBUG_OBJECT (sink, "could not parse caps");
661 GST_ELEMENT_ERROR (sink, STREAM, FORMAT,
662 (NULL), ("cannot parse audio format."));
667 GST_DEBUG_OBJECT (sink, "could not acquire ringbuffer");
673 gst_base_audio_sink_fixate (GstBaseSink * bsink, GstCaps * caps)
678 s = gst_caps_get_structure (caps, 0);
680 /* fields for all formats */
681 gst_structure_fixate_field_nearest_int (s, "rate", 44100);
682 gst_structure_fixate_field_nearest_int (s, "channels", 2);
683 gst_structure_fixate_field_nearest_int (s, "width", 16);
686 if (gst_structure_has_field (s, "depth")) {
687 gst_structure_get_int (s, "width", &width);
688 /* round width to nearest multiple of 8 for the depth */
689 depth = GST_ROUND_UP_8 (width);
690 gst_structure_fixate_field_nearest_int (s, "depth", depth);
692 if (gst_structure_has_field (s, "signed"))
693 gst_structure_fixate_field_boolean (s, "signed", TRUE);
694 if (gst_structure_has_field (s, "endianness"))
695 gst_structure_fixate_field_nearest_int (s, "endianness", G_BYTE_ORDER);
699 gst_base_audio_sink_get_times (GstBaseSink * bsink, GstBuffer * buffer,
700 GstClockTime * start, GstClockTime * end)
702 /* our clock sync is a bit too much for the base class to handle so
703 * we implement it ourselves. */
704 *start = GST_CLOCK_TIME_NONE;
705 *end = GST_CLOCK_TIME_NONE;
708 /* This waits for the drain to happen and can be canceled */
710 gst_base_audio_sink_drain (GstBaseAudioSink * sink)
712 if (!sink->ringbuffer)
714 if (!sink->ringbuffer->spec.rate)
717 /* need to start playback before we can drain, but only when
718 * we have successfully negotiated a format and thus acquired the
720 if (gst_ring_buffer_is_acquired (sink->ringbuffer))
721 gst_ring_buffer_start (sink->ringbuffer);
723 if (sink->priv->eos_time != -1) {
724 GST_DEBUG_OBJECT (sink,
725 "last sample time %" GST_TIME_FORMAT,
726 GST_TIME_ARGS (sink->priv->eos_time));
728 /* wait for the EOS time to be reached, this is the time when the last
729 * sample is played. */
730 gst_base_sink_wait_eos (GST_BASE_SINK (sink), sink->priv->eos_time, NULL);
732 GST_DEBUG_OBJECT (sink, "drained audio");
738 gst_base_audio_sink_event (GstBaseSink * bsink, GstEvent * event)
740 GstBaseAudioSink *sink = GST_BASE_AUDIO_SINK (bsink);
742 switch (GST_EVENT_TYPE (event)) {
743 case GST_EVENT_FLUSH_START:
744 if (sink->ringbuffer)
745 gst_ring_buffer_set_flushing (sink->ringbuffer, TRUE);
747 case GST_EVENT_FLUSH_STOP:
748 /* always resync on sample after a flush */
749 sink->priv->avg_skew = -1;
750 sink->next_sample = -1;
751 sink->priv->eos_time = -1;
752 if (sink->ringbuffer)
753 gst_ring_buffer_set_flushing (sink->ringbuffer, FALSE);
756 /* now wait till we played everything */
757 gst_base_audio_sink_drain (sink);
759 case GST_EVENT_NEWSEGMENT:
763 /* we only need the rate */
764 gst_event_parse_new_segment_full (event, NULL, &rate, NULL, NULL,
767 GST_DEBUG_OBJECT (sink, "new segment rate of %f", rate);
777 gst_base_audio_sink_preroll (GstBaseSink * bsink, GstBuffer * buffer)
779 GstBaseAudioSink *sink = GST_BASE_AUDIO_SINK (bsink);
781 if (!gst_ring_buffer_is_acquired (sink->ringbuffer))
784 /* we don't really do anything when prerolling. We could make a
785 * property to play this buffer to have some sort of scrubbing
791 GST_DEBUG_OBJECT (sink, "ringbuffer in wrong state");
792 GST_ELEMENT_ERROR (sink, STREAM, FORMAT, (NULL), ("sink not negotiated."));
793 return GST_FLOW_NOT_NEGOTIATED;
798 gst_base_audio_sink_get_offset (GstBaseAudioSink * sink)
801 gint writeseg, segdone, sps;
804 /* assume we can append to the previous sample */
805 sample = sink->next_sample;
806 /* no previous sample, try to insert at position 0 */
810 sps = sink->ringbuffer->samples_per_seg;
812 /* figure out the segment and the offset inside the segment where
813 * the sample should be written. */
814 writeseg = sample / sps;
816 /* get the currently processed segment */
817 segdone = g_atomic_int_get (&sink->ringbuffer->segdone)
818 - sink->ringbuffer->segbase;
820 /* see how far away it is from the write segment */
821 diff = writeseg - segdone;
823 /* sample would be dropped, position to next playable position */
824 sample = (segdone + 1) * sps;
831 clock_convert_external (GstClockTime external, GstClockTime cinternal,
832 GstClockTime cexternal, GstClockTime crate_num, GstClockTime crate_denom)
834 /* adjust for rate and speed */
835 if (external >= cexternal) {
837 gst_util_uint64_scale (external - cexternal, crate_denom, crate_num);
838 external += cinternal;
841 gst_util_uint64_scale (cexternal - external, crate_denom, crate_num);
842 if (cinternal > external)
843 external = cinternal - external;
850 /* algorithm to calculate sample positions that will result in resampling to
851 * match the clock rate of the master */
853 gst_base_audio_sink_resample_slaving (GstBaseAudioSink * sink,
854 GstClockTime render_start, GstClockTime render_stop,
855 GstClockTime * srender_start, GstClockTime * srender_stop)
857 GstClockTime cinternal, cexternal;
858 GstClockTime crate_num, crate_denom;
860 /* FIXME, we can sample and add observations here or use the timeouts on the
861 * clock. No idea which one is better or more stable. The timeout seems more
862 * arbitrary but this one seems more demanding and does not work when there is
863 * no data comming in to the sink. */
865 GstClockTime etime, itime;
868 /* sample clocks and figure out clock skew */
869 etime = gst_clock_get_time (GST_ELEMENT_CLOCK (sink));
870 itime = gst_clock_get_internal_time (sink->provided_clock);
872 /* add new observation */
873 gst_clock_add_observation (sink->provided_clock, itime, etime, &r_squared);
876 /* get calibration parameters to compensate for speed and offset differences
877 * when we are slaved */
878 gst_clock_get_calibration (sink->provided_clock, &cinternal, &cexternal,
879 &crate_num, &crate_denom);
881 GST_DEBUG_OBJECT (sink, "internal %" GST_TIME_FORMAT " external %"
882 GST_TIME_FORMAT " %" G_GUINT64_FORMAT "/%" G_GUINT64_FORMAT " = %f",
883 GST_TIME_ARGS (cinternal), GST_TIME_ARGS (cexternal), crate_num,
884 crate_denom, gst_guint64_to_gdouble (crate_num) /
885 gst_guint64_to_gdouble (crate_denom));
888 crate_denom = crate_num = 1;
890 /* bring external time to internal time */
891 render_start = clock_convert_external (render_start, cinternal, cexternal,
892 crate_num, crate_denom);
893 render_stop = clock_convert_external (render_stop, cinternal, cexternal,
894 crate_num, crate_denom);
896 GST_DEBUG_OBJECT (sink,
897 "after slaving: start %" GST_TIME_FORMAT " - stop %" GST_TIME_FORMAT,
898 GST_TIME_ARGS (render_start), GST_TIME_ARGS (render_stop));
900 *srender_start = render_start;
901 *srender_stop = render_stop;
904 /* algorithm to calculate sample positions that will result in changing the
905 * playout pointer to match the clock rate of the master */
907 gst_base_audio_sink_skew_slaving (GstBaseAudioSink * sink,
908 GstClockTime render_start, GstClockTime render_stop,
909 GstClockTime * srender_start, GstClockTime * srender_stop)
911 GstClockTime cinternal, cexternal, crate_num, crate_denom;
912 GstClockTime etime, itime;
913 GstClockTimeDiff skew, segtime, segtime2;
917 /* get calibration parameters to compensate for offsets */
918 gst_clock_get_calibration (sink->provided_clock, &cinternal, &cexternal,
919 &crate_num, &crate_denom);
921 /* sample clocks and figure out clock skew */
922 etime = gst_clock_get_time (GST_ELEMENT_CLOCK (sink));
923 itime = gst_clock_get_internal_time (sink->provided_clock);
925 GST_DEBUG_OBJECT (sink,
926 "internal %" GST_TIME_FORMAT " external %" GST_TIME_FORMAT
927 " cinternal %" GST_TIME_FORMAT " cexternal %" GST_TIME_FORMAT,
928 GST_TIME_ARGS (itime), GST_TIME_ARGS (etime),
929 GST_TIME_ARGS (cinternal), GST_TIME_ARGS (cexternal));
931 /* make sure we never go below 0 */
932 etime = etime > cexternal ? etime - cexternal : 0;
933 itime = itime > cinternal ? itime - cinternal : 0;
936 * positive value means external clock goes slower
937 * negative value means external clock goes faster */
938 skew = GST_CLOCK_DIFF (etime, itime);
939 if (sink->priv->avg_skew == -1) {
940 /* first observation */
941 sink->priv->avg_skew = skew;
943 /* next observations use a moving average */
944 sink->priv->avg_skew = (31 * sink->priv->avg_skew + skew) / 32;
947 GST_DEBUG_OBJECT (sink, "internal %" GST_TIME_FORMAT " external %"
948 GST_TIME_FORMAT " skew %" G_GINT64_FORMAT " avg %" G_GINT64_FORMAT,
949 GST_TIME_ARGS (itime), GST_TIME_ARGS (etime), skew, sink->priv->avg_skew);
951 /* the max drift we allow is the length of a segment */
952 segtime = sink->ringbuffer->spec.latency_time * 1000;
953 segtime2 = segtime / 2;
955 /* adjust playout pointer based on skew */
956 if (sink->priv->avg_skew > segtime2) {
957 /* master is running slower, move internal time forward */
958 GST_WARNING_OBJECT (sink,
959 "correct clock skew %" G_GINT64_FORMAT " > %" G_GINT64_FORMAT,
960 sink->priv->avg_skew, segtime2);
961 cexternal = cexternal > segtime ? cexternal - segtime : 0;
962 sink->priv->avg_skew -= segtime;
965 sink->ringbuffer->spec.segsize /
966 sink->ringbuffer->spec.bytes_per_sample;
967 last_align = sink->priv->last_align;
969 /* if we were aligning in the wrong direction or we aligned more than what we
970 * will correct, resync */
971 if (last_align < 0 || last_align > segsamples)
972 sink->next_sample = -1;
974 GST_DEBUG_OBJECT (sink,
975 "last_align %" G_GINT64_FORMAT " segsamples %u, next %"
976 G_GUINT64_FORMAT, last_align, segsamples, sink->next_sample);
978 gst_clock_set_calibration (sink->provided_clock, cinternal, cexternal,
979 crate_num, crate_denom);
980 } else if (sink->priv->avg_skew < -segtime2) {
981 /* master is running faster, move external time forwards */
982 GST_WARNING_OBJECT (sink,
983 "correct clock skew %" G_GINT64_FORMAT " < %" G_GINT64_FORMAT,
984 sink->priv->avg_skew, -segtime2);
985 cexternal += segtime;
986 sink->priv->avg_skew += segtime;
989 sink->ringbuffer->spec.segsize /
990 sink->ringbuffer->spec.bytes_per_sample;
991 last_align = sink->priv->last_align;
993 /* if we were aligning in the wrong direction or we aligned more than what we
994 * will correct, resync */
995 if (last_align > 0 || -last_align > segsamples)
996 sink->next_sample = -1;
998 GST_DEBUG_OBJECT (sink,
999 "last_align %" G_GINT64_FORMAT " segsamples %u, next %"
1000 G_GUINT64_FORMAT, last_align, segsamples, sink->next_sample);
1002 gst_clock_set_calibration (sink->provided_clock, cinternal, cexternal,
1003 crate_num, crate_denom);
1006 /* convert, ignoring speed */
1007 render_start = clock_convert_external (render_start, cinternal, cexternal,
1008 crate_num, crate_denom);
1009 render_stop = clock_convert_external (render_stop, cinternal, cexternal,
1010 crate_num, crate_denom);
1012 *srender_start = render_start;
1013 *srender_stop = render_stop;
1016 /* apply the clock offset but do no slaving otherwise */
1018 gst_base_audio_sink_none_slaving (GstBaseAudioSink * sink,
1019 GstClockTime render_start, GstClockTime render_stop,
1020 GstClockTime * srender_start, GstClockTime * srender_stop)
1022 GstClockTime cinternal, cexternal, crate_num, crate_denom;
1024 /* get calibration parameters to compensate for offsets */
1025 gst_clock_get_calibration (sink->provided_clock, &cinternal, &cexternal,
1026 &crate_num, &crate_denom);
1028 /* convert, ignoring speed */
1029 render_start = clock_convert_external (render_start, cinternal, cexternal,
1030 crate_num, crate_denom);
1031 render_stop = clock_convert_external (render_stop, cinternal, cexternal,
1032 crate_num, crate_denom);
1034 *srender_start = render_start;
1035 *srender_stop = render_stop;
1038 /* converts render_start and render_stop to their slaved values */
1040 gst_base_audio_sink_handle_slaving (GstBaseAudioSink * sink,
1041 GstClockTime render_start, GstClockTime render_stop,
1042 GstClockTime * srender_start, GstClockTime * srender_stop)
1044 switch (sink->priv->slave_method) {
1045 case GST_BASE_AUDIO_SINK_SLAVE_RESAMPLE:
1046 gst_base_audio_sink_resample_slaving (sink, render_start, render_stop,
1047 srender_start, srender_stop);
1049 case GST_BASE_AUDIO_SINK_SLAVE_SKEW:
1050 gst_base_audio_sink_skew_slaving (sink, render_start, render_stop,
1051 srender_start, srender_stop);
1053 case GST_BASE_AUDIO_SINK_SLAVE_NONE:
1054 gst_base_audio_sink_none_slaving (sink, render_start, render_stop,
1055 srender_start, srender_stop);
1058 g_warning ("unknown slaving method %d", sink->priv->slave_method);
1063 /* must be called with LOCK */
1064 static GstFlowReturn
1065 gst_base_audio_sink_sync_latency (GstBaseSink * bsink, GstMiniObject * obj)
1068 GstClockReturn status;
1071 GstBaseAudioSink *sink;
1072 GstClockTime itime, etime;
1073 GstClockTime rate_num, rate_denom;
1074 GstClockTimeDiff jitter;
1076 sink = GST_BASE_AUDIO_SINK (bsink);
1078 clock = GST_ELEMENT_CLOCK (sink);
1079 if (G_UNLIKELY (clock == NULL))
1082 /* we provided the global clock, don't need to do anything special */
1083 if (clock == sink->provided_clock)
1086 GST_OBJECT_UNLOCK (sink);
1089 GST_DEBUG_OBJECT (sink, "checking preroll");
1091 ret = gst_base_sink_do_preroll (bsink, obj);
1092 if (ret != GST_FLOW_OK)
1095 GST_OBJECT_LOCK (sink);
1096 time = sink->priv->us_latency;
1097 GST_OBJECT_UNLOCK (sink);
1099 /* preroll done, we can sync since we are in PLAYING now. */
1100 GST_DEBUG_OBJECT (sink, "possibly waiting for clock to reach %"
1101 GST_TIME_FORMAT, GST_TIME_ARGS (time));
1103 /* wait for the clock, this can be interrupted because we got shut down or
1105 status = gst_base_sink_wait_clock (bsink, time, &jitter);
1107 GST_DEBUG_OBJECT (sink, "clock returned %d %" GST_TIME_FORMAT, status,
1108 GST_TIME_ARGS (jitter));
1110 /* invalid time, no clock or sync disabled, just continue then */
1111 if (status == GST_CLOCK_BADTIME)
1114 /* waiting could have been interrupted and we can be flushing now */
1115 if (G_UNLIKELY (bsink->flushing))
1118 /* retry if we got unscheduled, which means we did not reach the timeout
1119 * yet. if some other error occures, we continue. */
1120 } while (status == GST_CLOCK_UNSCHEDULED);
1122 GST_OBJECT_LOCK (sink);
1123 GST_DEBUG_OBJECT (sink, "latency synced");
1125 /* when we prerolled in time, we can accurately set the calibration,
1126 * our internal clock should exactly have been the latency (== the running
1127 * time of the external clock) */
1128 etime = GST_ELEMENT_CAST (sink)->base_time + time;
1129 itime = gst_base_audio_sink_get_time (sink->provided_clock, sink);
1131 if (status == GST_CLOCK_EARLY) {
1132 /* when we prerolled late, we have to take into account the lateness */
1133 GST_DEBUG_OBJECT (sink, "late preroll, adding jitter");
1137 /* start ringbuffer so we can start slaving right away when we need to */
1138 gst_ring_buffer_start (sink->ringbuffer);
1140 GST_DEBUG_OBJECT (sink,
1141 "internal time: %" GST_TIME_FORMAT " external time: %" GST_TIME_FORMAT,
1142 GST_TIME_ARGS (itime), GST_TIME_ARGS (etime));
1144 /* copy the original calibrated rate but update the internal and external
1146 gst_clock_get_calibration (sink->provided_clock, NULL, NULL, &rate_num,
1148 gst_clock_set_calibration (sink->provided_clock, itime, etime,
1149 rate_num, rate_denom);
1151 switch (sink->priv->slave_method) {
1152 case GST_BASE_AUDIO_SINK_SLAVE_RESAMPLE:
1153 /* only set as master when we are resampling */
1154 GST_DEBUG_OBJECT (sink, "Setting clock as master");
1155 gst_clock_set_master (sink->provided_clock, clock);
1157 case GST_BASE_AUDIO_SINK_SLAVE_SKEW:
1158 case GST_BASE_AUDIO_SINK_SLAVE_NONE:
1163 sink->priv->avg_skew = -1;
1164 sink->next_sample = -1;
1165 sink->priv->eos_time = -1;
1172 GST_DEBUG_OBJECT (sink, "we have no clock");
1177 GST_DEBUG_OBJECT (sink, "we are not slaved");
1182 GST_DEBUG_OBJECT (sink, "we are flushing");
1183 GST_OBJECT_LOCK (sink);
1184 return GST_FLOW_WRONG_STATE;
1188 static GstFlowReturn
1189 gst_base_audio_sink_render (GstBaseSink * bsink, GstBuffer * buf)
1192 GstClockTime time, stop, render_start, render_stop, sample_offset;
1193 GstClockTimeDiff sync_offset, ts_offset;
1194 GstBaseAudioSink *sink;
1195 GstRingBuffer *ringbuf;
1196 gint64 diff, align, ctime, cstop;
1199 guint samples, written;
1203 GstClockTime base_time, render_delay, latency;
1205 gboolean sync, slaved, align_next;
1207 GstSegment clip_seg;
1209 sink = GST_BASE_AUDIO_SINK (bsink);
1211 ringbuf = sink->ringbuffer;
1213 /* can't do anything when we don't have the device */
1214 if (G_UNLIKELY (!gst_ring_buffer_is_acquired (ringbuf)))
1217 /* Wait for upstream latency before starting the ringbuffer, we do this so
1218 * that we can align the first sample of the ringbuffer to the base_time +
1220 GST_OBJECT_LOCK (sink);
1221 base_time = GST_ELEMENT_CAST (sink)->base_time;
1222 if (G_UNLIKELY (sink->priv->sync_latency)) {
1223 ret = gst_base_audio_sink_sync_latency (bsink, GST_MINI_OBJECT_CAST (buf));
1224 GST_OBJECT_UNLOCK (sink);
1225 if (G_UNLIKELY (ret != GST_FLOW_OK))
1226 goto sync_latency_failed;
1227 /* only do this once until we are set back to PLAYING */
1228 sink->priv->sync_latency = FALSE;
1230 GST_OBJECT_UNLOCK (sink);
1233 bps = ringbuf->spec.bytes_per_sample;
1235 size = GST_BUFFER_SIZE (buf);
1236 if (G_UNLIKELY (size % bps) != 0)
1239 samples = size / bps;
1240 out_samples = samples;
1242 in_offset = GST_BUFFER_OFFSET (buf);
1243 time = GST_BUFFER_TIMESTAMP (buf);
1245 GST_DEBUG_OBJECT (sink,
1246 "time %" GST_TIME_FORMAT ", offset %llu, start %" GST_TIME_FORMAT
1247 ", samples %u", GST_TIME_ARGS (time), in_offset,
1248 GST_TIME_ARGS (bsink->segment.start), samples);
1250 data = GST_BUFFER_DATA (buf);
1252 /* if not valid timestamp or we can't clip or sync, try to play
1254 if (!GST_CLOCK_TIME_IS_VALID (time)) {
1255 render_start = gst_base_audio_sink_get_offset (sink);
1256 render_stop = render_start + samples;
1257 GST_DEBUG_OBJECT (sink,
1258 "Buffer of size %u has no time. Using render_start=%" G_GUINT64_FORMAT,
1259 GST_BUFFER_SIZE (buf), render_start);
1260 /* we don't have a start so we don't know stop either */
1265 /* let's calc stop based on the number of samples in the buffer instead
1266 * of trusting the DURATION */
1267 stop = time + gst_util_uint64_scale_int (samples, GST_SECOND,
1268 ringbuf->spec.rate);
1270 /* prepare the clipping segment. Since we will be subtracting ts-offset and
1271 * device-delay later we scale the start and stop with those values so that we
1272 * can correctly clip them */
1273 clip_seg.format = GST_FORMAT_TIME;
1274 clip_seg.start = bsink->segment.start;
1275 clip_seg.stop = bsink->segment.stop;
1276 clip_seg.duration = -1;
1278 /* the sync offset is the combination of ts-offset and device-delay */
1279 latency = gst_base_sink_get_latency (bsink);
1280 ts_offset = gst_base_sink_get_ts_offset (bsink);
1281 render_delay = gst_base_sink_get_render_delay (bsink);
1282 sync_offset = ts_offset - render_delay + latency;
1284 GST_DEBUG_OBJECT (sink,
1285 "sync-offset %" G_GINT64_FORMAT ", render-delay %" GST_TIME_FORMAT
1286 ", ts-offset %" G_GINT64_FORMAT, sync_offset,
1287 GST_TIME_ARGS (render_delay), ts_offset);
1289 /* compensate for ts-offset and device-delay when negative we need to
1291 if (sync_offset < 0) {
1292 clip_seg.start += -sync_offset;
1293 if (clip_seg.stop != -1)
1294 clip_seg.stop += -sync_offset;
1297 /* samples should be rendered based on their timestamp. All samples
1298 * arriving before the segment.start or after segment.stop are to be
1299 * thrown away. All samples should also be clipped to the segment
1301 if (!gst_segment_clip (&clip_seg, GST_FORMAT_TIME, time, stop, &ctime,
1303 goto out_of_segment;
1305 /* see if some clipping happened */
1306 diff = ctime - time;
1308 /* bring clipped time to samples */
1309 diff = gst_util_uint64_scale_int (diff, ringbuf->spec.rate, GST_SECOND);
1310 GST_DEBUG_OBJECT (sink, "clipping start to %" GST_TIME_FORMAT " %"
1311 G_GUINT64_FORMAT " samples", GST_TIME_ARGS (ctime), diff);
1316 diff = stop - cstop;
1318 /* bring clipped time to samples */
1319 diff = gst_util_uint64_scale_int (diff, ringbuf->spec.rate, GST_SECOND);
1320 GST_DEBUG_OBJECT (sink, "clipping stop to %" GST_TIME_FORMAT " %"
1321 G_GUINT64_FORMAT " samples", GST_TIME_ARGS (cstop), diff);
1326 /* figure out how to sync */
1327 if ((clock = GST_ELEMENT_CLOCK (bsink)))
1333 /* no sync needed, play sample ASAP */
1334 render_start = gst_base_audio_sink_get_offset (sink);
1335 render_stop = render_start + samples;
1336 GST_DEBUG_OBJECT (sink,
1337 "no sync needed. Using render_start=%" G_GUINT64_FORMAT, render_start);
1341 /* bring buffer start and stop times to running time */
1343 gst_segment_to_running_time (&bsink->segment, GST_FORMAT_TIME, time);
1345 gst_segment_to_running_time (&bsink->segment, GST_FORMAT_TIME, stop);
1347 GST_DEBUG_OBJECT (sink,
1348 "running: start %" GST_TIME_FORMAT " - stop %" GST_TIME_FORMAT,
1349 GST_TIME_ARGS (render_start), GST_TIME_ARGS (render_stop));
1351 /* store the time of the last sample, we'll use this to perform sync on the
1352 * last sample when draining the buffer */
1353 if (bsink->segment.rate >= 0.0) {
1354 sink->priv->eos_time = render_stop;
1356 sink->priv->eos_time = render_start;
1359 /* compensate for ts-offset and delay we know this will not underflow because we
1361 GST_DEBUG_OBJECT (sink,
1362 "compensating for sync-offset %" GST_TIME_FORMAT,
1363 GST_TIME_ARGS (sync_offset));
1364 render_start += sync_offset;
1365 render_stop += sync_offset;
1367 GST_DEBUG_OBJECT (sink, "adding base_time %" GST_TIME_FORMAT,
1368 GST_TIME_ARGS (base_time));
1370 /* add base time to sync against the clock */
1371 render_start += base_time;
1372 render_stop += base_time;
1374 GST_DEBUG_OBJECT (sink,
1375 "after compensation: start %" GST_TIME_FORMAT " - stop %" GST_TIME_FORMAT,
1376 GST_TIME_ARGS (render_start), GST_TIME_ARGS (render_stop));
1378 if ((slaved = clock != sink->provided_clock)) {
1379 /* handle clock slaving */
1380 gst_base_audio_sink_handle_slaving (sink, render_start, render_stop,
1381 &render_start, &render_stop);
1383 /* no slaving needed but we need to adapt to the clock calibration
1385 gst_base_audio_sink_none_slaving (sink, render_start, render_stop,
1386 &render_start, &render_stop);
1389 /* and bring the time to the rate corrected offset in the buffer */
1390 render_start = gst_util_uint64_scale_int (render_start,
1391 ringbuf->spec.rate, GST_SECOND);
1392 render_stop = gst_util_uint64_scale_int (render_stop,
1393 ringbuf->spec.rate, GST_SECOND);
1395 /* positive playback rate, first sample is render_start, negative rate, first
1396 * sample is render_stop. When no rate conversion is active, render exactly
1397 * the amount of input samples to avoid aligning to rounding errors. */
1398 if (bsink->segment.rate >= 0.0) {
1399 sample_offset = render_start;
1400 if (bsink->segment.rate == 1.0)
1401 render_stop = sample_offset + samples;
1403 sample_offset = render_stop;
1404 if (bsink->segment.rate == -1.0)
1405 render_start = sample_offset + samples;
1408 /* always resync after a discont */
1409 if (G_UNLIKELY (GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_DISCONT))) {
1410 GST_DEBUG_OBJECT (sink, "resync after discont");
1414 /* resync when we don't know what to align the sample with */
1415 if (G_UNLIKELY (sink->next_sample == -1)) {
1416 GST_DEBUG_OBJECT (sink,
1417 "no align possible: no previous sample position known");
1421 /* now try to align the sample to the previous one, first see how big the
1423 if (sample_offset >= sink->next_sample)
1424 diff = sample_offset - sink->next_sample;
1426 diff = sink->next_sample - sample_offset;
1428 /* we tollerate half a second diff before we start resyncing. This
1429 * should be enough to compensate for various rounding errors in the timestamp
1430 * and sample offset position. We always resync if we got a discont anyway and
1431 * non-discont should be aligned by definition. */
1432 if (G_LIKELY (diff < ringbuf->spec.rate / DIFF_TOLERANCE)) {
1433 /* calc align with previous sample */
1434 align = sink->next_sample - sample_offset;
1435 GST_DEBUG_OBJECT (sink,
1436 "align with prev sample, ABS (%" G_GINT64_FORMAT ") < %d", align,
1437 ringbuf->spec.rate / DIFF_TOLERANCE);
1439 /* bring sample diff to seconds for error message */
1440 diff = gst_util_uint64_scale_int (diff, GST_SECOND, ringbuf->spec.rate);
1441 /* timestamps drifted apart from previous samples too much, we need to
1442 * resync. We log this as an element warning. */
1443 GST_ELEMENT_WARNING (sink, CORE, CLOCK,
1444 ("Compensating for audio synchronisation problems"),
1445 ("Unexpected discontinuity in audio timestamps of more "
1446 "than half a second (%" GST_TIME_FORMAT "), resyncing",
1447 GST_TIME_ARGS (diff)));
1450 sink->priv->last_align = align;
1452 /* apply alignment */
1453 render_start += align;
1455 /* only align stop if we are not slaved to resample */
1456 if (slaved && sink->priv->slave_method == GST_BASE_AUDIO_SINK_SLAVE_RESAMPLE) {
1457 GST_DEBUG_OBJECT (sink, "no stop time align needed: we are slaved");
1460 render_stop += align;
1463 /* number of target samples is difference between start and stop */
1464 out_samples = render_stop - render_start;
1467 /* we render the first or last sample first, depending on the rate */
1468 if (bsink->segment.rate >= 0.0)
1469 sample_offset = render_start;
1471 sample_offset = render_stop;
1473 GST_DEBUG_OBJECT (sink, "rendering at %" G_GUINT64_FORMAT " %d/%d",
1474 sample_offset, samples, out_samples);
1476 /* we need to accumulate over different runs for when we get interrupted */
1481 gst_ring_buffer_commit_full (ringbuf, &sample_offset, data, samples,
1482 out_samples, &accum);
1484 GST_DEBUG_OBJECT (sink, "wrote %u of %u", written, samples);
1485 /* if we wrote all, we're done */
1486 if (written == samples)
1489 /* else something interrupted us and we wait for preroll. */
1490 if (gst_base_sink_wait_preroll (bsink) != GST_FLOW_OK)
1493 /* if we got interrupted, we cannot assume that the next sample should
1494 * be aligned to this one */
1498 data += written * bps;
1502 sink->next_sample = sample_offset;
1504 sink->next_sample = -1;
1506 GST_DEBUG_OBJECT (sink, "next sample expected at %" G_GUINT64_FORMAT,
1509 if (GST_CLOCK_TIME_IS_VALID (stop) && stop >= bsink->segment.stop) {
1510 GST_DEBUG_OBJECT (sink,
1511 "start playback because we are at the end of segment");
1512 gst_ring_buffer_start (ringbuf);
1520 GST_DEBUG_OBJECT (sink,
1521 "dropping sample out of segment time %" GST_TIME_FORMAT ", start %"
1522 GST_TIME_FORMAT, GST_TIME_ARGS (time),
1523 GST_TIME_ARGS (bsink->segment.start));
1529 GST_DEBUG_OBJECT (sink, "ringbuffer not negotiated");
1530 GST_ELEMENT_ERROR (sink, STREAM, FORMAT, (NULL), ("sink not negotiated."));
1531 return GST_FLOW_NOT_NEGOTIATED;
1535 GST_DEBUG_OBJECT (sink, "wrong size");
1536 GST_ELEMENT_ERROR (sink, STREAM, WRONG_TYPE,
1537 (NULL), ("sink received buffer of wrong size."));
1538 return GST_FLOW_ERROR;
1542 GST_DEBUG_OBJECT (sink, "ringbuffer is stopping");
1543 return GST_FLOW_WRONG_STATE;
1545 sync_latency_failed:
1547 GST_DEBUG_OBJECT (sink, "failed waiting for latency");
1553 * gst_base_audio_sink_create_ringbuffer:
1554 * @sink: a #GstBaseAudioSink.
1556 * Create and return the #GstRingBuffer for @sink. This function will call the
1557 * ::create_ringbuffer vmethod and will set @sink as the parent of the returned
1558 * buffer (see gst_object_set_parent()).
1560 * Returns: The new ringbuffer of @sink.
1563 gst_base_audio_sink_create_ringbuffer (GstBaseAudioSink * sink)
1565 GstBaseAudioSinkClass *bclass;
1566 GstRingBuffer *buffer = NULL;
1568 bclass = GST_BASE_AUDIO_SINK_GET_CLASS (sink);
1569 if (bclass->create_ringbuffer)
1570 buffer = bclass->create_ringbuffer (sink);
1573 gst_object_set_parent (GST_OBJECT (buffer), GST_OBJECT (sink));
1579 gst_base_audio_sink_callback (GstRingBuffer * rbuf, guint8 * data, guint len,
1582 GstBaseSink *basesink;
1583 GstBaseAudioSink *sink;
1587 basesink = GST_BASE_SINK (user_data);
1588 sink = GST_BASE_AUDIO_SINK (user_data);
1590 GST_PAD_STREAM_LOCK (basesink->sinkpad);
1592 /* would be nice to arrange for pad_alloc_buffer to return data -- as it is we
1593 will copy twice, once into data, once into DMA */
1594 GST_LOG_OBJECT (basesink, "pulling %d bytes offset %" G_GUINT64_FORMAT
1595 " to fill audio buffer", len, basesink->offset);
1597 gst_pad_pull_range (basesink->sinkpad, basesink->segment.last_stop, len,
1600 if (ret != GST_FLOW_OK) {
1601 if (ret == GST_FLOW_UNEXPECTED)
1607 GST_PAD_PREROLL_LOCK (basesink->sinkpad);
1608 if (basesink->flushing)
1611 /* complete preroll and wait for PLAYING */
1612 ret = gst_base_sink_do_preroll (basesink, GST_MINI_OBJECT_CAST (buf));
1613 if (ret != GST_FLOW_OK)
1616 if (len != GST_BUFFER_SIZE (buf)) {
1617 GST_INFO_OBJECT (basesink,
1618 "got different size than requested from sink pad: %u != %u", len,
1619 GST_BUFFER_SIZE (buf));
1620 len = MIN (GST_BUFFER_SIZE (buf), len);
1623 basesink->segment.last_stop += len;
1625 memcpy (data, GST_BUFFER_DATA (buf), len);
1626 GST_PAD_PREROLL_UNLOCK (basesink->sinkpad);
1628 GST_PAD_STREAM_UNLOCK (basesink->sinkpad);
1634 GST_WARNING_OBJECT (basesink, "Got flow '%s' but can't return it: %d",
1635 gst_flow_get_name (ret), ret);
1636 gst_ring_buffer_pause (rbuf);
1637 GST_PAD_STREAM_UNLOCK (basesink->sinkpad);
1642 /* FIXME: this is not quite correct; we'll be called endlessly until
1643 * the sink gets shut down; maybe we should set a flag somewhere, or
1644 * set segment.stop and segment.duration to the last sample or so */
1645 GST_DEBUG_OBJECT (sink, "EOS");
1646 gst_base_audio_sink_drain (sink);
1647 gst_ring_buffer_pause (rbuf);
1648 gst_element_post_message (GST_ELEMENT_CAST (sink),
1649 gst_message_new_eos (GST_OBJECT_CAST (sink)));
1650 GST_PAD_STREAM_UNLOCK (basesink->sinkpad);
1654 GST_DEBUG_OBJECT (sink, "we are flushing");
1655 gst_ring_buffer_pause (rbuf);
1656 GST_PAD_PREROLL_UNLOCK (basesink->sinkpad);
1657 GST_PAD_STREAM_UNLOCK (basesink->sinkpad);
1662 GST_DEBUG_OBJECT (sink, "error %s", gst_flow_get_name (ret));
1663 gst_ring_buffer_pause (rbuf);
1664 GST_PAD_PREROLL_UNLOCK (basesink->sinkpad);
1665 GST_PAD_STREAM_UNLOCK (basesink->sinkpad);
1671 gst_base_audio_sink_activate_pull (GstBaseSink * basesink, gboolean active)
1674 GstBaseAudioSink *sink = GST_BASE_AUDIO_SINK (basesink);
1677 GST_DEBUG_OBJECT (basesink, "activating pull");
1679 gst_ring_buffer_set_callback (sink->ringbuffer,
1680 gst_base_audio_sink_callback, sink);
1682 ret = gst_ring_buffer_activate (sink->ringbuffer, TRUE);
1684 GST_DEBUG_OBJECT (basesink, "deactivating pull");
1685 gst_ring_buffer_set_callback (sink->ringbuffer, NULL, NULL);
1686 ret = gst_ring_buffer_activate (sink->ringbuffer, FALSE);
1692 /* should be called with the LOCK */
1693 static GstStateChangeReturn
1694 gst_base_audio_sink_async_play (GstBaseSink * basesink)
1696 GstBaseAudioSink *sink;
1698 sink = GST_BASE_AUDIO_SINK (basesink);
1700 GST_DEBUG_OBJECT (sink, "ringbuffer may start now");
1701 sink->priv->sync_latency = TRUE;
1702 gst_ring_buffer_may_start (sink->ringbuffer, TRUE);
1703 if (basesink->pad_mode == GST_ACTIVATE_PULL) {
1704 /* we always start the ringbuffer in pull mode immediatly */
1705 gst_ring_buffer_start (sink->ringbuffer);
1708 return GST_STATE_CHANGE_SUCCESS;
1711 static GstStateChangeReturn
1712 gst_base_audio_sink_do_play (GstBaseAudioSink * sink)
1714 GstStateChangeReturn ret;
1716 GST_OBJECT_LOCK (sink);
1717 ret = gst_base_audio_sink_async_play (GST_BASE_SINK_CAST (sink));
1718 GST_OBJECT_UNLOCK (sink);
1723 static GstStateChangeReturn
1724 gst_base_audio_sink_change_state (GstElement * element,
1725 GstStateChange transition)
1727 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
1728 GstBaseAudioSink *sink = GST_BASE_AUDIO_SINK (element);
1730 switch (transition) {
1731 case GST_STATE_CHANGE_NULL_TO_READY:
1732 if (sink->ringbuffer == NULL) {
1733 gst_audio_clock_reset (GST_AUDIO_CLOCK (sink->provided_clock), 0);
1734 sink->ringbuffer = gst_base_audio_sink_create_ringbuffer (sink);
1736 if (!gst_ring_buffer_open_device (sink->ringbuffer))
1739 case GST_STATE_CHANGE_READY_TO_PAUSED:
1740 sink->next_sample = -1;
1741 sink->priv->last_align = -1;
1742 sink->priv->eos_time = -1;
1743 gst_ring_buffer_set_flushing (sink->ringbuffer, FALSE);
1744 gst_ring_buffer_may_start (sink->ringbuffer, FALSE);
1746 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1747 gst_base_audio_sink_do_play (sink);
1749 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1750 /* need to take the lock so we don't interfere with an
1752 GST_OBJECT_LOCK (sink);
1753 /* ringbuffer cannot start anymore */
1754 gst_ring_buffer_may_start (sink->ringbuffer, FALSE);
1755 gst_ring_buffer_pause (sink->ringbuffer);
1756 sink->priv->sync_latency = FALSE;
1757 GST_OBJECT_UNLOCK (sink);
1759 case GST_STATE_CHANGE_PAUSED_TO_READY:
1760 /* make sure we unblock before calling the parent state change
1761 * so it can grab the STREAM_LOCK */
1762 gst_ring_buffer_set_flushing (sink->ringbuffer, TRUE);
1768 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1770 switch (transition) {
1771 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1772 /* stop slaving ourselves to the master, if any */
1773 gst_clock_set_master (sink->provided_clock, NULL);
1775 case GST_STATE_CHANGE_PAUSED_TO_READY:
1776 gst_ring_buffer_activate (sink->ringbuffer, FALSE);
1777 gst_ring_buffer_release (sink->ringbuffer);
1779 case GST_STATE_CHANGE_READY_TO_NULL:
1780 /* we release again here because the aqcuire happens when setting the
1781 * caps, which happens before we commit the state to PAUSED and thus the
1782 * PAUSED->READY state change (see above, where we release the ringbuffer)
1783 * might not be called when we get here. */
1784 gst_ring_buffer_activate (sink->ringbuffer, FALSE);
1785 gst_ring_buffer_release (sink->ringbuffer);
1786 gst_ring_buffer_close_device (sink->ringbuffer);
1797 /* subclass must post a meaningfull error message */
1798 GST_DEBUG_OBJECT (sink, "open failed");
1799 return GST_STATE_CHANGE_FAILURE;