diff options
Diffstat (limited to 'audio/out/ao_opensles.c')
-rw-r--r-- | audio/out/ao_opensles.c | 98 |
1 files changed, 52 insertions, 46 deletions
diff --git a/audio/out/ao_opensles.c b/audio/out/ao_opensles.c index ea48de892e..8c24320406 100644 --- a/audio/out/ao_opensles.c +++ b/audio/out/ao_opensles.c @@ -35,18 +35,13 @@ struct priv { SLBufferQueueItf buffer_queue; SLEngineItf engine; SLPlayItf play; - char *buf; - size_t buffer_size; + void *buf; + int bytes_per_enqueue; pthread_mutex_t buffer_lock; double audio_latency; - int cfg_frames_per_buffer; -}; - -static const int fmtmap[][2] = { - { AF_FORMAT_U8, SL_PCMSAMPLEFORMAT_FIXED_8 }, - { AF_FORMAT_S16, SL_PCMSAMPLEFORMAT_FIXED_16 }, - { 0 } + int frames_per_enqueue; + int buffer_size_in_ms; }; #define DESTROY(thing) \ @@ -71,7 +66,6 @@ static void uninit(struct ao *ao) free(p->buf); p->buf = NULL; - p->buffer_size = 0; } #undef DESTROY @@ -81,26 +75,22 @@ static void buffer_callback(SLBufferQueueItf buffer_queue, void *context) struct ao *ao = context; struct priv *p = ao->priv; SLresult res; - void *data[1]; double delay; pthread_mutex_lock(&p->buffer_lock); - data[0] = p->buf; - delay = 2 * p->buffer_size / (double)ao->bps; + delay = 2 * p->frames_per_enqueue / (double)ao->samplerate; delay += p->audio_latency; - ao_read_data(ao, data, p->buffer_size / ao->sstride, + ao_read_data(ao, &p->buf, p->frames_per_enqueue, mp_time_us() + 1000000LL * delay); - res = (*buffer_queue)->Enqueue(buffer_queue, p->buf, p->buffer_size); + res = (*buffer_queue)->Enqueue(buffer_queue, p->buf, p->bytes_per_enqueue); if (res != SL_RESULT_SUCCESS) MP_ERR(ao, "Failed to Enqueue: %d\n", res); pthread_mutex_unlock(&p->buffer_lock); } -#define DEFAULT_BUFFER_SIZE_MS 250 - #define CHK(stmt) \ { \ SLresult res = stmt; \ @@ -115,7 +105,7 @@ static int init(struct ao *ao) struct priv *p = ao->priv; SLDataLocator_BufferQueue locator_buffer_queue; SLDataLocator_OutputMix locator_output_mix; - SLDataFormat_PCM pcm; + SLAndroidDataFormat_PCM_EX pcm; SLDataSource audio_source; SLDataSink audio_sink; @@ -129,43 +119,55 @@ static int init(struct ao *ao) CHK((*p->output_mix)->Realize(p->output_mix, SL_BOOLEAN_FALSE)); locator_buffer_queue.locatorType = SL_DATALOCATOR_BUFFERQUEUE; - locator_buffer_queue.numBuffers = 1; - - pcm.formatType = SL_DATAFORMAT_PCM; - pcm.numChannels = 2; - - int compatible_formats[AF_FORMAT_COUNT + 1]; - af_get_best_sample_formats(ao->format, compatible_formats); - pcm.bitsPerSample = 0; - for (int i = 0; compatible_formats[i] && !pcm.bitsPerSample; ++i) - for (int j = 0; fmtmap[j][0]; ++j) - if (compatible_formats[i] == fmtmap[j][0]) { - ao->format = fmtmap[j][0]; - pcm.bitsPerSample = fmtmap[j][1]; - break; - } - if (!pcm.bitsPerSample) { - MP_ERR(ao, "Cannot find compatible audio format\n"); - goto error; + locator_buffer_queue.numBuffers = 8; + + if (af_fmt_is_int(ao->format)) { + // Be future-proof + if (af_fmt_to_bytes(ao->format) > 2) + ao->format = AF_FORMAT_S32; + else + ao->format = af_fmt_from_planar(ao->format); + pcm.formatType = SL_DATAFORMAT_PCM; + } else { + ao->format = AF_FORMAT_FLOAT; + pcm.formatType = SL_ANDROID_DATAFORMAT_PCM_EX; + pcm.representation = SL_ANDROID_PCM_REPRESENTATION_FLOAT; } - pcm.containerSize = 8 * af_fmt_to_bytes(ao->format); + pcm.numChannels = ao->channels.num; + pcm.containerSize = pcm.bitsPerSample = 8 * af_fmt_to_bytes(ao->format); pcm.channelMask = SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT; pcm.endianness = SL_BYTEORDER_LITTLEENDIAN; + pcm.sampleRate = ao->samplerate * 1000; + + if (p->buffer_size_in_ms) { + ao->device_buffer = ao->samplerate * p->buffer_size_in_ms / 1000; + // As the purpose of buffer_size_in_ms is to request a specific + // soft buffer size: + ao->def_buffer = 0; + } - // samplesPerSec is misnamed, actually it's samples per ms - pcm.samplesPerSec = ao->samplerate * 1000; + // But it does not make sense if it is smaller than the enqueue size: + if (p->frames_per_enqueue) { + ao->device_buffer = MPMAX(ao->device_buffer, p->frames_per_enqueue); + } else { + if (ao->device_buffer) { + p->frames_per_enqueue = ao->device_buffer; + } else if (ao->def_buffer) { + p->frames_per_enqueue = ao->def_buffer * ao->samplerate; + } else { + MP_ERR(ao, "Enqueue size is not set and can neither be derived\n"); + goto error; + } + } - if (p->cfg_frames_per_buffer) - ao->device_buffer = p->cfg_frames_per_buffer; - else - ao->device_buffer = ao->samplerate * DEFAULT_BUFFER_SIZE_MS / 1000; - p->buffer_size = ao->device_buffer * ao->channels.num * + p->bytes_per_enqueue = p->frames_per_enqueue * ao->channels.num * af_fmt_to_bytes(ao->format); - p->buf = calloc(1, p->buffer_size); + p->buf = calloc(1, p->bytes_per_enqueue); if (!p->buf) { MP_ERR(ao, "Failed to allocate device buffer\n"); goto error; } + int r = pthread_mutex_init(&p->buffer_lock, NULL); if (r) { MP_ERR(ao, "Failed to initialize the mutex: %d\n", r); @@ -248,8 +250,12 @@ const struct ao_driver audio_out_opensles = { .resume = resume, .priv_size = sizeof(struct priv), + .priv_defaults = &(const struct priv) { + .buffer_size_in_ms = 250, + }, .options = (const struct m_option[]) { - OPT_INTRANGE("frames-per-buffer", cfg_frames_per_buffer, 0, 1, 96000), + OPT_INTRANGE("frames-per-enqueue", frames_per_enqueue, 0, 1, 96000), + OPT_INTRANGE("buffer-size-in-ms", buffer_size_in_ms, 0, 0, 500), {0} }, .options_prefix = "opensles", |