2 * Copyright (C) 2005 Wim Taymans <wim@fluendo.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * SECTION:gstaudioringbuffer
22 * @short_description: Base class for audio ringbuffer implementations
23 * @see_also: #GstAudioBaseSink, #GstAudioSink
27 * This object is the base class for audio ringbuffers used by the base
28 * audio source and sink classes.
31 * The ringbuffer abstracts a circular buffer of data. One reader and
32 * one writer can operate on the data from different threads in a lockfree
33 * manner. The base class is sufficiently flexible to be used as an
34 * abstraction for DMA based ringbuffers as well as a pure software
39 * Last reviewed on 2006-02-02 (0.10.4)
44 #include "gstaudioringbuffer.h"
46 GST_DEBUG_CATEGORY_STATIC (gst_audio_ring_buffer_debug);
47 #define GST_CAT_DEFAULT gst_audio_ring_buffer_debug
49 static void gst_audio_ring_buffer_dispose (GObject * object);
50 static void gst_audio_ring_buffer_finalize (GObject * object);
52 static gboolean gst_audio_ring_buffer_pause_unlocked (GstAudioRingBuffer * buf);
53 static void default_clear_all (GstAudioRingBuffer * buf);
54 static guint default_commit (GstAudioRingBuffer * buf, guint64 * sample,
55 guchar * data, gint in_samples, gint out_samples, gint * accum);
57 /* ringbuffer abstract base class */
58 G_DEFINE_ABSTRACT_TYPE (GstAudioRingBuffer, gst_audio_ring_buffer,
62 gst_audio_ring_buffer_class_init (GstAudioRingBufferClass * klass)
64 GObjectClass *gobject_class;
65 GstAudioRingBufferClass *gstaudioringbuffer_class;
67 gobject_class = (GObjectClass *) klass;
68 gstaudioringbuffer_class = (GstAudioRingBufferClass *) klass;
70 GST_DEBUG_CATEGORY_INIT (gst_audio_ring_buffer_debug, "ringbuffer", 0,
73 gobject_class->dispose = gst_audio_ring_buffer_dispose;
74 gobject_class->finalize = gst_audio_ring_buffer_finalize;
76 gstaudioringbuffer_class->clear_all = GST_DEBUG_FUNCPTR (default_clear_all);
77 gstaudioringbuffer_class->commit = GST_DEBUG_FUNCPTR (default_commit);
81 gst_audio_ring_buffer_init (GstAudioRingBuffer * ringbuffer)
83 ringbuffer->open = FALSE;
84 ringbuffer->acquired = FALSE;
85 ringbuffer->state = GST_AUDIO_RING_BUFFER_STATE_STOPPED;
86 ringbuffer->cond = g_cond_new ();
87 ringbuffer->waiting = 0;
88 ringbuffer->empty_seg = NULL;
89 ringbuffer->flushing = TRUE;
93 gst_audio_ring_buffer_dispose (GObject * object)
95 GstAudioRingBuffer *ringbuffer = GST_AUDIO_RING_BUFFER (object);
97 gst_caps_replace (&ringbuffer->spec.caps, NULL);
99 G_OBJECT_CLASS (gst_audio_ring_buffer_parent_class)->dispose (G_OBJECT
104 gst_audio_ring_buffer_finalize (GObject * object)
106 GstAudioRingBuffer *ringbuffer = GST_AUDIO_RING_BUFFER (object);
108 g_cond_free (ringbuffer->cond);
109 g_free (ringbuffer->empty_seg);
111 G_OBJECT_CLASS (gst_audio_ring_buffer_parent_class)->finalize (G_OBJECT
115 #ifndef GST_DISABLE_GST_DEBUG
116 static const gchar *format_type_names[] = {
131 * gst_audio_ring_buffer_debug_spec_caps:
132 * @spec: the spec to debug
134 * Print debug info about the parsed caps in @spec to the debug log.
137 gst_audio_ring_buffer_debug_spec_caps (GstAudioRingBufferSpec * spec)
143 GST_DEBUG ("spec caps: %p %" GST_PTR_FORMAT, spec->caps, spec->caps);
144 GST_DEBUG ("parsed caps: type: %d, '%s'", spec->type,
145 format_type_names[spec->type]);
147 GST_DEBUG ("parsed caps: width: %d", spec->width);
148 GST_DEBUG ("parsed caps: sign: %d", spec->sign);
149 GST_DEBUG ("parsed caps: bigend: %d", spec->bigend);
150 GST_DEBUG ("parsed caps: rate: %d", spec->rate);
151 GST_DEBUG ("parsed caps: channels: %d", spec->channels);
152 GST_DEBUG ("parsed caps: sample bytes: %d", spec->bytes_per_sample);
153 bytes = (spec->width >> 3) * spec->channels;
154 for (i = 0; i < bytes; i++) {
155 GST_DEBUG ("silence byte %d: %02x", i, spec->silence_sample[i]);
161 * gst_audio_ring_buffer_debug_spec_buff:
162 * @spec: the spec to debug
164 * Print debug info about the buffer sized in @spec to the debug log.
167 gst_audio_ring_buffer_debug_spec_buff (GstAudioRingBufferSpec * spec)
169 gint bpf = GST_AUDIO_INFO_BPF (&spec->info);
171 GST_DEBUG ("acquire ringbuffer: buffer time: %" G_GINT64_FORMAT " usec",
173 GST_DEBUG ("acquire ringbuffer: latency time: %" G_GINT64_FORMAT " usec",
175 GST_DEBUG ("acquire ringbuffer: total segments: %d", spec->segtotal);
176 GST_DEBUG ("acquire ringbuffer: latency segments: %d", spec->seglatency);
177 GST_DEBUG ("acquire ringbuffer: segment size: %d bytes = %d samples",
178 spec->segsize, spec->segsize / bpf);
179 GST_DEBUG ("acquire ringbuffer: buffer size: %d bytes = %d samples",
180 spec->segsize * spec->segtotal, spec->segsize * spec->segtotal / bpf);
184 * gst_audio_ring_buffer_parse_caps:
188 * Parse @caps into @spec.
190 * Returns: TRUE if the caps could be parsed.
193 gst_audio_ring_buffer_parse_caps (GstAudioRingBufferSpec * spec, GstCaps * caps)
195 const gchar *mimetype;
196 GstStructure *structure;
200 structure = gst_caps_get_structure (caps, 0);
201 gst_audio_info_init (&info);
203 /* we have to differentiate between int and float formats */
204 mimetype = gst_structure_get_name (structure);
206 if (g_str_equal (mimetype, "audio/x-raw")) {
207 if (!gst_audio_info_from_caps (&info, caps))
210 spec->type = GST_BUFTYPE_RAW;
211 } else if (g_str_equal (mimetype, "audio/x-alaw")) {
212 /* extract the needed information from the cap */
213 if (!(gst_structure_get_int (structure, "rate", &info.rate) &&
214 gst_structure_get_int (structure, "channels", &info.channels)))
217 spec->type = GST_BUFTYPE_A_LAW;
218 info.bpf = info.channels;
219 } else if (g_str_equal (mimetype, "audio/x-mulaw")) {
220 /* extract the needed information from the cap */
221 if (!(gst_structure_get_int (structure, "rate", &info.rate) &&
222 gst_structure_get_int (structure, "channels", &info.channels)))
225 spec->type = GST_BUFTYPE_MU_LAW;
226 info.bpf = info.channels;
227 } else if (g_str_equal (mimetype, "audio/x-iec958")) {
228 /* extract the needed information from the cap */
229 if (!(gst_structure_get_int (structure, "rate", &info.rate)))
232 spec->type = GST_BUFTYPE_IEC958;
234 } else if (g_str_equal (mimetype, "audio/x-ac3")) {
235 /* extract the needed information from the cap */
236 if (!(gst_structure_get_int (structure, "rate", &info.rate)))
239 spec->type = GST_BUFTYPE_AC3;
241 } else if (g_str_equal (mimetype, "audio/x-eac3")) {
242 /* extract the needed information from the cap */
243 if (!(gst_structure_get_int (structure, "rate", &info.rate)))
246 spec->type = GST_BUFTYPE_EAC3;
248 } else if (g_str_equal (mimetype, "audio/x-dts")) {
249 /* extract the needed information from the cap */
250 if (!(gst_structure_get_int (structure, "rate", &info.rate)))
253 spec->type = GST_BUFTYPE_DTS;
255 } else if (g_str_equal (mimetype, "audio/mpeg") &&
256 gst_structure_get_int (structure, "mpegaudioversion", &i) &&
257 (i == 1 || i == 2)) {
258 /* Now we know this is MPEG-1 or MPEG-2 (non AAC) */
259 /* extract the needed information from the cap */
260 if (!(gst_structure_get_int (structure, "rate", &info.rate)))
263 spec->type = GST_BUFTYPE_MPEG;
269 gst_caps_replace (&spec->caps, caps);
271 g_return_val_if_fail (spec->latency_time != 0, FALSE);
273 /* calculate suggested segsize and segtotal. segsize should be one unit
274 * of 'latency_time' samples, scaling for the fact that latency_time is
275 * currently stored in microseconds (FIXME: in 0.11) */
276 spec->segsize = gst_util_uint64_scale (info.rate * info.bpf,
277 spec->latency_time, GST_SECOND / GST_USECOND);
278 /* Round to an integer number of samples */
279 spec->segsize -= spec->segsize % info.bpf;
281 spec->segtotal = spec->buffer_time / spec->latency_time;
282 /* leave the latency undefined now, implementations can change it but if it's
283 * not changed, we assume the same value as segtotal */
284 spec->seglatency = -1;
288 gst_audio_ring_buffer_debug_spec_caps (spec);
289 gst_audio_ring_buffer_debug_spec_buff (spec);
296 GST_DEBUG ("could not parse caps");
302 * gst_audio_ring_buffer_convert:
303 * @buf: the #GstAudioRingBuffer
304 * @src_fmt: the source format
305 * @src_val: the source value
306 * @dest_fmt: the destination format
307 * @dest_val: a location to store the converted value
309 * Convert @src_val in @src_fmt to the equivalent value in @dest_fmt. The result
310 * will be put in @dest_val.
312 * Returns: TRUE if the conversion succeeded.
317 gst_audio_ring_buffer_convert (GstAudioRingBuffer * buf,
318 GstFormat src_fmt, gint64 src_val, GstFormat dest_fmt, gint64 * dest_val)
322 GST_OBJECT_LOCK (buf);
324 gst_audio_info_convert (&buf->spec.info, src_fmt, src_val, dest_fmt,
326 GST_OBJECT_UNLOCK (buf);
332 * gst_audio_ring_buffer_set_callback:
333 * @buf: the #GstAudioRingBuffer to set the callback on
334 * @cb: the callback to set
335 * @user_data: user data passed to the callback
337 * Sets the given callback function on the buffer. This function
338 * will be called every time a segment has been written to a device.
343 gst_audio_ring_buffer_set_callback (GstAudioRingBuffer * buf,
344 GstAudioRingBufferCallback cb, gpointer user_data)
346 g_return_if_fail (GST_IS_AUDIO_RING_BUFFER (buf));
348 GST_OBJECT_LOCK (buf);
350 buf->cb_data = user_data;
351 GST_OBJECT_UNLOCK (buf);
356 * gst_audio_ring_buffer_open_device:
357 * @buf: the #GstAudioRingBuffer
359 * Open the audio device associated with the ring buffer. Does not perform any
360 * setup on the device. You must open the device before acquiring the ring
363 * Returns: TRUE if the device could be opened, FALSE on error.
368 gst_audio_ring_buffer_open_device (GstAudioRingBuffer * buf)
371 GstAudioRingBufferClass *rclass;
373 g_return_val_if_fail (GST_IS_AUDIO_RING_BUFFER (buf), FALSE);
375 GST_DEBUG_OBJECT (buf, "opening device");
377 GST_OBJECT_LOCK (buf);
378 if (G_UNLIKELY (buf->open))
383 /* if this fails, something is wrong in this file */
384 g_assert (!buf->acquired);
386 rclass = GST_AUDIO_RING_BUFFER_GET_CLASS (buf);
387 if (G_LIKELY (rclass->open_device))
388 res = rclass->open_device (buf);
390 if (G_UNLIKELY (!res))
393 GST_DEBUG_OBJECT (buf, "opened device");
396 GST_OBJECT_UNLOCK (buf);
403 GST_DEBUG_OBJECT (buf, "Device for ring buffer already open");
404 g_warning ("Device for ring buffer %p already open, fix your code", buf);
411 GST_DEBUG_OBJECT (buf, "failed opening device");
417 * gst_audio_ring_buffer_close_device:
418 * @buf: the #GstAudioRingBuffer
420 * Close the audio device associated with the ring buffer. The ring buffer
421 * should already have been released via gst_audio_ring_buffer_release().
423 * Returns: TRUE if the device could be closed, FALSE on error.
428 gst_audio_ring_buffer_close_device (GstAudioRingBuffer * buf)
431 GstAudioRingBufferClass *rclass;
433 g_return_val_if_fail (GST_IS_AUDIO_RING_BUFFER (buf), FALSE);
435 GST_DEBUG_OBJECT (buf, "closing device");
437 GST_OBJECT_LOCK (buf);
438 if (G_UNLIKELY (!buf->open))
441 if (G_UNLIKELY (buf->acquired))
446 rclass = GST_AUDIO_RING_BUFFER_GET_CLASS (buf);
447 if (G_LIKELY (rclass->close_device))
448 res = rclass->close_device (buf);
450 if (G_UNLIKELY (!res))
453 GST_DEBUG_OBJECT (buf, "closed device");
456 GST_OBJECT_UNLOCK (buf);
463 GST_DEBUG_OBJECT (buf, "Device for ring buffer already closed");
464 g_warning ("Device for ring buffer %p already closed, fix your code", buf);
470 GST_DEBUG_OBJECT (buf, "Resources for ring buffer still acquired");
471 g_critical ("Resources for ring buffer %p still acquired", buf);
478 GST_DEBUG_OBJECT (buf, "error closing device");
484 * gst_audio_ring_buffer_device_is_open:
485 * @buf: the #GstAudioRingBuffer
487 * Checks the status of the device associated with the ring buffer.
489 * Returns: TRUE if the device was open, FALSE if it was closed.
494 gst_audio_ring_buffer_device_is_open (GstAudioRingBuffer * buf)
498 g_return_val_if_fail (GST_IS_AUDIO_RING_BUFFER (buf), FALSE);
500 GST_OBJECT_LOCK (buf);
502 GST_OBJECT_UNLOCK (buf);
508 * gst_audio_ring_buffer_acquire:
509 * @buf: the #GstAudioRingBuffer to acquire
510 * @spec: the specs of the buffer
512 * Allocate the resources for the ringbuffer. This function fills
513 * in the data pointer of the ring buffer with a valid #GstBuffer
514 * to which samples can be written.
516 * Returns: TRUE if the device could be acquired, FALSE on error.
521 gst_audio_ring_buffer_acquire (GstAudioRingBuffer * buf,
522 GstAudioRingBufferSpec * spec)
524 gboolean res = FALSE;
525 GstAudioRingBufferClass *rclass;
528 g_return_val_if_fail (GST_IS_AUDIO_RING_BUFFER (buf), FALSE);
530 GST_DEBUG_OBJECT (buf, "acquiring device %p", buf);
532 GST_OBJECT_LOCK (buf);
533 if (G_UNLIKELY (!buf->open))
536 if (G_UNLIKELY (buf->acquired))
539 buf->acquired = TRUE;
541 rclass = GST_AUDIO_RING_BUFFER_GET_CLASS (buf);
542 if (G_LIKELY (rclass->acquire))
543 res = rclass->acquire (buf, spec);
545 if (G_UNLIKELY (!res))
548 if (G_UNLIKELY ((bpf = buf->spec.info.bpf) == 0))
551 /* if the seglatency was overwritten with something else than -1, use it, else
552 * assume segtotal as the latency */
553 if (buf->spec.seglatency == -1)
554 buf->spec.seglatency = buf->spec.segtotal;
556 segsize = buf->spec.segsize;
558 buf->samples_per_seg = segsize / bpf;
560 /* create an empty segment */
561 g_free (buf->empty_seg);
562 buf->empty_seg = g_malloc (segsize);
564 if (buf->spec.type == GST_BUFTYPE_RAW) {
565 gst_audio_format_fill_silence (buf->spec.info.finfo, buf->empty_seg,
568 /* FIXME, non-raw formats get 0 as the empty sample */
569 memset (buf->empty_seg, 0, segsize);
571 GST_DEBUG_OBJECT (buf, "acquired device");
574 GST_OBJECT_UNLOCK (buf);
581 GST_DEBUG_OBJECT (buf, "device not opened");
582 g_critical ("Device for %p not opened", buf);
589 GST_DEBUG_OBJECT (buf, "device was acquired");
594 buf->acquired = FALSE;
595 GST_DEBUG_OBJECT (buf, "failed to acquire device");
601 ("invalid bytes_per_frame from acquire ringbuffer %p, fix the element",
603 buf->acquired = FALSE;
610 * gst_audio_ring_buffer_release:
611 * @buf: the #GstAudioRingBuffer to release
613 * Free the resources of the ringbuffer.
615 * Returns: TRUE if the device could be released, FALSE on error.
620 gst_audio_ring_buffer_release (GstAudioRingBuffer * buf)
622 gboolean res = FALSE;
623 GstAudioRingBufferClass *rclass;
625 g_return_val_if_fail (GST_IS_AUDIO_RING_BUFFER (buf), FALSE);
627 GST_DEBUG_OBJECT (buf, "releasing device");
629 gst_audio_ring_buffer_stop (buf);
631 GST_OBJECT_LOCK (buf);
632 if (G_UNLIKELY (!buf->acquired))
635 buf->acquired = FALSE;
637 /* if this fails, something is wrong in this file */
638 g_assert (buf->open == TRUE);
640 rclass = GST_AUDIO_RING_BUFFER_GET_CLASS (buf);
641 if (G_LIKELY (rclass->release))
642 res = rclass->release (buf);
644 /* signal any waiters */
645 GST_DEBUG_OBJECT (buf, "signal waiter");
646 GST_AUDIO_RING_BUFFER_SIGNAL (buf);
648 if (G_UNLIKELY (!res))
651 g_free (buf->empty_seg);
652 buf->empty_seg = NULL;
653 GST_DEBUG_OBJECT (buf, "released device");
656 GST_OBJECT_UNLOCK (buf);
664 GST_DEBUG_OBJECT (buf, "device was released");
669 buf->acquired = TRUE;
670 GST_DEBUG_OBJECT (buf, "failed to release device");
676 * gst_audio_ring_buffer_is_acquired:
677 * @buf: the #GstAudioRingBuffer to check
679 * Check if the ringbuffer is acquired and ready to use.
681 * Returns: TRUE if the ringbuffer is acquired, FALSE on error.
686 gst_audio_ring_buffer_is_acquired (GstAudioRingBuffer * buf)
690 g_return_val_if_fail (GST_IS_AUDIO_RING_BUFFER (buf), FALSE);
692 GST_OBJECT_LOCK (buf);
694 GST_OBJECT_UNLOCK (buf);
700 * gst_audio_ring_buffer_activate:
701 * @buf: the #GstAudioRingBuffer to activate
702 * @active: the new mode
704 * Activate @buf to start or stop pulling data.
708 * Returns: TRUE if the device could be activated in the requested mode,
714 gst_audio_ring_buffer_activate (GstAudioRingBuffer * buf, gboolean active)
716 gboolean res = FALSE;
717 GstAudioRingBufferClass *rclass;
719 g_return_val_if_fail (GST_IS_AUDIO_RING_BUFFER (buf), FALSE);
721 GST_DEBUG_OBJECT (buf, "activate device");
723 GST_OBJECT_LOCK (buf);
724 if (G_UNLIKELY (active && !buf->acquired))
727 if (G_UNLIKELY (buf->active == active))
730 rclass = GST_AUDIO_RING_BUFFER_GET_CLASS (buf);
731 /* if there is no activate function we assume it was started/released
732 * in the acquire method */
733 if (G_LIKELY (rclass->activate))
734 res = rclass->activate (buf, active);
738 if (G_UNLIKELY (!res))
739 goto activate_failed;
741 buf->active = active;
744 GST_OBJECT_UNLOCK (buf);
751 GST_DEBUG_OBJECT (buf, "device not acquired");
752 g_critical ("Device for %p not acquired", buf);
759 GST_DEBUG_OBJECT (buf, "device was active in mode %d", active);
764 GST_DEBUG_OBJECT (buf, "failed to activate device");
770 * gst_audio_ring_buffer_is_active:
771 * @buf: the #GstAudioRingBuffer
773 * Check if @buf is activated.
777 * Returns: TRUE if the device is active.
782 gst_audio_ring_buffer_is_active (GstAudioRingBuffer * buf)
786 g_return_val_if_fail (GST_IS_AUDIO_RING_BUFFER (buf), FALSE);
788 GST_OBJECT_LOCK (buf);
790 GST_OBJECT_UNLOCK (buf);
797 * gst_audio_ring_buffer_set_flushing:
798 * @buf: the #GstAudioRingBuffer to flush
799 * @flushing: the new mode
801 * Set the ringbuffer to flushing mode or normal mode.
806 gst_audio_ring_buffer_set_flushing (GstAudioRingBuffer * buf, gboolean flushing)
808 g_return_if_fail (GST_IS_AUDIO_RING_BUFFER (buf));
810 GST_OBJECT_LOCK (buf);
811 buf->flushing = flushing;
814 gst_audio_ring_buffer_pause_unlocked (buf);
816 gst_audio_ring_buffer_clear_all (buf);
818 GST_OBJECT_UNLOCK (buf);
822 * gst_audio_ring_buffer_start:
823 * @buf: the #GstAudioRingBuffer to start
825 * Start processing samples from the ringbuffer.
827 * Returns: TRUE if the device could be started, FALSE on error.
832 gst_audio_ring_buffer_start (GstAudioRingBuffer * buf)
834 gboolean res = FALSE;
835 GstAudioRingBufferClass *rclass;
836 gboolean resume = FALSE;
838 g_return_val_if_fail (GST_IS_AUDIO_RING_BUFFER (buf), FALSE);
840 GST_DEBUG_OBJECT (buf, "starting ringbuffer");
842 GST_OBJECT_LOCK (buf);
843 if (G_UNLIKELY (buf->flushing))
846 if (G_UNLIKELY (!buf->acquired))
849 if (G_UNLIKELY (g_atomic_int_get (&buf->may_start) == FALSE))
852 /* if stopped, set to started */
853 res = g_atomic_int_compare_and_exchange (&buf->state,
854 GST_AUDIO_RING_BUFFER_STATE_STOPPED, GST_AUDIO_RING_BUFFER_STATE_STARTED);
857 GST_DEBUG_OBJECT (buf, "was not stopped, try paused");
858 /* was not stopped, try from paused */
859 res = g_atomic_int_compare_and_exchange (&buf->state,
860 GST_AUDIO_RING_BUFFER_STATE_PAUSED,
861 GST_AUDIO_RING_BUFFER_STATE_STARTED);
863 /* was not paused either, must be started then */
865 GST_DEBUG_OBJECT (buf, "was not paused, must have been started");
869 GST_DEBUG_OBJECT (buf, "resuming");
872 rclass = GST_AUDIO_RING_BUFFER_GET_CLASS (buf);
874 if (G_LIKELY (rclass->resume))
875 res = rclass->resume (buf);
877 if (G_LIKELY (rclass->start))
878 res = rclass->start (buf);
881 if (G_UNLIKELY (!res)) {
882 buf->state = GST_AUDIO_RING_BUFFER_STATE_PAUSED;
883 GST_DEBUG_OBJECT (buf, "failed to start");
885 GST_DEBUG_OBJECT (buf, "started");
889 GST_OBJECT_UNLOCK (buf);
895 GST_DEBUG_OBJECT (buf, "we are flushing");
896 GST_OBJECT_UNLOCK (buf);
901 GST_DEBUG_OBJECT (buf, "we are not acquired");
902 GST_OBJECT_UNLOCK (buf);
907 GST_DEBUG_OBJECT (buf, "we may not start");
908 GST_OBJECT_UNLOCK (buf);
914 gst_audio_ring_buffer_pause_unlocked (GstAudioRingBuffer * buf)
916 gboolean res = FALSE;
917 GstAudioRingBufferClass *rclass;
919 GST_DEBUG_OBJECT (buf, "pausing ringbuffer");
921 /* if started, set to paused */
922 res = g_atomic_int_compare_and_exchange (&buf->state,
923 GST_AUDIO_RING_BUFFER_STATE_STARTED, GST_AUDIO_RING_BUFFER_STATE_PAUSED);
928 /* signal any waiters */
929 GST_DEBUG_OBJECT (buf, "signal waiter");
930 GST_AUDIO_RING_BUFFER_SIGNAL (buf);
932 rclass = GST_AUDIO_RING_BUFFER_GET_CLASS (buf);
933 if (G_LIKELY (rclass->pause))
934 res = rclass->pause (buf);
936 if (G_UNLIKELY (!res)) {
937 buf->state = GST_AUDIO_RING_BUFFER_STATE_STARTED;
938 GST_DEBUG_OBJECT (buf, "failed to pause");
940 GST_DEBUG_OBJECT (buf, "paused");
947 /* was not started */
948 GST_DEBUG_OBJECT (buf, "was not started");
954 * gst_audio_ring_buffer_pause:
955 * @buf: the #GstAudioRingBuffer to pause
957 * Pause processing samples from the ringbuffer.
959 * Returns: TRUE if the device could be paused, FALSE on error.
964 gst_audio_ring_buffer_pause (GstAudioRingBuffer * buf)
966 gboolean res = FALSE;
968 g_return_val_if_fail (GST_IS_AUDIO_RING_BUFFER (buf), FALSE);
970 GST_OBJECT_LOCK (buf);
971 if (G_UNLIKELY (buf->flushing))
974 if (G_UNLIKELY (!buf->acquired))
977 res = gst_audio_ring_buffer_pause_unlocked (buf);
978 GST_OBJECT_UNLOCK (buf);
985 GST_DEBUG_OBJECT (buf, "we are flushing");
986 GST_OBJECT_UNLOCK (buf);
991 GST_DEBUG_OBJECT (buf, "not acquired");
992 GST_OBJECT_UNLOCK (buf);
998 * gst_audio_ring_buffer_stop:
999 * @buf: the #GstAudioRingBuffer to stop
1001 * Stop processing samples from the ringbuffer.
1003 * Returns: TRUE if the device could be stopped, FALSE on error.
1008 gst_audio_ring_buffer_stop (GstAudioRingBuffer * buf)
1010 gboolean res = FALSE;
1011 GstAudioRingBufferClass *rclass;
1013 g_return_val_if_fail (GST_IS_AUDIO_RING_BUFFER (buf), FALSE);
1015 GST_DEBUG_OBJECT (buf, "stopping");
1017 GST_OBJECT_LOCK (buf);
1019 /* if started, set to stopped */
1020 res = g_atomic_int_compare_and_exchange (&buf->state,
1021 GST_AUDIO_RING_BUFFER_STATE_STARTED, GST_AUDIO_RING_BUFFER_STATE_STOPPED);
1024 GST_DEBUG_OBJECT (buf, "was not started, try paused");
1025 /* was not started, try from paused */
1026 res = g_atomic_int_compare_and_exchange (&buf->state,
1027 GST_AUDIO_RING_BUFFER_STATE_PAUSED,
1028 GST_AUDIO_RING_BUFFER_STATE_STOPPED);
1030 /* was not paused either, must have been stopped then */
1032 GST_DEBUG_OBJECT (buf, "was not paused, must have been stopped");
1037 /* signal any waiters */
1038 GST_DEBUG_OBJECT (buf, "signal waiter");
1039 GST_AUDIO_RING_BUFFER_SIGNAL (buf);
1041 rclass = GST_AUDIO_RING_BUFFER_GET_CLASS (buf);
1042 if (G_LIKELY (rclass->stop))
1043 res = rclass->stop (buf);
1045 if (G_UNLIKELY (!res)) {
1046 buf->state = GST_AUDIO_RING_BUFFER_STATE_STARTED;
1047 GST_DEBUG_OBJECT (buf, "failed to stop");
1049 GST_DEBUG_OBJECT (buf, "stopped");
1052 GST_OBJECT_UNLOCK (buf);
1058 * gst_audio_ring_buffer_delay:
1059 * @buf: the #GstAudioRingBuffer to query
1061 * Get the number of samples queued in the audio device. This is
1062 * usually less than the segment size but can be bigger when the
1063 * implementation uses another internal buffer between the audio
1066 * For playback ringbuffers this is the amount of samples transfered from the
1067 * ringbuffer to the device but still not played.
1069 * For capture ringbuffers this is the amount of samples in the device that are
1070 * not yet transfered to the ringbuffer.
1072 * Returns: The number of samples queued in the audio device.
1077 gst_audio_ring_buffer_delay (GstAudioRingBuffer * buf)
1079 GstAudioRingBufferClass *rclass;
1082 g_return_val_if_fail (GST_IS_AUDIO_RING_BUFFER (buf), 0);
1084 /* buffer must be acquired */
1085 if (G_UNLIKELY (!gst_audio_ring_buffer_is_acquired (buf)))
1088 rclass = GST_AUDIO_RING_BUFFER_GET_CLASS (buf);
1089 if (G_LIKELY (rclass->delay))
1090 res = rclass->delay (buf);
1098 GST_DEBUG_OBJECT (buf, "not acquired");
1104 * gst_audio_ring_buffer_samples_done:
1105 * @buf: the #GstAudioRingBuffer to query
1107 * Get the number of samples that were processed by the ringbuffer
1108 * since it was last started. This does not include the number of samples not
1109 * yet processed (see gst_audio_ring_buffer_delay()).
1111 * Returns: The number of samples processed by the ringbuffer.
1116 gst_audio_ring_buffer_samples_done (GstAudioRingBuffer * buf)
1121 g_return_val_if_fail (GST_IS_AUDIO_RING_BUFFER (buf), 0);
1123 /* get the amount of segments we processed */
1124 segdone = g_atomic_int_get (&buf->segdone);
1126 /* convert to samples */
1127 samples = ((guint64) segdone) * buf->samples_per_seg;
1133 * gst_audio_ring_buffer_set_sample:
1134 * @buf: the #GstAudioRingBuffer to use
1135 * @sample: the sample number to set
1137 * Make sure that the next sample written to the device is
1138 * accounted for as being the @sample sample written to the
1139 * device. This value will be used in reporting the current
1140 * sample position of the ringbuffer.
1142 * This function will also clear the buffer with silence.
1147 gst_audio_ring_buffer_set_sample (GstAudioRingBuffer * buf, guint64 sample)
1149 g_return_if_fail (GST_IS_AUDIO_RING_BUFFER (buf));
1154 if (G_UNLIKELY (buf->samples_per_seg == 0))
1157 /* FIXME, we assume the ringbuffer can restart at a random
1158 * position, round down to the beginning and keep track of
1159 * offset when calculating the processed samples. */
1160 buf->segbase = buf->segdone - sample / buf->samples_per_seg;
1162 gst_audio_ring_buffer_clear_all (buf);
1164 GST_DEBUG_OBJECT (buf, "set sample to %" G_GUINT64_FORMAT ", segbase %d",
1165 sample, buf->segbase);
1169 default_clear_all (GstAudioRingBuffer * buf)
1173 /* not fatal, we just are not negotiated yet */
1174 if (G_UNLIKELY (buf->spec.segtotal <= 0))
1177 GST_DEBUG_OBJECT (buf, "clear all segments");
1179 for (i = 0; i < buf->spec.segtotal; i++) {
1180 gst_audio_ring_buffer_clear (buf, i);
1185 * gst_audio_ring_buffer_clear_all:
1186 * @buf: the #GstAudioRingBuffer to clear
1188 * Fill the ringbuffer with silence.
1193 gst_audio_ring_buffer_clear_all (GstAudioRingBuffer * buf)
1195 GstAudioRingBufferClass *rclass;
1197 g_return_if_fail (GST_IS_AUDIO_RING_BUFFER (buf));
1199 rclass = GST_AUDIO_RING_BUFFER_GET_CLASS (buf);
1201 if (G_LIKELY (rclass->clear_all))
1202 rclass->clear_all (buf);
1207 wait_segment (GstAudioRingBuffer * buf)
1210 gboolean wait = TRUE;
1212 /* buffer must be started now or we deadlock since nobody is reading */
1213 if (G_UNLIKELY (g_atomic_int_get (&buf->state) !=
1214 GST_AUDIO_RING_BUFFER_STATE_STARTED)) {
1215 /* see if we are allowed to start it */
1216 if (G_UNLIKELY (g_atomic_int_get (&buf->may_start) == FALSE))
1219 GST_DEBUG_OBJECT (buf, "start!");
1220 segments = g_atomic_int_get (&buf->segdone);
1221 gst_audio_ring_buffer_start (buf);
1223 /* After starting, the writer may have wrote segments already and then we
1224 * don't need to wait anymore */
1225 if (G_LIKELY (g_atomic_int_get (&buf->segdone) != segments))
1229 /* take lock first, then update our waiting flag */
1230 GST_OBJECT_LOCK (buf);
1231 if (G_UNLIKELY (buf->flushing))
1234 if (G_UNLIKELY (g_atomic_int_get (&buf->state) !=
1235 GST_AUDIO_RING_BUFFER_STATE_STARTED))
1238 if (G_LIKELY (wait)) {
1239 if (g_atomic_int_compare_and_exchange (&buf->waiting, 0, 1)) {
1240 GST_DEBUG_OBJECT (buf, "waiting..");
1241 GST_AUDIO_RING_BUFFER_WAIT (buf);
1243 if (G_UNLIKELY (buf->flushing))
1246 if (G_UNLIKELY (g_atomic_int_get (&buf->state) !=
1247 GST_AUDIO_RING_BUFFER_STATE_STARTED))
1251 GST_OBJECT_UNLOCK (buf);
1258 g_atomic_int_compare_and_exchange (&buf->waiting, 1, 0);
1259 GST_DEBUG_OBJECT (buf, "stopped processing");
1260 GST_OBJECT_UNLOCK (buf);
1265 g_atomic_int_compare_and_exchange (&buf->waiting, 1, 0);
1266 GST_DEBUG_OBJECT (buf, "flushing");
1267 GST_OBJECT_UNLOCK (buf);
1272 GST_DEBUG_OBJECT (buf, "not allowed to start");
1277 #define FWD_SAMPLES(s,se,d,de) \
1279 /* no rate conversion */ \
1280 guint towrite = MIN (se + bpf - s, de - d); \
1283 memcpy (d, s, towrite); \
1284 in_samples -= towrite / bpf; \
1285 out_samples -= towrite / bpf; \
1287 GST_DEBUG ("copy %u bytes", towrite); \
1290 /* in_samples >= out_samples, rate > 1.0 */
1291 #define FWD_UP_SAMPLES(s,se,d,de) \
1293 guint8 *sb = s, *db = d; \
1294 while (s <= se && d < de) { \
1296 memcpy (d, s, bpf); \
1299 if ((*accum << 1) >= inr) { \
1304 in_samples -= (s - sb)/bpf; \
1305 out_samples -= (d - db)/bpf; \
1306 GST_DEBUG ("fwd_up end %d/%d",*accum,*toprocess); \
1309 /* out_samples > in_samples, for rates smaller than 1.0 */
1310 #define FWD_DOWN_SAMPLES(s,se,d,de) \
1312 guint8 *sb = s, *db = d; \
1313 while (s <= se && d < de) { \
1315 memcpy (d, s, bpf); \
1318 if ((*accum << 1) >= outr) { \
1323 in_samples -= (s - sb)/bpf; \
1324 out_samples -= (d - db)/bpf; \
1325 GST_DEBUG ("fwd_down end %d/%d",*accum,*toprocess); \
1328 #define REV_UP_SAMPLES(s,se,d,de) \
1330 guint8 *sb = se, *db = d; \
1331 while (s <= se && d < de) { \
1333 memcpy (d, se, bpf); \
1336 while (d < de && (*accum << 1) >= inr) { \
1341 in_samples -= (sb - se)/bpf; \
1342 out_samples -= (d - db)/bpf; \
1343 GST_DEBUG ("rev_up end %d/%d",*accum,*toprocess); \
1346 #define REV_DOWN_SAMPLES(s,se,d,de) \
1348 guint8 *sb = se, *db = d; \
1349 while (s <= se && d < de) { \
1351 memcpy (d, se, bpf); \
1354 while (s <= se && (*accum << 1) >= outr) { \
1359 in_samples -= (sb - se)/bpf; \
1360 out_samples -= (d - db)/bpf; \
1361 GST_DEBUG ("rev_down end %d/%d",*accum,*toprocess); \
1365 default_commit (GstAudioRingBuffer * buf, guint64 * sample,
1366 guchar * data, gint in_samples, gint out_samples, gint * accum)
1369 gint segsize, segtotal, bpf, sps;
1370 guint8 *dest, *data_end;
1371 gint writeseg, sampleoff;
1376 g_return_val_if_fail (buf->memory != NULL, -1);
1377 g_return_val_if_fail (data != NULL, -1);
1380 segsize = buf->spec.segsize;
1381 segtotal = buf->spec.segtotal;
1382 bpf = buf->spec.info.bpf;
1383 sps = buf->samples_per_seg;
1385 reverse = out_samples < 0;
1386 out_samples = ABS (out_samples);
1388 if (in_samples >= out_samples)
1389 toprocess = &in_samples;
1391 toprocess = &out_samples;
1393 inr = in_samples - 1;
1394 outr = out_samples - 1;
1396 /* data_end points to the last sample we have to write, not past it. This is
1397 * needed to properly handle reverse playback: it points to the last sample. */
1398 data_end = data + (bpf * inr);
1400 /* figure out the segment and the offset inside the segment where
1401 * the first sample should be written. */
1402 writeseg = *sample / sps;
1403 sampleoff = (*sample % sps) * bpf;
1405 /* write out all samples */
1406 while (*toprocess > 0) {
1415 /* get the currently processed segment */
1416 segdone = g_atomic_int_get (&buf->segdone) - buf->segbase;
1418 /* see how far away it is from the write segment */
1419 diff = writeseg - segdone;
1422 ("pointer at %d, write to %d-%d, diff %d, segtotal %d, segsize %d, base %d",
1423 segdone, writeseg, sampleoff, diff, segtotal, segsize, buf->segbase);
1425 /* segment too far ahead, writer too slow, we need to drop, hopefully UNLIKELY */
1426 if (G_UNLIKELY (diff < 0)) {
1427 /* we need to drop one segment at a time, pretend we wrote a
1433 /* write segment is within writable range, we can break the loop and
1434 * start writing the data. */
1435 if (diff < segtotal) {
1440 /* else we need to wait for the segment to become writable. */
1441 if (!wait_segment (buf))
1445 /* we can write now */
1446 ws = writeseg % segtotal;
1447 avail = MIN (segsize - sampleoff, bpf * out_samples);
1449 d = dest + (ws * segsize) + sampleoff;
1451 *sample += avail / bpf;
1453 GST_DEBUG_OBJECT (buf, "write @%p seg %d, sps %d, off %d, avail %d",
1454 dest + ws * segsize, ws, sps, sampleoff, avail);
1456 if (G_LIKELY (inr == outr && !reverse)) {
1457 /* no rate conversion, simply copy samples */
1458 FWD_SAMPLES (data, data_end, d, d_end);
1459 } else if (!reverse) {
1461 /* forward speed up */
1462 FWD_UP_SAMPLES (data, data_end, d, d_end);
1464 /* forward slow down */
1465 FWD_DOWN_SAMPLES (data, data_end, d, d_end);
1468 /* reverse speed up */
1469 REV_UP_SAMPLES (data, data_end, d, d_end);
1471 /* reverse slow down */
1472 REV_DOWN_SAMPLES (data, data_end, d, d_end);
1475 /* for the next iteration we write to the next segment at the beginning. */
1479 /* we consumed all samples here */
1480 data = data_end + bpf;
1483 return inr - ((data_end - data) / bpf);
1488 GST_DEBUG_OBJECT (buf, "stopped processing");
1494 * gst_audio_ring_buffer_commit:
1495 * @buf: the #GstAudioRingBuffer to commit
1496 * @sample: the sample position of the data
1497 * @data: the data to commit
1498 * @in_samples: the number of samples in the data to commit
1499 * @out_samples: the number of samples to write to the ringbuffer
1500 * @accum: accumulator for rate conversion.
1502 * Commit @in_samples samples pointed to by @data to the ringbuffer @buf.
1504 * @in_samples and @out_samples define the rate conversion to perform on the
1505 * samples in @data. For negative rates, @out_samples must be negative and
1506 * @in_samples positive.
1508 * When @out_samples is positive, the first sample will be written at position @sample
1509 * in the ringbuffer. When @out_samples is negative, the last sample will be written to
1510 * @sample in reverse order.
1512 * @out_samples does not need to be a multiple of the segment size of the ringbuffer
1513 * although it is recommended for optimal performance.
1515 * @accum will hold a temporary accumulator used in rate conversion and should be
1516 * set to 0 when this function is first called. In case the commit operation is
1517 * interrupted, one can resume the processing by passing the previously returned
1518 * @accum value back to this function.
1522 * Returns: The number of samples written to the ringbuffer or -1 on error. The
1523 * number of samples written can be less than @out_samples when @buf was interrupted
1524 * with a flush or stop.
1529 gst_audio_ring_buffer_commit (GstAudioRingBuffer * buf, guint64 * sample,
1530 guchar * data, gint in_samples, gint out_samples, gint * accum)
1532 GstAudioRingBufferClass *rclass;
1535 g_return_val_if_fail (GST_IS_AUDIO_RING_BUFFER (buf), -1);
1537 if (G_UNLIKELY (in_samples == 0 || out_samples == 0))
1540 rclass = GST_AUDIO_RING_BUFFER_GET_CLASS (buf);
1542 if (G_LIKELY (rclass->commit))
1543 res = rclass->commit (buf, sample, data, in_samples, out_samples, accum);
1549 * gst_audio_ring_buffer_read:
1550 * @buf: the #GstAudioRingBuffer to read from
1551 * @sample: the sample position of the data
1552 * @data: where the data should be read
1553 * @len: the number of samples in data to read
1555 * Read @len samples from the ringbuffer into the memory pointed
1557 * The first sample should be read from position @sample in
1560 * @len should not be a multiple of the segment size of the ringbuffer
1561 * although it is recommended.
1563 * Returns: The number of samples read from the ringbuffer or -1 on
1569 gst_audio_ring_buffer_read (GstAudioRingBuffer * buf, guint64 sample,
1570 guchar * data, guint len)
1573 gint segsize, segtotal, bpf, sps;
1577 g_return_val_if_fail (GST_IS_AUDIO_RING_BUFFER (buf), -1);
1578 g_return_val_if_fail (buf->memory != NULL, -1);
1579 g_return_val_if_fail (data != NULL, -1);
1582 segsize = buf->spec.segsize;
1583 segtotal = buf->spec.segtotal;
1584 bpf = buf->spec.info.bpf;
1585 sps = buf->samples_per_seg;
1588 /* read enough samples */
1589 while (to_read > 0) {
1591 gint readseg, sampleoff;
1593 /* figure out the segment and the offset inside the segment where
1594 * the sample should be read from. */
1595 readseg = sample / sps;
1596 sampleoff = (sample % sps);
1601 /* get the currently processed segment */
1602 segdone = g_atomic_int_get (&buf->segdone) - buf->segbase;
1604 /* see how far away it is from the read segment, normally segdone (where
1605 * the hardware is writing) is bigger than readseg (where software is
1607 diff = segdone - readseg;
1610 ("pointer at %d, sample %" G_GUINT64_FORMAT
1611 ", read from %d-%d, to_read %d, diff %d, segtotal %d, segsize %d",
1612 segdone, sample, readseg, sampleoff, to_read, diff, segtotal,
1615 /* segment too far ahead, reader too slow */
1616 if (G_UNLIKELY (diff >= segtotal)) {
1617 /* pretend we read an empty segment. */
1618 sampleslen = MIN (sps, to_read);
1619 memcpy (data, buf->empty_seg, sampleslen * bpf);
1623 /* read segment is within readable range, we can break the loop and
1624 * start reading the data. */
1628 /* else we need to wait for the segment to become readable. */
1629 if (!wait_segment (buf))
1633 /* we can read now */
1634 readseg = readseg % segtotal;
1635 sampleslen = MIN (sps - sampleoff, to_read);
1637 GST_DEBUG_OBJECT (buf, "read @%p seg %d, off %d, sampleslen %d",
1638 dest + readseg * segsize, readseg, sampleoff, sampleslen);
1640 memcpy (data, dest + (readseg * segsize) + (sampleoff * bpf),
1641 (sampleslen * bpf));
1644 to_read -= sampleslen;
1645 sample += sampleslen;
1646 data += sampleslen * bpf;
1649 return len - to_read;
1654 GST_DEBUG_OBJECT (buf, "stopped processing");
1655 return len - to_read;
1660 * gst_audio_ring_buffer_prepare_read:
1661 * @buf: the #GstAudioRingBuffer to read from
1662 * @segment: the segment to read
1663 * @readptr: the pointer to the memory where samples can be read
1664 * @len: the number of bytes to read
1666 * Returns a pointer to memory where the data from segment @segment
1667 * can be found. This function is mostly used by subclasses.
1669 * Returns: FALSE if the buffer is not started.
1674 gst_audio_ring_buffer_prepare_read (GstAudioRingBuffer * buf, gint * segment,
1675 guint8 ** readptr, gint * len)
1680 g_return_val_if_fail (GST_IS_AUDIO_RING_BUFFER (buf), FALSE);
1682 if (buf->callback == NULL) {
1683 /* push mode, fail when nothing is started */
1684 if (g_atomic_int_get (&buf->state) != GST_AUDIO_RING_BUFFER_STATE_STARTED)
1688 g_return_val_if_fail (buf->memory != NULL, FALSE);
1689 g_return_val_if_fail (segment != NULL, FALSE);
1690 g_return_val_if_fail (readptr != NULL, FALSE);
1691 g_return_val_if_fail (len != NULL, FALSE);
1695 /* get the position of the pointer */
1696 segdone = g_atomic_int_get (&buf->segdone);
1698 *segment = segdone % buf->spec.segtotal;
1699 *len = buf->spec.segsize;
1700 *readptr = data + *segment * *len;
1702 GST_LOG ("prepare read from segment %d (real %d) @%p",
1703 *segment, segdone, *readptr);
1705 /* callback to fill the memory with data, for pull based
1708 buf->callback (buf, *readptr, *len, buf->cb_data);
1714 * gst_audio_ring_buffer_advance:
1715 * @buf: the #GstAudioRingBuffer to advance
1716 * @advance: the number of segments written
1718 * Subclasses should call this function to notify the fact that
1719 * @advance segments are now processed by the device.
1724 gst_audio_ring_buffer_advance (GstAudioRingBuffer * buf, guint advance)
1726 g_return_if_fail (GST_IS_AUDIO_RING_BUFFER (buf));
1728 /* update counter */
1729 g_atomic_int_add (&buf->segdone, advance);
1731 /* the lock is already taken when the waiting flag is set,
1732 * we grab the lock as well to make sure the waiter is actually
1733 * waiting for the signal */
1734 if (g_atomic_int_compare_and_exchange (&buf->waiting, 1, 0)) {
1735 GST_OBJECT_LOCK (buf);
1736 GST_DEBUG_OBJECT (buf, "signal waiter");
1737 GST_AUDIO_RING_BUFFER_SIGNAL (buf);
1738 GST_OBJECT_UNLOCK (buf);
1743 * gst_audio_ring_buffer_clear:
1744 * @buf: the #GstAudioRingBuffer to clear
1745 * @segment: the segment to clear
1747 * Clear the given segment of the buffer with silence samples.
1748 * This function is used by subclasses.
1753 gst_audio_ring_buffer_clear (GstAudioRingBuffer * buf, gint segment)
1757 g_return_if_fail (GST_IS_AUDIO_RING_BUFFER (buf));
1759 /* no data means it's already cleared */
1760 if (G_UNLIKELY (buf->memory == NULL))
1763 /* no empty_seg means it's not opened */
1764 if (G_UNLIKELY (buf->empty_seg == NULL))
1767 segment %= buf->spec.segtotal;
1770 data += segment * buf->spec.segsize;
1772 GST_LOG ("clear segment %d @%p", segment, data);
1774 memcpy (data, buf->empty_seg, buf->spec.segsize);
1778 * gst_audio_ring_buffer_may_start:
1779 * @buf: the #GstAudioRingBuffer
1780 * @allowed: the new value
1782 * Tell the ringbuffer that it is allowed to start playback when
1783 * the ringbuffer is filled with samples.
1790 gst_audio_ring_buffer_may_start (GstAudioRingBuffer * buf, gboolean allowed)
1792 g_return_if_fail (GST_IS_AUDIO_RING_BUFFER (buf));
1794 GST_LOG_OBJECT (buf, "may start: %d", allowed);
1795 g_atomic_int_set (&buf->may_start, allowed);