xref: /aosp_15_r20/external/oboe/tests/testReturnStop.cpp (revision 05767d913155b055644481607e6fa1e35e2fe72c)
1 /*
2  * Copyright 2021 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <atomic>
18 #include <tuple>
19 
20 #include <gtest/gtest.h>
21 #include <oboe/Oboe.h>
22 
23 
24 // Test returning DataCallbackResult::Stop from a callback.
25 using namespace oboe;
26 
27 static constexpr int kTimeoutInNanos = 500 * kNanosPerMillisecond;
28 
29 class ReturnStopCallback : public AudioStreamDataCallback {
30 public:
onAudioReady(AudioStream * oboeStream,void * audioData,int32_t numFrames)31     DataCallbackResult onAudioReady(AudioStream *oboeStream, void *audioData, int32_t numFrames) override {
32         return (++callbackCount < kMaxCallbacks) ? DataCallbackResult::Continue : DataCallbackResult::Stop;
33     }
34 
reset()35     void reset() {
36         callbackCount = 0;
37     }
38 
getMaxCallbacks() const39     int getMaxCallbacks() const { return kMaxCallbacks; }
40 
41     std::atomic<int> callbackCount{0};
42 
43 private:
44     // I get strange linker errors with GTest if I try to reference this directly.
45     static constexpr int kMaxCallbacks = 40;
46 };
47 
48 using StreamReturnStopParams = std::tuple<Direction, AudioApi, PerformanceMode, bool>;
49 
50 class StreamReturnStop : public ::testing::Test,
51                          public ::testing::WithParamInterface<StreamReturnStopParams> {
52 
53 protected:
54     void TearDown() override;
55 
56     AudioStreamBuilder mBuilder;
57     AudioStream *mStream = nullptr;
58 };
59 
TearDown()60 void StreamReturnStop::TearDown() {
61     if (mStream != nullptr) {
62         mStream->close();
63         mStream = nullptr;
64     }
65 }
66 
TEST_P(StreamReturnStop,VerifyStreamReturnStop)67 TEST_P(StreamReturnStop, VerifyStreamReturnStop) {
68     const Direction direction = std::get<0>(GetParam());
69     const AudioApi audioApi = std::get<1>(GetParam());
70     const PerformanceMode performanceMode = std::get<2>(GetParam());
71     const bool useRequestStart = std::get<3>(GetParam());
72 
73     ReturnStopCallback *callback = new ReturnStopCallback();
74     mBuilder.setDirection(direction)
75             ->setFormat(AudioFormat::I16)
76             ->setPerformanceMode(performanceMode)
77             ->setDataCallback(callback);
78     if (mBuilder.isAAudioRecommended()) {
79         mBuilder.setAudioApi(audioApi);
80     }
81     mStream = nullptr;
82     Result r = mBuilder.openStream(&mStream);
83     ASSERT_EQ(r, Result::OK) << "Failed to open stream. " << convertToText(r);
84 
85     // Start and stop several times.
86     for (int i = 0; i < 3; i++) {
87         callback->reset();
88         // Oboe has two ways to start a stream.
89         if (useRequestStart) {
90             r = mStream->requestStart();
91         } else {
92             r = mStream->start();
93         }
94         ASSERT_EQ(r, Result::OK) << "Failed to start stream. " << convertToText(r);
95 
96         // Wait for callbacks to complete.
97         const int kMaxCallbackPeriodMillis = 500;
98         const int kPollPeriodMillis = 20;
99         int timeout = 2 * callback->getMaxCallbacks() * kMaxCallbackPeriodMillis / kPollPeriodMillis;
100         do {
101             usleep(kPollPeriodMillis * 1000);
102         } while (callback->callbackCount < callback->getMaxCallbacks() && timeout-- > 0);
103         EXPECT_GT(timeout, 0) << "timed out waiting for enough callbacks";
104 
105         StreamState next = StreamState::Unknown;
106         r = mStream->waitForStateChange(StreamState::Started, &next, kTimeoutInNanos);
107         EXPECT_EQ(r, Result::OK) << "waitForStateChange(Started) timed out. " << convertToText(r);
108         r = mStream->waitForStateChange(StreamState::Stopping, &next, kTimeoutInNanos);
109         EXPECT_EQ(r, Result::OK) << "waitForStateChange(Stopping) timed out. " << convertToText(r);
110         EXPECT_EQ(next, StreamState::Stopped) << "Stream not in state Stopped, was " << convertToText(next);
111 
112         EXPECT_EQ(callback->callbackCount, callback->getMaxCallbacks()) << "Too many callbacks = " << callback->callbackCount;
113 
114         const int kOboeStartStopSleepMSec = 10;
115         usleep(kOboeStartStopSleepMSec * 1000); // avoid race condition in emulator
116     }
117 
118     ASSERT_EQ(Result::OK, mStream->close());
119 }
120 
121 INSTANTIATE_TEST_SUITE_P(
122         StreamReturnStopTest,
123         StreamReturnStop,
124         ::testing::Values(
125                 // Last boolean is true if requestStart() should be called instead of start().
126                 StreamReturnStopParams({Direction::Output, AudioApi::AAudio, PerformanceMode::LowLatency, true}),
127                 StreamReturnStopParams({Direction::Output, AudioApi::AAudio, PerformanceMode::LowLatency, false}),
128                 StreamReturnStopParams({Direction::Output, AudioApi::AAudio, PerformanceMode::None, true}),
129                 StreamReturnStopParams({Direction::Output, AudioApi::AAudio, PerformanceMode::None, false}),
130                 StreamReturnStopParams({Direction::Output, AudioApi::OpenSLES, PerformanceMode::LowLatency, true}),
131                 StreamReturnStopParams({Direction::Output, AudioApi::OpenSLES, PerformanceMode::LowLatency, false}),
132                 StreamReturnStopParams({Direction::Input, AudioApi::AAudio, PerformanceMode::LowLatency, true}),
133                 StreamReturnStopParams({Direction::Input, AudioApi::AAudio, PerformanceMode::LowLatency, false})
134                 )
135         );
136