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_RAPID_CYCLE_H 18 #define OBOETESTER_TEST_RAPID_CYCLE_H 19 20 #include "oboe/Oboe.h" 21 #include <thread> 22 23 24 /** 25 * Try to cause a crash by changing routing during a data callback. 26 * We use Use::VoiceCommunication for the stream and 27 * setSpeakerPhoneOn(b) to force a routing change. 28 * This works best when connected to a BT headset. 29 */ 30 class TestRapidCycle { 31 public: 32 33 int32_t start(bool useOpenSL); 34 int32_t stop(); 35 getCycleCount()36 int32_t getCycleCount() { 37 return mCycleCount.load(); 38 } 39 40 private: 41 42 void cycleRapidly(bool useOpenSL); 43 int32_t oneCycle(bool useOpenSL); 44 45 class MyDataCallback : public oboe::AudioStreamDataCallback { public: 46 MyDataCallback()47 MyDataCallback() {} 48 49 oboe::DataCallbackResult onAudioReady( 50 oboe::AudioStream *audioStream, 51 void *audioData, 52 int32_t numFrames) override; 53 54 bool returnStop = false; 55 }; 56 57 std::shared_ptr<oboe::AudioStream> mStream; 58 std::shared_ptr<MyDataCallback> mDataCallback; 59 std::atomic<int32_t> mCycleCount{0}; 60 std::atomic<bool> mThreadEnabled{false}; 61 std::thread mCycleThread; 62 63 static constexpr int kChannelCount = 1; 64 static constexpr int kMaxSleepMicros = 25000; 65 }; 66 67 #endif //OBOETESTER_TEST_RAPID_CYCLE_H 68