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

AAudio: add locks around reading and writing #1497

Merged
merged 6 commits into from
Mar 23, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 2 additions & 0 deletions src/aaudio/AudioStreamAAudio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,7 @@ Result AudioStreamAAudio::requestStop_l(AAudioStream *stream) {
ResultWithValue<int32_t> AudioStreamAAudio::write(const void *buffer,
int32_t numFrames,
int64_t timeoutNanoseconds) {
std::shared_lock<std::shared_mutex> lock(mAAudioStreamLock);
AAudioStream *stream = mAAudioStream.load();
if (stream != nullptr) {
int32_t result = mLibLoader->stream_write(mAAudioStream, buffer,
Expand All @@ -485,6 +486,7 @@ ResultWithValue<int32_t> AudioStreamAAudio::write(const void *buffer,
ResultWithValue<int32_t> AudioStreamAAudio::read(void *buffer,
int32_t numFrames,
int64_t timeoutNanoseconds) {
std::shared_lock<std::shared_mutex> lock(mAAudioStreamLock);
AAudioStream *stream = mAAudioStream.load();
if (stream != nullptr) {
int32_t result = mLibLoader->stream_read(mAAudioStream, buffer,
Expand Down
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ add_executable(
testStreamStates.cpp
testStreamFramesProcessed.cpp
testReturnStop.cpp
testStreamStop.cpp
)

target_link_libraries(testOboe gtest oboe)
7 changes: 6 additions & 1 deletion tests/UnitTestRunner/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,15 @@ android {
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
externalNativeBuild {
cmake {
path file('../../CMakeLists.txt')
}
}
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.0.0-rc02'
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
}
2 changes: 1 addition & 1 deletion tests/UnitTestRunner/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity" android:screenOrientation="landscape">
<activity android:name=".MainActivity" android:exported="true" android:screenOrientation="landscape">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ private String executeBinary() {

StringBuffer output = new StringBuffer();
String abi = Build.CPU_ABI;
String extraStringForDebugBuilds = "-hwasan";
if (abi.endsWith(extraStringForDebugBuilds)) {
abi = abi.substring(0, abi.length() - extraStringForDebugBuilds.length());
}
String filesDir = getFilesDir().getPath();
String testBinaryPath = abi + "/" + TEST_BINARY_FILEANAME;

Expand Down
22 changes: 12 additions & 10 deletions tests/UnitTestRunner/build.gradle
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {

repositories {
google()
jcenter()
mavenCentral()
maven {
url 'https://maven.google.com/'
name 'Google'
}
}
dependencies {
classpath 'com.android.tools.build:gradle:4.2.0'


// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
dependencies {
classpath 'com.android.tools.build:gradle:7.1.1'
}
}

allprojects {
repositories {
google()
jcenter()
mavenCentral()
maven {
url 'https://maven.google.com/'
name 'Google'
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.7.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.3-all.zip
4 changes: 4 additions & 0 deletions tests/testStreamOpen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
#include <oboe/Oboe.h>
#include <android/api-level.h>

#ifndef __ANDROID_API_S__
#define __ANDROID_API_S__ 31
#endif

using namespace oboe;

class CallbackSizeMonitor : public AudioStreamCallback {
Expand Down
124 changes: 124 additions & 0 deletions tests/testStreamStop.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/*
* Copyright 2022 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 <thread>

#include <gtest/gtest.h>

#include <oboe/Oboe.h>

using namespace oboe;

using TestStreamStopParams = std::tuple<Direction, AudioApi, PerformanceMode>;

class TestStreamStop : public ::testing::Test,
public ::testing::WithParamInterface<TestStreamStopParams> {

protected:

void SetUp(){
mBuilder.setPerformanceMode(PerformanceMode::None);
mBuilder.setDirection(Direction::Output);
}

bool openStream(Direction direction, AudioApi audioApi, PerformanceMode perfMode) {
mBuilder.setDirection(direction);
mBuilder.setAudioApi(audioApi);
mBuilder.setPerformanceMode(perfMode);
Result r = mBuilder.openStream(&mStream);
EXPECT_EQ(r, Result::OK) << "Failed to open stream " << convertToText(r);
if (r != Result::OK)
return false;

Direction d = mStream->getDirection();
EXPECT_EQ(d, direction) << convertToText(mStream->getDirection());
return (d == direction);
}

bool openStream(AudioStreamBuilder &builder) {
Result r = builder.openStream(&mStream);
EXPECT_EQ(r, Result::OK) << "Failed to open stream " << convertToText(r);
return (r == Result::OK);
}

void stopWhileUsingLargeBuffer(bool shouldWrite) {
StreamState next = StreamState::Unknown;
auto r = mStream->requestStart();
EXPECT_EQ(r, Result::OK);
r = mStream->waitForStateChange(StreamState::Starting, &next, kTimeoutInNanos);
EXPECT_EQ(r, Result::OK);
EXPECT_EQ(next, StreamState::Started) << "next = " << convertToText(next);

AudioStream *str = mStream;

int16_t buffer[kFramesToWrite * 4] = {};
robertwu1 marked this conversation as resolved.
Show resolved Hide resolved

std::thread stopper([str] {
usleep(3 * 1000); // 3 ms
robertwu1 marked this conversation as resolved.
Show resolved Hide resolved
str->close();
});

if (shouldWrite) {
robertwu1 marked this conversation as resolved.
Show resolved Hide resolved
r = mStream->write(&buffer, kFramesToWrite, kTimeoutInNanos);
} else {
r = mStream->read(&buffer, kFramesToWrite, kTimeoutInNanos);
}
if (r != Result::OK) {
FAIL() << "Could not write to audio stream: " << static_cast<int>(r);
robertwu1 marked this conversation as resolved.
Show resolved Hide resolved
}

stopper.join();
r = mStream->waitForStateChange(StreamState::Started, &next,
1000 * kNanosPerMillisecond);
if ((r != Result::ErrorClosed) && (r != Result::OK)) {
FAIL() << "Wrong closed result type: " << static_cast<int>(r);
}
}

AudioStreamBuilder mBuilder;
AudioStream *mStream = nullptr;
static constexpr int kTimeoutInNanos = 1000 * kNanosPerMillisecond;
static constexpr int kFramesToWrite = 10000;

};

TEST_P(TestStreamStop, VerifyTestStreamStop) {
const Direction direction = std::get<0>(GetParam());
const AudioApi audioApi = std::get<1>(GetParam());
const PerformanceMode performanceMode = std::get<2>(GetParam());

ASSERT_TRUE(openStream(direction, audioApi, performanceMode));
stopWhileUsingLargeBuffer(direction == Direction::Output);
}

INSTANTIATE_TEST_SUITE_P(
TestStreamStopTest,
TestStreamStop,
::testing::Values(
TestStreamStopParams({Direction::Output, AudioApi::AAudio, PerformanceMode::LowLatency}),
TestStreamStopParams({Direction::Output, AudioApi::AAudio, PerformanceMode::None}),
TestStreamStopParams({Direction::Output, AudioApi::AAudio, PerformanceMode::PowerSaving}),
TestStreamStopParams({Direction::Output, AudioApi::OpenSLES, PerformanceMode::LowLatency}),
TestStreamStopParams({Direction::Output, AudioApi::OpenSLES, PerformanceMode::None}),
TestStreamStopParams({Direction::Output, AudioApi::OpenSLES, PerformanceMode::PowerSaving}),
TestStreamStopParams({Direction::Input, AudioApi::AAudio, PerformanceMode::LowLatency}),
TestStreamStopParams({Direction::Input, AudioApi::AAudio, PerformanceMode::None}),
TestStreamStopParams({Direction::Input, AudioApi::AAudio, PerformanceMode::PowerSaving}),
TestStreamStopParams({Direction::Input, AudioApi::OpenSLES, PerformanceMode::LowLatency}),
TestStreamStopParams({Direction::Input, AudioApi::OpenSLES, PerformanceMode::None}),
TestStreamStopParams({Direction::Input, AudioApi::OpenSLES, PerformanceMode::PowerSaving})
)
);
robertwu1 marked this conversation as resolved.
Show resolved Hide resolved