-
Notifications
You must be signed in to change notification settings - Fork 557
/
testReturnStop.cpp
133 lines (109 loc) · 5.32 KB
/
testReturnStop.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
* Copyright 2021 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 <atomic>
#include <tuple>
#include <gtest/gtest.h>
#include <oboe/Oboe.h>
// Test returning DataCallbackResult::Stop from a callback.
using namespace oboe;
static constexpr int kTimeoutInNanos = 500 * kNanosPerMillisecond;
class ReturnStopCallback : public AudioStreamDataCallback {
public:
DataCallbackResult onAudioReady(AudioStream *oboeStream, void *audioData, int32_t numFrames) override {
return (++callbackCount < kMaxCallbacks) ? DataCallbackResult::Continue : DataCallbackResult::Stop;
}
void reset() {
callbackCount = 0;
}
int getMaxCallbacks() const { return kMaxCallbacks; }
std::atomic<int> callbackCount{0};
private:
// I get strange linker errors with GTest if I try to reference this directly.
static constexpr int kMaxCallbacks = 40;
};
using StreamReturnStopParams = std::tuple<Direction, AudioApi, PerformanceMode, bool>;
class StreamReturnStop : public ::testing::Test,
public ::testing::WithParamInterface<StreamReturnStopParams> {
protected:
void TearDown() override;
AudioStreamBuilder mBuilder;
std::shared_ptr<AudioStream> mStream;
};
void StreamReturnStop::TearDown() {
if (mStream) {
mStream->close();
}
}
TEST_P(StreamReturnStop, VerifyStreamReturnStop) {
const Direction direction = std::get<0>(GetParam());
const AudioApi audioApi = std::get<1>(GetParam());
const PerformanceMode performanceMode = std::get<2>(GetParam());
const bool useRequestStart = std::get<3>(GetParam());
ReturnStopCallback *callback = new ReturnStopCallback();
mBuilder.setDirection(direction)
->setFormat(AudioFormat::I16)
->setPerformanceMode(performanceMode)
->setDataCallback(callback);
if (mBuilder.isAAudioRecommended()) {
mBuilder.setAudioApi(audioApi);
}
Result r = mBuilder.openStream(mStream);
ASSERT_EQ(r, Result::OK) << "Failed to open stream. " << convertToText(r);
// Start and stop several times.
for (int i = 0; i < 3; i++) {
callback->reset();
// Oboe has two ways to start a stream.
if (useRequestStart) {
r = mStream->requestStart();
} else {
r = mStream->start();
}
ASSERT_EQ(r, Result::OK) << "Failed to start stream. " << convertToText(r);
// Wait for callbacks to complete.
const int kMaxCallbackPeriodMillis = 500;
const int kPollPeriodMillis = 20;
int timeout = 2 * callback->getMaxCallbacks() * kMaxCallbackPeriodMillis / kPollPeriodMillis;
do {
usleep(kPollPeriodMillis * 1000);
} while (callback->callbackCount < callback->getMaxCallbacks() && timeout-- > 0);
EXPECT_GT(timeout, 0) << "timed out waiting for enough callbacks";
StreamState next = StreamState::Unknown;
r = mStream->waitForStateChange(StreamState::Started, &next, kTimeoutInNanos);
EXPECT_EQ(r, Result::OK) << "waitForStateChange(Started) timed out. " << convertToText(r);
r = mStream->waitForStateChange(StreamState::Stopping, &next, kTimeoutInNanos);
EXPECT_EQ(r, Result::OK) << "waitForStateChange(Stopping) timed out. " << convertToText(r);
EXPECT_EQ(next, StreamState::Stopped) << "Stream not in state Stopped, was " << convertToText(next);
EXPECT_EQ(callback->callbackCount, callback->getMaxCallbacks()) << "Too many callbacks = " << callback->callbackCount;
const int kOboeStartStopSleepMSec = 10;
usleep(kOboeStartStopSleepMSec * 1000); // avoid race condition in emulator
}
ASSERT_EQ(Result::OK, mStream->close());
}
INSTANTIATE_TEST_SUITE_P(
StreamReturnStopTest,
StreamReturnStop,
::testing::Values(
// Last boolean is true if requestStart() should be called instead of start().
StreamReturnStopParams({Direction::Output, AudioApi::AAudio, PerformanceMode::LowLatency, true}),
StreamReturnStopParams({Direction::Output, AudioApi::AAudio, PerformanceMode::LowLatency, false}),
StreamReturnStopParams({Direction::Output, AudioApi::AAudio, PerformanceMode::None, true}),
StreamReturnStopParams({Direction::Output, AudioApi::AAudio, PerformanceMode::None, false}),
StreamReturnStopParams({Direction::Output, AudioApi::OpenSLES, PerformanceMode::LowLatency, true}),
StreamReturnStopParams({Direction::Output, AudioApi::OpenSLES, PerformanceMode::LowLatency, false}),
StreamReturnStopParams({Direction::Input, AudioApi::AAudio, PerformanceMode::LowLatency, true}),
StreamReturnStopParams({Direction::Input, AudioApi::AAudio, PerformanceMode::LowLatency, false})
)
);