xref: /aosp_15_r20/external/oboe/apps/OboeTester/app/src/main/cpp/TestRoutingCrash.h (revision 05767d913155b055644481607e6fa1e35e2fe72c)
1 /*
2  * Copyright 2023 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 #ifndef OBOETESTER_TEST_ROUTING_CRASH_H
18 #define OBOETESTER_TEST_ROUTING_CRASH_H
19 
20 #include "oboe/Oboe.h"
21 #include <thread>
22 
23 /**
24  * Try to cause a crash by changing routing during a data callback.
25  * We use Use::VoiceCommunication for the stream and
26  * setSpeakerPhoneOn(b) to force a routing change.
27  * This works best when connected to a BT headset.
28  */
29 class TestRoutingCrash {
30 public:
31 
32     int32_t start(bool useInput);
33     int32_t stop();
34 
getSleepTimeMicros()35     int32_t getSleepTimeMicros() {
36         return (int32_t) (averageSleepTimeMicros.load());
37     }
38 
39 protected:
40 
41     std::atomic<double> averageSleepTimeMicros{0};
42 
43 private:
44 
45     class MyDataCallback : public oboe::AudioStreamDataCallback {    public:
46 
MyDataCallback(TestRoutingCrash * parent)47         MyDataCallback(TestRoutingCrash *parent): mParent(parent) {}
48 
49         oboe::DataCallbackResult onAudioReady(
50                 oboe::AudioStream *audioStream,
51                 void *audioData,
52                 int32_t numFrames) override;
53     private:
54         TestRoutingCrash *mParent;
55         // For sine generator.
56         float mPhase = 0.0f;
57         static constexpr float kPhaseIncrement = 2.0f * (float) M_PI * 440.0f / 48000.0f;
58         float mInputSum = 0.0f; // For saving input data sum to prevent over-optimization.
59     };
60 
61     std::shared_ptr<oboe::AudioStream> mStream;
62     std::shared_ptr<MyDataCallback> mDataCallback;
63 
64     static constexpr int kChannelCount = 1;
65 };
66 
67 #endif //OBOETESTER_TEST_ROUTING_CRASH_H
68