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 GST_DEBUG_CATEGORY_STATIC (gst_ring_buffer_debug);
47 #define GST_CAT_DEFAULT gst_ring_buffer_debug
49 static void gst_ring_buffer_dispose (GObject * object);
50 static void gst_ring_buffer_finalize (GObject * object);
52 static gboolean gst_ring_buffer_pause_unlocked (GstRingBuffer * buf);
53 static void default_clear_all (GstRingBuffer * buf);
54 static guint default_commit (GstRingBuffer * buf, guint64 * sample,
55 guchar * data, gint in_samples, gint out_samples, gint * accum);
57 /* ringbuffer abstract base class */
58 G_DEFINE_ABSTRACT_TYPE (GstRingBuffer, gst_ring_buffer, GST_TYPE_OBJECT);
61 gst_ring_buffer_class_init (GstRingBufferClass * klass)
63 GObjectClass *gobject_class;
64 GstRingBufferClass *gstringbuffer_class;
66 gobject_class = (GObjectClass *) klass;
67 gstringbuffer_class = (GstRingBufferClass *) klass;
69 GST_DEBUG_CATEGORY_INIT (gst_ring_buffer_debug, "ringbuffer", 0,
72 gobject_class->dispose = gst_ring_buffer_dispose;
73 gobject_class->finalize = gst_ring_buffer_finalize;
75 gstringbuffer_class->clear_all = GST_DEBUG_FUNCPTR (default_clear_all);
76 gstringbuffer_class->commit = GST_DEBUG_FUNCPTR (default_commit);
80 gst_ring_buffer_init (GstRingBuffer * ringbuffer)
82 ringbuffer->open = FALSE;
83 ringbuffer->acquired = FALSE;
84 ringbuffer->state = GST_RING_BUFFER_STATE_STOPPED;
85 ringbuffer->cond = g_cond_new ();
86 ringbuffer->waiting = 0;
87 ringbuffer->empty_seg = NULL;
88 ringbuffer->abidata.ABI.flushing = TRUE;
92 gst_ring_buffer_dispose (GObject * object)
94 GstRingBuffer *ringbuffer = GST_RING_BUFFER (object);
96 gst_caps_replace (&ringbuffer->spec.caps, NULL);
98 G_OBJECT_CLASS (gst_ring_buffer_parent_class)->dispose (G_OBJECT
103 gst_ring_buffer_finalize (GObject * object)
105 GstRingBuffer *ringbuffer = GST_RING_BUFFER (object);
107 g_cond_free (ringbuffer->cond);
108 g_free (ringbuffer->empty_seg);
110 G_OBJECT_CLASS (gst_ring_buffer_parent_class)->finalize (G_OBJECT
116 const GstBufferFormat format;
117 const guint8 silence[4];
120 static const FormatDef linear_defs[4 * 2 * 2] = {
121 {GST_S8, {0x00, 0x00, 0x00, 0x00}},
122 {GST_S8, {0x00, 0x00, 0x00, 0x00}},
123 {GST_U8, {0x80, 0x80, 0x80, 0x80}},
124 {GST_U8, {0x80, 0x80, 0x80, 0x80}},
125 {GST_S16_LE, {0x00, 0x00, 0x00, 0x00}},
126 {GST_S16_BE, {0x00, 0x00, 0x00, 0x00}},
127 {GST_U16_LE, {0x00, 0x80, 0x00, 0x80}},
128 {GST_U16_BE, {0x80, 0x00, 0x80, 0x00}},
129 {GST_S24_LE, {0x00, 0x00, 0x00, 0x00}},
130 {GST_S24_BE, {0x00, 0x00, 0x00, 0x00}},
131 {GST_U24_LE, {0x00, 0x00, 0x80, 0x00}},
132 {GST_U24_BE, {0x80, 0x00, 0x00, 0x00}},
133 {GST_S32_LE, {0x00, 0x00, 0x00, 0x00}},
134 {GST_S32_BE, {0x00, 0x00, 0x00, 0x00}},
135 {GST_U32_LE, {0x00, 0x00, 0x00, 0x80}},
136 {GST_U32_BE, {0x80, 0x00, 0x00, 0x00}}
139 static const FormatDef linear24_defs[3 * 2 * 2] = {
140 {GST_S24_3LE, {0x00, 0x00, 0x00, 0x00}},
141 {GST_S24_3BE, {0x00, 0x00, 0x00, 0x00}},
142 {GST_U24_3LE, {0x00, 0x00, 0x80, 0x00}},
143 {GST_U24_3BE, {0x80, 0x00, 0x00, 0x00}},
144 {GST_S20_3LE, {0x00, 0x00, 0x00, 0x00}},
145 {GST_S20_3BE, {0x00, 0x00, 0x00, 0x00}},
146 {GST_U20_3LE, {0x00, 0x00, 0x08, 0x00}},
147 {GST_U20_3BE, {0x08, 0x00, 0x00, 0x00}},
148 {GST_S18_3LE, {0x00, 0x00, 0x00, 0x00}},
149 {GST_S18_3BE, {0x00, 0x00, 0x00, 0x00}},
150 {GST_U18_3LE, {0x00, 0x00, 0x02, 0x00}},
151 {GST_U18_3BE, {0x02, 0x00, 0x00, 0x00}}
154 static const FormatDef *
155 build_linear_format (int depth, int width, int unsignd, int big_endian)
157 const FormatDef *formats;
162 formats = &linear24_defs[0];
165 formats = &linear24_defs[4];
168 formats = &linear24_defs[8];
176 formats = &linear_defs[0];
179 formats = &linear_defs[4];
182 formats = &linear_defs[8];
185 formats = &linear_defs[12];
199 #ifndef GST_DISABLE_GST_DEBUG
200 static const gchar *format_type_names[] = {
214 static const gchar *format_names[] = {
259 * gst_ring_buffer_debug_spec_caps:
260 * @spec: the spec to debug
262 * Print debug info about the parsed caps in @spec to the debug log.
265 gst_ring_buffer_debug_spec_caps (GstRingBufferSpec * spec)
269 GST_DEBUG ("spec caps: %p %" GST_PTR_FORMAT, spec->caps, spec->caps);
270 GST_DEBUG ("parsed caps: type: %d, '%s'", spec->type,
271 format_type_names[spec->type]);
272 GST_DEBUG ("parsed caps: format: %d, '%s'", spec->format,
273 format_names[spec->format]);
274 GST_DEBUG ("parsed caps: width: %d", spec->width);
275 GST_DEBUG ("parsed caps: depth: %d", spec->depth);
276 GST_DEBUG ("parsed caps: sign: %d", spec->sign);
277 GST_DEBUG ("parsed caps: bigend: %d", spec->bigend);
278 GST_DEBUG ("parsed caps: rate: %d", spec->rate);
279 GST_DEBUG ("parsed caps: channels: %d", spec->channels);
280 GST_DEBUG ("parsed caps: sample bytes: %d", spec->bytes_per_sample);
281 bytes = (spec->width >> 3) * spec->channels;
282 for (i = 0; i < bytes; i++) {
283 GST_DEBUG ("silence byte %d: %02x", i, spec->silence_sample[i]);
288 * gst_ring_buffer_debug_spec_buff:
289 * @spec: the spec to debug
291 * Print debug info about the buffer sized in @spec to the debug log.
294 gst_ring_buffer_debug_spec_buff (GstRingBufferSpec * spec)
296 GST_DEBUG ("acquire ringbuffer: buffer time: %" G_GINT64_FORMAT " usec",
298 GST_DEBUG ("acquire ringbuffer: latency time: %" G_GINT64_FORMAT " usec",
300 GST_DEBUG ("acquire ringbuffer: total segments: %d", spec->segtotal);
301 GST_DEBUG ("acquire ringbuffer: latency segments: %d", spec->seglatency);
302 GST_DEBUG ("acquire ringbuffer: segment size: %d bytes = %d samples",
303 spec->segsize, spec->segsize / spec->bytes_per_sample);
304 GST_DEBUG ("acquire ringbuffer: buffer size: %d bytes = %d samples",
305 spec->segsize * spec->segtotal,
306 spec->segsize * spec->segtotal / spec->bytes_per_sample);
310 * gst_ring_buffer_parse_caps:
314 * Parse @caps into @spec.
316 * Returns: TRUE if the caps could be parsed.
319 gst_ring_buffer_parse_caps (GstRingBufferSpec * spec, GstCaps * caps)
321 const gchar *mimetype;
322 GstStructure *structure;
325 structure = gst_caps_get_structure (caps, 0);
327 /* we have to differentiate between int and float formats */
328 mimetype = gst_structure_get_name (structure);
330 if (g_str_equal (mimetype, "audio/x-raw-int")) {
332 const FormatDef *def;
335 spec->type = GST_BUFTYPE_LINEAR;
337 /* extract the needed information from the cap */
338 if (!(gst_structure_get_int (structure, "rate", &spec->rate) &&
339 gst_structure_get_int (structure, "channels", &spec->channels) &&
340 gst_structure_get_int (structure, "width", &spec->width) &&
341 gst_structure_get_int (structure, "depth", &spec->depth) &&
342 gst_structure_get_boolean (structure, "signed", &spec->sign)))
345 /* extract endianness if needed */
346 if (spec->width > 8) {
347 if (!gst_structure_get_int (structure, "endianness", &endianness))
350 endianness = G_BYTE_ORDER;
353 spec->bigend = endianness == G_LITTLE_ENDIAN ? FALSE : TRUE;
355 def = build_linear_format (spec->depth, spec->width, spec->sign ? 0 : 1,
356 spec->bigend ? 1 : 0);
361 spec->format = def->format;
363 bytes = spec->width >> 3;
365 for (i = 0; i < spec->channels; i++) {
366 for (j = 0; j < bytes; j++) {
367 spec->silence_sample[i * bytes + j] = def->silence[j];
370 } else if (g_str_equal (mimetype, "audio/x-raw-float")) {
372 spec->type = GST_BUFTYPE_FLOAT;
374 /* extract the needed information from the cap */
375 if (!(gst_structure_get_int (structure, "rate", &spec->rate) &&
376 gst_structure_get_int (structure, "channels", &spec->channels) &&
377 gst_structure_get_int (structure, "width", &spec->width)))
380 /* match layout to format wrt to endianness */
381 switch (spec->width) {
384 G_BYTE_ORDER == G_LITTLE_ENDIAN ? GST_FLOAT32_LE : GST_FLOAT32_BE;
388 G_BYTE_ORDER == G_LITTLE_ENDIAN ? GST_FLOAT64_LE : GST_FLOAT64_BE;
393 /* float silence is all zeros.. */
394 memset (spec->silence_sample, 0, 32);
395 } else if (g_str_equal (mimetype, "audio/x-alaw")) {
396 /* extract the needed information from the cap */
397 if (!(gst_structure_get_int (structure, "rate", &spec->rate) &&
398 gst_structure_get_int (structure, "channels", &spec->channels)))
401 spec->type = GST_BUFTYPE_A_LAW;
402 spec->format = GST_A_LAW;
405 for (i = 0; i < spec->channels; i++)
406 spec->silence_sample[i] = 0xd5;
407 } else if (g_str_equal (mimetype, "audio/x-mulaw")) {
408 /* extract the needed information from the cap */
409 if (!(gst_structure_get_int (structure, "rate", &spec->rate) &&
410 gst_structure_get_int (structure, "channels", &spec->channels)))
413 spec->type = GST_BUFTYPE_MU_LAW;
414 spec->format = GST_MU_LAW;
417 for (i = 0; i < spec->channels; i++)
418 spec->silence_sample[i] = 0xff;
419 } else if (g_str_equal (mimetype, "audio/x-iec958")) {
420 /* extract the needed information from the cap */
421 if (!(gst_structure_get_int (structure, "rate", &spec->rate)))
424 spec->type = GST_BUFTYPE_IEC958;
425 spec->format = GST_IEC958;
429 } else if (g_str_equal (mimetype, "audio/x-ac3")) {
430 /* extract the needed information from the cap */
431 if (!(gst_structure_get_int (structure, "rate", &spec->rate)))
434 spec->type = GST_BUFTYPE_AC3;
435 spec->format = GST_AC3;
439 } else if (g_str_equal (mimetype, "audio/x-eac3")) {
440 /* extract the needed information from the cap */
441 if (!(gst_structure_get_int (structure, "rate", &spec->rate)))
444 spec->type = GST_BUFTYPE_EAC3;
445 spec->format = GST_EAC3;
449 } else if (g_str_equal (mimetype, "audio/x-dts")) {
450 /* extract the needed information from the cap */
451 if (!(gst_structure_get_int (structure, "rate", &spec->rate)))
454 spec->type = GST_BUFTYPE_DTS;
455 spec->format = GST_DTS;
459 } else if (g_str_equal (mimetype, "audio/mpeg") &&
460 gst_structure_get_int (structure, "mpegaudioversion", &i) &&
461 (i == 1 || i == 2)) {
462 /* Now we know this is MPEG-1 or MPEG-2 (non AAC) */
463 /* extract the needed information from the cap */
464 if (!(gst_structure_get_int (structure, "rate", &spec->rate)))
467 spec->type = GST_BUFTYPE_MPEG;
468 spec->format = GST_MPEG;
476 spec->bytes_per_sample = (spec->width >> 3) * spec->channels;
478 gst_caps_replace (&spec->caps, caps);
480 g_return_val_if_fail (spec->latency_time != 0, FALSE);
482 /* calculate suggested segsize and segtotal. segsize should be one unit
483 * of 'latency_time' samples, scaling for the fact that latency_time is
484 * currently stored in microseconds (FIXME: in 0.11) */
485 spec->segsize = gst_util_uint64_scale (spec->rate * spec->bytes_per_sample,
486 spec->latency_time, GST_SECOND / GST_USECOND);
487 /* Round to an integer number of samples */
488 spec->segsize -= spec->segsize % spec->bytes_per_sample;
490 spec->segtotal = spec->buffer_time / spec->latency_time;
491 /* leave the latency undefined now, implementations can change it but if it's
492 * not changed, we assume the same value as segtotal */
493 spec->seglatency = -1;
495 gst_ring_buffer_debug_spec_caps (spec);
496 gst_ring_buffer_debug_spec_buff (spec);
503 GST_DEBUG ("could not parse caps");
509 * gst_ring_buffer_convert:
510 * @buf: the #GstRingBuffer
511 * @src_fmt: the source format
512 * @src_val: the source value
513 * @dest_fmt: the destination format
514 * @dest_val: a location to store the converted value
516 * Convert @src_val in @src_fmt to the equivalent value in @dest_fmt. The result
517 * will be put in @dest_val.
519 * Returns: TRUE if the conversion succeeded.
524 gst_ring_buffer_convert (GstRingBuffer * buf,
525 GstFormat src_fmt, gint64 src_val, GstFormat dest_fmt, gint64 * dest_val)
530 GST_DEBUG ("converting value %" G_GINT64_FORMAT " from %s (%d) to %s (%d)",
531 src_val, gst_format_get_name (src_fmt), src_fmt,
532 gst_format_get_name (dest_fmt), dest_fmt);
534 if (src_fmt == dest_fmt || src_val == -1) {
539 /* get important info */
540 GST_OBJECT_LOCK (buf);
541 bps = buf->spec.bytes_per_sample;
542 rate = buf->spec.rate;
543 GST_OBJECT_UNLOCK (buf);
545 if (bps == 0 || rate == 0) {
546 GST_DEBUG ("no rate or bps configured");
552 case GST_FORMAT_BYTES:
554 case GST_FORMAT_TIME:
555 *dest_val = gst_util_uint64_scale_int (src_val / bps, GST_SECOND,
558 case GST_FORMAT_DEFAULT:
559 *dest_val = src_val / bps;
566 case GST_FORMAT_DEFAULT:
568 case GST_FORMAT_TIME:
569 *dest_val = gst_util_uint64_scale_int (src_val, GST_SECOND, rate);
571 case GST_FORMAT_BYTES:
572 *dest_val = src_val * bps;
579 case GST_FORMAT_TIME:
581 case GST_FORMAT_DEFAULT:
582 *dest_val = gst_util_uint64_scale_int (src_val, rate, GST_SECOND);
584 case GST_FORMAT_BYTES:
585 *dest_val = gst_util_uint64_scale_int (src_val, rate, GST_SECOND);
598 GST_DEBUG ("ret=%d result %" G_GINT64_FORMAT, res, *dest_val);
604 * gst_ring_buffer_set_callback:
605 * @buf: the #GstRingBuffer to set the callback on
606 * @cb: the callback to set
607 * @user_data: user data passed to the callback
609 * Sets the given callback function on the buffer. This function
610 * will be called every time a segment has been written to a device.
615 gst_ring_buffer_set_callback (GstRingBuffer * buf, GstRingBufferCallback cb,
618 g_return_if_fail (GST_IS_RING_BUFFER (buf));
620 GST_OBJECT_LOCK (buf);
622 buf->cb_data = user_data;
623 GST_OBJECT_UNLOCK (buf);
628 * gst_ring_buffer_open_device:
629 * @buf: the #GstRingBuffer
631 * Open the audio device associated with the ring buffer. Does not perform any
632 * setup on the device. You must open the device before acquiring the ring
635 * Returns: TRUE if the device could be opened, FALSE on error.
640 gst_ring_buffer_open_device (GstRingBuffer * buf)
643 GstRingBufferClass *rclass;
645 g_return_val_if_fail (GST_IS_RING_BUFFER (buf), FALSE);
647 GST_DEBUG_OBJECT (buf, "opening device");
649 GST_OBJECT_LOCK (buf);
650 if (G_UNLIKELY (buf->open))
655 /* if this fails, something is wrong in this file */
656 g_assert (!buf->acquired);
658 rclass = GST_RING_BUFFER_GET_CLASS (buf);
659 if (G_LIKELY (rclass->open_device))
660 res = rclass->open_device (buf);
662 if (G_UNLIKELY (!res))
665 GST_DEBUG_OBJECT (buf, "opened device");
668 GST_OBJECT_UNLOCK (buf);
675 GST_DEBUG_OBJECT (buf, "Device for ring buffer already open");
676 g_warning ("Device for ring buffer %p already open, fix your code", buf);
683 GST_DEBUG_OBJECT (buf, "failed opening device");
689 * gst_ring_buffer_close_device:
690 * @buf: the #GstRingBuffer
692 * Close the audio device associated with the ring buffer. The ring buffer
693 * should already have been released via gst_ring_buffer_release().
695 * Returns: TRUE if the device could be closed, FALSE on error.
700 gst_ring_buffer_close_device (GstRingBuffer * buf)
703 GstRingBufferClass *rclass;
705 g_return_val_if_fail (GST_IS_RING_BUFFER (buf), FALSE);
707 GST_DEBUG_OBJECT (buf, "closing device");
709 GST_OBJECT_LOCK (buf);
710 if (G_UNLIKELY (!buf->open))
713 if (G_UNLIKELY (buf->acquired))
718 rclass = GST_RING_BUFFER_GET_CLASS (buf);
719 if (G_LIKELY (rclass->close_device))
720 res = rclass->close_device (buf);
722 if (G_UNLIKELY (!res))
725 GST_DEBUG_OBJECT (buf, "closed device");
728 GST_OBJECT_UNLOCK (buf);
735 GST_DEBUG_OBJECT (buf, "Device for ring buffer already closed");
736 g_warning ("Device for ring buffer %p already closed, fix your code", buf);
742 GST_DEBUG_OBJECT (buf, "Resources for ring buffer still acquired");
743 g_critical ("Resources for ring buffer %p still acquired", buf);
750 GST_DEBUG_OBJECT (buf, "error closing device");
756 * gst_ring_buffer_device_is_open:
757 * @buf: the #GstRingBuffer
759 * Checks the status of the device associated with the ring buffer.
761 * Returns: TRUE if the device was open, FALSE if it was closed.
766 gst_ring_buffer_device_is_open (GstRingBuffer * buf)
770 g_return_val_if_fail (GST_IS_RING_BUFFER (buf), FALSE);
772 GST_OBJECT_LOCK (buf);
774 GST_OBJECT_UNLOCK (buf);
780 * gst_ring_buffer_acquire:
781 * @buf: the #GstRingBuffer to acquire
782 * @spec: the specs of the buffer
784 * Allocate the resources for the ringbuffer. This function fills
785 * in the data pointer of the ring buffer with a valid #GstBuffer
786 * to which samples can be written.
788 * Returns: TRUE if the device could be acquired, FALSE on error.
793 gst_ring_buffer_acquire (GstRingBuffer * buf, GstRingBufferSpec * spec)
795 gboolean res = FALSE;
796 GstRingBufferClass *rclass;
800 g_return_val_if_fail (GST_IS_RING_BUFFER (buf), FALSE);
802 GST_DEBUG_OBJECT (buf, "acquiring device %p", buf);
804 GST_OBJECT_LOCK (buf);
805 if (G_UNLIKELY (!buf->open))
808 if (G_UNLIKELY (buf->acquired))
811 buf->acquired = TRUE;
813 rclass = GST_RING_BUFFER_GET_CLASS (buf);
814 if (G_LIKELY (rclass->acquire))
815 res = rclass->acquire (buf, spec);
817 if (G_UNLIKELY (!res))
820 if (G_UNLIKELY ((bps = buf->spec.bytes_per_sample) == 0))
823 /* if the seglatency was overwritten with something else than -1, use it, else
824 * assume segtotal as the latency */
825 if (buf->spec.seglatency == -1)
826 buf->spec.seglatency = buf->spec.segtotal;
828 segsize = buf->spec.segsize;
830 buf->samples_per_seg = segsize / bps;
832 /* create an empty segment */
833 g_free (buf->empty_seg);
834 buf->empty_seg = g_malloc (segsize);
836 /* FIXME, we only have 32 silence samples, which might not be enough to
837 * represent silence in all channels */
839 for (i = 0, j = 0; i < segsize; i++) {
840 buf->empty_seg[i] = buf->spec.silence_sample[j];
843 GST_DEBUG_OBJECT (buf, "acquired device");
846 GST_OBJECT_UNLOCK (buf);
853 GST_DEBUG_OBJECT (buf, "device not opened");
854 g_critical ("Device for %p not opened", buf);
861 GST_DEBUG_OBJECT (buf, "device was acquired");
866 buf->acquired = FALSE;
867 GST_DEBUG_OBJECT (buf, "failed to acquire device");
873 ("invalid bytes_per_sample from acquire ringbuffer %p, fix the element",
875 buf->acquired = FALSE;
882 * gst_ring_buffer_release:
883 * @buf: the #GstRingBuffer to release
885 * Free the resources of the ringbuffer.
887 * Returns: TRUE if the device could be released, FALSE on error.
892 gst_ring_buffer_release (GstRingBuffer * buf)
894 gboolean res = FALSE;
895 GstRingBufferClass *rclass;
897 g_return_val_if_fail (GST_IS_RING_BUFFER (buf), FALSE);
899 GST_DEBUG_OBJECT (buf, "releasing device");
901 gst_ring_buffer_stop (buf);
903 GST_OBJECT_LOCK (buf);
904 if (G_UNLIKELY (!buf->acquired))
907 buf->acquired = FALSE;
909 /* if this fails, something is wrong in this file */
910 g_assert (buf->open == TRUE);
912 rclass = GST_RING_BUFFER_GET_CLASS (buf);
913 if (G_LIKELY (rclass->release))
914 res = rclass->release (buf);
916 /* signal any waiters */
917 GST_DEBUG_OBJECT (buf, "signal waiter");
918 GST_RING_BUFFER_SIGNAL (buf);
920 if (G_UNLIKELY (!res))
923 g_free (buf->empty_seg);
924 buf->empty_seg = NULL;
925 GST_DEBUG_OBJECT (buf, "released device");
928 GST_OBJECT_UNLOCK (buf);
936 GST_DEBUG_OBJECT (buf, "device was released");
941 buf->acquired = TRUE;
942 GST_DEBUG_OBJECT (buf, "failed to release device");
948 * gst_ring_buffer_is_acquired:
949 * @buf: the #GstRingBuffer to check
951 * Check if the ringbuffer is acquired and ready to use.
953 * Returns: TRUE if the ringbuffer is acquired, FALSE on error.
958 gst_ring_buffer_is_acquired (GstRingBuffer * buf)
962 g_return_val_if_fail (GST_IS_RING_BUFFER (buf), FALSE);
964 GST_OBJECT_LOCK (buf);
966 GST_OBJECT_UNLOCK (buf);
972 * gst_ring_buffer_activate:
973 * @buf: the #GstRingBuffer to activate
974 * @active: the new mode
976 * Activate @buf to start or stop pulling data.
980 * Returns: TRUE if the device could be activated in the requested mode,
986 gst_ring_buffer_activate (GstRingBuffer * buf, gboolean active)
988 gboolean res = FALSE;
989 GstRingBufferClass *rclass;
991 g_return_val_if_fail (GST_IS_RING_BUFFER (buf), FALSE);
993 GST_DEBUG_OBJECT (buf, "activate device");
995 GST_OBJECT_LOCK (buf);
996 if (G_UNLIKELY (active && !buf->acquired))
999 if (G_UNLIKELY (buf->abidata.ABI.active == active))
1002 rclass = GST_RING_BUFFER_GET_CLASS (buf);
1003 /* if there is no activate function we assume it was started/released
1004 * in the acquire method */
1005 if (G_LIKELY (rclass->activate))
1006 res = rclass->activate (buf, active);
1010 if (G_UNLIKELY (!res))
1011 goto activate_failed;
1013 buf->abidata.ABI.active = active;
1016 GST_OBJECT_UNLOCK (buf);
1023 GST_DEBUG_OBJECT (buf, "device not acquired");
1024 g_critical ("Device for %p not acquired", buf);
1031 GST_DEBUG_OBJECT (buf, "device was active in mode %d", active);
1036 GST_DEBUG_OBJECT (buf, "failed to activate device");
1042 * gst_ring_buffer_is_active:
1043 * @buf: the #GstRingBuffer
1045 * Check if @buf is activated.
1049 * Returns: TRUE if the device is active.
1054 gst_ring_buffer_is_active (GstRingBuffer * buf)
1058 g_return_val_if_fail (GST_IS_RING_BUFFER (buf), FALSE);
1060 GST_OBJECT_LOCK (buf);
1061 res = buf->abidata.ABI.active;
1062 GST_OBJECT_UNLOCK (buf);
1069 * gst_ring_buffer_set_flushing:
1070 * @buf: the #GstRingBuffer to flush
1071 * @flushing: the new mode
1073 * Set the ringbuffer to flushing mode or normal mode.
1078 gst_ring_buffer_set_flushing (GstRingBuffer * buf, gboolean flushing)
1080 g_return_if_fail (GST_IS_RING_BUFFER (buf));
1082 GST_OBJECT_LOCK (buf);
1083 buf->abidata.ABI.flushing = flushing;
1086 gst_ring_buffer_pause_unlocked (buf);
1088 gst_ring_buffer_clear_all (buf);
1090 GST_OBJECT_UNLOCK (buf);
1094 * gst_ring_buffer_start:
1095 * @buf: the #GstRingBuffer to start
1097 * Start processing samples from the ringbuffer.
1099 * Returns: TRUE if the device could be started, FALSE on error.
1104 gst_ring_buffer_start (GstRingBuffer * buf)
1106 gboolean res = FALSE;
1107 GstRingBufferClass *rclass;
1108 gboolean resume = FALSE;
1110 g_return_val_if_fail (GST_IS_RING_BUFFER (buf), FALSE);
1112 GST_DEBUG_OBJECT (buf, "starting ringbuffer");
1114 GST_OBJECT_LOCK (buf);
1115 if (G_UNLIKELY (buf->abidata.ABI.flushing))
1118 if (G_UNLIKELY (!buf->acquired))
1121 if (G_UNLIKELY (g_atomic_int_get (&buf->abidata.ABI.may_start) == FALSE))
1124 /* if stopped, set to started */
1125 res = g_atomic_int_compare_and_exchange (&buf->state,
1126 GST_RING_BUFFER_STATE_STOPPED, GST_RING_BUFFER_STATE_STARTED);
1129 GST_DEBUG_OBJECT (buf, "was not stopped, try paused");
1130 /* was not stopped, try from paused */
1131 res = g_atomic_int_compare_and_exchange (&buf->state,
1132 GST_RING_BUFFER_STATE_PAUSED, GST_RING_BUFFER_STATE_STARTED);
1134 /* was not paused either, must be started then */
1136 GST_DEBUG_OBJECT (buf, "was not paused, must have been started");
1140 GST_DEBUG_OBJECT (buf, "resuming");
1143 rclass = GST_RING_BUFFER_GET_CLASS (buf);
1145 if (G_LIKELY (rclass->resume))
1146 res = rclass->resume (buf);
1148 if (G_LIKELY (rclass->start))
1149 res = rclass->start (buf);
1152 if (G_UNLIKELY (!res)) {
1153 buf->state = GST_RING_BUFFER_STATE_PAUSED;
1154 GST_DEBUG_OBJECT (buf, "failed to start");
1156 GST_DEBUG_OBJECT (buf, "started");
1160 GST_OBJECT_UNLOCK (buf);
1166 GST_DEBUG_OBJECT (buf, "we are flushing");
1167 GST_OBJECT_UNLOCK (buf);
1172 GST_DEBUG_OBJECT (buf, "we are not acquired");
1173 GST_OBJECT_UNLOCK (buf);
1178 GST_DEBUG_OBJECT (buf, "we may not start");
1179 GST_OBJECT_UNLOCK (buf);
1185 gst_ring_buffer_pause_unlocked (GstRingBuffer * buf)
1187 gboolean res = FALSE;
1188 GstRingBufferClass *rclass;
1190 GST_DEBUG_OBJECT (buf, "pausing ringbuffer");
1192 /* if started, set to paused */
1193 res = g_atomic_int_compare_and_exchange (&buf->state,
1194 GST_RING_BUFFER_STATE_STARTED, GST_RING_BUFFER_STATE_PAUSED);
1199 /* signal any waiters */
1200 GST_DEBUG_OBJECT (buf, "signal waiter");
1201 GST_RING_BUFFER_SIGNAL (buf);
1203 rclass = GST_RING_BUFFER_GET_CLASS (buf);
1204 if (G_LIKELY (rclass->pause))
1205 res = rclass->pause (buf);
1207 if (G_UNLIKELY (!res)) {
1208 buf->state = GST_RING_BUFFER_STATE_STARTED;
1209 GST_DEBUG_OBJECT (buf, "failed to pause");
1211 GST_DEBUG_OBJECT (buf, "paused");
1218 /* was not started */
1219 GST_DEBUG_OBJECT (buf, "was not started");
1225 * gst_ring_buffer_pause:
1226 * @buf: the #GstRingBuffer to pause
1228 * Pause processing samples from the ringbuffer.
1230 * Returns: TRUE if the device could be paused, FALSE on error.
1235 gst_ring_buffer_pause (GstRingBuffer * buf)
1237 gboolean res = FALSE;
1239 g_return_val_if_fail (GST_IS_RING_BUFFER (buf), FALSE);
1241 GST_OBJECT_LOCK (buf);
1242 if (G_UNLIKELY (buf->abidata.ABI.flushing))
1245 if (G_UNLIKELY (!buf->acquired))
1248 res = gst_ring_buffer_pause_unlocked (buf);
1249 GST_OBJECT_UNLOCK (buf);
1256 GST_DEBUG_OBJECT (buf, "we are flushing");
1257 GST_OBJECT_UNLOCK (buf);
1262 GST_DEBUG_OBJECT (buf, "not acquired");
1263 GST_OBJECT_UNLOCK (buf);
1269 * gst_ring_buffer_stop:
1270 * @buf: the #GstRingBuffer to stop
1272 * Stop processing samples from the ringbuffer.
1274 * Returns: TRUE if the device could be stopped, FALSE on error.
1279 gst_ring_buffer_stop (GstRingBuffer * buf)
1281 gboolean res = FALSE;
1282 GstRingBufferClass *rclass;
1284 g_return_val_if_fail (GST_IS_RING_BUFFER (buf), FALSE);
1286 GST_DEBUG_OBJECT (buf, "stopping");
1288 GST_OBJECT_LOCK (buf);
1290 /* if started, set to stopped */
1291 res = g_atomic_int_compare_and_exchange (&buf->state,
1292 GST_RING_BUFFER_STATE_STARTED, GST_RING_BUFFER_STATE_STOPPED);
1295 GST_DEBUG_OBJECT (buf, "was not started, try paused");
1296 /* was not started, try from paused */
1297 res = g_atomic_int_compare_and_exchange (&buf->state,
1298 GST_RING_BUFFER_STATE_PAUSED, GST_RING_BUFFER_STATE_STOPPED);
1300 /* was not paused either, must have been stopped then */
1302 GST_DEBUG_OBJECT (buf, "was not paused, must have been stopped");
1307 /* signal any waiters */
1308 GST_DEBUG_OBJECT (buf, "signal waiter");
1309 GST_RING_BUFFER_SIGNAL (buf);
1311 rclass = GST_RING_BUFFER_GET_CLASS (buf);
1312 if (G_LIKELY (rclass->stop))
1313 res = rclass->stop (buf);
1315 if (G_UNLIKELY (!res)) {
1316 buf->state = GST_RING_BUFFER_STATE_STARTED;
1317 GST_DEBUG_OBJECT (buf, "failed to stop");
1319 GST_DEBUG_OBJECT (buf, "stopped");
1322 GST_OBJECT_UNLOCK (buf);
1328 * gst_ring_buffer_delay:
1329 * @buf: the #GstRingBuffer to query
1331 * Get the number of samples queued in the audio device. This is
1332 * usually less than the segment size but can be bigger when the
1333 * implementation uses another internal buffer between the audio
1336 * For playback ringbuffers this is the amount of samples transfered from the
1337 * ringbuffer to the device but still not played.
1339 * For capture ringbuffers this is the amount of samples in the device that are
1340 * not yet transfered to the ringbuffer.
1342 * Returns: The number of samples queued in the audio device.
1347 gst_ring_buffer_delay (GstRingBuffer * buf)
1349 GstRingBufferClass *rclass;
1352 g_return_val_if_fail (GST_IS_RING_BUFFER (buf), 0);
1354 /* buffer must be acquired */
1355 if (G_UNLIKELY (!gst_ring_buffer_is_acquired (buf)))
1358 rclass = GST_RING_BUFFER_GET_CLASS (buf);
1359 if (G_LIKELY (rclass->delay))
1360 res = rclass->delay (buf);
1368 GST_DEBUG_OBJECT (buf, "not acquired");
1374 * gst_ring_buffer_samples_done:
1375 * @buf: the #GstRingBuffer to query
1377 * Get the number of samples that were processed by the ringbuffer
1378 * since it was last started. This does not include the number of samples not
1379 * yet processed (see gst_ring_buffer_delay()).
1381 * Returns: The number of samples processed by the ringbuffer.
1386 gst_ring_buffer_samples_done (GstRingBuffer * buf)
1391 g_return_val_if_fail (GST_IS_RING_BUFFER (buf), 0);
1393 /* get the amount of segments we processed */
1394 segdone = g_atomic_int_get (&buf->segdone);
1396 /* convert to samples */
1397 samples = ((guint64) segdone) * buf->samples_per_seg;
1403 * gst_ring_buffer_set_sample:
1404 * @buf: the #GstRingBuffer to use
1405 * @sample: the sample number to set
1407 * Make sure that the next sample written to the device is
1408 * accounted for as being the @sample sample written to the
1409 * device. This value will be used in reporting the current
1410 * sample position of the ringbuffer.
1412 * This function will also clear the buffer with silence.
1417 gst_ring_buffer_set_sample (GstRingBuffer * buf, guint64 sample)
1419 g_return_if_fail (GST_IS_RING_BUFFER (buf));
1424 if (G_UNLIKELY (buf->samples_per_seg == 0))
1427 /* FIXME, we assume the ringbuffer can restart at a random
1428 * position, round down to the beginning and keep track of
1429 * offset when calculating the processed samples. */
1430 buf->segbase = buf->segdone - sample / buf->samples_per_seg;
1432 gst_ring_buffer_clear_all (buf);
1434 GST_DEBUG_OBJECT (buf, "set sample to %" G_GUINT64_FORMAT ", segbase %d",
1435 sample, buf->segbase);
1439 default_clear_all (GstRingBuffer * buf)
1443 /* not fatal, we just are not negotiated yet */
1444 if (G_UNLIKELY (buf->spec.segtotal <= 0))
1447 GST_DEBUG_OBJECT (buf, "clear all segments");
1449 for (i = 0; i < buf->spec.segtotal; i++) {
1450 gst_ring_buffer_clear (buf, i);
1455 * gst_ring_buffer_clear_all:
1456 * @buf: the #GstRingBuffer to clear
1458 * Fill the ringbuffer with silence.
1463 gst_ring_buffer_clear_all (GstRingBuffer * buf)
1465 GstRingBufferClass *rclass;
1467 g_return_if_fail (GST_IS_RING_BUFFER (buf));
1469 rclass = GST_RING_BUFFER_GET_CLASS (buf);
1471 if (G_LIKELY (rclass->clear_all))
1472 rclass->clear_all (buf);
1477 wait_segment (GstRingBuffer * buf)
1480 gboolean wait = TRUE;
1482 /* buffer must be started now or we deadlock since nobody is reading */
1483 if (G_UNLIKELY (g_atomic_int_get (&buf->state) !=
1484 GST_RING_BUFFER_STATE_STARTED)) {
1485 /* see if we are allowed to start it */
1486 if (G_UNLIKELY (g_atomic_int_get (&buf->abidata.ABI.may_start) == FALSE))
1489 GST_DEBUG_OBJECT (buf, "start!");
1490 segments = g_atomic_int_get (&buf->segdone);
1491 gst_ring_buffer_start (buf);
1493 /* After starting, the writer may have wrote segments already and then we
1494 * don't need to wait anymore */
1495 if (G_LIKELY (g_atomic_int_get (&buf->segdone) != segments))
1499 /* take lock first, then update our waiting flag */
1500 GST_OBJECT_LOCK (buf);
1501 if (G_UNLIKELY (buf->abidata.ABI.flushing))
1504 if (G_UNLIKELY (g_atomic_int_get (&buf->state) !=
1505 GST_RING_BUFFER_STATE_STARTED))
1508 if (G_LIKELY (wait)) {
1509 if (g_atomic_int_compare_and_exchange (&buf->waiting, 0, 1)) {
1510 GST_DEBUG_OBJECT (buf, "waiting..");
1511 GST_RING_BUFFER_WAIT (buf);
1513 if (G_UNLIKELY (buf->abidata.ABI.flushing))
1516 if (G_UNLIKELY (g_atomic_int_get (&buf->state) !=
1517 GST_RING_BUFFER_STATE_STARTED))
1521 GST_OBJECT_UNLOCK (buf);
1528 g_atomic_int_compare_and_exchange (&buf->waiting, 1, 0);
1529 GST_DEBUG_OBJECT (buf, "stopped processing");
1530 GST_OBJECT_UNLOCK (buf);
1535 g_atomic_int_compare_and_exchange (&buf->waiting, 1, 0);
1536 GST_DEBUG_OBJECT (buf, "flushing");
1537 GST_OBJECT_UNLOCK (buf);
1542 GST_DEBUG_OBJECT (buf, "not allowed to start");
1547 #define FWD_SAMPLES(s,se,d,de) \
1549 /* no rate conversion */ \
1550 guint towrite = MIN (se + bps - s, de - d); \
1553 memcpy (d, s, towrite); \
1554 in_samples -= towrite / bps; \
1555 out_samples -= towrite / bps; \
1557 GST_DEBUG ("copy %u bytes", towrite); \
1560 /* in_samples >= out_samples, rate > 1.0 */
1561 #define FWD_UP_SAMPLES(s,se,d,de) \
1563 guint8 *sb = s, *db = d; \
1564 while (s <= se && d < de) { \
1566 memcpy (d, s, bps); \
1569 if ((*accum << 1) >= inr) { \
1574 in_samples -= (s - sb)/bps; \
1575 out_samples -= (d - db)/bps; \
1576 GST_DEBUG ("fwd_up end %d/%d",*accum,*toprocess); \
1579 /* out_samples > in_samples, for rates smaller than 1.0 */
1580 #define FWD_DOWN_SAMPLES(s,se,d,de) \
1582 guint8 *sb = s, *db = d; \
1583 while (s <= se && d < de) { \
1585 memcpy (d, s, bps); \
1588 if ((*accum << 1) >= outr) { \
1593 in_samples -= (s - sb)/bps; \
1594 out_samples -= (d - db)/bps; \
1595 GST_DEBUG ("fwd_down end %d/%d",*accum,*toprocess); \
1598 #define REV_UP_SAMPLES(s,se,d,de) \
1600 guint8 *sb = se, *db = d; \
1601 while (s <= se && d < de) { \
1603 memcpy (d, se, bps); \
1606 while (d < de && (*accum << 1) >= inr) { \
1611 in_samples -= (sb - se)/bps; \
1612 out_samples -= (d - db)/bps; \
1613 GST_DEBUG ("rev_up end %d/%d",*accum,*toprocess); \
1616 #define REV_DOWN_SAMPLES(s,se,d,de) \
1618 guint8 *sb = se, *db = d; \
1619 while (s <= se && d < de) { \
1621 memcpy (d, se, bps); \
1624 while (s <= se && (*accum << 1) >= outr) { \
1629 in_samples -= (sb - se)/bps; \
1630 out_samples -= (d - db)/bps; \
1631 GST_DEBUG ("rev_down end %d/%d",*accum,*toprocess); \
1635 default_commit (GstRingBuffer * buf, guint64 * sample,
1636 guchar * data, gint in_samples, gint out_samples, gint * accum)
1639 gint segsize, segtotal, bps, sps;
1640 guint8 *dest, *data_end;
1641 gint writeseg, sampleoff;
1646 g_return_val_if_fail (buf->data != NULL, -1);
1647 g_return_val_if_fail (data != NULL, -1);
1649 dest = GST_BUFFER_DATA (buf->data);
1650 segsize = buf->spec.segsize;
1651 segtotal = buf->spec.segtotal;
1652 bps = buf->spec.bytes_per_sample;
1653 sps = buf->samples_per_seg;
1655 reverse = out_samples < 0;
1656 out_samples = ABS (out_samples);
1658 if (in_samples >= out_samples)
1659 toprocess = &in_samples;
1661 toprocess = &out_samples;
1663 inr = in_samples - 1;
1664 outr = out_samples - 1;
1666 /* data_end points to the last sample we have to write, not past it. This is
1667 * needed to properly handle reverse playback: it points to the last sample. */
1668 data_end = data + (bps * inr);
1670 /* figure out the segment and the offset inside the segment where
1671 * the first sample should be written. */
1672 writeseg = *sample / sps;
1673 sampleoff = (*sample % sps) * bps;
1675 /* write out all samples */
1676 while (*toprocess > 0) {
1685 /* get the currently processed segment */
1686 segdone = g_atomic_int_get (&buf->segdone) - buf->segbase;
1688 /* see how far away it is from the write segment */
1689 diff = writeseg - segdone;
1692 ("pointer at %d, write to %d-%d, diff %d, segtotal %d, segsize %d, base %d",
1693 segdone, writeseg, sampleoff, diff, segtotal, segsize, buf->segbase);
1695 /* segment too far ahead, writer too slow, we need to drop, hopefully UNLIKELY */
1696 if (G_UNLIKELY (diff < 0)) {
1697 /* we need to drop one segment at a time, pretend we wrote a
1703 /* write segment is within writable range, we can break the loop and
1704 * start writing the data. */
1705 if (diff < segtotal) {
1710 /* else we need to wait for the segment to become writable. */
1711 if (!wait_segment (buf))
1715 /* we can write now */
1716 ws = writeseg % segtotal;
1717 avail = MIN (segsize - sampleoff, bps * out_samples);
1719 d = dest + (ws * segsize) + sampleoff;
1721 *sample += avail / bps;
1723 GST_DEBUG_OBJECT (buf, "write @%p seg %d, sps %d, off %d, avail %d",
1724 dest + ws * segsize, ws, sps, sampleoff, avail);
1726 if (G_LIKELY (inr == outr && !reverse)) {
1727 /* no rate conversion, simply copy samples */
1728 FWD_SAMPLES (data, data_end, d, d_end);
1729 } else if (!reverse) {
1731 /* forward speed up */
1732 FWD_UP_SAMPLES (data, data_end, d, d_end);
1734 /* forward slow down */
1735 FWD_DOWN_SAMPLES (data, data_end, d, d_end);
1738 /* reverse speed up */
1739 REV_UP_SAMPLES (data, data_end, d, d_end);
1741 /* reverse slow down */
1742 REV_DOWN_SAMPLES (data, data_end, d, d_end);
1745 /* for the next iteration we write to the next segment at the beginning. */
1749 /* we consumed all samples here */
1750 data = data_end + bps;
1753 return inr - ((data_end - data) / bps);
1758 GST_DEBUG_OBJECT (buf, "stopped processing");
1764 * gst_ring_buffer_commit_full:
1765 * @buf: the #GstRingBuffer to commit
1766 * @sample: the sample position of the data
1767 * @data: the data to commit
1768 * @in_samples: the number of samples in the data to commit
1769 * @out_samples: the number of samples to write to the ringbuffer
1770 * @accum: accumulator for rate conversion.
1772 * Commit @in_samples samples pointed to by @data to the ringbuffer @buf.
1774 * @in_samples and @out_samples define the rate conversion to perform on the
1775 * samples in @data. For negative rates, @out_samples must be negative and
1776 * @in_samples positive.
1778 * When @out_samples is positive, the first sample will be written at position @sample
1779 * in the ringbuffer. When @out_samples is negative, the last sample will be written to
1780 * @sample in reverse order.
1782 * @out_samples does not need to be a multiple of the segment size of the ringbuffer
1783 * although it is recommended for optimal performance.
1785 * @accum will hold a temporary accumulator used in rate conversion and should be
1786 * set to 0 when this function is first called. In case the commit operation is
1787 * interrupted, one can resume the processing by passing the previously returned
1788 * @accum value back to this function.
1792 * Returns: The number of samples written to the ringbuffer or -1 on error. The
1793 * number of samples written can be less than @out_samples when @buf was interrupted
1794 * with a flush or stop.
1799 gst_ring_buffer_commit_full (GstRingBuffer * buf, guint64 * sample,
1800 guchar * data, gint in_samples, gint out_samples, gint * accum)
1802 GstRingBufferClass *rclass;
1805 g_return_val_if_fail (GST_IS_RING_BUFFER (buf), -1);
1807 if (G_UNLIKELY (in_samples == 0 || out_samples == 0))
1810 rclass = GST_RING_BUFFER_GET_CLASS (buf);
1812 if (G_LIKELY (rclass->commit))
1813 res = rclass->commit (buf, sample, data, in_samples, out_samples, accum);
1819 * gst_ring_buffer_commit:
1820 * @buf: the #GstRingBuffer to commit
1821 * @sample: the sample position of the data
1822 * @data: the data to commit
1823 * @len: the number of samples in the data to commit
1825 * Same as gst_ring_buffer_commit_full() but with a in_samples and out_samples
1826 * equal to @len, ignoring accum.
1828 * Returns: The number of samples written to the ringbuffer or -1 on
1834 gst_ring_buffer_commit (GstRingBuffer * buf, guint64 sample, guchar * data,
1838 guint64 samplep = sample;
1840 res = gst_ring_buffer_commit_full (buf, &samplep, data, len, len, NULL);
1846 * gst_ring_buffer_read:
1847 * @buf: the #GstRingBuffer to read from
1848 * @sample: the sample position of the data
1849 * @data: where the data should be read
1850 * @len: the number of samples in data to read
1852 * Read @len samples from the ringbuffer into the memory pointed
1854 * The first sample should be read from position @sample in
1857 * @len should not be a multiple of the segment size of the ringbuffer
1858 * although it is recommended.
1860 * Returns: The number of samples read from the ringbuffer or -1 on
1866 gst_ring_buffer_read (GstRingBuffer * buf, guint64 sample, guchar * data,
1870 gint segsize, segtotal, bps, sps;
1874 g_return_val_if_fail (GST_IS_RING_BUFFER (buf), -1);
1875 g_return_val_if_fail (buf->data != NULL, -1);
1876 g_return_val_if_fail (data != NULL, -1);
1878 dest = GST_BUFFER_DATA (buf->data);
1879 segsize = buf->spec.segsize;
1880 segtotal = buf->spec.segtotal;
1881 bps = buf->spec.bytes_per_sample;
1882 sps = buf->samples_per_seg;
1885 /* read enough samples */
1886 while (to_read > 0) {
1888 gint readseg, sampleoff;
1890 /* figure out the segment and the offset inside the segment where
1891 * the sample should be read from. */
1892 readseg = sample / sps;
1893 sampleoff = (sample % sps);
1898 /* get the currently processed segment */
1899 segdone = g_atomic_int_get (&buf->segdone) - buf->segbase;
1901 /* see how far away it is from the read segment, normally segdone (where
1902 * the hardware is writing) is bigger than readseg (where software is
1904 diff = segdone - readseg;
1907 ("pointer at %d, sample %" G_GUINT64_FORMAT
1908 ", read from %d-%d, to_read %d, diff %d, segtotal %d, segsize %d",
1909 segdone, sample, readseg, sampleoff, to_read, diff, segtotal,
1912 /* segment too far ahead, reader too slow */
1913 if (G_UNLIKELY (diff >= segtotal)) {
1914 /* pretend we read an empty segment. */
1915 sampleslen = MIN (sps, to_read);
1916 memcpy (data, buf->empty_seg, sampleslen * bps);
1920 /* read segment is within readable range, we can break the loop and
1921 * start reading the data. */
1925 /* else we need to wait for the segment to become readable. */
1926 if (!wait_segment (buf))
1930 /* we can read now */
1931 readseg = readseg % segtotal;
1932 sampleslen = MIN (sps - sampleoff, to_read);
1934 GST_DEBUG_OBJECT (buf, "read @%p seg %d, off %d, sampleslen %d",
1935 dest + readseg * segsize, readseg, sampleoff, sampleslen);
1937 memcpy (data, dest + (readseg * segsize) + (sampleoff * bps),
1938 (sampleslen * bps));
1941 to_read -= sampleslen;
1942 sample += sampleslen;
1943 data += sampleslen * bps;
1946 return len - to_read;
1951 GST_DEBUG_OBJECT (buf, "stopped processing");
1952 return len - to_read;
1957 * gst_ring_buffer_prepare_read:
1958 * @buf: the #GstRingBuffer to read from
1959 * @segment: the segment to read
1960 * @readptr: the pointer to the memory where samples can be read
1961 * @len: the number of bytes to read
1963 * Returns a pointer to memory where the data from segment @segment
1964 * can be found. This function is mostly used by subclasses.
1966 * Returns: FALSE if the buffer is not started.
1971 gst_ring_buffer_prepare_read (GstRingBuffer * buf, gint * segment,
1972 guint8 ** readptr, gint * len)
1977 g_return_val_if_fail (GST_IS_RING_BUFFER (buf), FALSE);
1979 if (buf->callback == NULL) {
1980 /* push mode, fail when nothing is started */
1981 if (g_atomic_int_get (&buf->state) != GST_RING_BUFFER_STATE_STARTED)
1985 g_return_val_if_fail (buf->data != NULL, FALSE);
1986 g_return_val_if_fail (segment != NULL, FALSE);
1987 g_return_val_if_fail (readptr != NULL, FALSE);
1988 g_return_val_if_fail (len != NULL, FALSE);
1990 data = GST_BUFFER_DATA (buf->data);
1992 /* get the position of the pointer */
1993 segdone = g_atomic_int_get (&buf->segdone);
1995 *segment = segdone % buf->spec.segtotal;
1996 *len = buf->spec.segsize;
1997 *readptr = data + *segment * *len;
1999 GST_LOG ("prepare read from segment %d (real %d) @%p",
2000 *segment, segdone, *readptr);
2002 /* callback to fill the memory with data, for pull based
2005 buf->callback (buf, *readptr, *len, buf->cb_data);
2011 * gst_ring_buffer_advance:
2012 * @buf: the #GstRingBuffer to advance
2013 * @advance: the number of segments written
2015 * Subclasses should call this function to notify the fact that
2016 * @advance segments are now processed by the device.
2021 gst_ring_buffer_advance (GstRingBuffer * buf, guint advance)
2023 g_return_if_fail (GST_IS_RING_BUFFER (buf));
2025 /* update counter */
2026 g_atomic_int_add (&buf->segdone, advance);
2028 /* the lock is already taken when the waiting flag is set,
2029 * we grab the lock as well to make sure the waiter is actually
2030 * waiting for the signal */
2031 if (g_atomic_int_compare_and_exchange (&buf->waiting, 1, 0)) {
2032 GST_OBJECT_LOCK (buf);
2033 GST_DEBUG_OBJECT (buf, "signal waiter");
2034 GST_RING_BUFFER_SIGNAL (buf);
2035 GST_OBJECT_UNLOCK (buf);
2040 * gst_ring_buffer_clear:
2041 * @buf: the #GstRingBuffer to clear
2042 * @segment: the segment to clear
2044 * Clear the given segment of the buffer with silence samples.
2045 * This function is used by subclasses.
2050 gst_ring_buffer_clear (GstRingBuffer * buf, gint segment)
2054 g_return_if_fail (GST_IS_RING_BUFFER (buf));
2056 /* no data means it's already cleared */
2057 if (G_UNLIKELY (buf->data == NULL))
2060 /* no empty_seg means it's not opened */
2061 if (G_UNLIKELY (buf->empty_seg == NULL))
2064 segment %= buf->spec.segtotal;
2066 data = GST_BUFFER_DATA (buf->data);
2067 data += segment * buf->spec.segsize;
2069 GST_LOG ("clear segment %d @%p", segment, data);
2071 memcpy (data, buf->empty_seg, buf->spec.segsize);
2075 * gst_ring_buffer_may_start:
2076 * @buf: the #GstRingBuffer
2077 * @allowed: the new value
2079 * Tell the ringbuffer that it is allowed to start playback when
2080 * the ringbuffer is filled with samples.
2087 gst_ring_buffer_may_start (GstRingBuffer * buf, gboolean allowed)
2089 g_return_if_fail (GST_IS_RING_BUFFER (buf));
2091 GST_LOG_OBJECT (buf, "may start: %d", allowed);
2092 g_atomic_int_set (&buf->abidata.ABI.may_start, allowed);