ao_opensles: allow s32 and float output

OpenSLES (and its AudioTrack backend) in Android can take 32-bit
fixed and floating point input since Android L (API 21).
This commit is contained in:
Tom Yan 2018-07-16 01:22:22 +08:00 committed by sfan5
parent 4e91cb72ef
commit 8baad91e7b
1 changed files with 15 additions and 27 deletions

View File

@ -43,12 +43,6 @@ struct priv {
int cfg_frames_per_buffer; 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) \ #define DESTROY(thing) \
if (p->thing) { \ if (p->thing) { \
(*p->thing)->Destroy(p->thing); \ (*p->thing)->Destroy(p->thing); \
@ -115,7 +109,7 @@ static int init(struct ao *ao)
struct priv *p = ao->priv; struct priv *p = ao->priv;
SLDataLocator_BufferQueue locator_buffer_queue; SLDataLocator_BufferQueue locator_buffer_queue;
SLDataLocator_OutputMix locator_output_mix; SLDataLocator_OutputMix locator_output_mix;
SLDataFormat_PCM pcm; SLAndroidDataFormat_PCM_EX pcm;
SLDataSource audio_source; SLDataSource audio_source;
SLDataSink audio_sink; SLDataSink audio_sink;
@ -131,29 +125,23 @@ static int init(struct ao *ao)
locator_buffer_queue.locatorType = SL_DATALOCATOR_BUFFERQUEUE; locator_buffer_queue.locatorType = SL_DATALOCATOR_BUFFERQUEUE;
locator_buffer_queue.numBuffers = 1; locator_buffer_queue.numBuffers = 1;
pcm.formatType = SL_DATAFORMAT_PCM; if (af_fmt_is_int(ao->format)) {
pcm.numChannels = 2; // Be future-proof
if (af_fmt_to_bytes(ao->format) > 2)
int compatible_formats[AF_FORMAT_COUNT + 1]; ao->format = AF_FORMAT_S32;
af_get_best_sample_formats(ao->format, compatible_formats); else
pcm.bitsPerSample = 0; ao->format = af_fmt_from_planar(ao->format);
for (int i = 0; compatible_formats[i] && !pcm.bitsPerSample; ++i) pcm.formatType = SL_DATAFORMAT_PCM;
for (int j = 0; fmtmap[j][0]; ++j) } else {
if (compatible_formats[i] == fmtmap[j][0]) { ao->format = AF_FORMAT_FLOAT;
ao->format = fmtmap[j][0]; pcm.formatType = SL_ANDROID_DATAFORMAT_PCM_EX;
pcm.bitsPerSample = fmtmap[j][1]; pcm.representation = SL_ANDROID_PCM_REPRESENTATION_FLOAT;
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.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.channelMask = SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT;
pcm.endianness = SL_BYTEORDER_LITTLEENDIAN; pcm.endianness = SL_BYTEORDER_LITTLEENDIAN;
pcm.sampleRate = ao->samplerate * 1000;
// samplesPerSec is misnamed, actually it's samples per ms
pcm.samplesPerSec = ao->samplerate * 1000;
if (p->cfg_frames_per_buffer) if (p->cfg_frames_per_buffer)
ao->device_buffer = p->cfg_frames_per_buffer; ao->device_buffer = p->cfg_frames_per_buffer;