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
Changes from 2 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
18 changes: 11 additions & 7 deletions tests/testStreamStop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ class TestStreamStop : public ::testing::Test,
mBuilder.setDirection(direction);
mBuilder.setAudioApi(audioApi);
mBuilder.setPerformanceMode(perfMode);
mBuilder.setChannelCount(1);
mBuilder.setFormat(AudioFormat::I16);
Result r = mBuilder.openStream(&mStream);
EXPECT_EQ(r, Result::OK) << "Failed to open stream " << convertToText(r);
if (r != Result::OK)
Expand All @@ -54,7 +56,7 @@ class TestStreamStop : public ::testing::Test,
return (r == Result::OK);
}

void stopWhileUsingLargeBuffer(bool shouldWrite) {
void stopWhileUsingLargeBuffer() {
StreamState next = StreamState::Unknown;
auto r = mStream->requestStart();
EXPECT_EQ(r, Result::OK);
Expand All @@ -64,20 +66,21 @@ class TestStreamStop : public ::testing::Test,

AudioStream *str = mStream;

int16_t buffer[kFramesToWrite * 4] = {};
int16_t buffer[kFramesToWrite] = {};

std::thread stopper([str] {
usleep(3 * 1000); // 3 ms
int64_t estimatedCompletionTimeUs = kSecondsPerMicroSecond * kFramesToWrite / str->getSampleRate();
usleep(estimatedCompletionTimeUs / 2); // Stop halfway during the read/write
str->close();
});

if (shouldWrite) {
if (mBuilder.getDirection() == Direction::Output) {
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);
FAIL() << "Could not read/write to audio stream: " << static_cast<int>(r);
}

stopper.join();
Expand All @@ -91,6 +94,7 @@ class TestStreamStop : public ::testing::Test,
AudioStreamBuilder mBuilder;
AudioStream *mStream = nullptr;
static constexpr int kTimeoutInNanos = 1000 * kNanosPerMillisecond;
static constexpr int64_t kSecondsPerMicroSecond = 1000000;
robertwu1 marked this conversation as resolved.
Show resolved Hide resolved
static constexpr int kFramesToWrite = 10000;

};
Expand All @@ -101,7 +105,7 @@ TEST_P(TestStreamStop, VerifyTestStreamStop) {
const PerformanceMode performanceMode = std::get<2>(GetParam());

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

INSTANTIATE_TEST_SUITE_P(
Expand All @@ -121,4 +125,4 @@ INSTANTIATE_TEST_SUITE_P(
TestStreamStopParams({Direction::Input, AudioApi::OpenSLES, PerformanceMode::None}),
TestStreamStopParams({Direction::Input, AudioApi::OpenSLES, PerformanceMode::PowerSaving})
)
);
);