forked from google/oboe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testFullDuplexStream.cpp
208 lines (186 loc) · 10.5 KB
/
testFullDuplexStream.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
* Copyright 2023 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;
static constexpr int kTimeToSleepMicros = 5 * 1000 * 1000; // 5 s
using TestFullDuplexStreamParams = std::tuple<AudioApi, PerformanceMode, AudioApi, PerformanceMode>;
class TestFullDuplexStream : public ::testing::Test,
public ::testing::WithParamInterface<TestFullDuplexStreamParams>,
public FullDuplexStream {
public:
DataCallbackResult onBothStreamsReady(
const void *inputData,
int numInputFrames,
void *outputData,
int numOutputFrames) override {
mCallbackCount++;
if (numInputFrames == numOutputFrames) {
mGoodCallbackCount++;
}
return DataCallbackResult::Continue;
}
protected:
void openStream(AudioApi inputAudioApi, PerformanceMode inputPerfMode,
AudioApi outputAudioApi, PerformanceMode outputPerfMode) {
mOutputBuilder.setDirection(Direction::Output);
if (mOutputBuilder.isAAudioRecommended()) {
mOutputBuilder.setAudioApi(outputAudioApi);
}
mOutputBuilder.setPerformanceMode(outputPerfMode);
mOutputBuilder.setChannelCount(1);
mOutputBuilder.setFormat(AudioFormat::Float);
mOutputBuilder.setDataCallback(this);
Result r = mOutputBuilder.openStream(&mOutputStream);
ASSERT_EQ(r, Result::OK) << "Failed to open output stream " << convertToText(r);
mInputBuilder.setDirection(Direction::Input);
if (mInputBuilder.isAAudioRecommended()) {
mInputBuilder.setAudioApi(inputAudioApi);
}
mInputBuilder.setPerformanceMode(inputPerfMode);
mInputBuilder.setChannelCount(1);
mInputBuilder.setFormat(AudioFormat::Float);
mInputBuilder.setBufferCapacityInFrames(mOutputStream->getBufferCapacityInFrames() * 2);
mInputBuilder.setSampleRate(mOutputStream->getSampleRate());
r = mInputBuilder.openStream(&mInputStream);
ASSERT_EQ(r, Result::OK) << "Failed to open input stream " << convertToText(r);
setInputStream(mInputStream);
setOutputStream(mOutputStream);
}
void startStream() {
Result r = start();
ASSERT_EQ(r, Result::OK) << "Failed to start streams " << convertToText(r);
}
void stopStream() {
Result r = stop();
ASSERT_EQ(r, Result::OK) << "Failed to stop streams " << convertToText(r);
}
void closeStream() {
Result r = mOutputStream->close();
ASSERT_EQ(r, Result::OK) << "Failed to close output stream " << convertToText(r);
setOutputStream(nullptr);
r = mInputStream->close();
ASSERT_EQ(r, Result::OK) << "Failed to close input stream " << convertToText(r);
setInputStream(nullptr);
}
void checkXRuns() {
// Expect few xRuns with the use of full duplex stream
EXPECT_LT(mInputStream->getXRunCount().value(), 10);
EXPECT_LT(mOutputStream->getXRunCount().value(), 10);
}
void checkInputAndOutputBufferSizesMatch() {
// Expect the large majority of callbacks to have the same sized input and output
EXPECT_GE(mGoodCallbackCount, mCallbackCount * 9 / 10);
}
AudioStreamBuilder mInputBuilder;
AudioStreamBuilder mOutputBuilder;
AudioStream *mInputStream = nullptr;
AudioStream *mOutputStream = nullptr;
std::atomic<int32_t> mCallbackCount{0};
std::atomic<int32_t> mGoodCallbackCount{0};
};
TEST_P(TestFullDuplexStream, VerifyFullDuplexStream) {
const AudioApi inputAudioApi = std::get<0>(GetParam());
const PerformanceMode inputPerformanceMode = std::get<1>(GetParam());
const AudioApi outputAudioApi = std::get<2>(GetParam());
const PerformanceMode outputPerformanceMode = std::get<3>(GetParam());
openStream(inputAudioApi, inputPerformanceMode, outputAudioApi, outputPerformanceMode);
startStream();
usleep(kTimeToSleepMicros);
checkXRuns();
checkInputAndOutputBufferSizesMatch();
stopStream();
closeStream();
}
INSTANTIATE_TEST_SUITE_P(
TestFullDuplexStreamTest,
TestFullDuplexStream,
::testing::Values(
TestFullDuplexStreamParams({AudioApi::AAudio, PerformanceMode::LowLatency,
AudioApi::AAudio, PerformanceMode::LowLatency}),
TestFullDuplexStreamParams({AudioApi::AAudio, PerformanceMode::LowLatency,
AudioApi::AAudio, PerformanceMode::None}),
TestFullDuplexStreamParams({AudioApi::AAudio, PerformanceMode::LowLatency,
AudioApi::AAudio, PerformanceMode::PowerSaving}),
TestFullDuplexStreamParams({AudioApi::AAudio, PerformanceMode::LowLatency,
AudioApi::OpenSLES, PerformanceMode::LowLatency}),
TestFullDuplexStreamParams({AudioApi::AAudio, PerformanceMode::LowLatency,
AudioApi::OpenSLES, PerformanceMode::None}),
TestFullDuplexStreamParams({AudioApi::AAudio, PerformanceMode::LowLatency,
AudioApi::OpenSLES, PerformanceMode::PowerSaving}),
TestFullDuplexStreamParams({AudioApi::AAudio, PerformanceMode::None,
AudioApi::AAudio, PerformanceMode::LowLatency}),
TestFullDuplexStreamParams({AudioApi::AAudio, PerformanceMode::None,
AudioApi::AAudio, PerformanceMode::None}),
TestFullDuplexStreamParams({AudioApi::AAudio, PerformanceMode::None,
AudioApi::AAudio, PerformanceMode::PowerSaving}),
TestFullDuplexStreamParams({AudioApi::AAudio, PerformanceMode::None,
AudioApi::OpenSLES, PerformanceMode::LowLatency}),
TestFullDuplexStreamParams({AudioApi::AAudio, PerformanceMode::None,
AudioApi::OpenSLES, PerformanceMode::None}),
TestFullDuplexStreamParams({AudioApi::AAudio, PerformanceMode::None,
AudioApi::OpenSLES, PerformanceMode::PowerSaving}),
TestFullDuplexStreamParams({AudioApi::AAudio, PerformanceMode::PowerSaving,
AudioApi::AAudio, PerformanceMode::LowLatency}),
TestFullDuplexStreamParams({AudioApi::AAudio, PerformanceMode::PowerSaving,
AudioApi::AAudio, PerformanceMode::None}),
TestFullDuplexStreamParams({AudioApi::AAudio, PerformanceMode::PowerSaving,
AudioApi::AAudio, PerformanceMode::PowerSaving}),
TestFullDuplexStreamParams({AudioApi::AAudio, PerformanceMode::PowerSaving,
AudioApi::OpenSLES, PerformanceMode::LowLatency}),
TestFullDuplexStreamParams({AudioApi::AAudio, PerformanceMode::PowerSaving,
AudioApi::OpenSLES, PerformanceMode::None}),
TestFullDuplexStreamParams({AudioApi::AAudio, PerformanceMode::PowerSaving,
AudioApi::OpenSLES, PerformanceMode::PowerSaving}),
TestFullDuplexStreamParams({AudioApi::OpenSLES, PerformanceMode::LowLatency,
AudioApi::AAudio, PerformanceMode::LowLatency}),
TestFullDuplexStreamParams({AudioApi::OpenSLES, PerformanceMode::LowLatency,
AudioApi::AAudio, PerformanceMode::None}),
TestFullDuplexStreamParams({AudioApi::OpenSLES, PerformanceMode::LowLatency,
AudioApi::AAudio, PerformanceMode::PowerSaving}),
TestFullDuplexStreamParams({AudioApi::OpenSLES, PerformanceMode::LowLatency,
AudioApi::OpenSLES, PerformanceMode::LowLatency}),
TestFullDuplexStreamParams({AudioApi::OpenSLES, PerformanceMode::LowLatency,
AudioApi::OpenSLES, PerformanceMode::None}),
TestFullDuplexStreamParams({AudioApi::OpenSLES, PerformanceMode::LowLatency,
AudioApi::OpenSLES, PerformanceMode::PowerSaving}),
TestFullDuplexStreamParams({AudioApi::OpenSLES, PerformanceMode::None,
AudioApi::AAudio, PerformanceMode::LowLatency}),
TestFullDuplexStreamParams({AudioApi::OpenSLES, PerformanceMode::None,
AudioApi::AAudio, PerformanceMode::None}),
TestFullDuplexStreamParams({AudioApi::OpenSLES, PerformanceMode::None,
AudioApi::AAudio, PerformanceMode::PowerSaving}),
TestFullDuplexStreamParams({AudioApi::OpenSLES, PerformanceMode::None,
AudioApi::OpenSLES, PerformanceMode::LowLatency}),
TestFullDuplexStreamParams({AudioApi::OpenSLES, PerformanceMode::None,
AudioApi::OpenSLES, PerformanceMode::None}),
TestFullDuplexStreamParams({AudioApi::OpenSLES, PerformanceMode::None,
AudioApi::OpenSLES, PerformanceMode::PowerSaving}),
TestFullDuplexStreamParams({AudioApi::OpenSLES, PerformanceMode::PowerSaving,
AudioApi::AAudio, PerformanceMode::LowLatency}),
TestFullDuplexStreamParams({AudioApi::OpenSLES, PerformanceMode::PowerSaving,
AudioApi::AAudio, PerformanceMode::None}),
TestFullDuplexStreamParams({AudioApi::OpenSLES, PerformanceMode::PowerSaving,
AudioApi::AAudio, PerformanceMode::PowerSaving}),
TestFullDuplexStreamParams({AudioApi::OpenSLES, PerformanceMode::PowerSaving,
AudioApi::OpenSLES, PerformanceMode::LowLatency}),
TestFullDuplexStreamParams({AudioApi::OpenSLES, PerformanceMode::PowerSaving,
AudioApi::OpenSLES, PerformanceMode::None}),
TestFullDuplexStreamParams({AudioApi::OpenSLES, PerformanceMode::PowerSaving,
AudioApi::OpenSLES, PerformanceMode::PowerSaving})
)
);