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, bool>;
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 std::shared_ptr<AudioStream> mStream;
43 };
44
TearDown()45 void StreamFramesProcessed::TearDown() {
46 if (mStream) {
47 mStream->close();
48 }
49 }
50
TEST_P(StreamFramesProcessed,VerifyFramesProcessed)51 TEST_P(StreamFramesProcessed, VerifyFramesProcessed) {
52 const Direction direction = std::get<0>(GetParam());
53 const int32_t sampleRate = std::get<1>(GetParam());
54 const bool useOboeSampleRateConversion = std::get<2>(GetParam());
55
56 SampleRateConversionQuality srcQuality = useOboeSampleRateConversion ?
57 SampleRateConversionQuality::Medium : SampleRateConversionQuality::None;
58
59 AudioStreamDataCallback *callback = new FramesProcessedCallback();
60 mBuilder.setDirection(direction)
61 ->setFormat(AudioFormat::I16)
62 ->setSampleRate(sampleRate)
63 ->setSampleRateConversionQuality(srcQuality)
64 ->setPerformanceMode(PerformanceMode::LowLatency)
65 ->setSharingMode(SharingMode::Exclusive)
66 ->setDataCallback(callback);
67 Result r = mBuilder.openStream(mStream);
68 ASSERT_EQ(r, Result::OK) << "Failed to open stream." << convertToText(r);
69
70 r = mStream->start();
71 ASSERT_EQ(r, Result::OK) << "Failed to start stream." << convertToText(r);
72 sleep(PROCESS_TIME_SECONDS);
73
74 // The frames written should be close to sampleRate * PROCESS_TIME_SECONDS
75 const int kDeltaFramesWindowInFrames = 30000;
76 const int64_t framesWritten = mStream->getFramesWritten();
77 const int64_t framesRead = mStream->getFramesRead();
78 EXPECT_NEAR(framesWritten, sampleRate * PROCESS_TIME_SECONDS, kDeltaFramesWindowInFrames);
79 EXPECT_NEAR(framesRead, sampleRate * PROCESS_TIME_SECONDS, kDeltaFramesWindowInFrames);
80 }
81
82 INSTANTIATE_TEST_CASE_P(
83 StreamFramesProcessedTest,
84 StreamFramesProcessed,
85 ::testing::Values(
86 StreamFramesProcessedParams({Direction::Output, 8000, true}),
87 StreamFramesProcessedParams({Direction::Output, 44100, true}),
88 StreamFramesProcessedParams({Direction::Output, 96000, true}),
89 StreamFramesProcessedParams({Direction::Input, 8000, true}),
90 StreamFramesProcessedParams({Direction::Input, 44100, true}),
91 StreamFramesProcessedParams({Direction::Input, 96000, true}),
92 StreamFramesProcessedParams({Direction::Output, 8000, false}),
93 StreamFramesProcessedParams({Direction::Output, 44100, false}),
94 StreamFramesProcessedParams({Direction::Output, 96000, false}),
95 StreamFramesProcessedParams({Direction::Input, 8000, false}),
96 StreamFramesProcessedParams({Direction::Input, 44100, false}),
97 StreamFramesProcessedParams({Direction::Input, 96000, false})
98 )
99 );
100