summaryrefslogtreecommitdiffstats
path: root/audio/out/ao_coreaudio/ca_ringbuffer.c
diff options
context:
space:
mode:
authorStefano Pigozzi <stefano.pigozzi@gmail.com>2013-05-23 21:23:32 +0200
committerStefano Pigozzi <stefano.pigozzi@gmail.com>2013-06-16 18:20:39 +0200
commitbff03a181f8c4102a75144f818ea15ea53165170 (patch)
treee3c34701fdc88782cde8772baadcece5ad62b12a /audio/out/ao_coreaudio/ca_ringbuffer.c
parentb537467fd33d55b743bbf8fcac562d3ade7cb7c3 (diff)
downloadmpv-bff03a181f8c4102a75144f818ea15ea53165170.tar.bz2
mpv-bff03a181f8c4102a75144f818ea15ea53165170.tar.xz
core: add a spsc ringbuffer implementation
Currently every single AO was implementing it's own ringbuffer, many times with slightly different semantics. This is an attempt to fix the problem. I stole some good ideas from ao_portaudio's ringbuffer and went from there. The main difference is this one stores wpos and rpos which are absolute positions in an "infinite" buffer. To find the actual position for writing / reading just apply modulo size. The producer only modifies wpos while the consumer only modifies rpos. This makes it pretty easy to reason about and make the operations thread safe by using barriers (thread safety is guaranteed only in the Single-Producer/Single- Consumer case). Also adapted ao_coreaudio to use this ringbuffer.
Diffstat (limited to 'audio/out/ao_coreaudio/ca_ringbuffer.c')
-rw-r--r--audio/out/ao_coreaudio/ca_ringbuffer.c106
1 files changed, 0 insertions, 106 deletions
diff --git a/audio/out/ao_coreaudio/ca_ringbuffer.c b/audio/out/ao_coreaudio/ca_ringbuffer.c
deleted file mode 100644
index 717466945d..0000000000
--- a/audio/out/ao_coreaudio/ca_ringbuffer.c
+++ /dev/null
@@ -1,106 +0,0 @@
-/*
- * This file is part of mpv.
- *
- * mpv is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * mpv is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with mpv. If not, see <http://www.gnu.org/licenses/>.
- */
-
-#include <libavutil/fifo.h>
-#include "talloc.h"
-
-#include "ca_ringbuffer_internal.h"
-
-struct ca_ringbuffer {
- AVFifoBuffer *fifo;
- int len;
- int chunks;
- int chunk_size;
-};
-
-struct ca_ringbuffer *ca_ringbuffer_new(void *talloc_ctx, int chunks, int chunk_size)
-{
- struct ca_ringbuffer *buffer =
- talloc_zero(talloc_ctx, struct ca_ringbuffer);
-
- *buffer = (struct ca_ringbuffer) {
- .fifo = av_fifo_alloc(chunks * chunk_size),
- .len = chunks * chunk_size,
- .chunks = chunks,
- .chunk_size = chunk_size,
- };
-
- return buffer;
-}
-
-struct ca_ringbuffer *ca_ringbuffer_new2(void *talloc_ctx, int bps, int chunk_size)
-{
- int chunks = (bps + chunk_size - 1) / chunk_size;
- return ca_ringbuffer_new(talloc_ctx, chunks, chunk_size);
-}
-
-int ca_ringbuffer_read(struct ca_ringbuffer *buffer,
- unsigned char *data, int len)
-{
- int buffered = ca_ringbuffer_buffered(buffer);
- if (len > buffered)
- len = buffered;
- if (data)
- av_fifo_generic_read(buffer->fifo, data, len, NULL);
- else
- av_fifo_drain(buffer->fifo, len);
- return len;
-}
-
-int ca_ringbuffer_write(struct ca_ringbuffer *buffer,
- unsigned char *data, int len)
-{
- int free = buffer->len - av_fifo_size(buffer->fifo);
- if (len > free)
- len = free;
- return av_fifo_generic_write(buffer->fifo, data, len, NULL);
-}
-
-void ca_ringbuffer_reset(struct ca_ringbuffer *buffer)
-{
- av_fifo_reset(buffer->fifo);
-}
-
-int ca_ringbuffer_available(struct ca_ringbuffer *buffer)
-{
- return ca_ringbuffer_size(buffer) - ca_ringbuffer_buffered(buffer);
-}
-
-int ca_ringbuffer_size(struct ca_ringbuffer *buffer)
-{
- return buffer->len;
-}
-
-int ca_ringbuffer_buffered(struct ca_ringbuffer *buffer)
-{
- return av_fifo_size(buffer->fifo);
-}
-
-int ca_ringbuffer_chunk_size(struct ca_ringbuffer *buffer)
-{
- return buffer->chunk_size;
-}
-
-char *ca_ringbuffer_repr(struct ca_ringbuffer *buffer, void *talloc_ctx)
-{
- return talloc_asprintf(
- talloc_ctx,
- "Ringbuffer { .chunks = %d bytes, .chunk_size = %d bytes, .size = %d bytes }",
- buffer->chunks,
- ca_ringbuffer_chunk_size(buffer),
- ca_ringbuffer_size(buffer));
-}