diff options
Diffstat (limited to 'audio/out/ao_opensles.c')
-rw-r--r-- | audio/out/ao_opensles.c | 181 |
1 files changed, 98 insertions, 83 deletions
diff --git a/audio/out/ao_opensles.c b/audio/out/ao_opensles.c index 5357ab4920..ddcff1904a 100644 --- a/audio/out/ao_opensles.c +++ b/audio/out/ao_opensles.c @@ -23,30 +23,24 @@ #include "common/msg.h" #include "audio/format.h" #include "options/m_option.h" +#include "osdep/threads.h" #include "osdep/timer.h" #include <SLES/OpenSLES.h> #include <SLES/OpenSLES_Android.h> -#include <pthread.h> - struct priv { SLObjectItf sl, output_mix, player; SLBufferQueueItf buffer_queue; SLEngineItf engine; SLPlayItf play; - char *curbuf, *buf1, *buf2; - size_t buffer_size; - pthread_mutex_t buffer_lock; - - int cfg_frames_per_buffer; - int cfg_sample_rate; -}; + void *buf; + int bytes_per_enqueue; + mp_mutex buffer_lock; + double audio_latency; -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) \ @@ -67,12 +61,10 @@ static void uninit(struct ao *ao) p->engine = NULL; p->play = NULL; - pthread_mutex_destroy(&p->buffer_lock); + mp_mutex_destroy(&p->buffer_lock); - free(p->buf1); - free(p->buf2); - p->curbuf = p->buf1 = p->buf2 = NULL; - p->buffer_size = 0; + free(p->buf); + p->buf = NULL; } #undef DESTROY @@ -82,27 +74,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); + mp_mutex_lock(&p->buffer_lock); - data[0] = p->curbuf; - delay = 2 * p->buffer_size / (double)ao->bps; - ao_read_data(ao, data, p->buffer_size / ao->sstride, - mp_time_us() + 1000000LL * delay); + delay = p->frames_per_enqueue / (double)ao->samplerate; + delay += p->audio_latency; + ao_read_data(ao, &p->buf, p->frames_per_enqueue, + mp_time_ns() + MP_TIME_S_TO_NS(delay)); - res = (*buffer_queue)->Enqueue(buffer_queue, p->curbuf, 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); - else - p->curbuf = (p->curbuf == p->buf1) ? p->buf2 : p->buf1; - pthread_mutex_unlock(&p->buffer_lock); + mp_mutex_unlock(&p->buffer_lock); } -#define DEFAULT_BUFFER_SIZE_MS 50 - #define CHK(stmt) \ { \ SLresult res = stmt; \ @@ -117,12 +104,14 @@ 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; // This AO only supports two channels at the moment mp_chmap_from_channels(&ao->channels, 2); + // Upstream "Wilhelm" supports only 8000 <= rate <= 192000 + ao->samplerate = MPCLAMP(ao->samplerate, 8000, 192000); CHK(slCreateEngine(&p->sl, 0, NULL, 0, NULL, NULL)); CHK((*p->sl)->Realize(p->sl, SL_BOOLEAN_FALSE)); @@ -131,49 +120,56 @@ 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 = 2; - - pcm.formatType = SL_DATAFORMAT_PCM; - pcm.numChannels = 2; - - int compatible_formats[AF_FORMAT_COUNT]; - 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->cfg_sample_rate) - ao->samplerate = p->cfg_sample_rate; + 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->buf1 = calloc(1, p->buffer_size); - p->buf2 = calloc(1, p->buffer_size); - p->curbuf = p->buf1; - if (!p->buf1 || !p->buf2) { + 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); + + int r = mp_mutex_init(&p->buffer_lock); if (r) { MP_ERR(ao, "Failed to initialize the mutex: %d\n", r); goto error; @@ -188,16 +184,41 @@ static int init(struct ao *ao) audio_sink.pLocator = (void*)&locator_output_mix; audio_sink.pFormat = NULL; - SLboolean required[] = { SL_BOOLEAN_TRUE }; - SLInterfaceID iid_array[] = { SL_IID_BUFFERQUEUE }; + SLInterfaceID iid_array[] = { SL_IID_BUFFERQUEUE, SL_IID_ANDROIDCONFIGURATION }; + SLboolean required[] = { SL_BOOLEAN_TRUE, SL_BOOLEAN_FALSE }; CHK((*p->engine)->CreateAudioPlayer(p->engine, &p->player, &audio_source, - &audio_sink, 1, iid_array, required)); + &audio_sink, 2, iid_array, required)); + CHK((*p->player)->Realize(p->player, SL_BOOLEAN_FALSE)); CHK((*p->player)->GetInterface(p->player, SL_IID_PLAY, (void*)&p->play)); CHK((*p->player)->GetInterface(p->player, SL_IID_BUFFERQUEUE, (void*)&p->buffer_queue)); CHK((*p->buffer_queue)->RegisterCallback(p->buffer_queue, buffer_callback, ao)); + CHK((*p->play)->SetPlayState(p->play, SL_PLAYSTATE_PLAYING)); + + SLAndroidConfigurationItf android_config; + SLuint32 audio_latency = 0, value_size = sizeof(SLuint32); + + SLint32 get_interface_result = (*p->player)->GetInterface( + p->player, + SL_IID_ANDROIDCONFIGURATION, + &android_config + ); + + if (get_interface_result == SL_RESULT_SUCCESS) { + SLint32 get_configuration_result = (*android_config)->GetConfiguration( + android_config, + (const SLchar *)"androidGetAudioLatency", + &value_size, + &audio_latency + ); + + if (get_configuration_result == SL_RESULT_SUCCESS) { + p->audio_latency = (double)audio_latency / 1000.0; + MP_INFO(ao, "Device latency is %f\n", p->audio_latency); + } + } return 1; error: @@ -207,26 +228,15 @@ error: #undef CHK -static void set_play_state(struct ao *ao, SLuint32 state) -{ - struct priv *p = ao->priv; - SLresult res = (*p->play)->SetPlayState(p->play, state); - if (res != SL_RESULT_SUCCESS) - MP_ERR(ao, "Failed to SetPlayState(%d): %d\n", state, res); -} - static void reset(struct ao *ao) { - set_play_state(ao, SL_PLAYSTATE_STOPPED); + struct priv *p = ao->priv; + (*p->buffer_queue)->Clear(p->buffer_queue); } static void resume(struct ao *ao) { struct priv *p = ao->priv; - set_play_state(ao, SL_PLAYSTATE_PLAYING); - - // enqueue two buffers - buffer_callback(p->buffer_queue, ao); buffer_callback(p->buffer_queue, ao); } @@ -238,12 +248,17 @@ const struct ao_driver audio_out_opensles = { .init = init, .uninit = uninit, .reset = reset, - .resume = resume, + .start = 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, 10000), - OPT_INTRANGE("sample-rate", cfg_sample_rate, 0, 1000, 100000), + {"frames-per-enqueue", OPT_INT(frames_per_enqueue), + M_RANGE(1, 96000)}, + {"buffer-size-in-ms", OPT_INT(buffer_size_in_ms), + M_RANGE(0, 500)}, {0} }, .options_prefix = "opensles", |