diff --git a/src/opensles/AudioInputStreamOpenSLES.cpp b/src/opensles/AudioInputStreamOpenSLES.cpp index 323576ca1..74c56d8f9 100644 --- a/src/opensles/AudioInputStreamOpenSLES.cpp +++ b/src/opensles/AudioInputStreamOpenSLES.cpp @@ -95,6 +95,7 @@ Result AudioInputStreamOpenSLES::open() { channelCountToChannelMask(mChannelCount), // channelMask getDefaultByteOrder(), }; + bool usingExtendedDataFormat = false; SLDataSink audioSink = {&loc_bufq, &format_pcm}; @@ -111,6 +112,7 @@ Result AudioInputStreamOpenSLES::open() { format_pcm_ex = OpenSLES_createExtendedFormat(format_pcm, representation); // Use in place of the previous format. audioSink.pFormat = &format_pcm_ex; + usingExtendedDataFormat = true; } @@ -124,11 +126,29 @@ Result AudioInputStreamOpenSLES::open() { SLresult result = EngineOpenSLES::getInstance().createAudioRecorder(&mObjectInterface, &audioSrc, &audioSink); + + // If the result is "invalid parameter" and we're using the extended data format it could be + // that the underlying hardware doesn't support it + // (e.g. Moto G: https://github.com/google/oboe/issues/192) + // Try again using the old format + if (result == SL_RESULT_PARAMETER_INVALID && usingExtendedDataFormat){ + + LOGW("Unable to open recording stream using PCM_EX format, trying again using PCM..."); + audioSink.pFormat = &format_pcm; + result = EngineOpenSLES::getInstance().createAudioRecorder(&mObjectInterface, + &audioSrc, + &audioSink); + } + if (SL_RESULT_SUCCESS != result) { LOGE("createAudioRecorder() result:%s", getSLErrStr(result)); goto error; } + // If the stream is not using the extended data format it must have a format of I16 since no + // other format is supported, regardless of what format was requested. + if (!usingExtendedDataFormat) mFormat = AudioFormat::I16; + // Configure the stream. SLAndroidConfigurationItf configItf; result = (*mObjectInterface)->GetInterface(mObjectInterface,