-
Notifications
You must be signed in to change notification settings - Fork 565
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add input capture support to OpenSL wrapper #26
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
64c55f4
oboe: OpenSL ES refactor engine
9a147a1
oboe: move OpenSL ES output code to its own class
6d95291
oboe: implement input stream
420715b
oboe: cleanup OpenSL ES input
d9703b0
oboe: OPenSL ES opens moved into stream open
7040a9d
oboe: address review comments, better singleton
9b1c2ce
oboe: make chanCountToChanMask non-virtual
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,219 @@ | ||
/* | ||
* Copyright 2017 The Android Open Source Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#include <cassert> | ||
|
||
#include <SLES/OpenSLES.h> | ||
#include <SLES/OpenSLES_Android.h> | ||
|
||
#include "oboe/AudioStreamBuilder.h" | ||
#include "AudioInputStreamOpenSLES.h" | ||
#include "AudioStreamOpenSLES.h" | ||
#include "OpenSLESUtilities.h" | ||
|
||
using namespace oboe; | ||
|
||
AudioInputStreamOpenSLES::AudioInputStreamOpenSLES(const AudioStreamBuilder &builder) | ||
: AudioStreamOpenSLES(builder) { | ||
} | ||
|
||
AudioInputStreamOpenSLES::~AudioInputStreamOpenSLES() { | ||
} | ||
|
||
#define AUDIO_CHANNEL_COUNT_MAX 30u | ||
#define SL_ANDROID_UNKNOWN_CHANNELMASK 0 | ||
|
||
int AudioInputStreamOpenSLES::chanCountToChanMask(int channelCount) { | ||
// from internal sles_channel_in_mask_from_count(chanCount); | ||
switch (channelCount) { | ||
case 1: | ||
return SL_SPEAKER_FRONT_LEFT; | ||
case 2: | ||
return SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT; | ||
default: { | ||
if (channelCount > AUDIO_CHANNEL_COUNT_MAX) { | ||
return SL_ANDROID_UNKNOWN_CHANNELMASK; | ||
} else { | ||
SLuint32 bitfield = (1 << channelCount) - 1; | ||
return SL_ANDROID_MAKE_INDEXED_CHANNEL_MASK(bitfield); | ||
} | ||
} | ||
} | ||
} | ||
|
||
Result AudioInputStreamOpenSLES::open() { | ||
|
||
Result oboeResult = AudioStreamOpenSLES::open(); | ||
if (Result::OK != oboeResult) return oboeResult; | ||
|
||
SLuint32 bitsPerSample = getBytesPerSample() * kBitsPerByte; | ||
|
||
// configure audio sink | ||
SLDataLocator_AndroidSimpleBufferQueue loc_bufq = { | ||
SL_DATALOCATOR_ANDROIDSIMPLEBUFFERQUEUE, // locatorType | ||
static_cast<SLuint32>(mBurstsPerBuffer)}; // numBuffers | ||
|
||
// Define the audio data format. | ||
SLDataFormat_PCM format_pcm = { | ||
SL_DATAFORMAT_PCM, // formatType | ||
(SLuint32) mChannelCount, // numChannels | ||
(SLuint32) (mSampleRate * kMillisPerSecond), // milliSamplesPerSec | ||
bitsPerSample, // bitsPerSample | ||
bitsPerSample, // containerSize; | ||
(SLuint32) chanCountToChanMask(mChannelCount), // channelMask | ||
getDefaultByteOrder(), | ||
}; | ||
|
||
SLDataSink audioSink = {&loc_bufq, &format_pcm}; | ||
|
||
/** | ||
* API 21 (Lollipop) introduced support for floating-point data representation and an extended | ||
* data format type: SLAndroidDataFormat_PCM_EX. If running on API 21+ use this newer format | ||
* type, creating it from our original format. | ||
*/ | ||
SLAndroidDataFormat_PCM_EX format_pcm_ex; | ||
if (__ANDROID_API__ >= __ANDROID_API_L__) { | ||
SLuint32 representation = OpenSLES_ConvertFormatToRepresentation(getFormat()); | ||
// Fill in the format structure. | ||
format_pcm_ex = OpenSLES_createExtendedFormat(format_pcm, representation); | ||
// Use in place of the previous format. | ||
audioSink.pFormat = &format_pcm_ex; | ||
} | ||
|
||
// configure audio source | ||
SLDataLocator_IODevice loc_dev = {SL_DATALOCATOR_IODEVICE, | ||
SL_IODEVICE_AUDIOINPUT, | ||
SL_DEFAULTDEVICEID_AUDIOINPUT, | ||
NULL }; | ||
SLDataSource audioSrc = {&loc_dev, NULL }; | ||
|
||
SLresult result = EngineOpenSLES::getInstance().createAudioRecorder(&mObjectInterface, | ||
&audioSrc, | ||
&audioSink); | ||
if (SL_RESULT_SUCCESS != result) { | ||
LOGE("createAudioRecorder() result:%s", getSLErrStr(result)); | ||
goto error; | ||
} | ||
|
||
// Configure the voice recognition preset, which has no | ||
// signal processing, for lower latency. | ||
SLAndroidConfigurationItf inputConfig; | ||
result = (*mObjectInterface)->GetInterface(mObjectInterface, | ||
SL_IID_ANDROIDCONFIGURATION, | ||
&inputConfig); | ||
if (SL_RESULT_SUCCESS == result) { | ||
SLuint32 presetValue = SL_ANDROID_RECORDING_PRESET_VOICE_RECOGNITION; | ||
(*inputConfig)->SetConfiguration(inputConfig, | ||
SL_ANDROID_KEY_RECORDING_PRESET, | ||
&presetValue, | ||
sizeof(SLuint32)); | ||
} | ||
|
||
result = (*mObjectInterface)->Realize(mObjectInterface, SL_BOOLEAN_FALSE); | ||
if (SL_RESULT_SUCCESS != result) { | ||
LOGE("Realize recorder object result:%s", getSLErrStr(result)); | ||
goto error; | ||
} | ||
|
||
result = (*mObjectInterface)->GetInterface(mObjectInterface, SL_IID_RECORD, &mRecordInterface); | ||
if (SL_RESULT_SUCCESS != result) { | ||
LOGE("GetInterface RECORD result:%s", getSLErrStr(result)); | ||
goto error; | ||
} | ||
|
||
result = AudioStreamOpenSLES::registerBufferQueueCallback(); | ||
if (SL_RESULT_SUCCESS != result) { | ||
goto error; | ||
} | ||
|
||
return Result::OK; | ||
|
||
error: | ||
return Result::ErrorInternal; // TODO convert error from SLES to OBOE | ||
} | ||
|
||
Result AudioInputStreamOpenSLES::close() { | ||
requestStop(); | ||
mRecordInterface = NULL; | ||
return AudioStreamOpenSLES::close(); | ||
} | ||
|
||
Result AudioInputStreamOpenSLES::setRecordState(SLuint32 newState) { | ||
Result result = Result::OK; | ||
LOGD("AudioInputStreamOpenSLES::setRecordState(%d)", newState); | ||
if (mRecordInterface == NULL) { | ||
return Result::ErrorInvalidState; | ||
} | ||
SLresult slResult = (*mRecordInterface)->SetRecordState(mRecordInterface, newState); | ||
if(SL_RESULT_SUCCESS != slResult) { | ||
LOGD("AudioInputStreamOpenSLES::setPlayState() returned %s", getSLErrStr(slResult)); | ||
result = Result::ErrorInvalidState; // TODO review | ||
} else { | ||
setState(StreamState::Pausing); | ||
} | ||
return result; | ||
} | ||
|
||
Result AudioInputStreamOpenSLES::requestStart() | ||
{ | ||
LOGD("AudioInputStreamOpenSLES::requestStart()"); | ||
Result result = setRecordState(SL_RECORDSTATE_RECORDING); | ||
if(result != Result::OK) { | ||
result = Result::ErrorInvalidState; // TODO review | ||
} else { | ||
// Enqueue the first buffer so that we have data ready in the callback. | ||
enqueueCallbackBuffer(mSimpleBufferQueueInterface); | ||
setState(StreamState::Starting); | ||
} | ||
return result; | ||
} | ||
|
||
|
||
Result AudioInputStreamOpenSLES::requestPause() { | ||
LOGD("AudioInputStreamOpenSLES::requestStop()"); | ||
Result result = setRecordState(SL_RECORDSTATE_PAUSED); | ||
if(result != Result::OK) { | ||
result = Result::ErrorInvalidState; // TODO review | ||
} else { | ||
setState(StreamState::Pausing); | ||
} | ||
return result; | ||
} | ||
|
||
Result AudioInputStreamOpenSLES::requestFlush() { | ||
return Result::ErrorUnimplemented; // TODO | ||
} | ||
|
||
Result AudioInputStreamOpenSLES::requestStop() { | ||
LOGD("AudioInputStreamOpenSLES::requestStop()"); | ||
Result result = setRecordState(SL_RECORDSTATE_STOPPED); | ||
if(result != Result::OK) { | ||
result = Result::ErrorInvalidState; // TODO review | ||
} else { | ||
setState(StreamState::Stopping); | ||
} | ||
return result; | ||
} | ||
|
||
Result AudioInputStreamOpenSLES::waitForStateChange(StreamState currentState, | ||
StreamState *nextState, | ||
int64_t timeoutNanoseconds) { | ||
LOGD("AudioInputStreamOpenSLES::waitForStateChange()"); | ||
if (mRecordInterface == NULL) { | ||
return Result::ErrorInvalidState; | ||
} | ||
return Result::ErrorUnimplemented; // TODO | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* Copyright 2017 The Android Open Source Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#ifndef AUDIO_INPUT_STREAM_OPENSL_ES_H_ | ||
#define AUDIO_INPUT_STREAM_OPENSL_ES_H_ | ||
|
||
|
||
#include <SLES/OpenSLES.h> | ||
#include <SLES/OpenSLES_Android.h> | ||
|
||
#include "oboe/Oboe.h" | ||
#include "AudioStreamOpenSLES.h" | ||
|
||
namespace oboe { | ||
|
||
/** | ||
* INTERNAL USE ONLY | ||
*/ | ||
|
||
class AudioInputStreamOpenSLES : public AudioStreamOpenSLES { | ||
public: | ||
AudioInputStreamOpenSLES(); | ||
explicit AudioInputStreamOpenSLES(const AudioStreamBuilder &builder); | ||
|
||
virtual ~AudioInputStreamOpenSLES(); | ||
|
||
Result open() override; | ||
Result close() override; | ||
|
||
Result requestStart() override; | ||
Result requestPause() override; | ||
Result requestFlush() override; | ||
Result requestStop() override; | ||
|
||
Result waitForStateChange(StreamState currentState, | ||
StreamState *nextState, | ||
int64_t timeoutNanoseconds) override; | ||
|
||
int chanCountToChanMask(int chanCount); | ||
|
||
private: | ||
|
||
Result setRecordState(SLuint32 newState); | ||
|
||
SLRecordItf mRecordInterface; | ||
}; | ||
|
||
} // namespace oboe | ||
|
||
#endif //AUDIO_INPUT_STREAM_OPENSL_ES_H_ |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This file must be added to CMakeLists.txt so that clients can build it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here's the relevant part of the CMakeLists.txt updated so this version builds and links correctly:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done