• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     std::shared_ptr<AudioStream> mStream;
58 };
59 
TearDown()60 void StreamReturnStop::TearDown() {
61     if (mStream) {
62         mStream->close();
63     }
64 }
65 
TEST_P(StreamReturnStop,VerifyStreamReturnStop)66 TEST_P(StreamReturnStop, VerifyStreamReturnStop) {
67     const Direction direction = std::get<0>(GetParam());
68     const AudioApi audioApi = std::get<1>(GetParam());
69     const PerformanceMode performanceMode = std::get<2>(GetParam());
70     const bool useRequestStart = std::get<3>(GetParam());
71 
72     ReturnStopCallback *callback = new ReturnStopCallback();
73     mBuilder.setDirection(direction)
74             ->setFormat(AudioFormat::I16)
75             ->setPerformanceMode(performanceMode)
76             ->setDataCallback(callback);
77     if (mBuilder.isAAudioRecommended()) {
78         mBuilder.setAudioApi(audioApi);
79     }
80     Result r = mBuilder.openStream(mStream);
81     ASSERT_EQ(r, Result::OK) << "Failed to open stream. " << convertToText(r);
82 
83     // Start and stop several times.
84     for (int i = 0; i < 3; i++) {
85         callback->reset();
86         // Oboe has two ways to start a stream.
87         if (useRequestStart) {
88             r = mStream->requestStart();
89         } else {
90             r = mStream->start();
91         }
92         ASSERT_EQ(r, Result::OK) << "Failed to start stream. " << convertToText(r);
93 
94         // Wait for callbacks to complete.
95         const int kMaxCallbackPeriodMillis = 500;
96         const int kPollPeriodMillis = 20;
97         int timeout = 2 * callback->getMaxCallbacks() * kMaxCallbackPeriodMillis / kPollPeriodMillis;
98         do {
99             usleep(kPollPeriodMillis * 1000);
100         } while (callback->callbackCount < callback->getMaxCallbacks() && timeout-- > 0);
101         EXPECT_GT(timeout, 0) << "timed out waiting for enough callbacks";
102 
103         StreamState next = StreamState::Unknown;
104         r = mStream->waitForStateChange(StreamState::Started, &next, kTimeoutInNanos);
105         EXPECT_EQ(r, Result::OK) << "waitForStateChange(Started) timed out. " << convertToText(r);
106         r = mStream->waitForStateChange(StreamState::Stopping, &next, kTimeoutInNanos);
107         EXPECT_EQ(r, Result::OK) << "waitForStateChange(Stopping) timed out. " << convertToText(r);
108         EXPECT_EQ(next, StreamState::Stopped) << "Stream not in state Stopped, was " << convertToText(next);
109 
110         EXPECT_EQ(callback->callbackCount, callback->getMaxCallbacks()) << "Too many callbacks = " << callback->callbackCount;
111 
112         const int kOboeStartStopSleepMSec = 10;
113         usleep(kOboeStartStopSleepMSec * 1000); // avoid race condition in emulator
114     }
115 
116     ASSERT_EQ(Result::OK, mStream->close());
117 }
118 
119 INSTANTIATE_TEST_SUITE_P(
120         StreamReturnStopTest,
121         StreamReturnStop,
122         ::testing::Values(
123                 // Last boolean is true if requestStart() should be called instead of start().
124                 StreamReturnStopParams({Direction::Output, AudioApi::AAudio, PerformanceMode::LowLatency, true}),
125                 StreamReturnStopParams({Direction::Output, AudioApi::AAudio, PerformanceMode::LowLatency, false}),
126                 StreamReturnStopParams({Direction::Output, AudioApi::AAudio, PerformanceMode::None, true}),
127                 StreamReturnStopParams({Direction::Output, AudioApi::AAudio, PerformanceMode::None, false}),
128                 StreamReturnStopParams({Direction::Output, AudioApi::OpenSLES, PerformanceMode::LowLatency, true}),
129                 StreamReturnStopParams({Direction::Output, AudioApi::OpenSLES, PerformanceMode::LowLatency, false}),
130                 StreamReturnStopParams({Direction::Input, AudioApi::AAudio, PerformanceMode::LowLatency, true}),
131                 StreamReturnStopParams({Direction::Input, AudioApi::AAudio, PerformanceMode::LowLatency, false})
132                 )
133         );
134