Skip to content
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
merged 7 commits into from
Dec 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@ set (oboe_sources
src/fifo/FifoController.cpp
src/fifo/FifoControllerBase.cpp
src/fifo/FifoControllerIndirect.cpp
src/opensles/AudioInputStreamOpenSLES.cpp
src/opensles/AudioOutputStreamOpenSLES.cpp
src/opensles/AudioStreamBuffered.cpp
src/opensles/AudioStreamOpenSLES.cpp
src/opensles/EngineOpenSLES.cpp
src/opensles/OpenSLESUtilities.cpp
src/opensles/OutputMixerOpenSLES.cpp
)

add_library(oboe STATIC ${oboe_sources})
Expand All @@ -25,4 +29,4 @@ target_compile_options(oboe PRIVATE -std=c++11
PRIVATE -Wall
PRIVATE "$<$<CONFIG:DEBUG>:-Werror>") # Only include -Werror when building debug config

target_link_libraries(oboe PRIVATE log OpenSLES)
target_link_libraries(oboe PRIVATE log OpenSLES)
8 changes: 7 additions & 1 deletion src/common/AudioStreamBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
#include "OboeDebug.h"
#include "oboe/Oboe.h"
#include "oboe/AudioStreamBuilder.h"
#include "opensles/AudioInputStreamOpenSLES.h"
#include "opensles/AudioOutputStreamOpenSLES.h"
#include "opensles/AudioStreamOpenSLES.h"

namespace oboe {
Expand All @@ -41,7 +43,11 @@ AudioStream *AudioStreamBuilder::build() {
}
// fall into using older existing API
case AudioApi::OpenSLES:
stream = new AudioStreamOpenSLES(*this);
if (getDirection() == oboe::Direction::Output) {
stream = new AudioOutputStreamOpenSLES(*this);
} else if (getDirection() == oboe::Direction::Input) {
stream = new AudioInputStreamOpenSLES(*this);
}
break;
}
return stream;
Expand Down
219 changes: 219 additions & 0 deletions src/opensles/AudioInputStreamOpenSLES.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,219 @@
/*
Copy link
Collaborator

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

Copy link
Collaborator

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:

set (oboe_sources
    src/aaudio/AAudioLoader.cpp
    src/aaudio/AudioStreamAAudio.cpp
    src/common/LatencyTuner.cpp
    src/common/AudioStream.cpp
    src/common/AudioStreamBuilder.cpp
    src/common/Utilities.cpp
    src/fifo/FifoBuffer.cpp
    src/fifo/FifoController.cpp
    src/fifo/FifoControllerBase.cpp
    src/fifo/FifoControllerIndirect.cpp
    src/opensles/AudioInputStreamOpenSLES.cpp
    src/opensles/AudioOutputStreamOpenSLES.cpp
    src/opensles/AudioStreamBuffered.cpp
    src/opensles/AudioStreamOpenSLES.cpp
    src/opensles/EngineOpenSLES.cpp
    src/opensles/OpenSLESUtilities.cpp
    src/opensles/OutputMixerOpenSLES.cpp
    )

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

* 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
}
63 changes: 63 additions & 0 deletions src/opensles/AudioInputStreamOpenSLES.h
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_
Loading