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:gstringbuffer
22 * @short_description: Base class for audio ringbuffer implementations
23 * @see_also: #GstBaseAudioSink, #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 "gstringbuffer.h"
46 #include "gst/glib-compat-private.h"
48 GST_DEBUG_CATEGORY_STATIC (gst_ring_buffer_debug);
49 #define GST_CAT_DEFAULT gst_ring_buffer_debug
51 static void gst_ring_buffer_dispose (GObject * object);
52 static void gst_ring_buffer_finalize (GObject * object);
54 static gboolean gst_ring_buffer_pause_unlocked (GstRingBuffer * buf);
55 static void default_clear_all (GstRingBuffer * buf);
56 static guint default_commit (GstRingBuffer * buf, guint64 * sample,
57 guchar * data, gint in_samples, gint out_samples, gint * accum);
59 /* ringbuffer abstract base class */
60 G_DEFINE_ABSTRACT_TYPE (GstRingBuffer, gst_ring_buffer, GST_TYPE_OBJECT);
63 gst_ring_buffer_class_init (GstRingBufferClass * klass)
65 GObjectClass *gobject_class;
66 GstRingBufferClass *gstringbuffer_class;
68 gobject_class = (GObjectClass *) klass;
69 gstringbuffer_class = (GstRingBufferClass *) klass;
71 GST_DEBUG_CATEGORY_INIT (gst_ring_buffer_debug, "ringbuffer", 0,
74 gobject_class->dispose = gst_ring_buffer_dispose;
75 gobject_class->finalize = gst_ring_buffer_finalize;
77 gstringbuffer_class->clear_all = GST_DEBUG_FUNCPTR (default_clear_all);
78 gstringbuffer_class->commit = GST_DEBUG_FUNCPTR (default_commit);
82 gst_ring_buffer_init (GstRingBuffer * ringbuffer)
84 ringbuffer->open = FALSE;
85 ringbuffer->acquired = FALSE;
86 ringbuffer->state = GST_RING_BUFFER_STATE_STOPPED;
87 ringbuffer->cond = g_cond_new ();
88 ringbuffer->waiting = 0;
89 ringbuffer->empty_seg = NULL;
90 ringbuffer->abidata.ABI.flushing = TRUE;
94 gst_ring_buffer_dispose (GObject * object)
96 GstRingBuffer *ringbuffer = GST_RING_BUFFER (object);
98 gst_caps_replace (&ringbuffer->spec.caps, NULL);
100 G_OBJECT_CLASS (gst_ring_buffer_parent_class)->dispose (G_OBJECT
105 gst_ring_buffer_finalize (GObject * object)
107 GstRingBuffer *ringbuffer = GST_RING_BUFFER (object);
109 g_cond_free (ringbuffer->cond);
110 g_free (ringbuffer->empty_seg);
112 G_OBJECT_CLASS (gst_ring_buffer_parent_class)->finalize (G_OBJECT
118 const GstBufferFormat format;
119 const guint8 silence[4];
122 static const FormatDef linear_defs[4 * 2 * 2] = {
123 {GST_S8, {0x00, 0x00, 0x00, 0x00}},
124 {GST_S8, {0x00, 0x00, 0x00, 0x00}},
125 {GST_U8, {0x80, 0x80, 0x80, 0x80}},
126 {GST_U8, {0x80, 0x80, 0x80, 0x80}},
127 {GST_S16_LE, {0x00, 0x00, 0x00, 0x00}},
128 {GST_S16_BE, {0x00, 0x00, 0x00, 0x00}},
129 {GST_U16_LE, {0x00, 0x80, 0x00, 0x80}},
130 {GST_U16_BE, {0x80, 0x00, 0x80, 0x00}},
131 {GST_S24_LE, {0x00, 0x00, 0x00, 0x00}},
132 {GST_S24_BE, {0x00, 0x00, 0x00, 0x00}},
133 {GST_U24_LE, {0x00, 0x00, 0x80, 0x00}},
134 {GST_U24_BE, {0x80, 0x00, 0x00, 0x00}},
135 {GST_S32_LE, {0x00, 0x00, 0x00, 0x00}},
136 {GST_S32_BE, {0x00, 0x00, 0x00, 0x00}},
137 {GST_U32_LE, {0x00, 0x00, 0x00, 0x80}},
138 {GST_U32_BE, {0x80, 0x00, 0x00, 0x00}}
141 static const FormatDef linear24_defs[3 * 2 * 2] = {
142 {GST_S24_3LE, {0x00, 0x00, 0x00, 0x00}},
143 {GST_S24_3BE, {0x00, 0x00, 0x00, 0x00}},
144 {GST_U24_3LE, {0x00, 0x00, 0x80, 0x00}},
145 {GST_U24_3BE, {0x80, 0x00, 0x00, 0x00}},
146 {GST_S20_3LE, {0x00, 0x00, 0x00, 0x00}},
147 {GST_S20_3BE, {0x00, 0x00, 0x00, 0x00}},
148 {GST_U20_3LE, {0x00, 0x00, 0x08, 0x00}},
149 {GST_U20_3BE, {0x08, 0x00, 0x00, 0x00}},
150 {GST_S18_3LE, {0x00, 0x00, 0x00, 0x00}},
151 {GST_S18_3BE, {0x00, 0x00, 0x00, 0x00}},
152 {GST_U18_3LE, {0x00, 0x00, 0x02, 0x00}},
153 {GST_U18_3BE, {0x02, 0x00, 0x00, 0x00}}
156 static const FormatDef *
157 build_linear_format (int depth, int width, int unsignd, int big_endian)
159 const FormatDef *formats;
164 formats = &linear24_defs[0];
167 formats = &linear24_defs[4];
170 formats = &linear24_defs[8];
178 formats = &linear_defs[0];
181 formats = &linear_defs[4];
184 formats = &linear_defs[8];
187 formats = &linear_defs[12];
201 #ifndef GST_DISABLE_GST_DEBUG
202 static const gchar *format_type_names[] = {
216 static const gchar *format_names[] = {
261 * gst_ring_buffer_debug_spec_caps:
262 * @spec: the spec to debug
264 * Print debug info about the parsed caps in @spec to the debug log.
267 gst_ring_buffer_debug_spec_caps (GstRingBufferSpec * spec)
271 GST_DEBUG ("spec caps: %p %" GST_PTR_FORMAT, spec->caps, spec->caps);
272 GST_DEBUG ("parsed caps: type: %d, '%s'", spec->type,
273 format_type_names[spec->type]);
274 GST_DEBUG ("parsed caps: format: %d, '%s'", spec->format,
275 format_names[spec->format]);
276 GST_DEBUG ("parsed caps: width: %d", spec->width);
277 GST_DEBUG ("parsed caps: depth: %d", spec->depth);
278 GST_DEBUG ("parsed caps: sign: %d", spec->sign);
279 GST_DEBUG ("parsed caps: bigend: %d", spec->bigend);
280 GST_DEBUG ("parsed caps: rate: %d", spec->rate);
281 GST_DEBUG ("parsed caps: channels: %d", spec->channels);
282 GST_DEBUG ("parsed caps: sample bytes: %d", spec->bytes_per_sample);
283 bytes = (spec->width >> 3) * spec->channels;
284 for (i = 0; i < bytes; i++) {
285 GST_DEBUG ("silence byte %d: %02x", i, spec->silence_sample[i]);
290 * gst_ring_buffer_debug_spec_buff:
291 * @spec: the spec to debug
293 * Print debug info about the buffer sized in @spec to the debug log.
296 gst_ring_buffer_debug_spec_buff (GstRingBufferSpec * spec)
298 GST_DEBUG ("acquire ringbuffer: buffer time: %" G_GINT64_FORMAT " usec",
300 GST_DEBUG ("acquire ringbuffer: latency time: %" G_GINT64_FORMAT " usec",
302 GST_DEBUG ("acquire ringbuffer: total segments: %d", spec->segtotal);
303 GST_DEBUG ("acquire ringbuffer: latency segments: %d", spec->seglatency);
304 GST_DEBUG ("acquire ringbuffer: segment size: %d bytes = %d samples",
305 spec->segsize, spec->segsize / spec->bytes_per_sample);
306 GST_DEBUG ("acquire ringbuffer: buffer size: %d bytes = %d samples",
307 spec->segsize * spec->segtotal,
308 spec->segsize * spec->segtotal / spec->bytes_per_sample);
312 * gst_ring_buffer_parse_caps:
316 * Parse @caps into @spec.
318 * Returns: TRUE if the caps could be parsed.
321 gst_ring_buffer_parse_caps (GstRingBufferSpec * spec, GstCaps * caps)
323 const gchar *mimetype;
324 GstStructure *structure;
327 structure = gst_caps_get_structure (caps, 0);
329 /* we have to differentiate between int and float formats */
330 mimetype = gst_structure_get_name (structure);
332 if (g_str_equal (mimetype, "audio/x-raw-int")) {
334 const FormatDef *def;
337 spec->type = GST_BUFTYPE_LINEAR;
339 /* extract the needed information from the cap */
340 if (!(gst_structure_get_int (structure, "rate", &spec->rate) &&
341 gst_structure_get_int (structure, "channels", &spec->channels) &&
342 gst_structure_get_int (structure, "width", &spec->width) &&
343 gst_structure_get_int (structure, "depth", &spec->depth) &&
344 gst_structure_get_boolean (structure, "signed", &spec->sign)))
347 /* extract endianness if needed */
348 if (spec->width > 8) {
349 if (!gst_structure_get_int (structure, "endianness", &endianness))
352 endianness = G_BYTE_ORDER;
355 spec->bigend = endianness == G_LITTLE_ENDIAN ? FALSE : TRUE;
357 def = build_linear_format (spec->depth, spec->width, spec->sign ? 0 : 1,
358 spec->bigend ? 1 : 0);
363 spec->format = def->format;
365 bytes = spec->width >> 3;
367 for (i = 0; i < spec->channels; i++) {
368 for (j = 0; j < bytes; j++) {
369 spec->silence_sample[i * bytes + j] = def->silence[j];
372 } else if (g_str_equal (mimetype, "audio/x-raw-float")) {
374 spec->type = GST_BUFTYPE_FLOAT;
376 /* extract the needed information from the cap */
377 if (!(gst_structure_get_int (structure, "rate", &spec->rate) &&
378 gst_structure_get_int (structure, "channels", &spec->channels) &&
379 gst_structure_get_int (structure, "width", &spec->width)))
382 /* match layout to format wrt to endianness */
383 switch (spec->width) {
386 G_BYTE_ORDER == G_LITTLE_ENDIAN ? GST_FLOAT32_LE : GST_FLOAT32_BE;
390 G_BYTE_ORDER == G_LITTLE_ENDIAN ? GST_FLOAT64_LE : GST_FLOAT64_BE;
395 /* float silence is all zeros.. */
396 memset (spec->silence_sample, 0, 32);
397 } else if (g_str_equal (mimetype, "audio/x-alaw")) {
398 /* extract the needed information from the cap */
399 if (!(gst_structure_get_int (structure, "rate", &spec->rate) &&
400 gst_structure_get_int (structure, "channels", &spec->channels)))
403 spec->type = GST_BUFTYPE_A_LAW;
404 spec->format = GST_A_LAW;
407 for (i = 0; i < spec->channels; i++)
408 spec->silence_sample[i] = 0xd5;
409 } else if (g_str_equal (mimetype, "audio/x-mulaw")) {
410 /* extract the needed information from the cap */
411 if (!(gst_structure_get_int (structure, "rate", &spec->rate) &&
412 gst_structure_get_int (structure, "channels", &spec->channels)))
415 spec->type = GST_BUFTYPE_MU_LAW;
416 spec->format = GST_MU_LAW;
419 for (i = 0; i < spec->channels; i++)
420 spec->silence_sample[i] = 0xff;
421 } else if (g_str_equal (mimetype, "audio/x-iec958")) {
422 /* extract the needed information from the cap */
423 if (!(gst_structure_get_int (structure, "rate", &spec->rate)))
426 spec->type = GST_BUFTYPE_IEC958;
427 spec->format = GST_IEC958;
431 } else if (g_str_equal (mimetype, "audio/x-ac3")) {
432 /* extract the needed information from the cap */
433 if (!(gst_structure_get_int (structure, "rate", &spec->rate)))
436 spec->type = GST_BUFTYPE_AC3;
437 spec->format = GST_AC3;
441 } else if (g_str_equal (mimetype, "audio/x-eac3")) {
442 /* extract the needed information from the cap */
443 if (!(gst_structure_get_int (structure, "rate", &spec->rate)))
446 spec->type = GST_BUFTYPE_EAC3;
447 spec->format = GST_EAC3;
451 } else if (g_str_equal (mimetype, "audio/x-dts")) {
452 /* extract the needed information from the cap */
453 if (!(gst_structure_get_int (structure, "rate", &spec->rate)))
456 spec->type = GST_BUFTYPE_DTS;
457 spec->format = GST_DTS;
461 } else if (g_str_equal (mimetype, "audio/mpeg") &&
462 gst_structure_get_int (structure, "mpegaudioversion", &i) &&
463 (i == 1 || i == 2)) {
464 /* Now we know this is MPEG-1 or MPEG-2 (non AAC) */
465 /* extract the needed information from the cap */
466 if (!(gst_structure_get_int (structure, "rate", &spec->rate)))
469 spec->type = GST_BUFTYPE_MPEG;
470 spec->format = GST_MPEG;
478 spec->bytes_per_sample = (spec->width >> 3) * spec->channels;
480 gst_caps_replace (&spec->caps, caps);
482 g_return_val_if_fail (spec->latency_time != 0, FALSE);
484 /* calculate suggested segsize and segtotal. segsize should be one unit
485 * of 'latency_time' samples, scaling for the fact that latency_time is
486 * currently stored in microseconds (FIXME: in 0.11) */
487 spec->segsize = gst_util_uint64_scale (spec->rate * spec->bytes_per_sample,
488 spec->latency_time, GST_SECOND / GST_USECOND);
489 /* Round to an integer number of samples */
490 spec->segsize -= spec->segsize % spec->bytes_per_sample;
492 spec->segtotal = spec->buffer_time / spec->latency_time;
493 /* leave the latency undefined now, implementations can change it but if it's
494 * not changed, we assume the same value as segtotal */
495 spec->seglatency = -1;
497 gst_ring_buffer_debug_spec_caps (spec);
498 gst_ring_buffer_debug_spec_buff (spec);
505 GST_DEBUG ("could not parse caps");
511 * gst_ring_buffer_convert:
512 * @buf: the #GstRingBuffer
513 * @src_fmt: the source format
514 * @src_val: the source value
515 * @dest_fmt: the destination format
516 * @dest_val: a location to store the converted value
518 * Convert @src_val in @src_fmt to the equivalent value in @dest_fmt. The result
519 * will be put in @dest_val.
521 * Returns: TRUE if the conversion succeeded.
526 gst_ring_buffer_convert (GstRingBuffer * buf,
527 GstFormat src_fmt, gint64 src_val, GstFormat dest_fmt, gint64 * dest_val)
532 GST_DEBUG ("converting value %" G_GINT64_FORMAT " from %s (%d) to %s (%d)",
533 src_val, gst_format_get_name (src_fmt), src_fmt,
534 gst_format_get_name (dest_fmt), dest_fmt);
536 if (src_fmt == dest_fmt || src_val == -1) {
541 /* get important info */
542 GST_OBJECT_LOCK (buf);
543 bps = buf->spec.bytes_per_sample;
544 rate = buf->spec.rate;
545 GST_OBJECT_UNLOCK (buf);
547 if (bps == 0 || rate == 0) {
548 GST_DEBUG ("no rate or bps configured");
554 case GST_FORMAT_BYTES:
556 case GST_FORMAT_TIME:
557 *dest_val = gst_util_uint64_scale_int (src_val / bps, GST_SECOND,
560 case GST_FORMAT_DEFAULT:
561 *dest_val = src_val / bps;
568 case GST_FORMAT_DEFAULT:
570 case GST_FORMAT_TIME:
571 *dest_val = gst_util_uint64_scale_int (src_val, GST_SECOND, rate);
573 case GST_FORMAT_BYTES:
574 *dest_val = src_val * bps;
581 case GST_FORMAT_TIME:
583 case GST_FORMAT_DEFAULT:
584 *dest_val = gst_util_uint64_scale_int (src_val, rate, GST_SECOND);
586 case GST_FORMAT_BYTES:
587 *dest_val = gst_util_uint64_scale_int (src_val, rate, GST_SECOND);
600 GST_DEBUG ("ret=%d result %" G_GINT64_FORMAT, res, *dest_val);
606 * gst_ring_buffer_set_callback:
607 * @buf: the #GstRingBuffer to set the callback on
608 * @cb: the callback to set
609 * @user_data: user data passed to the callback
611 * Sets the given callback function on the buffer. This function
612 * will be called every time a segment has been written to a device.
617 gst_ring_buffer_set_callback (GstRingBuffer * buf, GstRingBufferCallback cb,
620 g_return_if_fail (GST_IS_RING_BUFFER (buf));
622 GST_OBJECT_LOCK (buf);
624 buf->cb_data = user_data;
625 GST_OBJECT_UNLOCK (buf);
630 * gst_ring_buffer_open_device:
631 * @buf: the #GstRingBuffer
633 * Open the audio device associated with the ring buffer. Does not perform any
634 * setup on the device. You must open the device before acquiring the ring
637 * Returns: TRUE if the device could be opened, FALSE on error.
642 gst_ring_buffer_open_device (GstRingBuffer * buf)
645 GstRingBufferClass *rclass;
647 g_return_val_if_fail (GST_IS_RING_BUFFER (buf), FALSE);
649 GST_DEBUG_OBJECT (buf, "opening device");
651 GST_OBJECT_LOCK (buf);
652 if (G_UNLIKELY (buf->open))
657 /* if this fails, something is wrong in this file */
658 g_assert (!buf->acquired);
660 rclass = GST_RING_BUFFER_GET_CLASS (buf);
661 if (G_LIKELY (rclass->open_device))
662 res = rclass->open_device (buf);
664 if (G_UNLIKELY (!res))
667 GST_DEBUG_OBJECT (buf, "opened device");
670 GST_OBJECT_UNLOCK (buf);
677 GST_DEBUG_OBJECT (buf, "Device for ring buffer already open");
678 g_warning ("Device for ring buffer %p already open, fix your code", buf);
685 GST_DEBUG_OBJECT (buf, "failed opening device");
691 * gst_ring_buffer_close_device:
692 * @buf: the #GstRingBuffer
694 * Close the audio device associated with the ring buffer. The ring buffer
695 * should already have been released via gst_ring_buffer_release().
697 * Returns: TRUE if the device could be closed, FALSE on error.
702 gst_ring_buffer_close_device (GstRingBuffer * buf)
705 GstRingBufferClass *rclass;
707 g_return_val_if_fail (GST_IS_RING_BUFFER (buf), FALSE);
709 GST_DEBUG_OBJECT (buf, "closing device");
711 GST_OBJECT_LOCK (buf);
712 if (G_UNLIKELY (!buf->open))
715 if (G_UNLIKELY (buf->acquired))
720 rclass = GST_RING_BUFFER_GET_CLASS (buf);
721 if (G_LIKELY (rclass->close_device))
722 res = rclass->close_device (buf);
724 if (G_UNLIKELY (!res))
727 GST_DEBUG_OBJECT (buf, "closed device");
730 GST_OBJECT_UNLOCK (buf);
737 GST_DEBUG_OBJECT (buf, "Device for ring buffer already closed");
738 g_warning ("Device for ring buffer %p already closed, fix your code", buf);
744 GST_DEBUG_OBJECT (buf, "Resources for ring buffer still acquired");
745 g_critical ("Resources for ring buffer %p still acquired", buf);
752 GST_DEBUG_OBJECT (buf, "error closing device");
758 * gst_ring_buffer_device_is_open:
759 * @buf: the #GstRingBuffer
761 * Checks the status of the device associated with the ring buffer.
763 * Returns: TRUE if the device was open, FALSE if it was closed.
768 gst_ring_buffer_device_is_open (GstRingBuffer * buf)
772 g_return_val_if_fail (GST_IS_RING_BUFFER (buf), FALSE);
774 GST_OBJECT_LOCK (buf);
776 GST_OBJECT_UNLOCK (buf);
782 * gst_ring_buffer_acquire:
783 * @buf: the #GstRingBuffer to acquire
784 * @spec: the specs of the buffer
786 * Allocate the resources for the ringbuffer. This function fills
787 * in the data pointer of the ring buffer with a valid #GstBuffer
788 * to which samples can be written.
790 * Returns: TRUE if the device could be acquired, FALSE on error.
795 gst_ring_buffer_acquire (GstRingBuffer * buf, GstRingBufferSpec * spec)
797 gboolean res = FALSE;
798 GstRingBufferClass *rclass;
802 g_return_val_if_fail (GST_IS_RING_BUFFER (buf), FALSE);
804 GST_DEBUG_OBJECT (buf, "acquiring device %p", buf);
806 GST_OBJECT_LOCK (buf);
807 if (G_UNLIKELY (!buf->open))
810 if (G_UNLIKELY (buf->acquired))
813 buf->acquired = TRUE;
815 rclass = GST_RING_BUFFER_GET_CLASS (buf);
816 if (G_LIKELY (rclass->acquire))
817 res = rclass->acquire (buf, spec);
819 if (G_UNLIKELY (!res))
822 if (G_UNLIKELY ((bps = buf->spec.bytes_per_sample) == 0))
825 /* if the seglatency was overwritten with something else than -1, use it, else
826 * assume segtotal as the latency */
827 if (buf->spec.seglatency == -1)
828 buf->spec.seglatency = buf->spec.segtotal;
830 segsize = buf->spec.segsize;
832 buf->samples_per_seg = segsize / bps;
834 /* create an empty segment */
835 g_free (buf->empty_seg);
836 buf->empty_seg = g_malloc (segsize);
838 /* FIXME, we only have 32 silence samples, which might not be enough to
839 * represent silence in all channels */
841 for (i = 0, j = 0; i < segsize; i++) {
842 buf->empty_seg[i] = buf->spec.silence_sample[j];
845 GST_DEBUG_OBJECT (buf, "acquired device");
848 GST_OBJECT_UNLOCK (buf);
855 GST_DEBUG_OBJECT (buf, "device not opened");
856 g_critical ("Device for %p not opened", buf);
863 GST_DEBUG_OBJECT (buf, "device was acquired");
868 buf->acquired = FALSE;
869 GST_DEBUG_OBJECT (buf, "failed to acquire device");
875 ("invalid bytes_per_sample from acquire ringbuffer %p, fix the element",
877 buf->acquired = FALSE;
884 * gst_ring_buffer_release:
885 * @buf: the #GstRingBuffer to release
887 * Free the resources of the ringbuffer.
889 * Returns: TRUE if the device could be released, FALSE on error.
894 gst_ring_buffer_release (GstRingBuffer * buf)
896 gboolean res = FALSE;
897 GstRingBufferClass *rclass;
899 g_return_val_if_fail (GST_IS_RING_BUFFER (buf), FALSE);
901 GST_DEBUG_OBJECT (buf, "releasing device");
903 gst_ring_buffer_stop (buf);
905 GST_OBJECT_LOCK (buf);
906 if (G_UNLIKELY (!buf->acquired))
909 buf->acquired = FALSE;
911 /* if this fails, something is wrong in this file */
912 g_assert (buf->open == TRUE);
914 rclass = GST_RING_BUFFER_GET_CLASS (buf);
915 if (G_LIKELY (rclass->release))
916 res = rclass->release (buf);
918 /* signal any waiters */
919 GST_DEBUG_OBJECT (buf, "signal waiter");
920 GST_RING_BUFFER_SIGNAL (buf);
922 if (G_UNLIKELY (!res))
925 g_free (buf->empty_seg);
926 buf->empty_seg = NULL;
927 GST_DEBUG_OBJECT (buf, "released device");
930 GST_OBJECT_UNLOCK (buf);
938 GST_DEBUG_OBJECT (buf, "device was released");
943 buf->acquired = TRUE;
944 GST_DEBUG_OBJECT (buf, "failed to release device");
950 * gst_ring_buffer_is_acquired:
951 * @buf: the #GstRingBuffer to check
953 * Check if the ringbuffer is acquired and ready to use.
955 * Returns: TRUE if the ringbuffer is acquired, FALSE on error.
960 gst_ring_buffer_is_acquired (GstRingBuffer * buf)
964 g_return_val_if_fail (GST_IS_RING_BUFFER (buf), FALSE);
966 GST_OBJECT_LOCK (buf);
968 GST_OBJECT_UNLOCK (buf);
974 * gst_ring_buffer_activate:
975 * @buf: the #GstRingBuffer to activate
976 * @active: the new mode
978 * Activate @buf to start or stop pulling data.
982 * Returns: TRUE if the device could be activated in the requested mode,
988 gst_ring_buffer_activate (GstRingBuffer * buf, gboolean active)
990 gboolean res = FALSE;
991 GstRingBufferClass *rclass;
993 g_return_val_if_fail (GST_IS_RING_BUFFER (buf), FALSE);
995 GST_DEBUG_OBJECT (buf, "activate device");
997 GST_OBJECT_LOCK (buf);
998 if (G_UNLIKELY (active && !buf->acquired))
1001 if (G_UNLIKELY (buf->abidata.ABI.active == active))
1004 rclass = GST_RING_BUFFER_GET_CLASS (buf);
1005 /* if there is no activate function we assume it was started/released
1006 * in the acquire method */
1007 if (G_LIKELY (rclass->activate))
1008 res = rclass->activate (buf, active);
1012 if (G_UNLIKELY (!res))
1013 goto activate_failed;
1015 buf->abidata.ABI.active = active;
1018 GST_OBJECT_UNLOCK (buf);
1025 GST_DEBUG_OBJECT (buf, "device not acquired");
1026 g_critical ("Device for %p not acquired", buf);
1033 GST_DEBUG_OBJECT (buf, "device was active in mode %d", active);
1038 GST_DEBUG_OBJECT (buf, "failed to activate device");
1044 * gst_ring_buffer_is_active:
1045 * @buf: the #GstRingBuffer
1047 * Check if @buf is activated.
1051 * Returns: TRUE if the device is active.
1056 gst_ring_buffer_is_active (GstRingBuffer * buf)
1060 g_return_val_if_fail (GST_IS_RING_BUFFER (buf), FALSE);
1062 GST_OBJECT_LOCK (buf);
1063 res = buf->abidata.ABI.active;
1064 GST_OBJECT_UNLOCK (buf);
1071 * gst_ring_buffer_set_flushing:
1072 * @buf: the #GstRingBuffer to flush
1073 * @flushing: the new mode
1075 * Set the ringbuffer to flushing mode or normal mode.
1080 gst_ring_buffer_set_flushing (GstRingBuffer * buf, gboolean flushing)
1082 g_return_if_fail (GST_IS_RING_BUFFER (buf));
1084 GST_OBJECT_LOCK (buf);
1085 buf->abidata.ABI.flushing = flushing;
1088 gst_ring_buffer_pause_unlocked (buf);
1090 gst_ring_buffer_clear_all (buf);
1092 GST_OBJECT_UNLOCK (buf);
1096 * gst_ring_buffer_start:
1097 * @buf: the #GstRingBuffer to start
1099 * Start processing samples from the ringbuffer.
1101 * Returns: TRUE if the device could be started, FALSE on error.
1106 gst_ring_buffer_start (GstRingBuffer * buf)
1108 gboolean res = FALSE;
1109 GstRingBufferClass *rclass;
1110 gboolean resume = FALSE;
1112 g_return_val_if_fail (GST_IS_RING_BUFFER (buf), FALSE);
1114 GST_DEBUG_OBJECT (buf, "starting ringbuffer");
1116 GST_OBJECT_LOCK (buf);
1117 if (G_UNLIKELY (buf->abidata.ABI.flushing))
1120 if (G_UNLIKELY (!buf->acquired))
1123 if (G_UNLIKELY (g_atomic_int_get (&buf->abidata.ABI.may_start) == FALSE))
1126 /* if stopped, set to started */
1127 res = g_atomic_int_compare_and_exchange (&buf->state,
1128 GST_RING_BUFFER_STATE_STOPPED, GST_RING_BUFFER_STATE_STARTED);
1131 GST_DEBUG_OBJECT (buf, "was not stopped, try paused");
1132 /* was not stopped, try from paused */
1133 res = g_atomic_int_compare_and_exchange (&buf->state,
1134 GST_RING_BUFFER_STATE_PAUSED, GST_RING_BUFFER_STATE_STARTED);
1136 /* was not paused either, must be started then */
1138 GST_DEBUG_OBJECT (buf, "was not paused, must have been started");
1142 GST_DEBUG_OBJECT (buf, "resuming");
1145 rclass = GST_RING_BUFFER_GET_CLASS (buf);
1147 if (G_LIKELY (rclass->resume))
1148 res = rclass->resume (buf);
1150 if (G_LIKELY (rclass->start))
1151 res = rclass->start (buf);
1154 if (G_UNLIKELY (!res)) {
1155 buf->state = GST_RING_BUFFER_STATE_PAUSED;
1156 GST_DEBUG_OBJECT (buf, "failed to start");
1158 GST_DEBUG_OBJECT (buf, "started");
1162 GST_OBJECT_UNLOCK (buf);
1168 GST_DEBUG_OBJECT (buf, "we are flushing");
1169 GST_OBJECT_UNLOCK (buf);
1174 GST_DEBUG_OBJECT (buf, "we are not acquired");
1175 GST_OBJECT_UNLOCK (buf);
1180 GST_DEBUG_OBJECT (buf, "we may not start");
1181 GST_OBJECT_UNLOCK (buf);
1187 gst_ring_buffer_pause_unlocked (GstRingBuffer * buf)
1189 gboolean res = FALSE;
1190 GstRingBufferClass *rclass;
1192 GST_DEBUG_OBJECT (buf, "pausing ringbuffer");
1194 /* if started, set to paused */
1195 res = g_atomic_int_compare_and_exchange (&buf->state,
1196 GST_RING_BUFFER_STATE_STARTED, GST_RING_BUFFER_STATE_PAUSED);
1201 /* signal any waiters */
1202 GST_DEBUG_OBJECT (buf, "signal waiter");
1203 GST_RING_BUFFER_SIGNAL (buf);
1205 rclass = GST_RING_BUFFER_GET_CLASS (buf);
1206 if (G_LIKELY (rclass->pause))
1207 res = rclass->pause (buf);
1209 if (G_UNLIKELY (!res)) {
1210 buf->state = GST_RING_BUFFER_STATE_STARTED;
1211 GST_DEBUG_OBJECT (buf, "failed to pause");
1213 GST_DEBUG_OBJECT (buf, "paused");
1220 /* was not started */
1221 GST_DEBUG_OBJECT (buf, "was not started");
1227 * gst_ring_buffer_pause:
1228 * @buf: the #GstRingBuffer to pause
1230 * Pause processing samples from the ringbuffer.
1232 * Returns: TRUE if the device could be paused, FALSE on error.
1237 gst_ring_buffer_pause (GstRingBuffer * buf)
1239 gboolean res = FALSE;
1241 g_return_val_if_fail (GST_IS_RING_BUFFER (buf), FALSE);
1243 GST_OBJECT_LOCK (buf);
1244 if (G_UNLIKELY (buf->abidata.ABI.flushing))
1247 if (G_UNLIKELY (!buf->acquired))
1250 res = gst_ring_buffer_pause_unlocked (buf);
1251 GST_OBJECT_UNLOCK (buf);
1258 GST_DEBUG_OBJECT (buf, "we are flushing");
1259 GST_OBJECT_UNLOCK (buf);
1264 GST_DEBUG_OBJECT (buf, "not acquired");
1265 GST_OBJECT_UNLOCK (buf);
1271 * gst_ring_buffer_stop:
1272 * @buf: the #GstRingBuffer to stop
1274 * Stop processing samples from the ringbuffer.
1276 * Returns: TRUE if the device could be stopped, FALSE on error.
1281 gst_ring_buffer_stop (GstRingBuffer * buf)
1283 gboolean res = FALSE;
1284 GstRingBufferClass *rclass;
1286 g_return_val_if_fail (GST_IS_RING_BUFFER (buf), FALSE);
1288 GST_DEBUG_OBJECT (buf, "stopping");
1290 GST_OBJECT_LOCK (buf);
1292 /* if started, set to stopped */
1293 res = g_atomic_int_compare_and_exchange (&buf->state,
1294 GST_RING_BUFFER_STATE_STARTED, GST_RING_BUFFER_STATE_STOPPED);
1297 GST_DEBUG_OBJECT (buf, "was not started, try paused");
1298 /* was not started, try from paused */
1299 res = g_atomic_int_compare_and_exchange (&buf->state,
1300 GST_RING_BUFFER_STATE_PAUSED, GST_RING_BUFFER_STATE_STOPPED);
1302 /* was not paused either, must have been stopped then */
1304 GST_DEBUG_OBJECT (buf, "was not paused, must have been stopped");
1309 /* signal any waiters */
1310 GST_DEBUG_OBJECT (buf, "signal waiter");
1311 GST_RING_BUFFER_SIGNAL (buf);
1313 rclass = GST_RING_BUFFER_GET_CLASS (buf);
1314 if (G_LIKELY (rclass->stop))
1315 res = rclass->stop (buf);
1317 if (G_UNLIKELY (!res)) {
1318 buf->state = GST_RING_BUFFER_STATE_STARTED;
1319 GST_DEBUG_OBJECT (buf, "failed to stop");
1321 GST_DEBUG_OBJECT (buf, "stopped");
1324 GST_OBJECT_UNLOCK (buf);
1330 * gst_ring_buffer_delay:
1331 * @buf: the #GstRingBuffer to query
1333 * Get the number of samples queued in the audio device. This is
1334 * usually less than the segment size but can be bigger when the
1335 * implementation uses another internal buffer between the audio
1338 * For playback ringbuffers this is the amount of samples transfered from the
1339 * ringbuffer to the device but still not played.
1341 * For capture ringbuffers this is the amount of samples in the device that are
1342 * not yet transfered to the ringbuffer.
1344 * Returns: The number of samples queued in the audio device.
1349 gst_ring_buffer_delay (GstRingBuffer * buf)
1351 GstRingBufferClass *rclass;
1354 g_return_val_if_fail (GST_IS_RING_BUFFER (buf), 0);
1356 /* buffer must be acquired */
1357 if (G_UNLIKELY (!gst_ring_buffer_is_acquired (buf)))
1360 rclass = GST_RING_BUFFER_GET_CLASS (buf);
1361 if (G_LIKELY (rclass->delay))
1362 res = rclass->delay (buf);
1370 GST_DEBUG_OBJECT (buf, "not acquired");
1376 * gst_ring_buffer_samples_done:
1377 * @buf: the #GstRingBuffer to query
1379 * Get the number of samples that were processed by the ringbuffer
1380 * since it was last started. This does not include the number of samples not
1381 * yet processed (see gst_ring_buffer_delay()).
1383 * Returns: The number of samples processed by the ringbuffer.
1388 gst_ring_buffer_samples_done (GstRingBuffer * buf)
1393 g_return_val_if_fail (GST_IS_RING_BUFFER (buf), 0);
1395 /* get the amount of segments we processed */
1396 segdone = g_atomic_int_get (&buf->segdone);
1398 /* convert to samples */
1399 samples = ((guint64) segdone) * buf->samples_per_seg;
1405 * gst_ring_buffer_set_sample:
1406 * @buf: the #GstRingBuffer to use
1407 * @sample: the sample number to set
1409 * Make sure that the next sample written to the device is
1410 * accounted for as being the @sample sample written to the
1411 * device. This value will be used in reporting the current
1412 * sample position of the ringbuffer.
1414 * This function will also clear the buffer with silence.
1419 gst_ring_buffer_set_sample (GstRingBuffer * buf, guint64 sample)
1421 g_return_if_fail (GST_IS_RING_BUFFER (buf));
1426 if (G_UNLIKELY (buf->samples_per_seg == 0))
1429 /* FIXME, we assume the ringbuffer can restart at a random
1430 * position, round down to the beginning and keep track of
1431 * offset when calculating the processed samples. */
1432 buf->segbase = buf->segdone - sample / buf->samples_per_seg;
1434 gst_ring_buffer_clear_all (buf);
1436 GST_DEBUG_OBJECT (buf, "set sample to %" G_GUINT64_FORMAT ", segbase %d",
1437 sample, buf->segbase);
1441 default_clear_all (GstRingBuffer * buf)
1445 /* not fatal, we just are not negotiated yet */
1446 if (G_UNLIKELY (buf->spec.segtotal <= 0))
1449 GST_DEBUG_OBJECT (buf, "clear all segments");
1451 for (i = 0; i < buf->spec.segtotal; i++) {
1452 gst_ring_buffer_clear (buf, i);
1457 * gst_ring_buffer_clear_all:
1458 * @buf: the #GstRingBuffer to clear
1460 * Fill the ringbuffer with silence.
1465 gst_ring_buffer_clear_all (GstRingBuffer * buf)
1467 GstRingBufferClass *rclass;
1469 g_return_if_fail (GST_IS_RING_BUFFER (buf));
1471 rclass = GST_RING_BUFFER_GET_CLASS (buf);
1473 if (G_LIKELY (rclass->clear_all))
1474 rclass->clear_all (buf);
1479 wait_segment (GstRingBuffer * buf)
1482 gboolean wait = TRUE;
1484 /* buffer must be started now or we deadlock since nobody is reading */
1485 if (G_UNLIKELY (g_atomic_int_get (&buf->state) !=
1486 GST_RING_BUFFER_STATE_STARTED)) {
1487 /* see if we are allowed to start it */
1488 if (G_UNLIKELY (g_atomic_int_get (&buf->abidata.ABI.may_start) == FALSE))
1491 GST_DEBUG_OBJECT (buf, "start!");
1492 segments = g_atomic_int_get (&buf->segdone);
1493 gst_ring_buffer_start (buf);
1495 /* After starting, the writer may have wrote segments already and then we
1496 * don't need to wait anymore */
1497 if (G_LIKELY (g_atomic_int_get (&buf->segdone) != segments))
1501 /* take lock first, then update our waiting flag */
1502 GST_OBJECT_LOCK (buf);
1503 if (G_UNLIKELY (buf->abidata.ABI.flushing))
1506 if (G_UNLIKELY (g_atomic_int_get (&buf->state) !=
1507 GST_RING_BUFFER_STATE_STARTED))
1510 if (G_LIKELY (wait)) {
1511 if (g_atomic_int_compare_and_exchange (&buf->waiting, 0, 1)) {
1512 GST_DEBUG_OBJECT (buf, "waiting..");
1513 GST_RING_BUFFER_WAIT (buf);
1515 if (G_UNLIKELY (buf->abidata.ABI.flushing))
1518 if (G_UNLIKELY (g_atomic_int_get (&buf->state) !=
1519 GST_RING_BUFFER_STATE_STARTED))
1523 GST_OBJECT_UNLOCK (buf);
1530 g_atomic_int_compare_and_exchange (&buf->waiting, 1, 0);
1531 GST_DEBUG_OBJECT (buf, "stopped processing");
1532 GST_OBJECT_UNLOCK (buf);
1537 g_atomic_int_compare_and_exchange (&buf->waiting, 1, 0);
1538 GST_DEBUG_OBJECT (buf, "flushing");
1539 GST_OBJECT_UNLOCK (buf);
1544 GST_DEBUG_OBJECT (buf, "not allowed to start");
1549 #define FWD_SAMPLES(s,se,d,de) \
1551 /* no rate conversion */ \
1552 guint towrite = MIN (se + bps - s, de - d); \
1555 memcpy (d, s, towrite); \
1556 in_samples -= towrite / bps; \
1557 out_samples -= towrite / bps; \
1559 GST_DEBUG ("copy %u bytes", towrite); \
1562 /* in_samples >= out_samples, rate > 1.0 */
1563 #define FWD_UP_SAMPLES(s,se,d,de) \
1565 guint8 *sb = s, *db = d; \
1566 while (s <= se && d < de) { \
1568 memcpy (d, s, bps); \
1571 if ((*accum << 1) >= inr) { \
1576 in_samples -= (s - sb)/bps; \
1577 out_samples -= (d - db)/bps; \
1578 GST_DEBUG ("fwd_up end %d/%d",*accum,*toprocess); \
1581 /* out_samples > in_samples, for rates smaller than 1.0 */
1582 #define FWD_DOWN_SAMPLES(s,se,d,de) \
1584 guint8 *sb = s, *db = d; \
1585 while (s <= se && d < de) { \
1587 memcpy (d, s, bps); \
1590 if ((*accum << 1) >= outr) { \
1595 in_samples -= (s - sb)/bps; \
1596 out_samples -= (d - db)/bps; \
1597 GST_DEBUG ("fwd_down end %d/%d",*accum,*toprocess); \
1600 #define REV_UP_SAMPLES(s,se,d,de) \
1602 guint8 *sb = se, *db = d; \
1603 while (s <= se && d < de) { \
1605 memcpy (d, se, bps); \
1608 while (d < de && (*accum << 1) >= inr) { \
1613 in_samples -= (sb - se)/bps; \
1614 out_samples -= (d - db)/bps; \
1615 GST_DEBUG ("rev_up end %d/%d",*accum,*toprocess); \
1618 #define REV_DOWN_SAMPLES(s,se,d,de) \
1620 guint8 *sb = se, *db = d; \
1621 while (s <= se && d < de) { \
1623 memcpy (d, se, bps); \
1626 while (s <= se && (*accum << 1) >= outr) { \
1631 in_samples -= (sb - se)/bps; \
1632 out_samples -= (d - db)/bps; \
1633 GST_DEBUG ("rev_down end %d/%d",*accum,*toprocess); \
1637 default_commit (GstRingBuffer * buf, guint64 * sample,
1638 guchar * data, gint in_samples, gint out_samples, gint * accum)
1641 gint segsize, segtotal, bps, sps;
1642 guint8 *dest, *data_end;
1643 gint writeseg, sampleoff;
1648 g_return_val_if_fail (buf->data != NULL, -1);
1649 g_return_val_if_fail (data != NULL, -1);
1651 dest = GST_BUFFER_DATA (buf->data);
1652 segsize = buf->spec.segsize;
1653 segtotal = buf->spec.segtotal;
1654 bps = buf->spec.bytes_per_sample;
1655 sps = buf->samples_per_seg;
1657 reverse = out_samples < 0;
1658 out_samples = ABS (out_samples);
1660 if (in_samples >= out_samples)
1661 toprocess = &in_samples;
1663 toprocess = &out_samples;
1665 inr = in_samples - 1;
1666 outr = out_samples - 1;
1668 /* data_end points to the last sample we have to write, not past it. This is
1669 * needed to properly handle reverse playback: it points to the last sample. */
1670 data_end = data + (bps * inr);
1672 /* figure out the segment and the offset inside the segment where
1673 * the first sample should be written. */
1674 writeseg = *sample / sps;
1675 sampleoff = (*sample % sps) * bps;
1677 /* write out all samples */
1678 while (*toprocess > 0) {
1687 /* get the currently processed segment */
1688 segdone = g_atomic_int_get (&buf->segdone) - buf->segbase;
1690 /* see how far away it is from the write segment */
1691 diff = writeseg - segdone;
1694 ("pointer at %d, write to %d-%d, diff %d, segtotal %d, segsize %d, base %d",
1695 segdone, writeseg, sampleoff, diff, segtotal, segsize, buf->segbase);
1697 /* segment too far ahead, writer too slow, we need to drop, hopefully UNLIKELY */
1698 if (G_UNLIKELY (diff < 0)) {
1699 /* we need to drop one segment at a time, pretend we wrote a
1705 /* write segment is within writable range, we can break the loop and
1706 * start writing the data. */
1707 if (diff < segtotal) {
1712 /* else we need to wait for the segment to become writable. */
1713 if (!wait_segment (buf))
1717 /* we can write now */
1718 ws = writeseg % segtotal;
1719 avail = MIN (segsize - sampleoff, bps * out_samples);
1721 d = dest + (ws * segsize) + sampleoff;
1723 *sample += avail / bps;
1725 GST_DEBUG_OBJECT (buf, "write @%p seg %d, sps %d, off %d, avail %d",
1726 dest + ws * segsize, ws, sps, sampleoff, avail);
1728 if (G_LIKELY (inr == outr && !reverse)) {
1729 /* no rate conversion, simply copy samples */
1730 FWD_SAMPLES (data, data_end, d, d_end);
1731 } else if (!reverse) {
1733 /* forward speed up */
1734 FWD_UP_SAMPLES (data, data_end, d, d_end);
1736 /* forward slow down */
1737 FWD_DOWN_SAMPLES (data, data_end, d, d_end);
1740 /* reverse speed up */
1741 REV_UP_SAMPLES (data, data_end, d, d_end);
1743 /* reverse slow down */
1744 REV_DOWN_SAMPLES (data, data_end, d, d_end);
1747 /* for the next iteration we write to the next segment at the beginning. */
1751 /* we consumed all samples here */
1752 data = data_end + bps;
1755 return inr - ((data_end - data) / bps);
1760 GST_DEBUG_OBJECT (buf, "stopped processing");
1766 * gst_ring_buffer_commit_full:
1767 * @buf: the #GstRingBuffer to commit
1768 * @sample: the sample position of the data
1769 * @data: the data to commit
1770 * @in_samples: the number of samples in the data to commit
1771 * @out_samples: the number of samples to write to the ringbuffer
1772 * @accum: accumulator for rate conversion.
1774 * Commit @in_samples samples pointed to by @data to the ringbuffer @buf.
1776 * @in_samples and @out_samples define the rate conversion to perform on the
1777 * samples in @data. For negative rates, @out_samples must be negative and
1778 * @in_samples positive.
1780 * When @out_samples is positive, the first sample will be written at position @sample
1781 * in the ringbuffer. When @out_samples is negative, the last sample will be written to
1782 * @sample in reverse order.
1784 * @out_samples does not need to be a multiple of the segment size of the ringbuffer
1785 * although it is recommended for optimal performance.
1787 * @accum will hold a temporary accumulator used in rate conversion and should be
1788 * set to 0 when this function is first called. In case the commit operation is
1789 * interrupted, one can resume the processing by passing the previously returned
1790 * @accum value back to this function.
1794 * Returns: The number of samples written to the ringbuffer or -1 on error. The
1795 * number of samples written can be less than @out_samples when @buf was interrupted
1796 * with a flush or stop.
1801 gst_ring_buffer_commit_full (GstRingBuffer * buf, guint64 * sample,
1802 guchar * data, gint in_samples, gint out_samples, gint * accum)
1804 GstRingBufferClass *rclass;
1807 g_return_val_if_fail (GST_IS_RING_BUFFER (buf), -1);
1809 if (G_UNLIKELY (in_samples == 0 || out_samples == 0))
1812 rclass = GST_RING_BUFFER_GET_CLASS (buf);
1814 if (G_LIKELY (rclass->commit))
1815 res = rclass->commit (buf, sample, data, in_samples, out_samples, accum);
1821 * gst_ring_buffer_commit:
1822 * @buf: the #GstRingBuffer to commit
1823 * @sample: the sample position of the data
1824 * @data: the data to commit
1825 * @len: the number of samples in the data to commit
1827 * Same as gst_ring_buffer_commit_full() but with a in_samples and out_samples
1828 * equal to @len, ignoring accum.
1830 * Returns: The number of samples written to the ringbuffer or -1 on
1836 gst_ring_buffer_commit (GstRingBuffer * buf, guint64 sample, guchar * data,
1840 guint64 samplep = sample;
1842 res = gst_ring_buffer_commit_full (buf, &samplep, data, len, len, NULL);
1848 * gst_ring_buffer_read:
1849 * @buf: the #GstRingBuffer to read from
1850 * @sample: the sample position of the data
1851 * @data: where the data should be read
1852 * @len: the number of samples in data to read
1854 * Read @len samples from the ringbuffer into the memory pointed
1856 * The first sample should be read from position @sample in
1859 * @len should not be a multiple of the segment size of the ringbuffer
1860 * although it is recommended.
1862 * Returns: The number of samples read from the ringbuffer or -1 on
1868 gst_ring_buffer_read (GstRingBuffer * buf, guint64 sample, guchar * data,
1872 gint segsize, segtotal, bps, sps;
1876 g_return_val_if_fail (GST_IS_RING_BUFFER (buf), -1);
1877 g_return_val_if_fail (buf->data != NULL, -1);
1878 g_return_val_if_fail (data != NULL, -1);
1880 dest = GST_BUFFER_DATA (buf->data);
1881 segsize = buf->spec.segsize;
1882 segtotal = buf->spec.segtotal;
1883 bps = buf->spec.bytes_per_sample;
1884 sps = buf->samples_per_seg;
1887 /* read enough samples */
1888 while (to_read > 0) {
1890 gint readseg, sampleoff;
1892 /* figure out the segment and the offset inside the segment where
1893 * the sample should be read from. */
1894 readseg = sample / sps;
1895 sampleoff = (sample % sps);
1900 /* get the currently processed segment */
1901 segdone = g_atomic_int_get (&buf->segdone) - buf->segbase;
1903 /* see how far away it is from the read segment, normally segdone (where
1904 * the hardware is writing) is bigger than readseg (where software is
1906 diff = segdone - readseg;
1909 ("pointer at %d, sample %" G_GUINT64_FORMAT
1910 ", read from %d-%d, to_read %d, diff %d, segtotal %d, segsize %d",
1911 segdone, sample, readseg, sampleoff, to_read, diff, segtotal,
1914 /* segment too far ahead, reader too slow */
1915 if (G_UNLIKELY (diff >= segtotal)) {
1916 /* pretend we read an empty segment. */
1917 sampleslen = MIN (sps, to_read);
1918 memcpy (data, buf->empty_seg, sampleslen * bps);
1922 /* read segment is within readable range, we can break the loop and
1923 * start reading the data. */
1927 /* else we need to wait for the segment to become readable. */
1928 if (!wait_segment (buf))
1932 /* we can read now */
1933 readseg = readseg % segtotal;
1934 sampleslen = MIN (sps - sampleoff, to_read);
1936 GST_DEBUG_OBJECT (buf, "read @%p seg %d, off %d, sampleslen %d",
1937 dest + readseg * segsize, readseg, sampleoff, sampleslen);
1939 memcpy (data, dest + (readseg * segsize) + (sampleoff * bps),
1940 (sampleslen * bps));
1943 to_read -= sampleslen;
1944 sample += sampleslen;
1945 data += sampleslen * bps;
1948 return len - to_read;
1953 GST_DEBUG_OBJECT (buf, "stopped processing");
1954 return len - to_read;
1959 * gst_ring_buffer_prepare_read:
1960 * @buf: the #GstRingBuffer to read from
1961 * @segment: the segment to read
1962 * @readptr: the pointer to the memory where samples can be read
1963 * @len: the number of bytes to read
1965 * Returns a pointer to memory where the data from segment @segment
1966 * can be found. This function is mostly used by subclasses.
1968 * Returns: FALSE if the buffer is not started.
1973 gst_ring_buffer_prepare_read (GstRingBuffer * buf, gint * segment,
1974 guint8 ** readptr, gint * len)
1979 g_return_val_if_fail (GST_IS_RING_BUFFER (buf), FALSE);
1981 if (buf->callback == NULL) {
1982 /* push mode, fail when nothing is started */
1983 if (g_atomic_int_get (&buf->state) != GST_RING_BUFFER_STATE_STARTED)
1987 g_return_val_if_fail (buf->data != NULL, FALSE);
1988 g_return_val_if_fail (segment != NULL, FALSE);
1989 g_return_val_if_fail (readptr != NULL, FALSE);
1990 g_return_val_if_fail (len != NULL, FALSE);
1992 data = GST_BUFFER_DATA (buf->data);
1994 /* get the position of the pointer */
1995 segdone = g_atomic_int_get (&buf->segdone);
1997 *segment = segdone % buf->spec.segtotal;
1998 *len = buf->spec.segsize;
1999 *readptr = data + *segment * *len;
2001 GST_LOG ("prepare read from segment %d (real %d) @%p",
2002 *segment, segdone, *readptr);
2004 /* callback to fill the memory with data, for pull based
2007 buf->callback (buf, *readptr, *len, buf->cb_data);
2013 * gst_ring_buffer_advance:
2014 * @buf: the #GstRingBuffer to advance
2015 * @advance: the number of segments written
2017 * Subclasses should call this function to notify the fact that
2018 * @advance segments are now processed by the device.
2023 gst_ring_buffer_advance (GstRingBuffer * buf, guint advance)
2025 g_return_if_fail (GST_IS_RING_BUFFER (buf));
2027 /* update counter */
2028 g_atomic_int_add (&buf->segdone, advance);
2030 /* the lock is already taken when the waiting flag is set,
2031 * we grab the lock as well to make sure the waiter is actually
2032 * waiting for the signal */
2033 if (g_atomic_int_compare_and_exchange (&buf->waiting, 1, 0)) {
2034 GST_OBJECT_LOCK (buf);
2035 GST_DEBUG_OBJECT (buf, "signal waiter");
2036 GST_RING_BUFFER_SIGNAL (buf);
2037 GST_OBJECT_UNLOCK (buf);
2042 * gst_ring_buffer_clear:
2043 * @buf: the #GstRingBuffer to clear
2044 * @segment: the segment to clear
2046 * Clear the given segment of the buffer with silence samples.
2047 * This function is used by subclasses.
2052 gst_ring_buffer_clear (GstRingBuffer * buf, gint segment)
2056 g_return_if_fail (GST_IS_RING_BUFFER (buf));
2058 /* no data means it's already cleared */
2059 if (G_UNLIKELY (buf->data == NULL))
2062 /* no empty_seg means it's not opened */
2063 if (G_UNLIKELY (buf->empty_seg == NULL))
2066 segment %= buf->spec.segtotal;
2068 data = GST_BUFFER_DATA (buf->data);
2069 data += segment * buf->spec.segsize;
2071 GST_LOG ("clear segment %d @%p", segment, data);
2073 memcpy (data, buf->empty_seg, buf->spec.segsize);
2077 * gst_ring_buffer_may_start:
2078 * @buf: the #GstRingBuffer
2079 * @allowed: the new value
2081 * Tell the ringbuffer that it is allowed to start playback when
2082 * the ringbuffer is filled with samples.
2089 gst_ring_buffer_may_start (GstRingBuffer * buf, gboolean allowed)
2091 g_return_if_fail (GST_IS_RING_BUFFER (buf));
2093 GST_LOG_OBJECT (buf, "may start: %d", allowed);
2094 g_atomic_int_set (&buf->abidata.ABI.may_start, allowed);