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"
|
|
|
|
#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;
|
2018-03-12 19:04:21 +00:00
|
|
|
char *buf;
|
2016-02-14 17:03:47 +00:00
|
|
|
size_t buffer_size;
|
|
|
|
pthread_mutex_t buffer_lock;
|
2018-03-11 02:55:44 +00:00
|
|
|
double audio_latency;
|
2016-02-14 17:03:47 +00:00
|
|
|
|
|
|
|
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 }
|
|
|
|
};
|
|
|
|
|
|
|
|
#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;
|
|
|
|
|
|
|
|
pthread_mutex_destroy(&p->buffer_lock);
|
|
|
|
|
2018-03-12 19:04:21 +00:00
|
|
|
free(p->buf);
|
|
|
|
p->buf = NULL;
|
2016-02-14 17:03:47 +00:00
|
|
|
p->buffer_size = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef DESTROY
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2018-03-12 19:04:21 +00:00
|
|
|
data[0] = p->buf;
|
2016-02-14 17:03:47 +00:00
|
|
|
delay = 2 * p->buffer_size / (double)ao->bps;
|
2018-03-11 02:55:44 +00:00
|
|
|
delay += p->audio_latency;
|
2016-02-14 17:03:47 +00:00
|
|
|
ao_read_data(ao, data, p->buffer_size / ao->sstride,
|
|
|
|
mp_time_us() + 1000000LL * delay);
|
|
|
|
|
2018-03-12 19:04:21 +00:00
|
|
|
res = (*buffer_queue)->Enqueue(buffer_queue, p->buf, p->buffer_size);
|
2016-02-14 17:03:47 +00:00
|
|
|
if (res != SL_RESULT_SUCCESS)
|
|
|
|
MP_ERR(ao, "Failed to Enqueue: %d\n", res);
|
|
|
|
|
|
|
|
pthread_mutex_unlock(&p->buffer_lock);
|
|
|
|
}
|
|
|
|
|
ao_opensles: bump device buffer size to 250ms
Although half (non-fast track on sink rate) or one-third (non-fast track not on sink rate) of the buffer size of the created AudioTrack instance as the SL Enqueue buffer size is basically enough for dropout-free playback, only using the full size can avoid stutter upon (re)start of playback.
Here are the various buffer sizes on different track/sink rate when on Bluetooth audio on Android O:
aptX @ 48kHz:
Sink rate: 48000 Hz
44100 Hz: 10632 frames (241.09 ms)
48000 Hz: 11544 frames (240.50 ms)
88200 Hz: 21216 frames (240.54 ms)
96000 Hz: 23088 frames (240.50 ms)
176400 Hz: 42384 frames (240.27 ms)
192000 Hz: 46128 frames (240.25 ms)
SBC/AAC/aptX @ 44.1kHz:
Sink rate: 44100 Hz
44100 Hz: 10776 frames (244.35 ms)
48000 Hz: 11748 frames (244.75 ms)
88200 Hz: 21552 frames (244.35 ms)
96000 Hz: 23448 frames (244.25 ms)
176400 Hz: 43056 frames (244.08 ms)
192000 Hz: 46848 frames (244.00 ms)
The above results were produced with the following code:
import android.media.AudioAttributes;
import android.media.AudioFormat;
import android.media.AudioTrack;
class AudioInfo {
public static void main(String[] args) {
int nosr = AudioTrack.getNativeOutputSampleRate(3);
System.out.printf("Sink rate: %d Hz\n", nosr);
int[] rates = {44100,48000,88200,96000,176400,192000};
for (int rate: rates) {
AudioAttributes aa = new AudioAttributes.Builder().setFlags(256).build();
AudioFormat af = new AudioFormat.Builder().setSampleRate(rate).build();
AudioTrack at = new AudioTrack(aa, af, 4, 1, 0);
int sr = at.getSampleRate();
int bs = at.getBufferSizeInFrames();
float ms = bs * (float) 1000 / sr;
at.release();
System.out.printf("%d Hz: %d frames (%.2f ms)\n", sr, bs, ms);
}
}
}
Therefore bumping the device buffer size to 250ms.
2018-03-12 19:06:23 +00:00
|
|
|
#define DEFAULT_BUFFER_SIZE_MS 250
|
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;
|
|
|
|
SLDataFormat_PCM pcm;
|
|
|
|
SLDataSource audio_source;
|
|
|
|
SLDataSink audio_sink;
|
|
|
|
|
|
|
|
// This AO only supports two channels at the moment
|
|
|
|
mp_chmap_from_channels(&ao->channels, 2);
|
|
|
|
|
|
|
|
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-03-12 19:04:21 +00:00
|
|
|
locator_buffer_queue.numBuffers = 1;
|
2016-02-14 17:03:47 +00:00
|
|
|
|
|
|
|
pcm.formatType = SL_DATAFORMAT_PCM;
|
|
|
|
pcm.numChannels = 2;
|
|
|
|
|
2018-01-23 23:02:13 +00:00
|
|
|
int compatible_formats[AF_FORMAT_COUNT + 1];
|
2016-02-14 17:03:47 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
pcm.containerSize = 8 * af_fmt_to_bytes(ao->format);
|
|
|
|
pcm.channelMask = SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT;
|
|
|
|
pcm.endianness = SL_BYTEORDER_LITTLEENDIAN;
|
|
|
|
|
|
|
|
// samplesPerSec is misnamed, actually it's samples per ms
|
|
|
|
pcm.samplesPerSec = ao->samplerate * 1000;
|
|
|
|
|
|
|
|
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 *
|
|
|
|
af_fmt_to_bytes(ao->format);
|
2018-03-12 19:04:21 +00:00
|
|
|
p->buf = calloc(1, p->buffer_size);
|
|
|
|
if (!p->buf) {
|
2016-02-14 17:03:47 +00:00
|
|
|
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);
|
|
|
|
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,
|
|
|
|
.resume = resume,
|
|
|
|
|
|
|
|
.priv_size = sizeof(struct priv),
|
|
|
|
.options = (const struct m_option[]) {
|
2018-03-12 19:31:35 +00:00
|
|
|
OPT_INTRANGE("frames-per-buffer", cfg_frames_per_buffer, 0, 1, 96000),
|
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
|
|
|
};
|