7544f458d565380ad851c0280c46b6d3a727e5a5
[platform/upstream/gstreamer.git] / gst-libs / gst / audio / gstbaseaudiosink.c
1 /* GStreamer
2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3  *                    2005 Wim Taymans <wim@fluendo.com>
4  *
5  * gstbaseaudiosink.c: 
6  *
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.
11  *
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.
16  *
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.
21  */
22
23 /**
24  * SECTION:gstbaseaudiosink
25  * @short_description: Base class for audio sinks
26  * @see_also: #GstAudioSink, #GstRingBuffer.
27  *
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.
31  *
32  * Last reviewed on 2006-09-27 (0.10.12)
33  */
34
35 #include <string.h>
36
37 #include "gstbaseaudiosink.h"
38
39 GST_DEBUG_CATEGORY_STATIC (gst_base_audio_sink_debug);
40 #define GST_CAT_DEFAULT gst_base_audio_sink_debug
41
42 #define GST_BASE_AUDIO_SINK_GET_PRIVATE(obj)  \
43    (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_BASE_AUDIO_SINK, GstBaseAudioSinkPrivate))
44
45 struct _GstBaseAudioSinkPrivate
46 {
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 */
54   gint64 last_align;
55
56   gboolean sync_latency;
57
58   GstClockTime eos_time;
59 };
60
61 /* BaseAudioSink signals and args */
62 enum
63 {
64   /* FILL ME */
65   LAST_SIGNAL
66 };
67
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.
74  */
75 #define DIFF_TOLERANCE  2
76
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
82
83 enum
84 {
85   PROP_0,
86   PROP_BUFFER_TIME,
87   PROP_LATENCY_TIME,
88   PROP_PROVIDE_CLOCK,
89   PROP_SLAVE_METHOD
90 };
91
92 GType
93 gst_base_audio_sink_slave_method_get_type (void)
94 {
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"},
100     {0, NULL, NULL},
101   };
102
103   if (!slave_method_type) {
104     slave_method_type =
105         g_enum_register_static ("GstBaseAudioSinkSlaveMethod", slave_method);
106   }
107   return slave_method_type;
108 }
109
110
111 #define _do_init(bla) \
112     GST_DEBUG_CATEGORY_INIT (gst_base_audio_sink_debug, "baseaudiosink", 0, "baseaudiosink element");
113
114 GST_BOILERPLATE_FULL (GstBaseAudioSink, gst_base_audio_sink, GstBaseSink,
115     GST_TYPE_BASE_SINK, _do_init);
116
117 static void gst_base_audio_sink_dispose (GObject * object);
118
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);
123
124 static GstStateChangeReturn gst_base_audio_sink_async_play (GstBaseSink *
125     basesink);
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,
129     gboolean active);
130 static gboolean gst_base_audio_sink_query (GstElement * element, GstQuery *
131     query);
132
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);
138
139 static GstFlowReturn gst_base_audio_sink_preroll (GstBaseSink * bsink,
140     GstBuffer * buffer);
141 static GstFlowReturn gst_base_audio_sink_render (GstBaseSink * bsink,
142     GstBuffer * buffer);
143 static gboolean gst_base_audio_sink_event (GstBaseSink * bsink,
144     GstEvent * event);
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,
148     GstCaps * caps);
149 static void gst_base_audio_sink_fixate (GstBaseSink * bsink, GstCaps * caps);
150
151 static gboolean gst_base_audio_sink_query_pad (GstPad * pad, GstQuery * query);
152
153
154 /* static guint gst_base_audio_sink_signals[LAST_SIGNAL] = { 0 }; */
155
156 static void
157 gst_base_audio_sink_base_init (gpointer g_class)
158 {
159 }
160
161 static void
162 gst_base_audio_sink_class_init (GstBaseAudioSinkClass * klass)
163 {
164   GObjectClass *gobject_class;
165   GstElementClass *gstelement_class;
166   GstBaseSinkClass *gstbasesink_class;
167
168   gobject_class = (GObjectClass *) klass;
169   gstelement_class = (GstElementClass *) klass;
170   gstbasesink_class = (GstBaseSinkClass *) klass;
171
172   g_type_class_add_private (klass, sizeof (GstBaseAudioSinkPrivate));
173
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);
179
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));
185
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));
191
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));
196
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));
202
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);
208
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);
220
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);
225 }
226
227 static void
228 gst_base_audio_sink_init (GstBaseAudioSink * baseaudiosink,
229     GstBaseAudioSinkClass * g_class)
230 {
231   baseaudiosink->priv = GST_BASE_AUDIO_SINK_GET_PRIVATE (baseaudiosink);
232
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;
237
238   baseaudiosink->provided_clock = gst_audio_clock_new ("GstAudioSinkClock",
239       (GstAudioClockGetTimeFunc) gst_base_audio_sink_get_time, baseaudiosink);
240
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;
245
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));
249 }
250
251 static void
252 gst_base_audio_sink_dispose (GObject * object)
253 {
254   GstBaseAudioSink *sink;
255
256   sink = GST_BASE_AUDIO_SINK (object);
257
258   if (sink->provided_clock)
259     gst_object_unref (sink->provided_clock);
260   sink->provided_clock = NULL;
261
262   if (sink->ringbuffer) {
263     gst_object_unparent (GST_OBJECT_CAST (sink->ringbuffer));
264     sink->ringbuffer = NULL;
265   }
266
267   G_OBJECT_CLASS (parent_class)->dispose (object);
268 }
269
270
271 static GstClock *
272 gst_base_audio_sink_provide_clock (GstElement * elem)
273 {
274   GstBaseAudioSink *sink;
275   GstClock *clock;
276
277   sink = GST_BASE_AUDIO_SINK (elem);
278
279   /* we have no ringbuffer (must be NULL state) */
280   if (sink->ringbuffer == NULL)
281     goto wrong_state;
282
283   if (!gst_ring_buffer_is_acquired (sink->ringbuffer))
284     goto wrong_state;
285
286   GST_OBJECT_LOCK (sink);
287   if (!sink->provide_clock)
288     goto clock_disabled;
289
290   clock = GST_CLOCK_CAST (gst_object_ref (sink->provided_clock));
291   GST_OBJECT_UNLOCK (sink);
292
293   return clock;
294
295   /* ERRORS */
296 wrong_state:
297   {
298     GST_DEBUG_OBJECT (sink, "ringbuffer not acquired");
299     return NULL;
300   }
301 clock_disabled:
302   {
303     GST_DEBUG_OBJECT (sink, "clock provide disabled");
304     GST_OBJECT_UNLOCK (sink);
305     return NULL;
306   }
307 }
308
309 static gboolean
310 gst_base_audio_sink_query_pad (GstPad * pad, GstQuery * query)
311 {
312   gboolean res = FALSE;
313   GstBaseAudioSink *basesink;
314
315   basesink = GST_BASE_AUDIO_SINK (gst_pad_get_parent (pad));
316
317   switch (GST_QUERY_TYPE (query)) {
318     case GST_QUERY_CONVERT:
319     {
320       GstFormat src_fmt, dest_fmt;
321       gint64 src_val, dest_val;
322
323       GST_LOG_OBJECT (pad, "query convert");
324
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);
329         if (res) {
330           gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
331         }
332       }
333       break;
334     }
335     default:
336       break;
337   }
338
339   gst_object_unref (basesink);
340
341   return res;
342 }
343
344 static gboolean
345 gst_base_audio_sink_query (GstElement * element, GstQuery * query)
346 {
347   gboolean res = FALSE;
348   GstBaseAudioSink *basesink;
349
350   basesink = GST_BASE_AUDIO_SINK (element);
351
352   switch (GST_QUERY_TYPE (query)) {
353     case GST_QUERY_LATENCY:
354     {
355       gboolean live, us_live;
356       GstClockTime min_l, max_l;
357
358       GST_DEBUG_OBJECT (basesink, "latency query");
359
360       if (!basesink->ringbuffer || !basesink->ringbuffer->spec.rate) {
361         GST_DEBUG_OBJECT (basesink,
362             "we are not yet negotiated, can't report latency yet");
363         res = FALSE;
364         goto done;
365       }
366
367       /* ask parent first, it will do an upstream query for us. */
368       if ((res =
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;
372
373         /* we and upstream are both live, adjust the min_latency */
374         if (live && us_live) {
375           GstRingBufferSpec *spec;
376
377           spec = &basesink->ringbuffer->spec;
378
379           basesink->priv->us_latency = min_l;
380
381           min_latency =
382               gst_util_uint64_scale_int (spec->seglatency * spec->segsize,
383               GST_SECOND, spec->rate * spec->bytes_per_sample);
384
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
388            * amount of time. */
389           max_latency = min_latency + (max_l == -1 ? 0 : max_l);
390
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));
395         } else {
396           GST_DEBUG_OBJECT (basesink,
397               "peer or we are not live, don't care about latency");
398           min_latency = min_l;
399           max_latency = max_l;
400         }
401         gst_query_set_latency (query, live, min_latency, max_latency);
402       }
403       break;
404     }
405     case GST_QUERY_CONVERT:
406     {
407       GstFormat src_fmt, dest_fmt;
408       gint64 src_val, dest_val;
409
410       GST_LOG_OBJECT (basesink, "query convert");
411
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);
416         if (res) {
417           gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
418         }
419       }
420       break;
421     }
422     default:
423       res = GST_ELEMENT_CLASS (parent_class)->query (element, query);
424       break;
425   }
426
427 done:
428   return res;
429 }
430
431
432 static GstClockTime
433 gst_base_audio_sink_get_time (GstClock * clock, GstBaseAudioSink * sink)
434 {
435   guint64 raw, samples;
436   guint delay;
437   GstClockTime result;
438
439   if (sink->ringbuffer == NULL || sink->ringbuffer->spec.rate == 0)
440     return GST_CLOCK_TIME_NONE;
441
442   /* our processed samples are always increasing */
443   raw = samples = gst_ring_buffer_samples_done (sink->ringbuffer);
444
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);
448
449   if (G_LIKELY (samples >= delay))
450     samples -= delay;
451   else
452     samples = 0;
453
454   result = gst_util_uint64_scale_int (samples, GST_SECOND,
455       sink->ringbuffer->spec.rate);
456
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));
460
461   return result;
462 }
463
464 /**
465  * gst_base_audio_sink_set_provide_clock:
466  * @sink: a #GstBaseAudioSink
467  * @provide: new state
468  *
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.
472  *
473  * Since: 0.10.16
474  */
475 void
476 gst_base_audio_sink_set_provide_clock (GstBaseAudioSink * sink,
477     gboolean provide)
478 {
479   g_return_if_fail (GST_IS_BASE_AUDIO_SINK (sink));
480
481   GST_OBJECT_LOCK (sink);
482   sink->provide_clock = provide;
483   GST_OBJECT_UNLOCK (sink);
484 }
485
486 /**
487  * gst_base_audio_sink_get_provide_clock:
488  * @sink: a #GstBaseAudioSink
489  *
490  * Queries whether @sink will provide a clock or not. See also
491  * gst_base_audio_sink_set_provide_clock.
492  *
493  * Returns: %TRUE if @sink will provide a clock.
494  *
495  * Since: 0.10.16
496  */
497 gboolean
498 gst_base_audio_sink_get_provide_clock (GstBaseAudioSink * sink)
499 {
500   gboolean result;
501
502   g_return_val_if_fail (GST_IS_BASE_AUDIO_SINK (sink), FALSE);
503
504   GST_OBJECT_LOCK (sink);
505   result = sink->provide_clock;
506   GST_OBJECT_UNLOCK (sink);
507
508   return result;
509 }
510
511 /**
512  * gst_base_audio_sink_set_slave_method:
513  * @sink: a #GstBaseAudioSink
514  * @method: the new slave method
515  *
516  * Controls how clock slaving will be performed in @sink. 
517  *
518  * Since: 0.10.16
519  */
520 void
521 gst_base_audio_sink_set_slave_method (GstBaseAudioSink * sink,
522     GstBaseAudioSinkSlaveMethod method)
523 {
524   g_return_if_fail (GST_IS_BASE_AUDIO_SINK (sink));
525
526   GST_OBJECT_LOCK (sink);
527   sink->priv->slave_method = method;
528   GST_OBJECT_UNLOCK (sink);
529 }
530
531 /**
532  * gst_base_audio_sink_get_slave_method:
533  * @sink: a #GstBaseAudioSink
534  *
535  * Get the current slave method used by @sink.
536  *
537  * Returns: The current slave method used by @sink.
538  *
539  * Since: 0.10.16
540  */
541 GstBaseAudioSinkSlaveMethod
542 gst_base_audio_sink_get_slave_method (GstBaseAudioSink * sink)
543 {
544   GstBaseAudioSinkSlaveMethod result;
545
546   g_return_val_if_fail (GST_IS_BASE_AUDIO_SINK (sink), -1);
547
548   GST_OBJECT_LOCK (sink);
549   result = sink->priv->slave_method;
550   GST_OBJECT_UNLOCK (sink);
551
552   return result;
553 }
554
555 static void
556 gst_base_audio_sink_set_property (GObject * object, guint prop_id,
557     const GValue * value, GParamSpec * pspec)
558 {
559   GstBaseAudioSink *sink;
560
561   sink = GST_BASE_AUDIO_SINK (object);
562
563   switch (prop_id) {
564     case PROP_BUFFER_TIME:
565       sink->buffer_time = g_value_get_int64 (value);
566       break;
567     case PROP_LATENCY_TIME:
568       sink->latency_time = g_value_get_int64 (value);
569       break;
570     case PROP_PROVIDE_CLOCK:
571       gst_base_audio_sink_set_provide_clock (sink, g_value_get_boolean (value));
572       break;
573     case PROP_SLAVE_METHOD:
574       gst_base_audio_sink_set_slave_method (sink, g_value_get_enum (value));
575       break;
576     default:
577       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
578       break;
579   }
580 }
581
582 static void
583 gst_base_audio_sink_get_property (GObject * object, guint prop_id,
584     GValue * value, GParamSpec * pspec)
585 {
586   GstBaseAudioSink *sink;
587
588   sink = GST_BASE_AUDIO_SINK (object);
589
590   switch (prop_id) {
591     case PROP_BUFFER_TIME:
592       g_value_set_int64 (value, sink->buffer_time);
593       break;
594     case PROP_LATENCY_TIME:
595       g_value_set_int64 (value, sink->latency_time);
596       break;
597     case PROP_PROVIDE_CLOCK:
598       g_value_set_boolean (value, gst_base_audio_sink_get_provide_clock (sink));
599       break;
600     case PROP_SLAVE_METHOD:
601       g_value_set_enum (value, gst_base_audio_sink_get_slave_method (sink));
602       break;
603     default:
604       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
605       break;
606   }
607 }
608
609 static gboolean
610 gst_base_audio_sink_setcaps (GstBaseSink * bsink, GstCaps * caps)
611 {
612   GstBaseAudioSink *sink = GST_BASE_AUDIO_SINK (bsink);
613   GstRingBufferSpec *spec;
614
615   if (!sink->ringbuffer)
616     return FALSE;
617
618   spec = &sink->ringbuffer->spec;
619
620   GST_DEBUG_OBJECT (sink, "release old ringbuffer");
621
622   /* release old ringbuffer */
623   gst_ring_buffer_activate (sink->ringbuffer, FALSE);
624   gst_ring_buffer_release (sink->ringbuffer);
625
626   GST_DEBUG_OBJECT (sink, "parse caps");
627
628   spec->buffer_time = sink->buffer_time;
629   spec->latency_time = sink->latency_time;
630
631   /* parse new caps */
632   if (!gst_ring_buffer_parse_caps (spec, caps))
633     goto parse_error;
634
635   gst_ring_buffer_debug_spec_buff (spec);
636
637   GST_DEBUG_OBJECT (sink, "acquire ringbuffer");
638   if (!gst_ring_buffer_acquire (sink->ringbuffer, spec))
639     goto acquire_error;
640
641   if (bsink->pad_mode == GST_ACTIVATE_PUSH) {
642     GST_DEBUG_OBJECT (sink, "activate ringbuffer");
643     gst_ring_buffer_activate (sink->ringbuffer, TRUE);
644   }
645
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);
650
651   spec->buffer_time = spec->segtotal * spec->latency_time;
652
653   gst_ring_buffer_debug_spec_buff (spec);
654
655   return TRUE;
656
657   /* ERRORS */
658 parse_error:
659   {
660     GST_DEBUG_OBJECT (sink, "could not parse caps");
661     GST_ELEMENT_ERROR (sink, STREAM, FORMAT,
662         (NULL), ("cannot parse audio format."));
663     return FALSE;
664   }
665 acquire_error:
666   {
667     GST_DEBUG_OBJECT (sink, "could not acquire ringbuffer");
668     return FALSE;
669   }
670 }
671
672 static void
673 gst_base_audio_sink_fixate (GstBaseSink * bsink, GstCaps * caps)
674 {
675   GstStructure *s;
676   gint width, depth;
677
678   s = gst_caps_get_structure (caps, 0);
679
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);
684
685   /* fields for int */
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);
691   }
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);
696 }
697
698 static void
699 gst_base_audio_sink_get_times (GstBaseSink * bsink, GstBuffer * buffer,
700     GstClockTime * start, GstClockTime * end)
701 {
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;
706 }
707
708 /* This waits for the drain to happen and can be canceled */
709 static gboolean
710 gst_base_audio_sink_drain (GstBaseAudioSink * sink)
711 {
712   if (!sink->ringbuffer)
713     return TRUE;
714   if (!sink->ringbuffer->spec.rate)
715     return TRUE;
716
717   /* need to start playback before we can drain, but only when
718    * we have successfully negotiated a format and thus acquired the
719    * ringbuffer. */
720   if (gst_ring_buffer_is_acquired (sink->ringbuffer))
721     gst_ring_buffer_start (sink->ringbuffer);
722
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));
727
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);
731
732     GST_DEBUG_OBJECT (sink, "drained audio");
733   }
734   return TRUE;
735 }
736
737 static gboolean
738 gst_base_audio_sink_event (GstBaseSink * bsink, GstEvent * event)
739 {
740   GstBaseAudioSink *sink = GST_BASE_AUDIO_SINK (bsink);
741
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);
746       break;
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);
754       break;
755     case GST_EVENT_EOS:
756       /* now wait till we played everything */
757       gst_base_audio_sink_drain (sink);
758       break;
759     case GST_EVENT_NEWSEGMENT:
760     {
761       gdouble rate;
762
763       /* we only need the rate */
764       gst_event_parse_new_segment_full (event, NULL, &rate, NULL, NULL,
765           NULL, NULL, NULL);
766
767       GST_DEBUG_OBJECT (sink, "new segment rate of %f", rate);
768       break;
769     }
770     default:
771       break;
772   }
773   return TRUE;
774 }
775
776 static GstFlowReturn
777 gst_base_audio_sink_preroll (GstBaseSink * bsink, GstBuffer * buffer)
778 {
779   GstBaseAudioSink *sink = GST_BASE_AUDIO_SINK (bsink);
780
781   if (!gst_ring_buffer_is_acquired (sink->ringbuffer))
782     goto wrong_state;
783
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
786    * support. */
787   return GST_FLOW_OK;
788
789 wrong_state:
790   {
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;
794   }
795 }
796
797 static guint64
798 gst_base_audio_sink_get_offset (GstBaseAudioSink * sink)
799 {
800   guint64 sample;
801   gint writeseg, segdone, sps;
802   gint diff;
803
804   /* assume we can append to the previous sample */
805   sample = sink->next_sample;
806   /* no previous sample, try to insert at position 0 */
807   if (sample == -1)
808     sample = 0;
809
810   sps = sink->ringbuffer->samples_per_seg;
811
812   /* figure out the segment and the offset inside the segment where
813    * the sample should be written. */
814   writeseg = sample / sps;
815
816   /* get the currently processed segment */
817   segdone = g_atomic_int_get (&sink->ringbuffer->segdone)
818       - sink->ringbuffer->segbase;
819
820   /* see how far away it is from the write segment */
821   diff = writeseg - segdone;
822   if (diff < 0) {
823     /* sample would be dropped, position to next playable position */
824     sample = (segdone + 1) * sps;
825   }
826
827   return sample;
828 }
829
830 static GstClockTime
831 clock_convert_external (GstClockTime external, GstClockTime cinternal,
832     GstClockTime cexternal, GstClockTime crate_num, GstClockTime crate_denom)
833 {
834   /* adjust for rate and speed */
835   if (external >= cexternal) {
836     external =
837         gst_util_uint64_scale (external - cexternal, crate_denom, crate_num);
838     external += cinternal;
839   } else {
840     external =
841         gst_util_uint64_scale (cexternal - external, crate_denom, crate_num);
842     if (cinternal > external)
843       external = cinternal - external;
844     else
845       external = 0;
846   }
847   return external;
848 }
849
850 /* algorithm to calculate sample positions that will result in resampling to
851  * match the clock rate of the master */
852 static void
853 gst_base_audio_sink_resample_slaving (GstBaseAudioSink * sink,
854     GstClockTime render_start, GstClockTime render_stop,
855     GstClockTime * srender_start, GstClockTime * srender_stop)
856 {
857   GstClockTime cinternal, cexternal;
858   GstClockTime crate_num, crate_denom;
859
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. */
864 #if 0
865   GstClockTime etime, itime;
866   gdouble r_squared;
867
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);
871
872   /* add new observation */
873   gst_clock_add_observation (sink->provided_clock, itime, etime, &r_squared);
874 #endif
875
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);
880
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));
886
887   if (crate_num == 0)
888     crate_denom = crate_num = 1;
889
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);
895
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));
899
900   *srender_start = render_start;
901   *srender_stop = render_stop;
902 }
903
904 /* algorithm to calculate sample positions that will result in changing the
905  * playout pointer to match the clock rate of the master */
906 static void
907 gst_base_audio_sink_skew_slaving (GstBaseAudioSink * sink,
908     GstClockTime render_start, GstClockTime render_stop,
909     GstClockTime * srender_start, GstClockTime * srender_stop)
910 {
911   GstClockTime cinternal, cexternal, crate_num, crate_denom;
912   GstClockTime etime, itime;
913   GstClockTimeDiff skew, segtime, segtime2;
914   gint segsamples;
915   gint64 last_align;
916
917   /* get calibration parameters to compensate for offsets */
918   gst_clock_get_calibration (sink->provided_clock, &cinternal, &cexternal,
919       &crate_num, &crate_denom);
920
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);
924
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));
930
931   /* make sure we never go below 0 */
932   etime = etime > cexternal ? etime - cexternal : 0;
933   itime = itime > cinternal ? itime - cinternal : 0;
934
935   /* do itime - etime.
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;
942   } else {
943     /* next observations use a moving average */
944     sink->priv->avg_skew = (31 * sink->priv->avg_skew + skew) / 32;
945   }
946
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);
950
951   /* the max drift we allow is the length of a segment */
952   segtime = sink->ringbuffer->spec.latency_time * 1000;
953   segtime2 = segtime / 2;
954
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;
963
964     segsamples =
965         sink->ringbuffer->spec.segsize /
966         sink->ringbuffer->spec.bytes_per_sample;
967     last_align = sink->priv->last_align;
968
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;
973
974     GST_DEBUG_OBJECT (sink,
975         "last_align %" G_GINT64_FORMAT " segsamples %u, next %"
976         G_GUINT64_FORMAT, last_align, segsamples, sink->next_sample);
977
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;
987
988     segsamples =
989         sink->ringbuffer->spec.segsize /
990         sink->ringbuffer->spec.bytes_per_sample;
991     last_align = sink->priv->last_align;
992
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;
997
998     GST_DEBUG_OBJECT (sink,
999         "last_align %" G_GINT64_FORMAT " segsamples %u, next %"
1000         G_GUINT64_FORMAT, last_align, segsamples, sink->next_sample);
1001
1002     gst_clock_set_calibration (sink->provided_clock, cinternal, cexternal,
1003         crate_num, crate_denom);
1004   }
1005
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);
1011
1012   *srender_start = render_start;
1013   *srender_stop = render_stop;
1014 }
1015
1016 /* apply the clock offset but do no slaving otherwise */
1017 static void
1018 gst_base_audio_sink_none_slaving (GstBaseAudioSink * sink,
1019     GstClockTime render_start, GstClockTime render_stop,
1020     GstClockTime * srender_start, GstClockTime * srender_stop)
1021 {
1022   GstClockTime cinternal, cexternal, crate_num, crate_denom;
1023
1024   /* get calibration parameters to compensate for offsets */
1025   gst_clock_get_calibration (sink->provided_clock, &cinternal, &cexternal,
1026       &crate_num, &crate_denom);
1027
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);
1033
1034   *srender_start = render_start;
1035   *srender_stop = render_stop;
1036 }
1037
1038 /* converts render_start and render_stop to their slaved values */
1039 static void
1040 gst_base_audio_sink_handle_slaving (GstBaseAudioSink * sink,
1041     GstClockTime render_start, GstClockTime render_stop,
1042     GstClockTime * srender_start, GstClockTime * srender_stop)
1043 {
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);
1048       break;
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);
1052       break;
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);
1056       break;
1057     default:
1058       g_warning ("unknown slaving method %d", sink->priv->slave_method);
1059       break;
1060   }
1061 }
1062
1063 /* must be called with LOCK */
1064 static GstFlowReturn
1065 gst_base_audio_sink_sync_latency (GstBaseSink * bsink, GstMiniObject * obj)
1066 {
1067   GstClock *clock;
1068   GstClockReturn status;
1069   GstClockTime time;
1070   GstFlowReturn ret;
1071   GstBaseAudioSink *sink;
1072   GstClockTime itime, etime;
1073   GstClockTime rate_num, rate_denom;
1074   GstClockTimeDiff jitter;
1075
1076   sink = GST_BASE_AUDIO_SINK (bsink);
1077
1078   clock = GST_ELEMENT_CLOCK (sink);
1079   if (G_UNLIKELY (clock == NULL))
1080     goto no_clock;
1081
1082   /* we provided the global clock, don't need to do anything special */
1083   if (clock == sink->provided_clock)
1084     goto no_slaving;
1085
1086   GST_OBJECT_UNLOCK (sink);
1087
1088   do {
1089     GST_DEBUG_OBJECT (sink, "checking preroll");
1090
1091     ret = gst_base_sink_do_preroll (bsink, obj);
1092     if (ret != GST_FLOW_OK)
1093       goto flushing;
1094
1095     GST_OBJECT_LOCK (sink);
1096     time = sink->priv->us_latency;
1097     GST_OBJECT_UNLOCK (sink);
1098
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));
1102
1103     /* wait for the clock, this can be interrupted because we got shut down or 
1104      * we PAUSED. */
1105     status = gst_base_sink_wait_clock (bsink, time, &jitter);
1106
1107     GST_DEBUG_OBJECT (sink, "clock returned %d %" GST_TIME_FORMAT, status,
1108         GST_TIME_ARGS (jitter));
1109
1110     /* invalid time, no clock or sync disabled, just continue then */
1111     if (status == GST_CLOCK_BADTIME)
1112       break;
1113
1114     /* waiting could have been interrupted and we can be flushing now */
1115     if (G_UNLIKELY (bsink->flushing))
1116       goto flushing;
1117
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);
1121
1122   GST_OBJECT_LOCK (sink);
1123   GST_DEBUG_OBJECT (sink, "latency synced");
1124
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);
1130
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");
1134     etime += jitter;
1135   }
1136
1137   /* start ringbuffer so we can start slaving right away when we need to */
1138   gst_ring_buffer_start (sink->ringbuffer);
1139
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));
1143
1144   /* copy the original calibrated rate but update the internal and external
1145    * times. */
1146   gst_clock_get_calibration (sink->provided_clock, NULL, NULL, &rate_num,
1147       &rate_denom);
1148   gst_clock_set_calibration (sink->provided_clock, itime, etime,
1149       rate_num, rate_denom);
1150
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);
1156       break;
1157     case GST_BASE_AUDIO_SINK_SLAVE_SKEW:
1158     case GST_BASE_AUDIO_SINK_SLAVE_NONE:
1159     default:
1160       break;
1161   }
1162
1163   sink->priv->avg_skew = -1;
1164   sink->next_sample = -1;
1165   sink->priv->eos_time = -1;
1166
1167   return GST_FLOW_OK;
1168
1169   /* ERRORS */
1170 no_clock:
1171   {
1172     GST_DEBUG_OBJECT (sink, "we have no clock");
1173     return GST_FLOW_OK;
1174   }
1175 no_slaving:
1176   {
1177     GST_DEBUG_OBJECT (sink, "we are not slaved");
1178     return GST_FLOW_OK;
1179   }
1180 flushing:
1181   {
1182     GST_DEBUG_OBJECT (sink, "we are flushing");
1183     GST_OBJECT_LOCK (sink);
1184     return GST_FLOW_WRONG_STATE;
1185   }
1186 }
1187
1188 static GstFlowReturn
1189 gst_base_audio_sink_render (GstBaseSink * bsink, GstBuffer * buf)
1190 {
1191   guint64 in_offset;
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;
1197   guint8 *data;
1198   guint size;
1199   guint samples, written;
1200   gint bps;
1201   gint accum;
1202   gint out_samples;
1203   GstClockTime base_time, render_delay, latency;
1204   GstClock *clock;
1205   gboolean sync, slaved, align_next;
1206   GstFlowReturn ret;
1207   GstSegment clip_seg;
1208
1209   sink = GST_BASE_AUDIO_SINK (bsink);
1210
1211   ringbuf = sink->ringbuffer;
1212
1213   /* can't do anything when we don't have the device */
1214   if (G_UNLIKELY (!gst_ring_buffer_is_acquired (ringbuf)))
1215     goto wrong_state;
1216
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 +
1219    * latency. */
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;
1229   } else {
1230     GST_OBJECT_UNLOCK (sink);
1231   }
1232
1233   bps = ringbuf->spec.bytes_per_sample;
1234
1235   size = GST_BUFFER_SIZE (buf);
1236   if (G_UNLIKELY (size % bps) != 0)
1237     goto wrong_size;
1238
1239   samples = size / bps;
1240   out_samples = samples;
1241
1242   in_offset = GST_BUFFER_OFFSET (buf);
1243   time = GST_BUFFER_TIMESTAMP (buf);
1244
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);
1249
1250   data = GST_BUFFER_DATA (buf);
1251
1252   /* if not valid timestamp or we can't clip or sync, try to play
1253    * sample ASAP */
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 */
1261     stop = -1;
1262     goto no_sync;
1263   }
1264
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);
1269
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;
1277
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;
1283
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);
1288
1289   /* compensate for ts-offset and device-delay when negative we need to
1290    * clip. */
1291   if (sync_offset < 0) {
1292     clip_seg.start += -sync_offset;
1293     if (clip_seg.stop != -1)
1294       clip_seg.stop += -sync_offset;
1295   }
1296
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 
1300    * boundaries */
1301   if (!gst_segment_clip (&clip_seg, GST_FORMAT_TIME, time, stop, &ctime,
1302           &cstop))
1303     goto out_of_segment;
1304
1305   /* see if some clipping happened */
1306   diff = ctime - time;
1307   if (diff > 0) {
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);
1312     samples -= diff;
1313     data += diff * bps;
1314     time = ctime;
1315   }
1316   diff = stop - cstop;
1317   if (diff > 0) {
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);
1322     samples -= diff;
1323     stop = cstop;
1324   }
1325
1326   /* figure out how to sync */
1327   if ((clock = GST_ELEMENT_CLOCK (bsink)))
1328     sync = bsink->sync;
1329   else
1330     sync = FALSE;
1331
1332   if (!sync) {
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);
1338     goto no_sync;
1339   }
1340
1341   /* bring buffer start and stop times to running time */
1342   render_start =
1343       gst_segment_to_running_time (&bsink->segment, GST_FORMAT_TIME, time);
1344   render_stop =
1345       gst_segment_to_running_time (&bsink->segment, GST_FORMAT_TIME, stop);
1346
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));
1350
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;
1355   } else {
1356     sink->priv->eos_time = render_start;
1357   }
1358
1359   /* compensate for ts-offset and delay we know this will not underflow because we
1360    * clipped above. */
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;
1366
1367   GST_DEBUG_OBJECT (sink, "adding base_time %" GST_TIME_FORMAT,
1368       GST_TIME_ARGS (base_time));
1369
1370   /* add base time to sync against the clock */
1371   render_start += base_time;
1372   render_stop += base_time;
1373
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));
1377
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);
1382   } else {
1383     /* no slaving needed but we need to adapt to the clock calibration
1384      * parameters */
1385     gst_base_audio_sink_none_slaving (sink, render_start, render_stop,
1386         &render_start, &render_stop);
1387   }
1388
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);
1394
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;
1402   } else {
1403     sample_offset = render_stop;
1404     if (bsink->segment.rate == -1.0)
1405       render_start = sample_offset + samples;
1406   }
1407
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");
1411     goto no_align;
1412   }
1413
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");
1418     goto no_align;
1419   }
1420
1421   /* now try to align the sample to the previous one, first see how big the
1422    * difference is. */
1423   if (sample_offset >= sink->next_sample)
1424     diff = sample_offset - sink->next_sample;
1425   else
1426     diff = sink->next_sample - sample_offset;
1427
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);
1438   } else {
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)));
1448     align = 0;
1449   }
1450   sink->priv->last_align = align;
1451
1452   /* apply alignment */
1453   render_start += align;
1454
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");
1458     goto no_align;
1459   }
1460   render_stop += align;
1461
1462 no_align:
1463   /* number of target samples is difference between start and stop */
1464   out_samples = render_stop - render_start;
1465
1466 no_sync:
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;
1470   else
1471     sample_offset = render_stop;
1472
1473   GST_DEBUG_OBJECT (sink, "rendering at %" G_GUINT64_FORMAT " %d/%d",
1474       sample_offset, samples, out_samples);
1475
1476   /* we need to accumulate over different runs for when we get interrupted */
1477   accum = 0;
1478   align_next = TRUE;
1479   do {
1480     written =
1481         gst_ring_buffer_commit_full (ringbuf, &sample_offset, data, samples,
1482         out_samples, &accum);
1483
1484     GST_DEBUG_OBJECT (sink, "wrote %u of %u", written, samples);
1485     /* if we wrote all, we're done */
1486     if (written == samples)
1487       break;
1488
1489     /* else something interrupted us and we wait for preroll. */
1490     if (gst_base_sink_wait_preroll (bsink) != GST_FLOW_OK)
1491       goto stopping;
1492
1493     /* if we got interrupted, we cannot assume that the next sample should
1494      * be aligned to this one */
1495     align_next = FALSE;
1496
1497     samples -= written;
1498     data += written * bps;
1499   } while (TRUE);
1500
1501   if (align_next)
1502     sink->next_sample = sample_offset;
1503   else
1504     sink->next_sample = -1;
1505
1506   GST_DEBUG_OBJECT (sink, "next sample expected at %" G_GUINT64_FORMAT,
1507       sink->next_sample);
1508
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);
1513   }
1514
1515   return GST_FLOW_OK;
1516
1517   /* SPECIAL cases */
1518 out_of_segment:
1519   {
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));
1524     return GST_FLOW_OK;
1525   }
1526   /* ERRORS */
1527 wrong_state:
1528   {
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;
1532   }
1533 wrong_size:
1534   {
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;
1539   }
1540 stopping:
1541   {
1542     GST_DEBUG_OBJECT (sink, "ringbuffer is stopping");
1543     return GST_FLOW_WRONG_STATE;
1544   }
1545 sync_latency_failed:
1546   {
1547     GST_DEBUG_OBJECT (sink, "failed waiting for latency");
1548     return ret;
1549   }
1550 }
1551
1552 /**
1553  * gst_base_audio_sink_create_ringbuffer:
1554  * @sink: a #GstBaseAudioSink.
1555  *
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()).
1559  *
1560  * Returns: The new ringbuffer of @sink.
1561  */
1562 GstRingBuffer *
1563 gst_base_audio_sink_create_ringbuffer (GstBaseAudioSink * sink)
1564 {
1565   GstBaseAudioSinkClass *bclass;
1566   GstRingBuffer *buffer = NULL;
1567
1568   bclass = GST_BASE_AUDIO_SINK_GET_CLASS (sink);
1569   if (bclass->create_ringbuffer)
1570     buffer = bclass->create_ringbuffer (sink);
1571
1572   if (buffer)
1573     gst_object_set_parent (GST_OBJECT (buffer), GST_OBJECT (sink));
1574
1575   return buffer;
1576 }
1577
1578 static void
1579 gst_base_audio_sink_callback (GstRingBuffer * rbuf, guint8 * data, guint len,
1580     gpointer user_data)
1581 {
1582   GstBaseSink *basesink;
1583   GstBaseAudioSink *sink;
1584   GstBuffer *buf;
1585   GstFlowReturn ret;
1586
1587   basesink = GST_BASE_SINK (user_data);
1588   sink = GST_BASE_AUDIO_SINK (user_data);
1589
1590   GST_PAD_STREAM_LOCK (basesink->sinkpad);
1591
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);
1596   ret =
1597       gst_pad_pull_range (basesink->sinkpad, basesink->segment.last_stop, len,
1598       &buf);
1599
1600   if (ret != GST_FLOW_OK) {
1601     if (ret == GST_FLOW_UNEXPECTED)
1602       goto eos;
1603     else
1604       goto error;
1605   }
1606
1607   GST_PAD_PREROLL_LOCK (basesink->sinkpad);
1608   if (basesink->flushing)
1609     goto flushing;
1610
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)
1614     goto preroll_error;
1615
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);
1621   }
1622
1623   basesink->segment.last_stop += len;
1624
1625   memcpy (data, GST_BUFFER_DATA (buf), len);
1626   GST_PAD_PREROLL_UNLOCK (basesink->sinkpad);
1627
1628   GST_PAD_STREAM_UNLOCK (basesink->sinkpad);
1629
1630   return;
1631
1632 error:
1633   {
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);
1638     return;
1639   }
1640 eos:
1641   {
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);
1651   }
1652 flushing:
1653   {
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);
1658     return;
1659   }
1660 preroll_error:
1661   {
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);
1666     return;
1667   }
1668 }
1669
1670 static gboolean
1671 gst_base_audio_sink_activate_pull (GstBaseSink * basesink, gboolean active)
1672 {
1673   gboolean ret;
1674   GstBaseAudioSink *sink = GST_BASE_AUDIO_SINK (basesink);
1675
1676   if (active) {
1677     GST_DEBUG_OBJECT (basesink, "activating pull");
1678
1679     gst_ring_buffer_set_callback (sink->ringbuffer,
1680         gst_base_audio_sink_callback, sink);
1681
1682     ret = gst_ring_buffer_activate (sink->ringbuffer, TRUE);
1683   } else {
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);
1687   }
1688
1689   return ret;
1690 }
1691
1692 /* should be called with the LOCK */
1693 static GstStateChangeReturn
1694 gst_base_audio_sink_async_play (GstBaseSink * basesink)
1695 {
1696   GstBaseAudioSink *sink;
1697
1698   sink = GST_BASE_AUDIO_SINK (basesink);
1699
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);
1706   }
1707
1708   return GST_STATE_CHANGE_SUCCESS;
1709 }
1710
1711 static GstStateChangeReturn
1712 gst_base_audio_sink_do_play (GstBaseAudioSink * sink)
1713 {
1714   GstStateChangeReturn ret;
1715
1716   GST_OBJECT_LOCK (sink);
1717   ret = gst_base_audio_sink_async_play (GST_BASE_SINK_CAST (sink));
1718   GST_OBJECT_UNLOCK (sink);
1719
1720   return ret;
1721 }
1722
1723 static GstStateChangeReturn
1724 gst_base_audio_sink_change_state (GstElement * element,
1725     GstStateChange transition)
1726 {
1727   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
1728   GstBaseAudioSink *sink = GST_BASE_AUDIO_SINK (element);
1729
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);
1735       }
1736       if (!gst_ring_buffer_open_device (sink->ringbuffer))
1737         goto open_failed;
1738       break;
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);
1745       break;
1746     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1747       gst_base_audio_sink_do_play (sink);
1748       break;
1749     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1750       /* need to take the lock so we don't interfere with an
1751        * async play */
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);
1758       break;
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);
1763       break;
1764     default:
1765       break;
1766   }
1767
1768   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1769
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);
1774       break;
1775     case GST_STATE_CHANGE_PAUSED_TO_READY:
1776       gst_ring_buffer_activate (sink->ringbuffer, FALSE);
1777       gst_ring_buffer_release (sink->ringbuffer);
1778       break;
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);
1787       break;
1788     default:
1789       break;
1790   }
1791
1792   return ret;
1793
1794   /* ERRORS */
1795 open_failed:
1796   {
1797     /* subclass must post a meaningfull error message */
1798     GST_DEBUG_OBJECT (sink, "open failed");
1799     return GST_STATE_CHANGE_FAILURE;
1800   }
1801 }