diff options
author | Stefano Pigozzi <stefano.pigozzi@gmail.com> | 2013-04-20 10:02:23 +0200 |
---|---|---|
committer | Stefano Pigozzi <stefano.pigozzi@gmail.com> | 2013-06-16 18:20:39 +0200 |
commit | a66041a332970ced1473f44bce9516fc9ed5a16e (patch) | |
tree | 0dfe1b94087b93db8c056faf9f38db906813e97d /audio/out/ao_coreaudio | |
parent | 680790617704b294476f27f265fd4041f06ae5a9 (diff) | |
download | mpv-a66041a332970ced1473f44bce9516fc9ed5a16e.tar.bz2 mpv-a66041a332970ced1473f44bce9516fc9ed5a16e.tar.xz |
ao_coreaudio: split ringbuffer in it's own file
This is hopefully the start of something good. ca_ringbuffer_read and
ca_ringbuffer_write can probably cleaned up from all the NULL checks once
ao_coreaudio.c gets simplyfied.
Conflicts:
audio/out/ao_coreaudio.c
Diffstat (limited to 'audio/out/ao_coreaudio')
-rw-r--r-- | audio/out/ao_coreaudio/ca_ringbuffer.c | 106 | ||||
-rw-r--r-- | audio/out/ao_coreaudio/ca_ringbuffer_internal.h | 37 |
2 files changed, 143 insertions, 0 deletions
diff --git a/audio/out/ao_coreaudio/ca_ringbuffer.c b/audio/out/ao_coreaudio/ca_ringbuffer.c new file mode 100644 index 0000000000..717466945d --- /dev/null +++ b/audio/out/ao_coreaudio/ca_ringbuffer.c @@ -0,0 +1,106 @@ +/* + * 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)); +} diff --git a/audio/out/ao_coreaudio/ca_ringbuffer_internal.h b/audio/out/ao_coreaudio/ca_ringbuffer_internal.h new file mode 100644 index 0000000000..50b41871b9 --- /dev/null +++ b/audio/out/ao_coreaudio/ca_ringbuffer_internal.h @@ -0,0 +1,37 @@ +/* + * 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/>. + */ + +#ifndef MPV_AUDIO_OUT_CA_RINGBUFFER_H +#define MPV_AUDIO_OUT_CA_RINGBUFFER_H + +struct ca_ringbuffer; + +struct ca_ringbuffer *ca_ringbuffer_new(void *talloc_ctx, int chunks, int chunk_size); +struct ca_ringbuffer *ca_ringbuffer_new2(void *talloc_ctx, int bps, int chunk_size); +int ca_ringbuffer_read(struct ca_ringbuffer *buffer, unsigned char *data, int len); +int ca_ringbuffer_write(struct ca_ringbuffer *buffer, unsigned char *data, int len); + +void ca_ringbuffer_reset(struct ca_ringbuffer *buffer); + +int ca_ringbuffer_available(struct ca_ringbuffer *buffer); +int ca_ringbuffer_size(struct ca_ringbuffer *buffer); +int ca_ringbuffer_buffered(struct ca_ringbuffer *buffer); +int ca_ringbuffer_chunk_size(struct ca_ringbuffer *buffer); + +char *ca_ringbuffer_repr(struct ca_ringbuffer *buffer, void *talloc_ctx); + +#endif |