1 /* GStreamer
2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2005 Wim Taymans <wim@fluendo.com>
4 *
5 * gstaudiobasesink.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., 51 Franklin St, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
21 */
22
23 /**
24 * SECTION:gstaudiobasesink
25 * @title: GstAudioBaseSink
26 * @short_description: Base class for audio sinks
27 * @see_also: #GstAudioSink, #GstAudioRingBuffer.
28 *
29 * This is the base class for audio sinks. Subclasses need to implement the
30 * ::create_ringbuffer vmethod. This base class will then take care of
31 * writing samples to the ringbuffer, synchronisation, clipping and flushing.
32 */
33 #ifdef HAVE_CONFIG_H
34 #include "config.h"
35 #endif
36
37 #include <string.h>
38
39 #include <gst/audio/audio.h>
40 #include "gstaudiobasesink.h"
41
42 GST_DEBUG_CATEGORY_STATIC (gst_audio_base_sink_debug);
43 #define GST_CAT_DEFAULT gst_audio_base_sink_debug
44
45 struct _GstAudioBaseSinkPrivate
46 {
47 /* upstream latency */
48 GstClockTime us_latency;
49 /* the clock slaving algorithm in use */
50 GstAudioBaseSinkSlaveMethod 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 /* number of microseconds we allow clock slaving to drift
61 * before resyncing */
62 guint64 drift_tolerance;
63
64 /* number of nanoseconds we allow timestamps to drift
65 * before resyncing */
66 GstClockTime alignment_threshold;
67
68 /* time of the previous detected discont candidate */
69 GstClockTime discont_time;
70
71 /* number of nanoseconds to wait until creating a discontinuity */
72 GstClockTime discont_wait;
73
74 /* custom slaving algorithm callback */
75 GstAudioBaseSinkCustomSlavingCallback custom_slaving_callback;
76 gpointer custom_slaving_cb_data;
77 GDestroyNotify custom_slaving_cb_notify;
78 };
79
80 /* BaseAudioSink signals and args */
81 enum
82 {
83 /* FILL ME */
84 LAST_SIGNAL
85 };
86
87 /* FIXME: 2.0, store the buffer_time and latency_time in nanoseconds */
88 #define DEFAULT_BUFFER_TIME ((200 * GST_MSECOND) / GST_USECOND)
89 #define DEFAULT_LATENCY_TIME ((10 * GST_MSECOND) / GST_USECOND)
90 #define DEFAULT_PROVIDE_CLOCK TRUE
91 #define DEFAULT_SLAVE_METHOD GST_AUDIO_BASE_SINK_SLAVE_SKEW
92
93 /* FIXME, enable pull mode when clock slaving and trick modes are figured out */
94 #define DEFAULT_CAN_ACTIVATE_PULL FALSE
95
96 /* when timestamps drift for more than 40ms we resync. This should
97 * be enough to compensate for timestamp rounding errors. */
98 #define DEFAULT_ALIGNMENT_THRESHOLD (40 * GST_MSECOND)
99
100 /* when clock slaving drift for more than 40ms we resync. This is
101 * a reasonable default */
102 #define DEFAULT_DRIFT_TOLERANCE ((40 * GST_MSECOND) / GST_USECOND)
103
104 /* allow for one second before resyncing to see if the timestamps drift will
105 * fix itself, or is a permanent offset */
106 #define DEFAULT_DISCONT_WAIT (1 * GST_SECOND)
107
108 enum
109 {
110 PROP_0,
111
112 PROP_BUFFER_TIME,
113 PROP_LATENCY_TIME,
114 PROP_PROVIDE_CLOCK,
115 PROP_SLAVE_METHOD,
116 PROP_CAN_ACTIVATE_PULL,
117 PROP_ALIGNMENT_THRESHOLD,
118 PROP_DRIFT_TOLERANCE,
119 PROP_DISCONT_WAIT,
120
121 PROP_LAST
122 };
123
124 #define _do_init \
125 GST_DEBUG_CATEGORY_INIT (gst_audio_base_sink_debug, "audiobasesink", 0, "audiobasesink element");
126 #define gst_audio_base_sink_parent_class parent_class
127 G_DEFINE_TYPE_WITH_CODE (GstAudioBaseSink, gst_audio_base_sink,
128 GST_TYPE_BASE_SINK, G_ADD_PRIVATE (GstAudioBaseSink) _do_init);
129
130 static void gst_audio_base_sink_dispose (GObject * object);
131
132 static void gst_audio_base_sink_set_property (GObject * object, guint prop_id,
133 const GValue * value, GParamSpec * pspec);
134 static void gst_audio_base_sink_get_property (GObject * object, guint prop_id,
135 GValue * value, GParamSpec * pspec);
136
137 static GstStateChangeReturn gst_audio_base_sink_change_state (GstElement *
138 element, GstStateChange transition);
139 static gboolean gst_audio_base_sink_activate_pull (GstBaseSink * basesink,
140 gboolean active);
141 static gboolean gst_audio_base_sink_query (GstElement * element, GstQuery *
142 query);
143
144 static GstClock *gst_audio_base_sink_provide_clock (GstElement * elem);
145 static inline void gst_audio_base_sink_reset_sync (GstAudioBaseSink * sink);
146 static GstClockTime gst_audio_base_sink_get_time (GstClock * clock,
147 GstAudioBaseSink * sink);
148 static void gst_audio_base_sink_callback (GstAudioRingBuffer * rbuf,
149 guint8 * data, guint len, gpointer user_data);
150
151 static GstFlowReturn gst_audio_base_sink_preroll (GstBaseSink * bsink,
152 GstBuffer * buffer);
153 static GstFlowReturn gst_audio_base_sink_render (GstBaseSink * bsink,
154 GstBuffer * buffer);
155 static gboolean gst_audio_base_sink_event (GstBaseSink * bsink,
156 GstEvent * event);
157 static GstFlowReturn gst_audio_base_sink_wait_event (GstBaseSink * bsink,
158 GstEvent * event);
159 static void gst_audio_base_sink_get_times (GstBaseSink * bsink,
160 GstBuffer * buffer, GstClockTime * start, GstClockTime * end);
161 static gboolean gst_audio_base_sink_setcaps (GstBaseSink * bsink,
162 GstCaps * caps);
163 static GstCaps *gst_audio_base_sink_fixate (GstBaseSink * bsink,
164 GstCaps * caps);
165
166 static gboolean gst_audio_base_sink_query_pad (GstBaseSink * bsink,
167 GstQuery * query);
168
169
170 /* static guint gst_audio_base_sink_signals[LAST_SIGNAL] = { 0 }; */
171
172 static void
gst_audio_base_sink_class_init(GstAudioBaseSinkClass * klass)173 gst_audio_base_sink_class_init (GstAudioBaseSinkClass * klass)
174 {
175 GObjectClass *gobject_class;
176 GstElementClass *gstelement_class;
177 GstBaseSinkClass *gstbasesink_class;
178
179 gobject_class = (GObjectClass *) klass;
180 gstelement_class = (GstElementClass *) klass;
181 gstbasesink_class = (GstBaseSinkClass *) klass;
182
183 gobject_class->set_property = gst_audio_base_sink_set_property;
184 gobject_class->get_property = gst_audio_base_sink_get_property;
185 gobject_class->dispose = gst_audio_base_sink_dispose;
186
187 g_object_class_install_property (gobject_class, PROP_BUFFER_TIME,
188 g_param_spec_int64 ("buffer-time", "Buffer Time",
189 "Size of audio buffer in microseconds, this is the minimum "
190 "latency that the sink reports", 1, G_MAXINT64, DEFAULT_BUFFER_TIME,
191 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
192
193 g_object_class_install_property (gobject_class, PROP_LATENCY_TIME,
194 g_param_spec_int64 ("latency-time", "Latency Time",
195 "The minimum amount of data to write in each iteration "
196 "in microseconds", 1, G_MAXINT64, DEFAULT_LATENCY_TIME,
197 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
198
199 g_object_class_install_property (gobject_class, PROP_PROVIDE_CLOCK,
200 g_param_spec_boolean ("provide-clock", "Provide Clock",
201 "Provide a clock to be used as the global pipeline clock",
202 DEFAULT_PROVIDE_CLOCK, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
203
204 g_object_class_install_property (gobject_class, PROP_SLAVE_METHOD,
205 g_param_spec_enum ("slave-method", "Slave Method",
206 "Algorithm used to match the rate of the masterclock",
207 GST_TYPE_AUDIO_BASE_SINK_SLAVE_METHOD, DEFAULT_SLAVE_METHOD,
208 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
209
210 g_object_class_install_property (gobject_class, PROP_CAN_ACTIVATE_PULL,
211 g_param_spec_boolean ("can-activate-pull", "Allow Pull Scheduling",
212 "Allow pull-based scheduling", DEFAULT_CAN_ACTIVATE_PULL,
213 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
214 /**
215 * GstAudioBaseSink:drift-tolerance:
216 *
217 * Controls the amount of time in microseconds that clocks are allowed
218 * to drift before resynchronisation happens.
219 */
220 g_object_class_install_property (gobject_class, PROP_DRIFT_TOLERANCE,
221 g_param_spec_int64 ("drift-tolerance", "Drift Tolerance",
222 "Tolerance for clock drift in microseconds", 1,
223 G_MAXINT64, DEFAULT_DRIFT_TOLERANCE,
224 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
225 /**
226 * GstAudioBaseSink:alignment_threshold:
227 *
228 * Controls the amount of time in nanoseconds that timestamps are allowed
229 * to drift from their ideal time before choosing not to align them.
230 */
231 g_object_class_install_property (gobject_class, PROP_ALIGNMENT_THRESHOLD,
232 g_param_spec_uint64 ("alignment-threshold", "Alignment Threshold",
233 "Timestamp alignment threshold in nanoseconds", 1,
234 G_MAXUINT64 - 1, DEFAULT_ALIGNMENT_THRESHOLD,
235 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
236
237 /**
238 * GstAudioBaseSink:discont-wait:
239 *
240 * A window of time in nanoseconds to wait before creating a discontinuity as
241 * a result of breaching the drift-tolerance.
242 */
243 g_object_class_install_property (gobject_class, PROP_DISCONT_WAIT,
244 g_param_spec_uint64 ("discont-wait", "Discont Wait",
245 "Window of time in nanoseconds to wait before "
246 "creating a discontinuity", 0,
247 G_MAXUINT64 - 1, DEFAULT_DISCONT_WAIT,
248 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
249
250 gstelement_class->change_state =
251 GST_DEBUG_FUNCPTR (gst_audio_base_sink_change_state);
252 gstelement_class->provide_clock =
253 GST_DEBUG_FUNCPTR (gst_audio_base_sink_provide_clock);
254 gstelement_class->query = GST_DEBUG_FUNCPTR (gst_audio_base_sink_query);
255
256 gstbasesink_class->fixate = GST_DEBUG_FUNCPTR (gst_audio_base_sink_fixate);
257 gstbasesink_class->set_caps = GST_DEBUG_FUNCPTR (gst_audio_base_sink_setcaps);
258 gstbasesink_class->event = GST_DEBUG_FUNCPTR (gst_audio_base_sink_event);
259 gstbasesink_class->wait_event =
260 GST_DEBUG_FUNCPTR (gst_audio_base_sink_wait_event);
261 gstbasesink_class->get_times =
262 GST_DEBUG_FUNCPTR (gst_audio_base_sink_get_times);
263 gstbasesink_class->preroll = GST_DEBUG_FUNCPTR (gst_audio_base_sink_preroll);
264 gstbasesink_class->render = GST_DEBUG_FUNCPTR (gst_audio_base_sink_render);
265 gstbasesink_class->query = GST_DEBUG_FUNCPTR (gst_audio_base_sink_query_pad);
266 gstbasesink_class->activate_pull =
267 GST_DEBUG_FUNCPTR (gst_audio_base_sink_activate_pull);
268
269 /* ref class from a thread-safe context to work around missing bit of
270 * thread-safety in GObject */
271 g_type_class_ref (GST_TYPE_AUDIO_CLOCK);
272 g_type_class_ref (GST_TYPE_AUDIO_RING_BUFFER);
273
274 }
275
276 static void
gst_audio_base_sink_init(GstAudioBaseSink * audiobasesink)277 gst_audio_base_sink_init (GstAudioBaseSink * audiobasesink)
278 {
279 GstBaseSink *basesink = GST_BASE_SINK_CAST (audiobasesink);
280
281 audiobasesink->priv =
282 gst_audio_base_sink_get_instance_private (audiobasesink);
283
284 audiobasesink->buffer_time = DEFAULT_BUFFER_TIME;
285 audiobasesink->latency_time = DEFAULT_LATENCY_TIME;
286 audiobasesink->priv->slave_method = DEFAULT_SLAVE_METHOD;
287 audiobasesink->priv->drift_tolerance = DEFAULT_DRIFT_TOLERANCE;
288 audiobasesink->priv->alignment_threshold = DEFAULT_ALIGNMENT_THRESHOLD;
289 audiobasesink->priv->discont_wait = DEFAULT_DISCONT_WAIT;
290 audiobasesink->priv->custom_slaving_callback = NULL;
291 audiobasesink->priv->custom_slaving_cb_data = NULL;
292 audiobasesink->priv->custom_slaving_cb_notify = NULL;
293
294 audiobasesink->provided_clock = gst_audio_clock_new ("GstAudioSinkClock",
295 (GstAudioClockGetTimeFunc) gst_audio_base_sink_get_time, audiobasesink,
296 NULL);
297
298 basesink->can_activate_push = TRUE;
299 basesink->can_activate_pull = DEFAULT_CAN_ACTIVATE_PULL;
300
301 gst_base_sink_set_last_sample_enabled (basesink, FALSE);
302 if (DEFAULT_PROVIDE_CLOCK)
303 GST_OBJECT_FLAG_SET (basesink, GST_ELEMENT_FLAG_PROVIDE_CLOCK);
304 else
305 GST_OBJECT_FLAG_UNSET (basesink, GST_ELEMENT_FLAG_PROVIDE_CLOCK);
306 }
307
308 static void
gst_audio_base_sink_dispose(GObject * object)309 gst_audio_base_sink_dispose (GObject * object)
310 {
311 GstAudioBaseSink *sink;
312
313 sink = GST_AUDIO_BASE_SINK (object);
314
315 if (sink->priv->custom_slaving_cb_notify)
316 sink->priv->custom_slaving_cb_notify (sink->priv->custom_slaving_cb_data);
317
318 if (sink->provided_clock) {
319 gst_audio_clock_invalidate (GST_AUDIO_CLOCK (sink->provided_clock));
320 gst_object_unref (sink->provided_clock);
321 sink->provided_clock = NULL;
322 }
323
324 if (sink->ringbuffer) {
325 gst_object_unparent (GST_OBJECT_CAST (sink->ringbuffer));
326 sink->ringbuffer = NULL;
327 }
328
329 G_OBJECT_CLASS (parent_class)->dispose (object);
330 }
331
332
333 static GstClock *
gst_audio_base_sink_provide_clock(GstElement * elem)334 gst_audio_base_sink_provide_clock (GstElement * elem)
335 {
336 GstAudioBaseSink *sink;
337 GstClock *clock;
338
339 sink = GST_AUDIO_BASE_SINK (elem);
340
341 /* we have no ringbuffer (must be NULL state) */
342 if (sink->ringbuffer == NULL)
343 goto wrong_state;
344
345 if (!gst_audio_ring_buffer_is_acquired (sink->ringbuffer))
346 goto wrong_state;
347
348 GST_OBJECT_LOCK (sink);
349 if (!GST_OBJECT_FLAG_IS_SET (sink, GST_ELEMENT_FLAG_PROVIDE_CLOCK))
350 goto clock_disabled;
351
352 clock = GST_CLOCK_CAST (gst_object_ref (sink->provided_clock));
353 GST_OBJECT_UNLOCK (sink);
354
355 return clock;
356
357 /* ERRORS */
358 wrong_state:
359 {
360 GST_DEBUG_OBJECT (sink, "ringbuffer not acquired");
361 return NULL;
362 }
363 clock_disabled:
364 {
365 GST_DEBUG_OBJECT (sink, "clock provide disabled");
366 GST_OBJECT_UNLOCK (sink);
367 return NULL;
368 }
369 }
370
371 static gboolean
gst_audio_base_sink_is_self_provided_clock(GstAudioBaseSink * sink)372 gst_audio_base_sink_is_self_provided_clock (GstAudioBaseSink * sink)
373 {
374 return (sink->provided_clock && GST_IS_AUDIO_CLOCK (sink->provided_clock) &&
375 GST_AUDIO_CLOCK_CAST (sink->provided_clock)->func ==
376 (GstAudioClockGetTimeFunc) gst_audio_base_sink_get_time);
377 }
378
379 static gboolean
gst_audio_base_sink_query_pad(GstBaseSink * bsink,GstQuery * query)380 gst_audio_base_sink_query_pad (GstBaseSink * bsink, GstQuery * query)
381 {
382 gboolean res = FALSE;
383 GstAudioBaseSink *basesink;
384
385 basesink = GST_AUDIO_BASE_SINK (bsink);
386
387 switch (GST_QUERY_TYPE (query)) {
388 case GST_QUERY_CONVERT:
389 {
390 GstFormat src_fmt, dest_fmt;
391 gint64 src_val, dest_val;
392
393 GST_LOG_OBJECT (basesink, "query convert");
394
395 if (basesink->ringbuffer) {
396 gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, NULL);
397 res =
398 gst_audio_ring_buffer_convert (basesink->ringbuffer, src_fmt,
399 src_val, dest_fmt, &dest_val);
400 if (res) {
401 gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
402 }
403 }
404 break;
405 }
406 default:
407 res = GST_BASE_SINK_CLASS (parent_class)->query (bsink, query);
408 break;
409 }
410 return res;
411 }
412
413 static gboolean
gst_audio_base_sink_query(GstElement * element,GstQuery * query)414 gst_audio_base_sink_query (GstElement * element, GstQuery * query)
415 {
416 gboolean res = FALSE;
417 GstAudioBaseSink *basesink;
418
419 basesink = GST_AUDIO_BASE_SINK (element);
420
421 switch (GST_QUERY_TYPE (query)) {
422 case GST_QUERY_LATENCY:
423 {
424 gboolean live, us_live;
425 GstClockTime min_l, max_l;
426
427 GST_DEBUG_OBJECT (basesink, "latency query");
428
429 /* ask parent first, it will do an upstream query for us. */
430 if ((res =
431 gst_base_sink_query_latency (GST_BASE_SINK_CAST (basesink), &live,
432 &us_live, &min_l, &max_l))) {
433 GstClockTime base_latency, min_latency, max_latency;
434
435 /* we and upstream are both live, adjust the min_latency */
436 if (live && us_live) {
437 GstAudioRingBufferSpec *spec;
438
439 GST_OBJECT_LOCK (basesink);
440 if (!basesink->ringbuffer || !basesink->ringbuffer->spec.info.rate) {
441 GST_OBJECT_UNLOCK (basesink);
442
443 GST_DEBUG_OBJECT (basesink,
444 "we are not negotiated, can't report latency yet");
445 res = FALSE;
446 goto done;
447 }
448 spec = &basesink->ringbuffer->spec;
449
450 basesink->priv->us_latency = min_l;
451
452 base_latency =
453 gst_util_uint64_scale_int (spec->seglatency * spec->segsize,
454 GST_SECOND, spec->info.rate * spec->info.bpf);
455 GST_OBJECT_UNLOCK (basesink);
456
457 /* we cannot go lower than the buffer size and the min peer latency */
458 min_latency = base_latency + min_l;
459 /* the max latency is the max of the peer, we can delay an infinite
460 * amount of time. */
461 max_latency = (max_l == -1) ? -1 : (base_latency + max_l);
462
463 GST_DEBUG_OBJECT (basesink,
464 "peer min %" GST_TIME_FORMAT ", our min latency: %"
465 GST_TIME_FORMAT, GST_TIME_ARGS (min_l),
466 GST_TIME_ARGS (min_latency));
467 GST_DEBUG_OBJECT (basesink,
468 "peer max %" GST_TIME_FORMAT ", our max latency: %"
469 GST_TIME_FORMAT, GST_TIME_ARGS (max_l),
470 GST_TIME_ARGS (max_latency));
471 } else {
472 GST_DEBUG_OBJECT (basesink,
473 "peer or we are not live, don't care about latency");
474 min_latency = min_l;
475 max_latency = max_l;
476 }
477 gst_query_set_latency (query, live, min_latency, max_latency);
478 }
479 break;
480 }
481 case GST_QUERY_CONVERT:
482 {
483 GstFormat src_fmt, dest_fmt;
484 gint64 src_val, dest_val;
485
486 GST_LOG_OBJECT (basesink, "query convert");
487
488 if (basesink->ringbuffer) {
489 gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, NULL);
490 res =
491 gst_audio_ring_buffer_convert (basesink->ringbuffer, src_fmt,
492 src_val, dest_fmt, &dest_val);
493 if (res) {
494 gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
495 }
496 }
497 break;
498 }
499 default:
500 res = GST_ELEMENT_CLASS (parent_class)->query (element, query);
501 break;
502 }
503
504 done:
505 return res;
506 }
507
508
509 /* we call this function without holding the lock on sink for performance
510 * reasons. Try hard to not deal with and invalid ringbuffer and rate. */
511 static GstClockTime
gst_audio_base_sink_get_time(GstClock * clock,GstAudioBaseSink * sink)512 gst_audio_base_sink_get_time (GstClock * clock, GstAudioBaseSink * sink)
513 {
514 guint64 raw, samples;
515 guint delay;
516 GstClockTime result;
517 GstAudioRingBuffer *ringbuffer;
518 gint rate;
519
520 if ((ringbuffer = sink->ringbuffer) == NULL)
521 return GST_CLOCK_TIME_NONE;
522
523 if ((rate = ringbuffer->spec.info.rate) == 0)
524 return GST_CLOCK_TIME_NONE;
525
526 /* our processed samples are always increasing */
527 raw = samples = gst_audio_ring_buffer_samples_done (ringbuffer);
528
529 /* the number of samples not yet processed, this is still queued in the
530 * device (not played for playback). */
531 delay = gst_audio_ring_buffer_delay (ringbuffer);
532
533 if (G_LIKELY (samples >= delay))
534 samples -= delay;
535 else
536 samples = 0;
537
538 result = gst_util_uint64_scale_int (samples, GST_SECOND, rate);
539
540 GST_DEBUG_OBJECT (sink,
541 "processed samples: raw %" G_GUINT64_FORMAT ", delay %u, real %"
542 G_GUINT64_FORMAT ", time %" GST_TIME_FORMAT,
543 raw, delay, samples, GST_TIME_ARGS (result));
544
545 return result;
546 }
547
548 /**
549 * gst_audio_base_sink_set_provide_clock:
550 * @sink: a #GstAudioBaseSink
551 * @provide: new state
552 *
553 * Controls whether @sink will provide a clock or not. If @provide is %TRUE,
554 * gst_element_provide_clock() will return a clock that reflects the datarate
555 * of @sink. If @provide is %FALSE, gst_element_provide_clock() will return
556 * NULL.
557 */
558 void
gst_audio_base_sink_set_provide_clock(GstAudioBaseSink * sink,gboolean provide)559 gst_audio_base_sink_set_provide_clock (GstAudioBaseSink * sink,
560 gboolean provide)
561 {
562 g_return_if_fail (GST_IS_AUDIO_BASE_SINK (sink));
563
564 GST_OBJECT_LOCK (sink);
565 if (provide)
566 GST_OBJECT_FLAG_SET (sink, GST_ELEMENT_FLAG_PROVIDE_CLOCK);
567 else
568 GST_OBJECT_FLAG_UNSET (sink, GST_ELEMENT_FLAG_PROVIDE_CLOCK);
569 GST_OBJECT_UNLOCK (sink);
570 }
571
572 /**
573 * gst_audio_base_sink_get_provide_clock:
574 * @sink: a #GstAudioBaseSink
575 *
576 * Queries whether @sink will provide a clock or not. See also
577 * gst_audio_base_sink_set_provide_clock.
578 *
579 * Returns: %TRUE if @sink will provide a clock.
580 */
581 gboolean
gst_audio_base_sink_get_provide_clock(GstAudioBaseSink * sink)582 gst_audio_base_sink_get_provide_clock (GstAudioBaseSink * sink)
583 {
584 gboolean result;
585
586 g_return_val_if_fail (GST_IS_AUDIO_BASE_SINK (sink), FALSE);
587
588 GST_OBJECT_LOCK (sink);
589 result = GST_OBJECT_FLAG_IS_SET (sink, GST_ELEMENT_FLAG_PROVIDE_CLOCK);
590 GST_OBJECT_UNLOCK (sink);
591
592 return result;
593 }
594
595 /**
596 * gst_audio_base_sink_set_slave_method:
597 * @sink: a #GstAudioBaseSink
598 * @method: the new slave method
599 *
600 * Controls how clock slaving will be performed in @sink.
601 */
602 void
gst_audio_base_sink_set_slave_method(GstAudioBaseSink * sink,GstAudioBaseSinkSlaveMethod method)603 gst_audio_base_sink_set_slave_method (GstAudioBaseSink * sink,
604 GstAudioBaseSinkSlaveMethod method)
605 {
606 g_return_if_fail (GST_IS_AUDIO_BASE_SINK (sink));
607
608 GST_OBJECT_LOCK (sink);
609 sink->priv->slave_method = method;
610 GST_OBJECT_UNLOCK (sink);
611 }
612
613 /**
614 * gst_audio_base_sink_get_slave_method:
615 * @sink: a #GstAudioBaseSink
616 *
617 * Get the current slave method used by @sink.
618 *
619 * Returns: The current slave method used by @sink.
620 */
621 GstAudioBaseSinkSlaveMethod
gst_audio_base_sink_get_slave_method(GstAudioBaseSink * sink)622 gst_audio_base_sink_get_slave_method (GstAudioBaseSink * sink)
623 {
624 GstAudioBaseSinkSlaveMethod result;
625
626 g_return_val_if_fail (GST_IS_AUDIO_BASE_SINK (sink), -1);
627
628 GST_OBJECT_LOCK (sink);
629 result = sink->priv->slave_method;
630 GST_OBJECT_UNLOCK (sink);
631
632 return result;
633 }
634
635
636 /**
637 * gst_audio_base_sink_set_drift_tolerance:
638 * @sink: a #GstAudioBaseSink
639 * @drift_tolerance: the new drift tolerance in microseconds
640 *
641 * Controls the sink's drift tolerance.
642 */
643 void
gst_audio_base_sink_set_drift_tolerance(GstAudioBaseSink * sink,gint64 drift_tolerance)644 gst_audio_base_sink_set_drift_tolerance (GstAudioBaseSink * sink,
645 gint64 drift_tolerance)
646 {
647 g_return_if_fail (GST_IS_AUDIO_BASE_SINK (sink));
648
649 GST_OBJECT_LOCK (sink);
650 sink->priv->drift_tolerance = drift_tolerance;
651 GST_OBJECT_UNLOCK (sink);
652 }
653
654 /**
655 * gst_audio_base_sink_get_drift_tolerance:
656 * @sink: a #GstAudioBaseSink
657 *
658 * Get the current drift tolerance, in microseconds, used by @sink.
659 *
660 * Returns: The current drift tolerance used by @sink.
661 */
662 gint64
gst_audio_base_sink_get_drift_tolerance(GstAudioBaseSink * sink)663 gst_audio_base_sink_get_drift_tolerance (GstAudioBaseSink * sink)
664 {
665 gint64 result;
666
667 g_return_val_if_fail (GST_IS_AUDIO_BASE_SINK (sink), -1);
668
669 GST_OBJECT_LOCK (sink);
670 result = sink->priv->drift_tolerance;
671 GST_OBJECT_UNLOCK (sink);
672
673 return result;
674 }
675
676 /**
677 * gst_audio_base_sink_set_alignment_threshold:
678 * @sink: a #GstAudioBaseSink
679 * @alignment_threshold: the new alignment threshold in nanoseconds
680 *
681 * Controls the sink's alignment threshold.
682 */
683 void
gst_audio_base_sink_set_alignment_threshold(GstAudioBaseSink * sink,GstClockTime alignment_threshold)684 gst_audio_base_sink_set_alignment_threshold (GstAudioBaseSink * sink,
685 GstClockTime alignment_threshold)
686 {
687 g_return_if_fail (GST_IS_AUDIO_BASE_SINK (sink));
688
689 GST_OBJECT_LOCK (sink);
690 sink->priv->alignment_threshold = alignment_threshold;
691 GST_OBJECT_UNLOCK (sink);
692 }
693
694 /**
695 * gst_audio_base_sink_get_alignment_threshold:
696 * @sink: a #GstAudioBaseSink
697 *
698 * Get the current alignment threshold, in nanoseconds, used by @sink.
699 *
700 * Returns: The current alignment threshold used by @sink.
701 */
702 GstClockTime
gst_audio_base_sink_get_alignment_threshold(GstAudioBaseSink * sink)703 gst_audio_base_sink_get_alignment_threshold (GstAudioBaseSink * sink)
704 {
705 GstClockTime result;
706
707 g_return_val_if_fail (GST_IS_AUDIO_BASE_SINK (sink), GST_CLOCK_TIME_NONE);
708
709 GST_OBJECT_LOCK (sink);
710 result = sink->priv->alignment_threshold;
711 GST_OBJECT_UNLOCK (sink);
712
713 return result;
714 }
715
716 /**
717 * gst_audio_base_sink_set_discont_wait:
718 * @sink: a #GstAudioBaseSink
719 * @discont_wait: the new discont wait in nanoseconds
720 *
721 * Controls how long the sink will wait before creating a discontinuity.
722 */
723 void
gst_audio_base_sink_set_discont_wait(GstAudioBaseSink * sink,GstClockTime discont_wait)724 gst_audio_base_sink_set_discont_wait (GstAudioBaseSink * sink,
725 GstClockTime discont_wait)
726 {
727 g_return_if_fail (GST_IS_AUDIO_BASE_SINK (sink));
728
729 GST_OBJECT_LOCK (sink);
730 sink->priv->discont_wait = discont_wait;
731 GST_OBJECT_UNLOCK (sink);
732 }
733
734 /**
735 * gst_audio_base_sink_set_custom_slaving_callback:
736 * @sink: a #GstAudioBaseSink
737 * @callback: a #GstAudioBaseSinkCustomSlavingCallback
738 * @user_data: user data passed to the callback
739 * @notify : called when user_data becomes unused
740 *
741 * Sets the custom slaving callback. This callback will
742 * be invoked if the slave-method property is set to
743 * GST_AUDIO_BASE_SINK_SLAVE_CUSTOM and the audio sink
744 * receives and plays samples.
745 *
746 * Setting the callback to NULL causes the sink to
747 * behave as if the GST_AUDIO_BASE_SINK_SLAVE_NONE
748 * method were used.
749 *
750 * Since: 1.6
751 */
752 void
gst_audio_base_sink_set_custom_slaving_callback(GstAudioBaseSink * sink,GstAudioBaseSinkCustomSlavingCallback callback,gpointer user_data,GDestroyNotify notify)753 gst_audio_base_sink_set_custom_slaving_callback (GstAudioBaseSink * sink,
754 GstAudioBaseSinkCustomSlavingCallback callback,
755 gpointer user_data, GDestroyNotify notify)
756 {
757 g_return_if_fail (GST_IS_AUDIO_BASE_SINK (sink));
758
759 GST_OBJECT_LOCK (sink);
760 sink->priv->custom_slaving_callback = callback;
761 sink->priv->custom_slaving_cb_data = user_data;
762 sink->priv->custom_slaving_cb_notify = notify;
763 GST_OBJECT_UNLOCK (sink);
764 }
765
766 static void
gst_audio_base_sink_custom_cb_report_discont(GstAudioBaseSink * sink,GstAudioBaseSinkDiscontReason discont_reason)767 gst_audio_base_sink_custom_cb_report_discont (GstAudioBaseSink * sink,
768 GstAudioBaseSinkDiscontReason discont_reason)
769 {
770 if ((sink->priv->custom_slaving_callback != NULL) &&
771 (sink->priv->slave_method == GST_AUDIO_BASE_SINK_SLAVE_CUSTOM)) {
772 sink->priv->custom_slaving_callback (sink, GST_CLOCK_TIME_NONE,
773 GST_CLOCK_TIME_NONE, NULL, discont_reason,
774 sink->priv->custom_slaving_cb_data);
775 }
776 }
777
778 /**
779 * gst_audio_base_sink_report_device_failure:
780 * @sink: a #GstAudioBaseSink
781 *
782 * Informs this base class that the audio output device has failed for
783 * some reason, causing a discontinuity (for example, because the device
784 * recovered from the error, but lost all contents of its ring buffer).
785 * This function is typically called by derived classes, and is useful
786 * for the custom slave method.
787 *
788 * Since: 1.6
789 */
790 void
gst_audio_base_sink_report_device_failure(GstAudioBaseSink * sink)791 gst_audio_base_sink_report_device_failure (GstAudioBaseSink * sink)
792 {
793 g_return_if_fail (GST_IS_AUDIO_BASE_SINK (sink));
794
795 GST_OBJECT_LOCK (sink);
796 gst_audio_base_sink_custom_cb_report_discont (sink,
797 GST_AUDIO_BASE_SINK_DISCONT_REASON_DEVICE_FAILURE);
798 GST_OBJECT_UNLOCK (sink);
799 }
800
801 /**
802 * gst_audio_base_sink_get_discont_wait:
803 * @sink: a #GstAudioBaseSink
804 *
805 * Get the current discont wait, in nanoseconds, used by @sink.
806 *
807 * Returns: The current discont wait used by @sink.
808 */
809 GstClockTime
gst_audio_base_sink_get_discont_wait(GstAudioBaseSink * sink)810 gst_audio_base_sink_get_discont_wait (GstAudioBaseSink * sink)
811 {
812 GstClockTime result;
813
814 g_return_val_if_fail (GST_IS_AUDIO_BASE_SINK (sink), -1);
815
816 GST_OBJECT_LOCK (sink);
817 result = sink->priv->discont_wait;
818 GST_OBJECT_UNLOCK (sink);
819
820 return result;
821 }
822
823 static void
gst_audio_base_sink_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)824 gst_audio_base_sink_set_property (GObject * object, guint prop_id,
825 const GValue * value, GParamSpec * pspec)
826 {
827 GstAudioBaseSink *sink;
828
829 sink = GST_AUDIO_BASE_SINK (object);
830
831 switch (prop_id) {
832 case PROP_BUFFER_TIME:
833 sink->buffer_time = g_value_get_int64 (value);
834 break;
835 case PROP_LATENCY_TIME:
836 sink->latency_time = g_value_get_int64 (value);
837 break;
838 case PROP_PROVIDE_CLOCK:
839 gst_audio_base_sink_set_provide_clock (sink, g_value_get_boolean (value));
840 break;
841 case PROP_SLAVE_METHOD:
842 gst_audio_base_sink_set_slave_method (sink, g_value_get_enum (value));
843 break;
844 case PROP_CAN_ACTIVATE_PULL:
845 GST_BASE_SINK (sink)->can_activate_pull = g_value_get_boolean (value);
846 break;
847 case PROP_DRIFT_TOLERANCE:
848 gst_audio_base_sink_set_drift_tolerance (sink, g_value_get_int64 (value));
849 break;
850 case PROP_ALIGNMENT_THRESHOLD:
851 gst_audio_base_sink_set_alignment_threshold (sink,
852 g_value_get_uint64 (value));
853 break;
854 case PROP_DISCONT_WAIT:
855 gst_audio_base_sink_set_discont_wait (sink, g_value_get_uint64 (value));
856 break;
857 default:
858 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
859 break;
860 }
861 }
862
863 static void
gst_audio_base_sink_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)864 gst_audio_base_sink_get_property (GObject * object, guint prop_id,
865 GValue * value, GParamSpec * pspec)
866 {
867 GstAudioBaseSink *sink;
868
869 sink = GST_AUDIO_BASE_SINK (object);
870
871 switch (prop_id) {
872 case PROP_BUFFER_TIME:
873 g_value_set_int64 (value, sink->buffer_time);
874 break;
875 case PROP_LATENCY_TIME:
876 g_value_set_int64 (value, sink->latency_time);
877 break;
878 case PROP_PROVIDE_CLOCK:
879 g_value_set_boolean (value, gst_audio_base_sink_get_provide_clock (sink));
880 break;
881 case PROP_SLAVE_METHOD:
882 g_value_set_enum (value, gst_audio_base_sink_get_slave_method (sink));
883 break;
884 case PROP_CAN_ACTIVATE_PULL:
885 g_value_set_boolean (value, GST_BASE_SINK (sink)->can_activate_pull);
886 break;
887 case PROP_DRIFT_TOLERANCE:
888 g_value_set_int64 (value, gst_audio_base_sink_get_drift_tolerance (sink));
889 break;
890 case PROP_ALIGNMENT_THRESHOLD:
891 g_value_set_uint64 (value,
892 gst_audio_base_sink_get_alignment_threshold (sink));
893 break;
894 case PROP_DISCONT_WAIT:
895 g_value_set_uint64 (value, gst_audio_base_sink_get_discont_wait (sink));
896 break;
897 default:
898 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
899 break;
900 }
901 }
902
903 static gboolean
gst_audio_base_sink_setcaps(GstBaseSink * bsink,GstCaps * caps)904 gst_audio_base_sink_setcaps (GstBaseSink * bsink, GstCaps * caps)
905 {
906 GstAudioBaseSink *sink = GST_AUDIO_BASE_SINK (bsink);
907 GstAudioRingBufferSpec *spec;
908 GstClockTime now, internal_time;
909 GstClockTime crate_num, crate_denom;
910
911 if (!sink->ringbuffer)
912 return FALSE;
913
914 spec = &sink->ringbuffer->spec;
915
916 if (G_UNLIKELY (spec->caps && gst_caps_is_equal (spec->caps, caps))) {
917 GST_DEBUG_OBJECT (sink,
918 "Ringbuffer caps haven't changed, skipping reconfiguration");
919 return TRUE;
920 }
921
922 GST_DEBUG_OBJECT (sink, "release old ringbuffer");
923
924 /* get current time, updates the last_time. When the subclass has a clock that
925 * restarts from 0 when a new format is negotiated, it will call
926 * gst_audio_clock_reset() which will use this last_time to create an offset
927 * so that time from the clock keeps on increasing monotonically. */
928 now = gst_clock_get_time (sink->provided_clock);
929 internal_time = gst_clock_get_internal_time (sink->provided_clock);
930
931 GST_DEBUG_OBJECT (sink, "time was %" GST_TIME_FORMAT, GST_TIME_ARGS (now));
932
933 /* release old ringbuffer */
934 gst_audio_ring_buffer_pause (sink->ringbuffer);
935 gst_audio_ring_buffer_activate (sink->ringbuffer, FALSE);
936 gst_audio_ring_buffer_release (sink->ringbuffer);
937
938 GST_DEBUG_OBJECT (sink, "parse caps");
939
940 spec->buffer_time = sink->buffer_time;
941 spec->latency_time = sink->latency_time;
942
943 /* parse new caps */
944 if (!gst_audio_ring_buffer_parse_caps (spec, caps))
945 goto parse_error;
946
947 gst_audio_ring_buffer_debug_spec_buff (spec);
948
949 GST_DEBUG_OBJECT (sink, "acquire ringbuffer");
950 if (!gst_audio_ring_buffer_acquire (sink->ringbuffer, spec))
951 goto acquire_error;
952
953 /* If we use our own clock, we need to adjust the offset since it will now
954 * restart from zero */
955 if (gst_audio_base_sink_is_self_provided_clock (sink))
956 gst_audio_clock_reset (GST_AUDIO_CLOCK (sink->provided_clock), 0);
957
958 /* We need to resync since the ringbuffer restarted */
959 gst_audio_base_sink_reset_sync (sink);
960
961 gst_audio_base_sink_custom_cb_report_discont (sink,
962 GST_AUDIO_BASE_SINK_DISCONT_REASON_NEW_CAPS);
963
964 if (bsink->pad_mode == GST_PAD_MODE_PUSH) {
965 GST_DEBUG_OBJECT (sink, "activate ringbuffer");
966 gst_audio_ring_buffer_activate (sink->ringbuffer, TRUE);
967 }
968
969 /* due to possible changes in the spec file we should recalibrate the clock */
970 gst_clock_get_calibration (sink->provided_clock, NULL, NULL,
971 &crate_num, &crate_denom);
972 gst_clock_set_calibration (sink->provided_clock,
973 internal_time, now, crate_num, crate_denom);
974
975 /* calculate actual latency and buffer times.
976 * FIXME: In 2.0, store the latency_time internally in ns */
977 spec->latency_time = gst_util_uint64_scale (spec->segsize,
978 (GST_SECOND / GST_USECOND), spec->info.rate * spec->info.bpf);
979
980 spec->buffer_time = spec->segtotal * spec->latency_time;
981
982 gst_audio_ring_buffer_debug_spec_buff (spec);
983
984 gst_element_post_message (GST_ELEMENT_CAST (bsink),
985 gst_message_new_latency (GST_OBJECT (bsink)));
986
987 return TRUE;
988
989 /* ERRORS */
990 parse_error:
991 {
992 GST_DEBUG_OBJECT (sink, "could not parse caps");
993 GST_ELEMENT_ERROR (sink, STREAM, FORMAT,
994 (NULL), ("cannot parse audio format."));
995 return FALSE;
996 }
997 acquire_error:
998 {
999 GST_DEBUG_OBJECT (sink, "could not acquire ringbuffer");
1000 return FALSE;
1001 }
1002 }
1003
1004 static GstCaps *
gst_audio_base_sink_fixate(GstBaseSink * bsink,GstCaps * caps)1005 gst_audio_base_sink_fixate (GstBaseSink * bsink, GstCaps * caps)
1006 {
1007 GstStructure *s;
1008 gint width, depth;
1009
1010 caps = gst_caps_make_writable (caps);
1011
1012 s = gst_caps_get_structure (caps, 0);
1013
1014 /* fields for all formats */
1015 gst_structure_fixate_field_nearest_int (s, "rate", 44100);
1016 gst_structure_fixate_field_nearest_int (s, "channels", 2);
1017 gst_structure_fixate_field_nearest_int (s, "width", 16);
1018
1019 /* fields for int */
1020 if (gst_structure_has_field (s, "depth")) {
1021 gst_structure_get_int (s, "width", &width);
1022 /* round width to nearest multiple of 8 for the depth */
1023 depth = GST_ROUND_UP_8 (width);
1024 gst_structure_fixate_field_nearest_int (s, "depth", depth);
1025 }
1026 if (gst_structure_has_field (s, "signed"))
1027 gst_structure_fixate_field_boolean (s, "signed", TRUE);
1028 if (gst_structure_has_field (s, "endianness"))
1029 gst_structure_fixate_field_nearest_int (s, "endianness", G_BYTE_ORDER);
1030
1031 caps = GST_BASE_SINK_CLASS (parent_class)->fixate (bsink, caps);
1032
1033 return caps;
1034 }
1035
1036 static inline void
gst_audio_base_sink_reset_sync(GstAudioBaseSink * sink)1037 gst_audio_base_sink_reset_sync (GstAudioBaseSink * sink)
1038 {
1039 sink->next_sample = -1;
1040 sink->priv->eos_time = -1;
1041 sink->priv->discont_time = -1;
1042 sink->priv->avg_skew = -1;
1043 sink->priv->last_align = 0;
1044 }
1045
1046 static void
gst_audio_base_sink_get_times(GstBaseSink * bsink,GstBuffer * buffer,GstClockTime * start,GstClockTime * end)1047 gst_audio_base_sink_get_times (GstBaseSink * bsink, GstBuffer * buffer,
1048 GstClockTime * start, GstClockTime * end)
1049 {
1050 /* our clock sync is a bit too much for the base class to handle so
1051 * we implement it ourselves. */
1052 *start = GST_CLOCK_TIME_NONE;
1053 *end = GST_CLOCK_TIME_NONE;
1054 }
1055
1056 static void
gst_audio_base_sink_force_start(GstAudioBaseSink * sink)1057 gst_audio_base_sink_force_start (GstAudioBaseSink * sink)
1058 {
1059 /* Set the eos_rendering flag so sub-classes definitely start the clock.
1060 * FIXME 2.0: Pass this as a flag to gst_audio_ring_buffer_start() */
1061 g_atomic_int_set (&sink->eos_rendering, 1);
1062 gst_audio_ring_buffer_start (sink->ringbuffer);
1063 g_atomic_int_set (&sink->eos_rendering, 0);
1064 }
1065
1066 /* This waits for the drain to happen and can be canceled */
1067 static GstFlowReturn
gst_audio_base_sink_drain(GstAudioBaseSink * sink)1068 gst_audio_base_sink_drain (GstAudioBaseSink * sink)
1069 {
1070 GstFlowReturn ret = GST_FLOW_OK;
1071 if (!sink->ringbuffer)
1072 return ret;
1073 if (!sink->ringbuffer->spec.info.rate)
1074 return ret;
1075
1076 /* if PLAYING is interrupted,
1077 * arrange to have clock running when going to PLAYING again */
1078 g_atomic_int_set (&sink->eos_rendering, 1);
1079
1080 /* need to start playback before we can drain, but only when
1081 * we have successfully negotiated a format and thus acquired the
1082 * ringbuffer. */
1083 if (gst_audio_ring_buffer_is_acquired (sink->ringbuffer))
1084 gst_audio_ring_buffer_start (sink->ringbuffer);
1085
1086 if (sink->priv->eos_time != -1) {
1087 GST_DEBUG_OBJECT (sink,
1088 "last sample time %" GST_TIME_FORMAT,
1089 GST_TIME_ARGS (sink->priv->eos_time));
1090
1091 /* wait for the EOS time to be reached, this is the time when the last
1092 * sample is played. */
1093 ret = gst_base_sink_wait (GST_BASE_SINK (sink), sink->priv->eos_time, NULL);
1094
1095 GST_DEBUG_OBJECT (sink, "drained audio");
1096 }
1097 g_atomic_int_set (&sink->eos_rendering, 0);
1098 return ret;
1099 }
1100
1101 static GstFlowReturn
gst_audio_base_sink_wait_event(GstBaseSink * bsink,GstEvent * event)1102 gst_audio_base_sink_wait_event (GstBaseSink * bsink, GstEvent * event)
1103 {
1104 GstAudioBaseSink *sink = GST_AUDIO_BASE_SINK (bsink);
1105 GstFlowReturn ret = GST_FLOW_OK;
1106 gboolean clear_force_start_flag = FALSE;
1107
1108 /* For both gap and EOS events, make sure the ringbuffer is running
1109 * before trying to wait on the event! */
1110 switch (GST_EVENT_TYPE (event)) {
1111 case GST_EVENT_EOS:
1112 case GST_EVENT_GAP:
1113 /* We must have a negotiated format before starting the ringbuffer */
1114 if (G_UNLIKELY (!gst_audio_ring_buffer_is_acquired (sink->ringbuffer))) {
1115 GST_ELEMENT_ERROR (sink, STREAM, FORMAT, (NULL),
1116 ("Sink not negotiated before %s event.",
1117 GST_EVENT_TYPE_NAME (event)));
1118 return GST_FLOW_ERROR;
1119 }
1120
1121 gst_audio_base_sink_force_start (sink);
1122 /* Make sure the ringbuffer will start again if interrupted during event_wait() */
1123 g_atomic_int_set (&sink->eos_rendering, 1);
1124 clear_force_start_flag = TRUE;
1125 break;
1126 default:
1127 break;
1128 }
1129
1130 ret = GST_BASE_SINK_CLASS (parent_class)->wait_event (bsink, event);
1131 if (ret != GST_FLOW_OK)
1132 goto done;
1133
1134 switch (GST_EVENT_TYPE (event)) {
1135 case GST_EVENT_EOS:
1136 /* now wait till we played everything */
1137 ret = gst_audio_base_sink_drain (sink);
1138 break;
1139 default:
1140 break;
1141 }
1142
1143 done:
1144 if (clear_force_start_flag)
1145 g_atomic_int_set (&sink->eos_rendering, 0);
1146 return ret;
1147 }
1148
1149 static gboolean
gst_audio_base_sink_event(GstBaseSink * bsink,GstEvent * event)1150 gst_audio_base_sink_event (GstBaseSink * bsink, GstEvent * event)
1151 {
1152 GstAudioBaseSink *sink = GST_AUDIO_BASE_SINK (bsink);
1153
1154 switch (GST_EVENT_TYPE (event)) {
1155 case GST_EVENT_FLUSH_START:
1156 if (sink->ringbuffer)
1157 gst_audio_ring_buffer_set_flushing (sink->ringbuffer, TRUE);
1158 break;
1159 case GST_EVENT_FLUSH_STOP:
1160 /* always resync on sample after a flush */
1161 gst_audio_base_sink_reset_sync (sink);
1162
1163 gst_audio_base_sink_custom_cb_report_discont (sink,
1164 GST_AUDIO_BASE_SINK_DISCONT_REASON_FLUSH);
1165
1166 if (sink->ringbuffer)
1167 gst_audio_ring_buffer_set_flushing (sink->ringbuffer, FALSE);
1168 break;
1169 default:
1170 break;
1171 }
1172 return GST_BASE_SINK_CLASS (parent_class)->event (bsink, event);
1173 }
1174
1175 static GstFlowReturn
gst_audio_base_sink_preroll(GstBaseSink * bsink,GstBuffer * buffer)1176 gst_audio_base_sink_preroll (GstBaseSink * bsink, GstBuffer * buffer)
1177 {
1178 GstAudioBaseSink *sink = GST_AUDIO_BASE_SINK (bsink);
1179
1180 if (!gst_audio_ring_buffer_is_acquired (sink->ringbuffer))
1181 goto wrong_state;
1182
1183 /* we don't really do anything when prerolling. We could make a
1184 * property to play this buffer to have some sort of scrubbing
1185 * support. */
1186 return GST_FLOW_OK;
1187
1188 wrong_state:
1189 {
1190 GST_DEBUG_OBJECT (sink, "ringbuffer in wrong state");
1191 GST_ELEMENT_ERROR (sink, STREAM, FORMAT, (NULL), ("sink not negotiated."));
1192 return GST_FLOW_NOT_NEGOTIATED;
1193 }
1194 }
1195
1196 static guint64
gst_audio_base_sink_get_offset(GstAudioBaseSink * sink)1197 gst_audio_base_sink_get_offset (GstAudioBaseSink * sink)
1198 {
1199 guint64 sample, sps;
1200 gint writeseg, segdone;
1201 gint diff;
1202
1203 /* assume we can append to the previous sample */
1204 sample = sink->next_sample;
1205 /* no previous sample, try to insert at position 0 */
1206 if (sample == -1)
1207 sample = 0;
1208
1209 sps = sink->ringbuffer->samples_per_seg;
1210
1211 /* figure out the segment and the offset inside the segment where
1212 * the sample should be written. */
1213 writeseg = sample / sps;
1214
1215 /* get the currently processed segment */
1216 segdone = g_atomic_int_get (&sink->ringbuffer->segdone)
1217 - sink->ringbuffer->segbase;
1218
1219 /* see how far away it is from the write segment */
1220 diff = writeseg - segdone;
1221 if (diff < 0) {
1222 /* sample would be dropped, position to next playable position */
1223 sample = (segdone + 1) * sps;
1224 }
1225
1226 return sample;
1227 }
1228
1229 static GstClockTime
clock_convert_external(GstClockTime external,GstClockTime cinternal,GstClockTime cexternal,GstClockTime crate_num,GstClockTime crate_denom)1230 clock_convert_external (GstClockTime external, GstClockTime cinternal,
1231 GstClockTime cexternal, GstClockTime crate_num, GstClockTime crate_denom)
1232 {
1233 /* adjust for rate and speed */
1234 if (external >= cexternal) {
1235 external =
1236 gst_util_uint64_scale (external - cexternal, crate_denom, crate_num);
1237 external += cinternal;
1238 } else {
1239 external =
1240 gst_util_uint64_scale (cexternal - external, crate_denom, crate_num);
1241 if (cinternal > external)
1242 external = cinternal - external;
1243 else
1244 external = 0;
1245 }
1246 return external;
1247 }
1248
1249
1250 /* apply the clock offset and invoke a custom callback
1251 * which might also request changes to the playout pointer
1252 *
1253 * this reuses code from the skewing algorithm, but leaves
1254 * decision on whether or not to skew (and how much to skew)
1255 * up to the callback */
1256 static void
gst_audio_base_sink_custom_slaving(GstAudioBaseSink * sink,GstClockTime render_start,GstClockTime render_stop,GstClockTime * srender_start,GstClockTime * srender_stop)1257 gst_audio_base_sink_custom_slaving (GstAudioBaseSink * sink,
1258 GstClockTime render_start, GstClockTime render_stop,
1259 GstClockTime * srender_start, GstClockTime * srender_stop)
1260 {
1261 GstClockTime cinternal, cexternal, crate_num, crate_denom;
1262 GstClockTime etime, itime;
1263 GstClockTimeDiff requested_skew;
1264 gint driftsamples;
1265 gint64 last_align;
1266
1267 /* get calibration parameters to compensate for offsets */
1268 gst_clock_get_calibration (sink->provided_clock, &cinternal, &cexternal,
1269 &crate_num, &crate_denom);
1270
1271 /* sample clocks and figure out clock skew */
1272 etime = gst_clock_get_time (GST_ELEMENT_CLOCK (sink));
1273 itime = gst_audio_clock_get_time (GST_AUDIO_CLOCK (sink->provided_clock));
1274 itime =
1275 gst_audio_clock_adjust (GST_AUDIO_CLOCK (sink->provided_clock), itime);
1276
1277 GST_DEBUG_OBJECT (sink,
1278 "internal %" GST_TIME_FORMAT " external %" GST_TIME_FORMAT
1279 " cinternal %" GST_TIME_FORMAT " cexternal %" GST_TIME_FORMAT,
1280 GST_TIME_ARGS (itime), GST_TIME_ARGS (etime),
1281 GST_TIME_ARGS (cinternal), GST_TIME_ARGS (cexternal));
1282
1283 /* make sure we never go below 0 */
1284 etime = etime > cexternal ? etime - cexternal : 0;
1285 itime = itime > cinternal ? itime - cinternal : 0;
1286
1287 /* don't do any skewing unless the callback explicitely requests one */
1288 requested_skew = 0;
1289
1290 if (sink->priv->custom_slaving_callback != NULL) {
1291 sink->priv->custom_slaving_callback (sink, etime, itime, &requested_skew,
1292 FALSE, sink->priv->custom_slaving_cb_data);
1293 GST_DEBUG_OBJECT (sink, "custom slaving requested skew %" GST_STIME_FORMAT,
1294 GST_STIME_ARGS (requested_skew));
1295 } else {
1296 GST_DEBUG_OBJECT (sink,
1297 "no custom slaving callback set - clock drift will not be compensated");
1298 }
1299
1300 if (requested_skew > 0) {
1301 cexternal = (cexternal > requested_skew) ? (cexternal - requested_skew) : 0;
1302
1303 driftsamples =
1304 (sink->ringbuffer->spec.info.rate * requested_skew) / GST_SECOND;
1305 last_align = sink->priv->last_align;
1306
1307 /* if we were aligning in the wrong direction or we aligned more than what we
1308 * will correct, resync */
1309 if ((last_align < 0) || (last_align > driftsamples))
1310 sink->next_sample = -1;
1311
1312 GST_DEBUG_OBJECT (sink,
1313 "last_align %" G_GINT64_FORMAT " driftsamples %u, next %"
1314 G_GUINT64_FORMAT, last_align, driftsamples, sink->next_sample);
1315
1316 gst_clock_set_calibration (sink->provided_clock, cinternal, cexternal,
1317 crate_num, crate_denom);
1318 } else if (requested_skew < 0) {
1319 cexternal += ABS (requested_skew);
1320
1321 driftsamples =
1322 (sink->ringbuffer->spec.info.rate * ABS (requested_skew)) / GST_SECOND;
1323 last_align = sink->priv->last_align;
1324
1325 /* if we were aligning in the wrong direction or we aligned more than what we
1326 * will correct, resync */
1327 if ((last_align > 0) || (-last_align > driftsamples))
1328 sink->next_sample = -1;
1329
1330 GST_DEBUG_OBJECT (sink,
1331 "last_align %" G_GINT64_FORMAT " driftsamples %u, next %"
1332 G_GUINT64_FORMAT, last_align, driftsamples, sink->next_sample);
1333
1334 gst_clock_set_calibration (sink->provided_clock, cinternal, cexternal,
1335 crate_num, crate_denom);
1336 }
1337
1338 /* convert, ignoring speed */
1339 render_start = clock_convert_external (render_start, cinternal, cexternal,
1340 crate_num, crate_denom);
1341 render_stop = clock_convert_external (render_stop, cinternal, cexternal,
1342 crate_num, crate_denom);
1343
1344 *srender_start = render_start;
1345 *srender_stop = render_stop;
1346 }
1347
1348 /* algorithm to calculate sample positions that will result in resampling to
1349 * match the clock rate of the master */
1350 static void
gst_audio_base_sink_resample_slaving(GstAudioBaseSink * sink,GstClockTime render_start,GstClockTime render_stop,GstClockTime * srender_start,GstClockTime * srender_stop)1351 gst_audio_base_sink_resample_slaving (GstAudioBaseSink * sink,
1352 GstClockTime render_start, GstClockTime render_stop,
1353 GstClockTime * srender_start, GstClockTime * srender_stop)
1354 {
1355 GstClockTime cinternal, cexternal;
1356 GstClockTime crate_num, crate_denom;
1357
1358 /* FIXME, we can sample and add observations here or use the timeouts on the
1359 * clock. No idea which one is better or more stable. The timeout seems more
1360 * arbitrary but this one seems more demanding and does not work when there is
1361 * no data comming in to the sink. */
1362 #if 0
1363 GstClockTime etime, itime;
1364 gdouble r_squared;
1365
1366 /* sample clocks and figure out clock skew */
1367 etime = gst_clock_get_time (GST_ELEMENT_CLOCK (sink));
1368 itime = gst_audio_clock_get_time (sink->provided_clock);
1369
1370 /* add new observation */
1371 gst_clock_add_observation (sink->provided_clock, itime, etime, &r_squared);
1372 #endif
1373
1374 /* get calibration parameters to compensate for speed and offset differences
1375 * when we are slaved */
1376 gst_clock_get_calibration (sink->provided_clock, &cinternal, &cexternal,
1377 &crate_num, &crate_denom);
1378
1379 GST_DEBUG_OBJECT (sink, "internal %" GST_TIME_FORMAT " external %"
1380 GST_TIME_FORMAT " %" G_GUINT64_FORMAT "/%" G_GUINT64_FORMAT " = %f",
1381 GST_TIME_ARGS (cinternal), GST_TIME_ARGS (cexternal), crate_num,
1382 crate_denom, gst_guint64_to_gdouble (crate_num) /
1383 gst_guint64_to_gdouble (crate_denom));
1384
1385 if (crate_num == 0)
1386 crate_denom = crate_num = 1;
1387
1388 /* bring external time to internal time */
1389 render_start = clock_convert_external (render_start, cinternal, cexternal,
1390 crate_num, crate_denom);
1391 render_stop = clock_convert_external (render_stop, cinternal, cexternal,
1392 crate_num, crate_denom);
1393
1394 GST_DEBUG_OBJECT (sink,
1395 "after slaving: start %" GST_TIME_FORMAT " - stop %" GST_TIME_FORMAT,
1396 GST_TIME_ARGS (render_start), GST_TIME_ARGS (render_stop));
1397
1398 *srender_start = render_start;
1399 *srender_stop = render_stop;
1400 }
1401
1402 /* algorithm to calculate sample positions that will result in changing the
1403 * playout pointer to match the clock rate of the master */
1404 static void
gst_audio_base_sink_skew_slaving(GstAudioBaseSink * sink,GstClockTime render_start,GstClockTime render_stop,GstClockTime * srender_start,GstClockTime * srender_stop)1405 gst_audio_base_sink_skew_slaving (GstAudioBaseSink * sink,
1406 GstClockTime render_start, GstClockTime render_stop,
1407 GstClockTime * srender_start, GstClockTime * srender_stop)
1408 {
1409 GstClockTime cinternal, cexternal, crate_num, crate_denom;
1410 GstClockTime etime, itime;
1411 GstClockTimeDiff skew, drift, mdrift2;
1412 gint driftsamples;
1413 gint64 last_align;
1414
1415 /* get calibration parameters to compensate for offsets */
1416 gst_clock_get_calibration (sink->provided_clock, &cinternal, &cexternal,
1417 &crate_num, &crate_denom);
1418
1419 /* sample clocks and figure out clock skew */
1420 etime = gst_clock_get_time (GST_ELEMENT_CLOCK (sink));
1421 itime = gst_audio_clock_get_time (GST_AUDIO_CLOCK (sink->provided_clock));
1422 itime =
1423 gst_audio_clock_adjust (GST_AUDIO_CLOCK (sink->provided_clock), itime);
1424
1425 GST_DEBUG_OBJECT (sink,
1426 "internal %" GST_TIME_FORMAT " external %" GST_TIME_FORMAT
1427 " cinternal %" GST_TIME_FORMAT " cexternal %" GST_TIME_FORMAT,
1428 GST_TIME_ARGS (itime), GST_TIME_ARGS (etime),
1429 GST_TIME_ARGS (cinternal), GST_TIME_ARGS (cexternal));
1430
1431 /* make sure we never go below 0 */
1432 etime = etime > cexternal ? etime - cexternal : 0;
1433 itime = itime > cinternal ? itime - cinternal : 0;
1434
1435 /* do itime - etime.
1436 * positive value means external clock goes slower
1437 * negative value means external clock goes faster */
1438 skew = GST_CLOCK_DIFF (etime, itime);
1439 if (sink->priv->avg_skew == -1) {
1440 /* first observation */
1441 sink->priv->avg_skew = skew;
1442 } else {
1443 /* next observations use a moving average */
1444 sink->priv->avg_skew = (31 * sink->priv->avg_skew + skew) / 32;
1445 }
1446
1447 GST_DEBUG_OBJECT (sink, "internal %" GST_TIME_FORMAT " external %"
1448 GST_TIME_FORMAT " skew %" GST_STIME_FORMAT " avg %" GST_STIME_FORMAT,
1449 GST_TIME_ARGS (itime), GST_TIME_ARGS (etime), GST_STIME_ARGS (skew),
1450 GST_STIME_ARGS (sink->priv->avg_skew));
1451
1452 /* the max drift we allow */
1453 mdrift2 = (sink->priv->drift_tolerance * 1000) / 2;
1454
1455 /* adjust playout pointer based on skew */
1456 if (sink->priv->avg_skew > mdrift2) {
1457 /* master is running slower, move external time backwards */
1458 GST_WARNING_OBJECT (sink,
1459 "correct clock skew %" GST_STIME_FORMAT " > %" GST_STIME_FORMAT,
1460 GST_STIME_ARGS (sink->priv->avg_skew), GST_STIME_ARGS (mdrift2));
1461
1462 /* Move the external time backward by the average skew, but don't ever
1463 * go negative. Moving the average skew by the same distance defines
1464 * the new clock skew window center point. This allows the clock to
1465 * drift equally into either direction after the correction. */
1466 if (G_LIKELY (cexternal > sink->priv->avg_skew))
1467 drift = sink->priv->avg_skew;
1468 else
1469 drift = cexternal;
1470 cexternal -= drift;
1471 sink->priv->avg_skew -= drift;
1472
1473 driftsamples = (sink->ringbuffer->spec.info.rate * drift) / GST_SECOND;
1474 last_align = sink->priv->last_align;
1475
1476 /* if we were aligning in the wrong direction or we aligned more than what
1477 * we will correct, resync */
1478 if (last_align < 0 || last_align > driftsamples)
1479 sink->next_sample = -1;
1480
1481 GST_DEBUG_OBJECT (sink,
1482 "last_align %" G_GINT64_FORMAT " driftsamples %u, next %"
1483 G_GUINT64_FORMAT, last_align, driftsamples, sink->next_sample);
1484
1485 gst_clock_set_calibration (sink->provided_clock, cinternal, cexternal,
1486 crate_num, crate_denom);
1487 } else if (sink->priv->avg_skew < -mdrift2) {
1488 /* master is running faster, move external time forwards */
1489 GST_WARNING_OBJECT (sink,
1490 "correct clock skew %" GST_STIME_FORMAT " < -%" GST_STIME_FORMAT,
1491 GST_STIME_ARGS (sink->priv->avg_skew), GST_STIME_ARGS (mdrift2));
1492
1493 /* Move the external time forward by the average skew, and move the
1494 * average skew by the same distance (which equals a reset to 0). This
1495 * defines the new clock skew window center point. This allows the
1496 * clock to drift equally into either direction after the correction. */
1497 drift = -sink->priv->avg_skew;
1498 cexternal += drift;
1499 sink->priv->avg_skew = 0;
1500
1501 driftsamples = (sink->ringbuffer->spec.info.rate * drift) / GST_SECOND;
1502 last_align = sink->priv->last_align;
1503
1504 /* if we were aligning in the wrong direction or we aligned more than what
1505 * we will correct, resync */
1506 if (last_align > 0 || -last_align > driftsamples)
1507 sink->next_sample = -1;
1508
1509 GST_DEBUG_OBJECT (sink,
1510 "last_align %" G_GINT64_FORMAT " driftsamples %u, next %"
1511 G_GUINT64_FORMAT, last_align, driftsamples, sink->next_sample);
1512
1513 gst_clock_set_calibration (sink->provided_clock, cinternal, cexternal,
1514 crate_num, crate_denom);
1515 }
1516
1517 /* convert, ignoring speed */
1518 render_start = clock_convert_external (render_start, cinternal, cexternal,
1519 crate_num, crate_denom);
1520 render_stop = clock_convert_external (render_stop, cinternal, cexternal,
1521 crate_num, crate_denom);
1522
1523 *srender_start = render_start;
1524 *srender_stop = render_stop;
1525 }
1526
1527 /* apply the clock offset but do no slaving otherwise */
1528 static void
gst_audio_base_sink_none_slaving(GstAudioBaseSink * sink,GstClockTime render_start,GstClockTime render_stop,GstClockTime * srender_start,GstClockTime * srender_stop)1529 gst_audio_base_sink_none_slaving (GstAudioBaseSink * sink,
1530 GstClockTime render_start, GstClockTime render_stop,
1531 GstClockTime * srender_start, GstClockTime * srender_stop)
1532 {
1533 GstClockTime cinternal, cexternal, crate_num, crate_denom;
1534
1535 /* get calibration parameters to compensate for offsets */
1536 gst_clock_get_calibration (sink->provided_clock, &cinternal, &cexternal,
1537 &crate_num, &crate_denom);
1538
1539 /* convert, ignoring speed */
1540 render_start = clock_convert_external (render_start, cinternal, cexternal,
1541 crate_num, crate_denom);
1542 render_stop = clock_convert_external (render_stop, cinternal, cexternal,
1543 crate_num, crate_denom);
1544
1545 *srender_start = render_start;
1546 *srender_stop = render_stop;
1547 }
1548
1549 /* converts render_start and render_stop to their slaved values */
1550 static void
gst_audio_base_sink_handle_slaving(GstAudioBaseSink * sink,GstClockTime render_start,GstClockTime render_stop,GstClockTime * srender_start,GstClockTime * srender_stop)1551 gst_audio_base_sink_handle_slaving (GstAudioBaseSink * sink,
1552 GstClockTime render_start, GstClockTime render_stop,
1553 GstClockTime * srender_start, GstClockTime * srender_stop)
1554 {
1555 switch (sink->priv->slave_method) {
1556 case GST_AUDIO_BASE_SINK_SLAVE_RESAMPLE:
1557 gst_audio_base_sink_resample_slaving (sink, render_start, render_stop,
1558 srender_start, srender_stop);
1559 break;
1560 case GST_AUDIO_BASE_SINK_SLAVE_SKEW:
1561 gst_audio_base_sink_skew_slaving (sink, render_start, render_stop,
1562 srender_start, srender_stop);
1563 break;
1564 case GST_AUDIO_BASE_SINK_SLAVE_NONE:
1565 gst_audio_base_sink_none_slaving (sink, render_start, render_stop,
1566 srender_start, srender_stop);
1567 break;
1568 case GST_AUDIO_BASE_SINK_SLAVE_CUSTOM:
1569 gst_audio_base_sink_custom_slaving (sink, render_start, render_stop,
1570 srender_start, srender_stop);
1571 break;
1572 default:
1573 g_warning ("unknown slaving method %d", sink->priv->slave_method);
1574 break;
1575 }
1576 }
1577
1578 /* must be called with LOCK */
1579 static GstFlowReturn
gst_audio_base_sink_sync_latency(GstBaseSink * bsink,GstMiniObject * obj)1580 gst_audio_base_sink_sync_latency (GstBaseSink * bsink, GstMiniObject * obj)
1581 {
1582 GstClock *clock;
1583 GstClockReturn status;
1584 GstClockTime time, render_delay;
1585 GstFlowReturn ret;
1586 GstAudioBaseSink *sink;
1587 GstClockTime itime, etime;
1588 GstClockTime rate_num, rate_denom;
1589 GstClockTimeDiff jitter;
1590
1591 sink = GST_AUDIO_BASE_SINK (bsink);
1592
1593 clock = GST_ELEMENT_CLOCK (sink);
1594 if (G_UNLIKELY (clock == NULL))
1595 goto no_clock;
1596
1597 /* we provided the global clock, don't need to do anything special */
1598 if (clock == sink->provided_clock)
1599 goto no_slaving;
1600
1601 GST_OBJECT_UNLOCK (sink);
1602
1603 do {
1604 GST_DEBUG_OBJECT (sink, "checking preroll");
1605
1606 ret = gst_base_sink_do_preroll (bsink, obj);
1607 if (ret != GST_FLOW_OK)
1608 goto flushing;
1609
1610 GST_OBJECT_LOCK (sink);
1611 time = sink->priv->us_latency;
1612 GST_OBJECT_UNLOCK (sink);
1613
1614 /* Renderdelay is added onto our own latency, and needs
1615 * to be subtracted as well */
1616 render_delay = gst_base_sink_get_render_delay (bsink);
1617
1618 if (G_LIKELY (time > render_delay))
1619 time -= render_delay;
1620 else
1621 time = 0;
1622
1623 /* preroll done, we can sync since we are in PLAYING now. */
1624 GST_DEBUG_OBJECT (sink, "possibly waiting for clock to reach %"
1625 GST_TIME_FORMAT, GST_TIME_ARGS (time));
1626
1627 /* wait for the clock, this can be interrupted because we got shut down or
1628 * we PAUSED. */
1629 status = gst_base_sink_wait_clock (bsink, time, &jitter);
1630
1631 GST_DEBUG_OBJECT (sink, "clock returned %d %" GST_TIME_FORMAT, status,
1632 GST_TIME_ARGS (jitter));
1633
1634 /* invalid time, no clock or sync disabled, just continue then */
1635 if (status == GST_CLOCK_BADTIME)
1636 break;
1637
1638 /* waiting could have been interrupted and we can be flushing now */
1639 if (G_UNLIKELY (bsink->flushing))
1640 goto flushing;
1641
1642 /* retry if we got unscheduled, which means we did not reach the timeout
1643 * yet. if some other error occures, we continue. */
1644 } while (status == GST_CLOCK_UNSCHEDULED);
1645
1646 GST_DEBUG_OBJECT (sink, "latency synced");
1647
1648 /* We might need to take the object lock within gst_audio_clock_get_time(),
1649 * so call that before we take it again */
1650 itime = gst_audio_clock_get_time (GST_AUDIO_CLOCK (sink->provided_clock));
1651 itime =
1652 gst_audio_clock_adjust (GST_AUDIO_CLOCK (sink->provided_clock), itime);
1653
1654 GST_OBJECT_LOCK (sink);
1655
1656 /* when we prerolled in time, we can accurately set the calibration,
1657 * our internal clock should exactly have been the latency (== the running
1658 * time of the external clock) */
1659 etime = GST_ELEMENT_CAST (sink)->base_time + time;
1660
1661 if (status == GST_CLOCK_EARLY) {
1662 /* when we prerolled late, we have to take into account the lateness */
1663 GST_DEBUG_OBJECT (sink, "late preroll, adding jitter");
1664 etime += jitter;
1665 }
1666
1667 /* start ringbuffer so we can start slaving right away when we need to */
1668 gst_audio_base_sink_force_start (sink);
1669
1670 GST_DEBUG_OBJECT (sink,
1671 "internal time: %" GST_TIME_FORMAT " external time: %" GST_TIME_FORMAT,
1672 GST_TIME_ARGS (itime), GST_TIME_ARGS (etime));
1673
1674 /* copy the original calibrated rate but update the internal and external
1675 * times. */
1676 gst_clock_get_calibration (sink->provided_clock, NULL, NULL, &rate_num,
1677 &rate_denom);
1678 gst_clock_set_calibration (sink->provided_clock, itime, etime,
1679 rate_num, rate_denom);
1680
1681 switch (sink->priv->slave_method) {
1682 case GST_AUDIO_BASE_SINK_SLAVE_RESAMPLE:
1683 /* only set as master when we are resampling */
1684 GST_DEBUG_OBJECT (sink, "Setting clock as master");
1685 gst_clock_set_master (sink->provided_clock, clock);
1686 break;
1687 case GST_AUDIO_BASE_SINK_SLAVE_SKEW:
1688 case GST_AUDIO_BASE_SINK_SLAVE_NONE:
1689 case GST_AUDIO_BASE_SINK_SLAVE_CUSTOM:
1690 default:
1691 break;
1692 }
1693
1694 gst_audio_base_sink_reset_sync (sink);
1695
1696 gst_audio_base_sink_custom_cb_report_discont (sink,
1697 GST_AUDIO_BASE_SINK_DISCONT_REASON_SYNC_LATENCY);
1698
1699 return GST_FLOW_OK;
1700
1701 /* ERRORS */
1702 no_clock:
1703 {
1704 GST_DEBUG_OBJECT (sink, "we have no clock");
1705 return GST_FLOW_OK;
1706 }
1707 no_slaving:
1708 {
1709 GST_DEBUG_OBJECT (sink, "we are not slaved");
1710 return GST_FLOW_OK;
1711 }
1712 flushing:
1713 {
1714 GST_DEBUG_OBJECT (sink, "we are flushing");
1715 GST_OBJECT_LOCK (sink);
1716 return GST_FLOW_FLUSHING;
1717 }
1718 }
1719
1720 static gint64
gst_audio_base_sink_get_alignment(GstAudioBaseSink * sink,GstClockTime sample_offset)1721 gst_audio_base_sink_get_alignment (GstAudioBaseSink * sink,
1722 GstClockTime sample_offset)
1723 {
1724 GstAudioRingBuffer *ringbuf = sink->ringbuffer;
1725 gint64 align;
1726 gint64 sample_diff;
1727 gint64 max_sample_diff;
1728 gint segdone = g_atomic_int_get (&ringbuf->segdone) - ringbuf->segbase;
1729 gint64 samples_done = segdone * (gint64) ringbuf->samples_per_seg;
1730 gint64 headroom = sample_offset - samples_done;
1731 gboolean allow_align = TRUE;
1732 gboolean discont = FALSE;
1733 gint rate;
1734
1735 /* now try to align the sample to the previous one. */
1736
1737 /* calc align with previous sample and determine how big the
1738 * difference is. */
1739 align = sink->next_sample - sample_offset;
1740 sample_diff = ABS (align);
1741
1742 /* calculate the max allowed drift in units of samples. */
1743 rate = GST_AUDIO_INFO_RATE (&ringbuf->spec.info);
1744 max_sample_diff = gst_util_uint64_scale_int (sink->priv->alignment_threshold,
1745 rate, GST_SECOND);
1746
1747 /* don't align if it means writing behind the read-segment */
1748 if (sample_diff > headroom && align < 0)
1749 allow_align = FALSE;
1750
1751 if (G_UNLIKELY (sample_diff >= max_sample_diff)) {
1752 /* wait before deciding to make a discontinuity */
1753 if (sink->priv->discont_wait > 0) {
1754 GstClockTime time = gst_util_uint64_scale_int (sample_offset,
1755 GST_SECOND, rate);
1756 if (sink->priv->discont_time == -1) {
1757 /* discont candidate */
1758 sink->priv->discont_time = time;
1759 } else if (time - sink->priv->discont_time >= sink->priv->discont_wait) {
1760 /* discont_wait expired, discontinuity detected */
1761 discont = TRUE;
1762 sink->priv->discont_time = -1;
1763 }
1764 } else {
1765 discont = TRUE;
1766 }
1767 } else if (G_UNLIKELY (sink->priv->discont_time != -1)) {
1768 /* we have had a discont, but are now back on track! */
1769 sink->priv->discont_time = -1;
1770 }
1771
1772 if (G_LIKELY (!discont && allow_align)) {
1773 GST_DEBUG_OBJECT (sink,
1774 "align with prev sample, ABS (%" G_GINT64_FORMAT ") < %"
1775 G_GINT64_FORMAT, align, max_sample_diff);
1776 } else {
1777 gint64 diff_s G_GNUC_UNUSED;
1778
1779 /* calculate sample diff in seconds for error message */
1780 diff_s = gst_util_uint64_scale_int (sample_diff, GST_SECOND, rate);
1781
1782 /* timestamps drifted apart from previous samples too much, we need to
1783 * resync. We log this as an element warning. */
1784 GST_WARNING_OBJECT (sink,
1785 "Unexpected discontinuity in audio timestamps of "
1786 "%s%" GST_TIME_FORMAT ", resyncing",
1787 sample_offset > sink->next_sample ? "+" : "-", GST_TIME_ARGS (diff_s));
1788 align = 0;
1789
1790 gst_audio_base_sink_custom_cb_report_discont (sink,
1791 GST_AUDIO_BASE_SINK_DISCONT_REASON_ALIGNMENT);
1792 }
1793
1794 return align;
1795 }
1796
1797 static GstFlowReturn
gst_audio_base_sink_render(GstBaseSink * bsink,GstBuffer * buf)1798 gst_audio_base_sink_render (GstBaseSink * bsink, GstBuffer * buf)
1799 {
1800 GstClockTime time, stop, render_start, render_stop, sample_offset;
1801 GstClockTimeDiff sync_offset, ts_offset;
1802 GstAudioBaseSinkClass *bclass;
1803 GstAudioBaseSink *sink;
1804 GstAudioRingBuffer *ringbuf;
1805 gint64 diff, align;
1806 guint64 ctime, cstop;
1807 gsize offset;
1808 GstMapInfo info;
1809 gsize size;
1810 guint samples, written;
1811 gint bpf, rate;
1812 gint accum;
1813 gint out_samples;
1814 GstClockTime base_time, render_delay, latency;
1815 GstClock *clock;
1816 gboolean sync, slaved, align_next;
1817 GstFlowReturn ret;
1818 GstSegment clip_seg;
1819 gint64 time_offset;
1820 GstBuffer *out = NULL;
1821
1822 sink = GST_AUDIO_BASE_SINK (bsink);
1823 bclass = GST_AUDIO_BASE_SINK_GET_CLASS (sink);
1824
1825 ringbuf = sink->ringbuffer;
1826
1827 /* can't do anything when we don't have the device */
1828 if (G_UNLIKELY (!gst_audio_ring_buffer_is_acquired (ringbuf)))
1829 goto wrong_state;
1830
1831 /* Wait for upstream latency before starting the ringbuffer, we do this so
1832 * that we can align the first sample of the ringbuffer to the base_time +
1833 * latency. */
1834 GST_OBJECT_LOCK (sink);
1835 base_time = GST_ELEMENT_CAST (sink)->base_time;
1836 if (G_UNLIKELY (sink->priv->sync_latency)) {
1837 ret = gst_audio_base_sink_sync_latency (bsink, GST_MINI_OBJECT_CAST (buf));
1838 GST_OBJECT_UNLOCK (sink);
1839 if (G_UNLIKELY (ret != GST_FLOW_OK))
1840 goto sync_latency_failed;
1841 /* only do this once until we are set back to PLAYING */
1842 sink->priv->sync_latency = FALSE;
1843 } else {
1844 GST_OBJECT_UNLOCK (sink);
1845 }
1846
1847 /* Before we go on, let's see if we need to payload the data. If yes, we also
1848 * need to unref the output buffer before leaving. */
1849 if (bclass->payload) {
1850 out = bclass->payload (sink, buf);
1851
1852 if (!out)
1853 goto payload_failed;
1854
1855 buf = out;
1856 }
1857
1858 bpf = GST_AUDIO_INFO_BPF (&ringbuf->spec.info);
1859 rate = GST_AUDIO_INFO_RATE (&ringbuf->spec.info);
1860
1861 size = gst_buffer_get_size (buf);
1862 if (G_UNLIKELY (size % bpf) != 0)
1863 goto wrong_size;
1864
1865 samples = size / bpf;
1866
1867 time = GST_BUFFER_TIMESTAMP (buf);
1868
1869 /* Last ditch attempt to ensure that we only play silence if
1870 * we are in trickmode no-audio mode (or if a buffer is marked as a GAP)
1871 * by dropping the buffer contents and rendering as a gap event instead */
1872 if (G_UNLIKELY ((bsink->segment.flags & GST_SEGMENT_FLAG_TRICKMODE_NO_AUDIO)
1873 || (buf && GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_GAP)))) {
1874 GstClockTime duration;
1875 GstEvent *event;
1876 GstBaseSinkClass *bclass;
1877 GST_DEBUG_OBJECT (bsink,
1878 "Received GAP or ignoring audio for trickplay. Dropping contents");
1879
1880 duration = gst_util_uint64_scale_int (samples, GST_SECOND, rate);
1881 event = gst_event_new_gap (time, duration);
1882
1883 bclass = GST_BASE_SINK_GET_CLASS (bsink);
1884 ret = bclass->wait_event (bsink, event);
1885 gst_event_unref (event);
1886
1887 /* Ensure we'll resync on the next buffer as if discont */
1888 sink->next_sample = -1;
1889 goto done;
1890 }
1891
1892 GST_DEBUG_OBJECT (sink,
1893 "time %" GST_TIME_FORMAT ", start %"
1894 GST_TIME_FORMAT ", samples %u", GST_TIME_ARGS (time),
1895 GST_TIME_ARGS (bsink->segment.start), samples);
1896
1897 offset = 0;
1898
1899 /* if not valid timestamp or we can't clip or sync, try to play
1900 * sample ASAP */
1901 if (!GST_CLOCK_TIME_IS_VALID (time)) {
1902 render_start = gst_audio_base_sink_get_offset (sink);
1903 render_stop = render_start + samples;
1904 GST_DEBUG_OBJECT (sink, "Buffer of size %" G_GSIZE_FORMAT " has no time."
1905 " Using render_start=%" G_GUINT64_FORMAT, size, render_start);
1906 /* we don't have a start so we don't know stop either */
1907 stop = -1;
1908 goto no_align;
1909 }
1910
1911 /* let's calc stop based on the number of samples in the buffer instead
1912 * of trusting the DURATION */
1913 stop = time + gst_util_uint64_scale_int (samples, GST_SECOND, rate);
1914
1915 /* prepare the clipping segment. Since we will be subtracting ts-offset and
1916 * device-delay later we scale the start and stop with those values so that we
1917 * can correctly clip them */
1918 clip_seg.format = GST_FORMAT_TIME;
1919 clip_seg.start = bsink->segment.start;
1920 clip_seg.stop = bsink->segment.stop;
1921 clip_seg.duration = -1;
1922
1923 /* the sync offset is the combination of ts-offset and device-delay */
1924 latency = gst_base_sink_get_latency (bsink);
1925 ts_offset = gst_base_sink_get_ts_offset (bsink);
1926 render_delay = gst_base_sink_get_render_delay (bsink);
1927 sync_offset = ts_offset - render_delay + latency;
1928
1929 GST_DEBUG_OBJECT (sink,
1930 "sync-offset %" GST_STIME_FORMAT ", render-delay %" GST_TIME_FORMAT
1931 ", ts-offset %" GST_STIME_FORMAT, GST_STIME_ARGS (sync_offset),
1932 GST_TIME_ARGS (render_delay), GST_STIME_ARGS (ts_offset));
1933
1934 /* compensate for ts-offset and device-delay when negative we need to
1935 * clip. */
1936 if (G_UNLIKELY (sync_offset < 0)) {
1937 clip_seg.start += -sync_offset;
1938 if (clip_seg.stop != -1)
1939 clip_seg.stop += -sync_offset;
1940 }
1941
1942 /* samples should be rendered based on their timestamp. All samples
1943 * arriving before the segment.start or after segment.stop are to be
1944 * thrown away. All samples should also be clipped to the segment
1945 * boundaries */
1946 if (G_UNLIKELY (!gst_segment_clip (&clip_seg, GST_FORMAT_TIME, time, stop,
1947 &ctime, &cstop)))
1948 goto out_of_segment;
1949
1950 /* see if some clipping happened */
1951 diff = ctime - time;
1952 if (G_UNLIKELY (diff > 0)) {
1953 /* bring clipped time to samples */
1954 diff = gst_util_uint64_scale_int (diff, rate, GST_SECOND);
1955 GST_DEBUG_OBJECT (sink, "clipping start to %" GST_TIME_FORMAT " %"
1956 G_GUINT64_FORMAT " samples", GST_TIME_ARGS (ctime), diff);
1957 samples -= diff;
1958 offset += diff * bpf;
1959 time = ctime;
1960 }
1961 diff = stop - cstop;
1962 if (G_UNLIKELY (diff > 0)) {
1963 /* bring clipped time to samples */
1964 diff = gst_util_uint64_scale_int (diff, rate, GST_SECOND);
1965 GST_DEBUG_OBJECT (sink, "clipping stop to %" GST_TIME_FORMAT " %"
1966 G_GUINT64_FORMAT " samples", GST_TIME_ARGS (cstop), diff);
1967 samples -= diff;
1968 stop = cstop;
1969 }
1970
1971 /* figure out how to sync */
1972 if (G_LIKELY ((clock = GST_ELEMENT_CLOCK (bsink))))
1973 sync = bsink->sync;
1974 else
1975 sync = FALSE;
1976
1977 if (G_UNLIKELY (!sync)) {
1978 /* no sync needed, play sample ASAP */
1979 render_start = gst_audio_base_sink_get_offset (sink);
1980 render_stop = render_start + samples;
1981 GST_DEBUG_OBJECT (sink,
1982 "no sync needed. Using render_start=%" G_GUINT64_FORMAT, render_start);
1983 goto no_align;
1984 }
1985
1986 /* bring buffer start and stop times to running time */
1987 render_start =
1988 gst_segment_to_running_time (&bsink->segment, GST_FORMAT_TIME, time);
1989 render_stop =
1990 gst_segment_to_running_time (&bsink->segment, GST_FORMAT_TIME, stop);
1991
1992 GST_DEBUG_OBJECT (sink,
1993 "running: start %" GST_TIME_FORMAT " - stop %" GST_TIME_FORMAT,
1994 GST_TIME_ARGS (render_start), GST_TIME_ARGS (render_stop));
1995
1996 /* store the time of the last sample, we'll use this to perform sync on the
1997 * last sample when draining the buffer */
1998 if (G_LIKELY (bsink->segment.rate >= 0.0)) {
1999 sink->priv->eos_time = render_stop;
2000 } else {
2001 sink->priv->eos_time = render_start;
2002 }
2003
2004 if (G_UNLIKELY (sync_offset != 0)) {
2005 /* compensate for ts-offset and delay. We know this will not underflow
2006 * because we clipped above. */
2007 GST_DEBUG_OBJECT (sink,
2008 "compensating for sync-offset %" GST_TIME_FORMAT,
2009 GST_TIME_ARGS (sync_offset));
2010 render_start += sync_offset;
2011 render_stop += sync_offset;
2012 }
2013
2014 if (base_time != 0) {
2015 GST_DEBUG_OBJECT (sink, "adding base_time %" GST_TIME_FORMAT,
2016 GST_TIME_ARGS (base_time));
2017
2018 /* add base time to sync against the clock */
2019 render_start += base_time;
2020 render_stop += base_time;
2021 }
2022
2023 if (G_UNLIKELY ((slaved = (clock != sink->provided_clock)))) {
2024 /* handle clock slaving */
2025 gst_audio_base_sink_handle_slaving (sink, render_start, render_stop,
2026 &render_start, &render_stop);
2027 } else {
2028 /* no slaving needed but we need to adapt to the clock calibration
2029 * parameters */
2030 gst_audio_base_sink_none_slaving (sink, render_start, render_stop,
2031 &render_start, &render_stop);
2032 }
2033
2034 GST_DEBUG_OBJECT (sink,
2035 "final timestamps: start %" GST_TIME_FORMAT " - stop %" GST_TIME_FORMAT,
2036 GST_TIME_ARGS (render_start), GST_TIME_ARGS (render_stop));
2037
2038 /* bring to position in the ringbuffer */
2039 time_offset = GST_AUDIO_CLOCK_CAST (sink->provided_clock)->time_offset;
2040
2041 if (G_UNLIKELY (time_offset != 0)) {
2042 GST_DEBUG_OBJECT (sink,
2043 "apply time offset %" GST_STIME_FORMAT, GST_STIME_ARGS (time_offset));
2044
2045 if (render_start > time_offset)
2046 render_start -= time_offset;
2047 else
2048 render_start = 0;
2049 if (render_stop > time_offset)
2050 render_stop -= time_offset;
2051 else
2052 render_stop = 0;
2053 }
2054
2055 /* in some clock slaving cases, all late samples end up at 0 first,
2056 * and subsequent ones align with that until threshold exceeded,
2057 * and then sync back to 0 and so on, so avoid that altogether */
2058 if (G_UNLIKELY (render_start == 0 && render_stop == 0))
2059 goto too_late;
2060
2061 /* and bring the time to the rate corrected offset in the buffer */
2062 render_start = gst_util_uint64_scale_int (render_start, rate, GST_SECOND);
2063 render_stop = gst_util_uint64_scale_int (render_stop, rate, GST_SECOND);
2064
2065 /* If the slaving got us an interval spanning 0, render_start will
2066 have been set to 0. So if render_start is 0, we check whether
2067 render_stop is set to contain all samples. If not, we need to
2068 drop samples to match. */
2069 if (render_start == 0) {
2070 guint nsamples = render_stop - render_start;
2071 if (nsamples < samples) {
2072 guint diff;
2073
2074 diff = samples - nsamples;
2075 GST_DEBUG_OBJECT (bsink, "Clipped start: %u/%u samples", nsamples,
2076 samples);
2077 samples -= diff;
2078 offset += diff * bpf;
2079 }
2080 }
2081
2082 /* positive playback rate, first sample is render_start, negative rate, first
2083 * sample is render_stop. When no rate conversion is active, render exactly
2084 * the amount of input samples to avoid aligning to rounding errors. */
2085 if (G_LIKELY (bsink->segment.rate >= 0.0)) {
2086 sample_offset = render_start;
2087 if (G_LIKELY (bsink->segment.rate == 1.0))
2088 render_stop = sample_offset + samples;
2089 } else {
2090 sample_offset = render_stop;
2091 if (bsink->segment.rate == -1.0)
2092 render_start = sample_offset + samples;
2093 }
2094
2095 /* always resync after a discont */
2096 if (G_UNLIKELY (GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_DISCONT) ||
2097 GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_RESYNC))) {
2098 GST_DEBUG_OBJECT (sink, "resync after discont/resync");
2099 goto no_align;
2100 }
2101
2102 /* resync when we don't know what to align the sample with */
2103 if (G_UNLIKELY (sink->next_sample == -1)) {
2104 GST_DEBUG_OBJECT (sink,
2105 "no align possible: no previous sample position known");
2106 goto no_align;
2107 }
2108
2109 align = gst_audio_base_sink_get_alignment (sink, sample_offset);
2110 sink->priv->last_align = align;
2111
2112 /* apply alignment */
2113 render_start += align;
2114
2115 /* only align stop if we are not slaved to resample */
2116 if (G_UNLIKELY (slaved
2117 && sink->priv->slave_method == GST_AUDIO_BASE_SINK_SLAVE_RESAMPLE)) {
2118 GST_DEBUG_OBJECT (sink, "no stop time align needed: we are slaved");
2119 goto no_align;
2120 }
2121 render_stop += align;
2122
2123 no_align:
2124 /* number of target samples is difference between start and stop */
2125 out_samples = render_stop - render_start;
2126
2127 /* we render the first or last sample first, depending on the rate */
2128 if (G_LIKELY (bsink->segment.rate >= 0.0))
2129 sample_offset = render_start;
2130 else
2131 sample_offset = render_stop;
2132
2133 GST_DEBUG_OBJECT (sink, "rendering at %" G_GUINT64_FORMAT " %d/%d",
2134 sample_offset, samples, out_samples);
2135
2136 /* we need to accumulate over different runs for when we get interrupted */
2137 accum = 0;
2138 align_next = TRUE;
2139 gst_buffer_map (buf, &info, GST_MAP_READ);
2140 do {
2141 written =
2142 gst_audio_ring_buffer_commit (ringbuf, &sample_offset,
2143 info.data + offset, samples, out_samples, &accum);
2144
2145 GST_DEBUG_OBJECT (sink, "wrote %u of %u", written, samples);
2146 /* if we wrote all, we're done */
2147 if (G_LIKELY (written == samples))
2148 break;
2149
2150 /* else something interrupted us and we wait for preroll. */
2151 if ((ret = gst_base_sink_wait_preroll (bsink)) != GST_FLOW_OK)
2152 goto stopping;
2153
2154 /* if we got interrupted, we cannot assume that the next sample should
2155 * be aligned to this one */
2156 align_next = FALSE;
2157
2158 /* update the output samples. FIXME, this will just skip them when pausing
2159 * during trick mode */
2160 if (out_samples > written) {
2161 out_samples -= written;
2162 accum = 0;
2163 } else
2164 break;
2165
2166 samples -= written;
2167 offset += written * bpf;
2168 } while (TRUE);
2169 gst_buffer_unmap (buf, &info);
2170
2171 if (G_LIKELY (align_next))
2172 sink->next_sample = sample_offset;
2173 else
2174 sink->next_sample = -1;
2175
2176 GST_DEBUG_OBJECT (sink, "next sample expected at %" G_GUINT64_FORMAT,
2177 sink->next_sample);
2178
2179 if (G_UNLIKELY (GST_CLOCK_TIME_IS_VALID (stop)
2180 && stop >= bsink->segment.stop)) {
2181 GST_DEBUG_OBJECT (sink,
2182 "start playback because we are at the end of segment");
2183 gst_audio_base_sink_force_start (sink);
2184 }
2185
2186 ret = GST_FLOW_OK;
2187
2188 done:
2189 if (out)
2190 gst_buffer_unref (out);
2191
2192 return ret;
2193
2194 /* SPECIAL cases */
2195 out_of_segment:
2196 {
2197 GST_DEBUG_OBJECT (sink,
2198 "dropping sample out of segment time %" GST_TIME_FORMAT ", start %"
2199 GST_TIME_FORMAT, GST_TIME_ARGS (time),
2200 GST_TIME_ARGS (bsink->segment.start));
2201 ret = GST_FLOW_OK;
2202 goto done;
2203 }
2204 too_late:
2205 {
2206 GST_DEBUG_OBJECT (sink, "dropping late sample");
2207 ret = GST_FLOW_OK;
2208 goto done;
2209 }
2210 /* ERRORS */
2211 payload_failed:
2212 {
2213 GST_ELEMENT_ERROR (sink, STREAM, FORMAT, (NULL), ("failed to payload."));
2214 ret = GST_FLOW_ERROR;
2215 goto done;
2216 }
2217 wrong_state:
2218 {
2219 GST_DEBUG_OBJECT (sink, "ringbuffer not negotiated");
2220 GST_ELEMENT_ERROR (sink, STREAM, FORMAT, (NULL), ("sink not negotiated."));
2221 ret = GST_FLOW_NOT_NEGOTIATED;
2222 goto done;
2223 }
2224 wrong_size:
2225 {
2226 GST_DEBUG_OBJECT (sink, "wrong size");
2227 GST_ELEMENT_ERROR (sink, STREAM, WRONG_TYPE,
2228 (NULL), ("sink received buffer of wrong size."));
2229 ret = GST_FLOW_ERROR;
2230 goto done;
2231 }
2232 stopping:
2233 {
2234 GST_DEBUG_OBJECT (sink, "preroll got interrupted: %d (%s)", ret,
2235 gst_flow_get_name (ret));
2236 gst_buffer_unmap (buf, &info);
2237 goto done;
2238 }
2239 sync_latency_failed:
2240 {
2241 GST_DEBUG_OBJECT (sink, "failed waiting for latency");
2242 goto done;
2243 }
2244 }
2245
2246 /**
2247 * gst_audio_base_sink_create_ringbuffer:
2248 * @sink: a #GstAudioBaseSink.
2249 *
2250 * Create and return the #GstAudioRingBuffer for @sink. This function will
2251 * call the ::create_ringbuffer vmethod and will set @sink as the parent of
2252 * the returned buffer (see gst_object_set_parent()).
2253 *
2254 * Returns: (transfer none): The new ringbuffer of @sink.
2255 */
2256 GstAudioRingBuffer *
gst_audio_base_sink_create_ringbuffer(GstAudioBaseSink * sink)2257 gst_audio_base_sink_create_ringbuffer (GstAudioBaseSink * sink)
2258 {
2259 GstAudioBaseSinkClass *bclass;
2260 GstAudioRingBuffer *buffer = NULL;
2261
2262 bclass = GST_AUDIO_BASE_SINK_GET_CLASS (sink);
2263 if (bclass->create_ringbuffer)
2264 buffer = bclass->create_ringbuffer (sink);
2265
2266 if (buffer)
2267 gst_object_set_parent (GST_OBJECT (buffer), GST_OBJECT (sink));
2268
2269 return buffer;
2270 }
2271
2272 static void
gst_audio_base_sink_callback(GstAudioRingBuffer * rbuf,guint8 * data,guint len,gpointer user_data)2273 gst_audio_base_sink_callback (GstAudioRingBuffer * rbuf, guint8 * data,
2274 guint len, gpointer user_data)
2275 {
2276 GstBaseSink *basesink;
2277 GstAudioBaseSink *sink;
2278 GstBuffer *buf = NULL;
2279 GstFlowReturn ret;
2280 gsize size;
2281
2282 basesink = GST_BASE_SINK (user_data);
2283 sink = GST_AUDIO_BASE_SINK (user_data);
2284
2285 GST_PAD_STREAM_LOCK (basesink->sinkpad);
2286
2287 /* would be nice to arrange for pad_alloc_buffer to return data -- as it is we
2288 * will copy twice, once into data, once into DMA */
2289 GST_LOG_OBJECT (basesink, "pulling %u bytes offset %" G_GUINT64_FORMAT
2290 " to fill audio buffer", len, basesink->offset);
2291 ret =
2292 gst_pad_pull_range (basesink->sinkpad, basesink->segment.position, len,
2293 &buf);
2294
2295 if (ret != GST_FLOW_OK) {
2296 if (ret == GST_FLOW_EOS)
2297 goto eos;
2298 else
2299 goto error;
2300 }
2301
2302 GST_BASE_SINK_PREROLL_LOCK (basesink);
2303 if (basesink->flushing)
2304 goto flushing;
2305
2306 /* complete preroll and wait for PLAYING */
2307 ret = gst_base_sink_do_preroll (basesink, GST_MINI_OBJECT_CAST (buf));
2308 if (ret != GST_FLOW_OK)
2309 goto preroll_error;
2310
2311 size = gst_buffer_get_size (buf);
2312
2313 if (len != size) {
2314 GST_INFO_OBJECT (basesink,
2315 "got different size than requested from sink pad: %u"
2316 " != %" G_GSIZE_FORMAT, len, size);
2317 len = MIN (size, len);
2318 }
2319
2320 basesink->segment.position += len;
2321
2322 gst_buffer_extract (buf, 0, data, len);
2323 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
2324
2325 GST_PAD_STREAM_UNLOCK (basesink->sinkpad);
2326
2327 return;
2328
2329 error:
2330 {
2331 GST_WARNING_OBJECT (basesink, "Got flow '%s' but can't return it: %d",
2332 gst_flow_get_name (ret), ret);
2333 gst_audio_ring_buffer_pause (rbuf);
2334 GST_PAD_STREAM_UNLOCK (basesink->sinkpad);
2335 return;
2336 }
2337 eos:
2338 {
2339 /* FIXME: this is not quite correct; we'll be called endlessly until
2340 * the sink gets shut down; maybe we should set a flag somewhere, or
2341 * set segment.stop and segment.duration to the last sample or so */
2342 GST_DEBUG_OBJECT (sink, "EOS");
2343 gst_audio_base_sink_drain (sink);
2344 gst_audio_ring_buffer_pause (rbuf);
2345 gst_element_post_message (GST_ELEMENT_CAST (sink),
2346 gst_message_new_eos (GST_OBJECT_CAST (sink)));
2347 GST_PAD_STREAM_UNLOCK (basesink->sinkpad);
2348 }
2349 flushing:
2350 {
2351 GST_DEBUG_OBJECT (sink, "we are flushing");
2352 gst_audio_ring_buffer_pause (rbuf);
2353 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
2354 GST_PAD_STREAM_UNLOCK (basesink->sinkpad);
2355 return;
2356 }
2357 preroll_error:
2358 {
2359 GST_DEBUG_OBJECT (sink, "error %s", gst_flow_get_name (ret));
2360 gst_audio_ring_buffer_pause (rbuf);
2361 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
2362 GST_PAD_STREAM_UNLOCK (basesink->sinkpad);
2363 return;
2364 }
2365 }
2366
2367 static gboolean
gst_audio_base_sink_activate_pull(GstBaseSink * basesink,gboolean active)2368 gst_audio_base_sink_activate_pull (GstBaseSink * basesink, gboolean active)
2369 {
2370 gboolean ret;
2371 GstAudioBaseSink *sink = GST_AUDIO_BASE_SINK (basesink);
2372
2373 if (active) {
2374 GST_DEBUG_OBJECT (basesink, "activating pull");
2375
2376 gst_audio_ring_buffer_set_callback (sink->ringbuffer,
2377 gst_audio_base_sink_callback, sink);
2378
2379 ret = gst_audio_ring_buffer_activate (sink->ringbuffer, TRUE);
2380 } else {
2381 GST_DEBUG_OBJECT (basesink, "deactivating pull");
2382 gst_audio_ring_buffer_set_callback (sink->ringbuffer, NULL, NULL);
2383 ret = gst_audio_ring_buffer_activate (sink->ringbuffer, FALSE);
2384 }
2385
2386 return ret;
2387 }
2388
2389 static GstStateChangeReturn
gst_audio_base_sink_change_state(GstElement * element,GstStateChange transition)2390 gst_audio_base_sink_change_state (GstElement * element,
2391 GstStateChange transition)
2392 {
2393 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
2394 GstAudioBaseSink *sink = GST_AUDIO_BASE_SINK (element);
2395
2396 switch (transition) {
2397 case GST_STATE_CHANGE_NULL_TO_READY:{
2398 GstAudioRingBuffer *rb;
2399
2400 gst_audio_clock_reset (GST_AUDIO_CLOCK (sink->provided_clock), 0);
2401 rb = gst_audio_base_sink_create_ringbuffer (sink);
2402 if (rb == NULL)
2403 goto create_failed;
2404
2405 GST_OBJECT_LOCK (sink);
2406 sink->ringbuffer = rb;
2407 GST_OBJECT_UNLOCK (sink);
2408
2409 if (!gst_audio_ring_buffer_open_device (sink->ringbuffer)) {
2410 GST_OBJECT_LOCK (sink);
2411 gst_object_unparent (GST_OBJECT_CAST (sink->ringbuffer));
2412 sink->ringbuffer = NULL;
2413 GST_OBJECT_UNLOCK (sink);
2414 goto open_failed;
2415 }
2416 break;
2417 }
2418 case GST_STATE_CHANGE_READY_TO_PAUSED:
2419 gst_audio_base_sink_reset_sync (sink);
2420 gst_audio_ring_buffer_set_flushing (sink->ringbuffer, FALSE);
2421 gst_audio_ring_buffer_may_start (sink->ringbuffer, FALSE);
2422
2423 /* Only post clock-provide messages if this is the clock that
2424 * we've created. If the subclass has overriden it the subclass
2425 * should post this messages whenever necessary */
2426 if (gst_audio_base_sink_is_self_provided_clock (sink))
2427 gst_element_post_message (element,
2428 gst_message_new_clock_provide (GST_OBJECT_CAST (element),
2429 sink->provided_clock, TRUE));
2430 break;
2431 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
2432 {
2433 gboolean eos;
2434
2435 GST_OBJECT_LOCK (sink);
2436 GST_DEBUG_OBJECT (sink, "ringbuffer may start now");
2437 sink->priv->sync_latency = TRUE;
2438 eos = GST_BASE_SINK (sink)->eos;
2439 GST_OBJECT_UNLOCK (sink);
2440
2441 gst_audio_ring_buffer_may_start (sink->ringbuffer, TRUE);
2442 if (GST_BASE_SINK_CAST (sink)->pad_mode == GST_PAD_MODE_PULL ||
2443 g_atomic_int_get (&sink->eos_rendering) || eos) {
2444 /* we always start the ringbuffer in pull mode immediatly */
2445 /* sync rendering on eos needs running clock,
2446 * and others need running clock when finished rendering eos */
2447 gst_audio_ring_buffer_start (sink->ringbuffer);
2448 }
2449 break;
2450 }
2451 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
2452 /* ringbuffer cannot start anymore */
2453 gst_audio_ring_buffer_may_start (sink->ringbuffer, FALSE);
2454 gst_audio_ring_buffer_pause (sink->ringbuffer);
2455
2456 GST_OBJECT_LOCK (sink);
2457 sink->priv->sync_latency = FALSE;
2458 GST_OBJECT_UNLOCK (sink);
2459 break;
2460 case GST_STATE_CHANGE_PAUSED_TO_READY:
2461 /* Only post clock-lost messages if this is the clock that
2462 * we've created. If the subclass has overriden it the subclass
2463 * should post this messages whenever necessary */
2464 if (gst_audio_base_sink_is_self_provided_clock (sink))
2465 gst_element_post_message (element,
2466 gst_message_new_clock_lost (GST_OBJECT_CAST (element),
2467 sink->provided_clock));
2468
2469 /* make sure we unblock before calling the parent state change
2470 * so it can grab the STREAM_LOCK */
2471 gst_audio_ring_buffer_set_flushing (sink->ringbuffer, TRUE);
2472 break;
2473 default:
2474 break;
2475 }
2476
2477 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
2478
2479 switch (transition) {
2480 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
2481 /* stop slaving ourselves to the master, if any */
2482 gst_clock_set_master (sink->provided_clock, NULL);
2483 break;
2484 case GST_STATE_CHANGE_PAUSED_TO_READY:
2485 gst_audio_ring_buffer_activate (sink->ringbuffer, FALSE);
2486 gst_audio_ring_buffer_release (sink->ringbuffer);
2487 break;
2488 case GST_STATE_CHANGE_READY_TO_NULL:
2489 /* we release again here because the acquire happens when setting the
2490 * caps, which happens before we commit the state to PAUSED and thus the
2491 * PAUSED->READY state change (see above, where we release the ringbuffer)
2492 * might not be called when we get here. */
2493 gst_audio_ring_buffer_activate (sink->ringbuffer, FALSE);
2494 gst_audio_ring_buffer_release (sink->ringbuffer);
2495 gst_audio_ring_buffer_close_device (sink->ringbuffer);
2496 GST_OBJECT_LOCK (sink);
2497 gst_object_unparent (GST_OBJECT_CAST (sink->ringbuffer));
2498 sink->ringbuffer = NULL;
2499 GST_OBJECT_UNLOCK (sink);
2500 break;
2501 default:
2502 break;
2503 }
2504
2505 return ret;
2506
2507 /* ERRORS */
2508 create_failed:
2509 {
2510 /* subclass must post a meaningful error message */
2511 GST_DEBUG_OBJECT (sink, "create failed");
2512 return GST_STATE_CHANGE_FAILURE;
2513 }
2514 open_failed:
2515 {
2516 /* subclass must post a meaningful error message */
2517 GST_DEBUG_OBJECT (sink, "open failed");
2518 return GST_STATE_CHANGE_FAILURE;
2519 }
2520 }
2521