1 /*
2 * Copyright 2018 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
18 #include <gtest/gtest.h>
19 #include <oboe/Oboe.h>
20
21 using namespace oboe;
22
23
24 class MyCallback : public AudioStreamCallback {
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 class XRunBehaviour : public ::testing::Test {
32
33 protected:
34
SetUp()35 void SetUp(){
36
37 }
38
39
openStream()40 void openStream(){
41 Result r = mBuilder.openStream(&mStream);
42 if (r != Result::OK){
43 FAIL() << "Failed to open stream. " << convertToText(r);
44 }
45 }
46
closeStream()47 void closeStream(){
48 if (mStream != nullptr){
49 Result r = mStream->close();
50 if (r != Result::OK){
51 FAIL() << "Failed to close stream. " << convertToText(r);
52 }
53 }
54 }
55
openAndCloseStream()56 void openAndCloseStream(){
57
58 openStream();
59 closeStream();
60 ASSERT_EQ(mStream->getState(), StreamState::Closed);
61 }
62
63 AudioStreamBuilder mBuilder;
64 AudioStream *mStream = nullptr;
65
66 };
67
68 // TODO figure out this behaviour - On OpenSLES xRuns are supported within AudioStreamBuffered,
69 // however, these aren't the same as the actual stream underruns
TEST_F(XRunBehaviour,SupportedWhenStreamIsUsingAAudio)70 TEST_F(XRunBehaviour, SupportedWhenStreamIsUsingAAudio){
71
72 openStream();
73 if (mStream->getAudioApi() == AudioApi::AAudio){
74 ASSERT_TRUE(mStream->isXRunCountSupported());
75 }
76 closeStream();
77 }
78
TEST_F(XRunBehaviour,NotSupportedOnOpenSLESWhenStreamIsUsingCallback)79 TEST_F(XRunBehaviour, NotSupportedOnOpenSLESWhenStreamIsUsingCallback){
80
81 MyCallback callback;
82 mBuilder.setCallback(&callback);
83 openStream();
84 if (mStream->getAudioApi() == AudioApi::OpenSLES){
85 ASSERT_FALSE(mStream->isXRunCountSupported());
86 }
87 closeStream();
88 }
89
TEST_F(XRunBehaviour,SupportedOnOpenSLESWhenStreamIsUsingBlockingIO)90 TEST_F(XRunBehaviour, SupportedOnOpenSLESWhenStreamIsUsingBlockingIO){
91
92 openStream();
93 if (mStream->getAudioApi() == AudioApi::OpenSLES){
94 ASSERT_TRUE(mStream->isXRunCountSupported());
95 }
96 closeStream();
97 }