xref: /aosp_15_r20/external/oboe/tests/testStreamFramesProcessed.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 <gtest/gtest.h>
18 #include <oboe/Oboe.h>
19 
20 #include <tuple>
21 
22 using namespace oboe;
23 
24 class FramesProcessedCallback : public AudioStreamDataCallback {
25 public:
onAudioReady(AudioStream * oboeStream,void * audioData,int32_t numFrames)26     DataCallbackResult onAudioReady(AudioStream *oboeStream, void *audioData, int32_t numFrames) override {
27         return DataCallbackResult::Continue;
28     }
29 };
30 
31 using StreamFramesProcessedParams = std::tuple<Direction, int32_t>;
32 
33 class StreamFramesProcessed : public ::testing::Test,
34                               public ::testing::WithParamInterface<StreamFramesProcessedParams> {
35 
36 protected:
37     void TearDown() override;
38 
39     static constexpr int PROCESS_TIME_SECONDS = 5;
40 
41     AudioStreamBuilder mBuilder;
42     AudioStream *mStream = nullptr;
43 };
44 
TearDown()45 void StreamFramesProcessed::TearDown() {
46     if (mStream != nullptr) {
47         mStream->close();
48         mStream = nullptr;
49     }
50 }
51 
TEST_P(StreamFramesProcessed,VerifyFramesProcessed)52 TEST_P(StreamFramesProcessed, VerifyFramesProcessed) {
53     const Direction direction = std::get<0>(GetParam());
54     const int32_t sampleRate = std::get<1>(GetParam());
55 
56     AudioStreamDataCallback *callback = new FramesProcessedCallback();
57     mBuilder.setDirection(direction)
58             ->setFormat(AudioFormat::I16)
59             ->setSampleRate(sampleRate)
60             ->setSampleRateConversionQuality(SampleRateConversionQuality::Medium)
61             ->setPerformanceMode(PerformanceMode::LowLatency)
62             ->setSharingMode(SharingMode::Exclusive)
63             ->setDataCallback(callback);
64     mStream = nullptr;
65     Result r = mBuilder.openStream(&mStream);
66     ASSERT_EQ(r, Result::OK) << "Failed to open stream." << convertToText(r);
67 
68     r = mStream->start();
69     ASSERT_EQ(r, Result::OK) << "Failed to start stream." << convertToText(r);
70     sleep(PROCESS_TIME_SECONDS);
71 
72     // The frames written should be close to sampleRate * PROCESS_TIME_SECONDS
73     const int kDeltaFramesWindowInFrames = 30000;
74     const int64_t framesWritten = mStream->getFramesWritten();
75     const int64_t framesRead = mStream->getFramesRead();
76     EXPECT_NEAR(framesWritten, sampleRate * PROCESS_TIME_SECONDS, kDeltaFramesWindowInFrames);
77     EXPECT_NEAR(framesRead, sampleRate * PROCESS_TIME_SECONDS, kDeltaFramesWindowInFrames);
78 }
79 
80 INSTANTIATE_TEST_CASE_P(
81         StreamFramesProcessedTest,
82         StreamFramesProcessed,
83         ::testing::Values(
84                 StreamFramesProcessedParams({Direction::Output, 8000}),
85                 StreamFramesProcessedParams({Direction::Output, 44100}),
86                 StreamFramesProcessedParams({Direction::Output, 96000}),
87                 StreamFramesProcessedParams({Direction::Input, 8000}),
88                 StreamFramesProcessedParams({Direction::Input, 44100}),
89                 StreamFramesProcessedParams({Direction::Input, 96000})
90                 )
91         );
92