2016-02-14 17:03:47 +00:00
|
|
|
/*
|
|
|
|
* OpenSL ES audio output driver.
|
|
|
|
* Copyright (C) 2016 Ilya Zhuravlev <whatever@xyz.is>
|
|
|
|
*
|
|
|
|
* This file is part of mpv.
|
|
|
|
*
|
|
|
|
* mpv is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 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 Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with mpv. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "ao.h"
|
|
|
|
#include "internal.h"
|
|
|
|
#include "common/msg.h"
|
|
|
|
#include "audio/format.h"
|
|
|
|
#include "options/m_option.h"
|
2023-10-21 02:55:41 +00:00
|
|
|
#include "osdep/threads.h"
|
2016-02-14 17:03:47 +00:00
|
|
|
#include "osdep/timer.h"
|
|
|
|
|
|
|
|
#include <SLES/OpenSLES.h>
|
|
|
|
#include <SLES/OpenSLES_Android.h>
|
|
|
|
|
|
|
|
struct priv {
|
|
|
|
SLObjectItf sl, output_mix, player;
|
|
|
|
SLBufferQueueItf buffer_queue;
|
|
|
|
SLEngineItf engine;
|
|
|
|
SLPlayItf play;
|
2018-07-20 19:48:45 +00:00
|
|
|
void *buf;
|
|
|
|
int bytes_per_enqueue;
|
2023-10-21 02:55:41 +00:00
|
|
|
mp_mutex buffer_lock;
|
2018-03-11 02:55:44 +00:00
|
|
|
double audio_latency;
|
2016-02-14 17:03:47 +00:00
|
|
|
|
2018-07-20 19:48:45 +00:00
|
|
|
int frames_per_enqueue;
|
|
|
|
int buffer_size_in_ms;
|
2016-02-14 17:03:47 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#define DESTROY(thing) \
|
|
|
|
if (p->thing) { \
|
|
|
|
(*p->thing)->Destroy(p->thing); \
|
|
|
|
p->thing = NULL; \
|
|
|
|
}
|
|
|
|
|
|
|
|
static void uninit(struct ao *ao)
|
|
|
|
{
|
|
|
|
struct priv *p = ao->priv;
|
|
|
|
|
|
|
|
DESTROY(player);
|
|
|
|
DESTROY(output_mix);
|
|
|
|
DESTROY(sl);
|
|
|
|
|
|
|
|
p->buffer_queue = NULL;
|
|
|
|
p->engine = NULL;
|
|
|
|
p->play = NULL;
|
|
|
|
|
2023-10-21 02:55:41 +00:00
|
|
|
mp_mutex_destroy(&p->buffer_lock);
|
2016-02-14 17:03:47 +00:00
|
|
|
|
2018-03-12 19:04:21 +00:00
|
|
|
free(p->buf);
|
|
|
|
p->buf = NULL;
|
2016-02-14 17:03:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#undef DESTROY
|
|
|
|
|
|
|
|
static void buffer_callback(SLBufferQueueItf buffer_queue, void *context)
|
|
|
|
{
|
|
|
|
struct ao *ao = context;
|
|
|
|
struct priv *p = ao->priv;
|
|
|
|
SLresult res;
|
|
|
|
double delay;
|
|
|
|
|
2023-10-21 02:55:41 +00:00
|
|
|
mp_mutex_lock(&p->buffer_lock);
|
2016-02-14 17:03:47 +00:00
|
|
|
|
2019-08-15 13:36:16 +00:00
|
|
|
delay = p->frames_per_enqueue / (double)ao->samplerate;
|
2018-03-11 02:55:44 +00:00
|
|
|
delay += p->audio_latency;
|
2018-07-20 19:48:45 +00:00
|
|
|
ao_read_data(ao, &p->buf, p->frames_per_enqueue,
|
2023-10-11 18:45:40 +00:00
|
|
|
mp_time_ns() + MP_TIME_S_TO_NS(delay));
|
2016-02-14 17:03:47 +00:00
|
|
|
|
2018-07-20 19:48:45 +00:00
|
|
|
res = (*buffer_queue)->Enqueue(buffer_queue, p->buf, p->bytes_per_enqueue);
|
2016-02-14 17:03:47 +00:00
|
|
|
if (res != SL_RESULT_SUCCESS)
|
|
|
|
MP_ERR(ao, "Failed to Enqueue: %d\n", res);
|
|
|
|
|
2023-10-21 02:55:41 +00:00
|
|
|
mp_mutex_unlock(&p->buffer_lock);
|
2016-02-14 17:03:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#define CHK(stmt) \
|
|
|
|
{ \
|
|
|
|
SLresult res = stmt; \
|
|
|
|
if (res != SL_RESULT_SUCCESS) { \
|
|
|
|
MP_ERR(ao, "%s: %d\n", #stmt, res); \
|
|
|
|
goto error; \
|
|
|
|
} \
|
|
|
|
}
|
|
|
|
|
|
|
|
static int init(struct ao *ao)
|
|
|
|
{
|
|
|
|
struct priv *p = ao->priv;
|
|
|
|
SLDataLocator_BufferQueue locator_buffer_queue;
|
|
|
|
SLDataLocator_OutputMix locator_output_mix;
|
2018-07-15 17:22:22 +00:00
|
|
|
SLAndroidDataFormat_PCM_EX pcm;
|
2016-02-14 17:03:47 +00:00
|
|
|
SLDataSource audio_source;
|
|
|
|
SLDataSink audio_sink;
|
|
|
|
|
|
|
|
// This AO only supports two channels at the moment
|
|
|
|
mp_chmap_from_channels(&ao->channels, 2);
|
2021-11-19 04:40:06 +00:00
|
|
|
// Upstream "Wilhelm" supports only 8000 <= rate <= 192000
|
|
|
|
ao->samplerate = MPCLAMP(ao->samplerate, 8000, 192000);
|
2016-02-14 17:03:47 +00:00
|
|
|
|
|
|
|
CHK(slCreateEngine(&p->sl, 0, NULL, 0, NULL, NULL));
|
|
|
|
CHK((*p->sl)->Realize(p->sl, SL_BOOLEAN_FALSE));
|
|
|
|
CHK((*p->sl)->GetInterface(p->sl, SL_IID_ENGINE, (void*)&p->engine));
|
|
|
|
CHK((*p->engine)->CreateOutputMix(p->engine, &p->output_mix, 0, NULL, NULL));
|
|
|
|
CHK((*p->output_mix)->Realize(p->output_mix, SL_BOOLEAN_FALSE));
|
|
|
|
|
|
|
|
locator_buffer_queue.locatorType = SL_DATALOCATOR_BUFFERQUEUE;
|
2018-08-07 07:45:23 +00:00
|
|
|
locator_buffer_queue.numBuffers = 8;
|
2016-02-14 17:03:47 +00:00
|
|
|
|
2018-07-15 17:22:22 +00:00
|
|
|
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;
|
2016-02-14 17:03:47 +00:00
|
|
|
}
|
2018-07-15 17:22:22 +00:00
|
|
|
pcm.numChannels = ao->channels.num;
|
|
|
|
pcm.containerSize = pcm.bitsPerSample = 8 * af_fmt_to_bytes(ao->format);
|
2016-02-14 17:03:47 +00:00
|
|
|
pcm.channelMask = SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT;
|
|
|
|
pcm.endianness = SL_BYTEORDER_LITTLEENDIAN;
|
2018-07-15 17:22:22 +00:00
|
|
|
pcm.sampleRate = ao->samplerate * 1000;
|
2016-02-14 17:03:47 +00:00
|
|
|
|
2018-07-20 19:48:45 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
p->bytes_per_enqueue = p->frames_per_enqueue * ao->channels.num *
|
2016-02-14 17:03:47 +00:00
|
|
|
af_fmt_to_bytes(ao->format);
|
2018-07-20 19:48:45 +00:00
|
|
|
p->buf = calloc(1, p->bytes_per_enqueue);
|
2018-03-12 19:04:21 +00:00
|
|
|
if (!p->buf) {
|
2016-02-14 17:03:47 +00:00
|
|
|
MP_ERR(ao, "Failed to allocate device buffer\n");
|
|
|
|
goto error;
|
|
|
|
}
|
2018-07-20 19:48:45 +00:00
|
|
|
|
2023-10-21 02:55:41 +00:00
|
|
|
int r = mp_mutex_init(&p->buffer_lock);
|
2016-02-14 17:03:47 +00:00
|
|
|
if (r) {
|
|
|
|
MP_ERR(ao, "Failed to initialize the mutex: %d\n", r);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
audio_source.pFormat = (void*)&pcm;
|
|
|
|
audio_source.pLocator = (void*)&locator_buffer_queue;
|
|
|
|
|
|
|
|
locator_output_mix.locatorType = SL_DATALOCATOR_OUTPUTMIX;
|
|
|
|
locator_output_mix.outputMix = p->output_mix;
|
|
|
|
|
|
|
|
audio_sink.pLocator = (void*)&locator_output_mix;
|
|
|
|
audio_sink.pFormat = NULL;
|
|
|
|
|
2018-03-11 02:55:44 +00:00
|
|
|
SLInterfaceID iid_array[] = { SL_IID_BUFFERQUEUE, SL_IID_ANDROIDCONFIGURATION };
|
|
|
|
SLboolean required[] = { SL_BOOLEAN_TRUE, SL_BOOLEAN_FALSE };
|
2016-02-14 17:03:47 +00:00
|
|
|
CHK((*p->engine)->CreateAudioPlayer(p->engine, &p->player, &audio_source,
|
2018-03-11 02:55:44 +00:00
|
|
|
&audio_sink, 2, iid_array, required));
|
|
|
|
|
2016-02-14 17:03:47 +00:00
|
|
|
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));
|
2018-01-24 06:21:52 +00:00
|
|
|
CHK((*p->play)->SetPlayState(p->play, SL_PLAYSTATE_PLAYING));
|
2016-02-14 17:03:47 +00:00
|
|
|
|
2018-03-11 02:55:44 +00:00
|
|
|
SLAndroidConfigurationItf android_config;
|
|
|
|
SLuint32 audio_latency = 0, value_size = sizeof(SLuint32);
|
2018-03-24 01:30:51 +00:00
|
|
|
|
|
|
|
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,
|
2018-03-11 02:55:44 +00:00
|
|
|
(const SLchar *)"androidGetAudioLatency",
|
2018-03-24 01:30:51 +00:00
|
|
|
&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);
|
|
|
|
}
|
2018-03-11 02:55:44 +00:00
|
|
|
}
|
|
|
|
|
2016-02-14 17:03:47 +00:00
|
|
|
return 1;
|
|
|
|
error:
|
|
|
|
uninit(ao);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef CHK
|
|
|
|
|
|
|
|
static void reset(struct ao *ao)
|
|
|
|
{
|
2018-01-24 00:23:37 +00:00
|
|
|
struct priv *p = ao->priv;
|
|
|
|
(*p->buffer_queue)->Clear(p->buffer_queue);
|
2016-02-14 17:03:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void resume(struct ao *ao)
|
|
|
|
{
|
|
|
|
struct priv *p = ao->priv;
|
|
|
|
buffer_callback(p->buffer_queue, ao);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define OPT_BASE_STRUCT struct priv
|
|
|
|
|
|
|
|
const struct ao_driver audio_out_opensles = {
|
|
|
|
.description = "OpenSL ES audio output",
|
|
|
|
.name = "opensles",
|
|
|
|
.init = init,
|
|
|
|
.uninit = uninit,
|
|
|
|
.reset = reset,
|
audio: redo internal AO API
This affects "pull" AOs only: ao_alsa, ao_pulse, ao_openal, ao_pcm,
ao_lavc. There are changes to the other AOs too, but that's only about
renaming ao_driver.resume to ao_driver.start.
ao_openal is broken because I didn't manage to fix it, so it exits with
an error message. If you want it, why don't _you_ put effort into it? I
see no reason to waste my own precious lifetime over this (I realize the
irony).
ao_alsa loses the poll() mechanism, but it was mostly broken and didn't
really do what it was supposed to. There doesn't seem to be anything in
the ALSA API to watch the playback status without polling (unless you
want to use raw UNIX signals).
No idea if ao_pulse is correct, or whether it's subtly broken now. There
is no documentation, so I can't tell what is correct, without reverse
engineering the whole project. I recommend using ALSA.
This was supposed to be just a simple fix, but somehow it expanded scope
like a train wreck. Very high chance of regressions, but probably only
for the AOs listed above. The rest you can figure out from reading the
diff.
2020-05-31 13:00:35 +00:00
|
|
|
.start = resume,
|
2016-02-14 17:03:47 +00:00
|
|
|
|
|
|
|
.priv_size = sizeof(struct priv),
|
2018-07-20 19:48:45 +00:00
|
|
|
.priv_defaults = &(const struct priv) {
|
|
|
|
.buffer_size_in_ms = 250,
|
|
|
|
},
|
2016-02-14 17:03:47 +00:00
|
|
|
.options = (const struct m_option[]) {
|
options: change option macros and all option declarations
Change all OPT_* macros such that they don't define the entire m_option
initializer, and instead expand only to a part of it, which sets certain
fields. This requires changing almost every option declaration, because
they all use these macros. A declaration now always starts with
{"name", ...
followed by designated initializers only (possibly wrapped in macros).
The OPT_* macros now initialize the .offset and .type fields only,
sometimes also .priv and others.
I think this change makes the option macros less tricky. The old code
had to stuff everything into macro arguments (and attempted to allow
setting arbitrary fields by letting the user pass designated
initializers in the vararg parts). Some of this was made messy due to
C99 and C11 not allowing 0-sized varargs with ',' removal. It's also
possible that this change is pointless, other than cosmetic preferences.
Not too happy about some things. For example, the OPT_CHOICE()
indentation I applied looks a bit ugly.
Much of this change was done with regex search&replace, but some places
required manual editing. In particular, code in "obscure" areas (which I
didn't include in compilation) might be broken now.
In wayland_common.c the author of some option declarations confused the
flags parameter with the default value (though the default value was
also properly set below). I fixed this with this change.
2020-03-14 20:28:01 +00:00
|
|
|
{"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)},
|
2016-02-14 17:03:47 +00:00
|
|
|
{0}
|
|
|
|
},
|
2016-11-25 20:00:39 +00:00
|
|
|
.options_prefix = "opensles",
|
2016-02-14 17:03:47 +00:00
|
|
|
};
|