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
This commit is contained in:
Stefano Pigozzi 2013-04-20 10:02:23 +02:00
parent 6807906177
commit a66041a332
4 changed files with 161 additions and 74 deletions

View File

@ -84,7 +84,8 @@ SOURCES-$(ALSA) += audio/out/ao_alsa.c
SOURCES-$(CACA) += video/out/vo_caca.c
SOURCES-$(SDL) += audio/out/ao_sdl.c
SOURCES-$(SDL2) += video/out/vo_sdl.c
SOURCES-$(COREAUDIO) += audio/out/ao_coreaudio.c
SOURCES-$(COREAUDIO) += audio/out/ao_coreaudio.c \
audio/out/ao_coreaudio/ca_ringbuffer.c
SOURCES-$(COREVIDEO) += video/out/vo_corevideo.m
SOURCES-$(DIRECT3D) += video/out/vo_direct3d.c \
video/out/w32_common.c

View File

@ -48,21 +48,23 @@
#include "ao.h"
#include "audio/format.h"
#include "osdep/timer.h"
#include "libavutil/fifo.h"
#include "core/subopt-helper.h"
#include "ao_coreaudio/ca_ringbuffer_internal.h"
#define ca_msg(a, b, c ...) mp_msg(a, b, "AO: [coreaudio] " c)
static void audio_pause(struct ao *ao);
static void audio_resume(struct ao *ao);
static void reset(struct ao *ao);
struct ca_ringbuffer {
AVFifoBuffer *fifo;
int len;
int chunks;
int chunk_size;
};
static void print_buffer(struct ca_ringbuffer *buffer)
{
void *tctx = talloc_new(NULL);
ca_msg(MSGT_AO, MSGL_V, "%s\n", ca_ringbuffer_repr(buffer, tctx));
talloc_free(tctx);
}
struct priv
{
@ -93,54 +95,6 @@ struct priv
struct ca_ringbuffer *buffer;
};
static struct ca_ringbuffer *new_ca_ringbuffer(void *talloc_ctx, size_t chunks,
size_t 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;
}
static int ca_ringbuffer_buffered(struct ca_ringbuffer *buffer)
{
return av_fifo_size(buffer->fifo);
}
static void ca_ringbuffer_reset(struct ca_ringbuffer *buffer)
{
av_fifo_reset(buffer->fifo);
}
static 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;
}
static 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);
}
static OSStatus theRenderProc(void *inRefCon,
AudioUnitRenderActionFlags *inActionFlags,
const AudioTimeStamp *inTimeStamp,
@ -668,14 +622,10 @@ static int init(struct ao *ao, char *params)
ao->bps = ao->samplerate * inDesc.mBytesPerFrame;
ao->buffersize = ao->bps;
int chunk_size = maxFrames; //*inDesc.mBytesPerFrame;
int chunks = (ao->bps + chunk_size - 1) / chunk_size;
p->buffer = new_ca_ringbuffer(p, chunks, chunk_size);
ao->outburst = chunk_size;
p->buffer = ca_ringbuffer_new2(p, ao->bps, maxFrames);
ao->outburst = ca_ringbuffer_chunk_size(p->buffer);
ca_msg(MSGT_AO, MSGL_V,
"using %d chunks of %d bytes (buffer len %d bytes)\n",
p->buffer->chunks, p->buffer->chunk_size, p->buffer->len);
print_buffer(p->buffer);
renderCallback.inputProc = theRenderProc;
renderCallback.inputProcRefCon = ao;
@ -908,18 +858,11 @@ static int OpenSPDIF(struct ao *ao)
/* For ac3/dts, just use packet size 6144 bytes as chunk size. */
int chunk_size = p->stream_format.mBytesPerPacket;
int chunks = (ao->bps + chunk_size - 1) / chunk_size;
ao->outburst = chunk_size;
ao->buffersize = ao->bps;
p->buffer = ca_ringbuffer_new2(p, ao->bps, chunk_size);
ao->outburst = ca_ringbuffer_chunk_size(p->buffer);
p->buffer->chunks = (ao->bps + p->buffer->chunk_size - 1) / p->buffer->chunk_size;
p->buffer->len = p->buffer->chunks * p->buffer->chunk_size;
p->buffer = new_ca_ringbuffer(p, chunks, chunk_size);
ca_msg(MSGT_AO, MSGL_V,
"using %5d chunks of %d bytes (buffer len %d bytes)\n",
p->buffer->chunks, p->buffer->chunk_size, p->buffer->len);
print_buffer(p->buffer);
/* Create IOProc callback. */
err = AudioDeviceCreateIOProcID(p->i_selected_dev,
@ -1193,7 +1136,7 @@ static void reset(struct ao *ao)
static int get_space(struct ao *ao)
{
struct priv *p = ao->priv;
return p->buffer->len - ca_ringbuffer_buffered(p->buffer);
return ca_ringbuffer_available(p->buffer);
}

View File

@ -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));
}

View File

@ -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