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 #include <stdlib.h> 18 #include <aaudio/AAudioExtensions.h> 19 20 #include "common/OboeDebug.h" 21 #include "common/AudioClock.h" 22 #include "TestRoutingCrash.h" 23 24 using namespace oboe; 25 26 // open start start an Oboe stream start(bool useInput)27int32_t TestRoutingCrash::start(bool useInput) { 28 29 mDataCallback = std::make_shared<MyDataCallback>(this); 30 31 // Disable MMAP because we are trying to crash a Legacy Stream. 32 bool wasMMapEnabled = AAudioExtensions::getInstance().isMMapEnabled(); 33 AAudioExtensions::getInstance().setMMapEnabled(false); 34 35 AudioStreamBuilder builder; 36 oboe::Result result = builder.setFormat(oboe::AudioFormat::Float) 37 #if 1 38 ->setPerformanceMode(oboe::PerformanceMode::LowLatency) 39 #else 40 ->setPerformanceMode(oboe::PerformanceMode::None) 41 #endif 42 ->setDirection(useInput ? oboe::Direction::Input : oboe::Direction::Output) 43 ->setChannelCount(kChannelCount) 44 ->setDataCallback(mDataCallback) 45 // Use VoiceCommunication so we can reroute it by setting SpeakerPhone ON/OFF. 46 ->setUsage(oboe::Usage::VoiceCommunication) 47 ->openStream(mStream); 48 if (result != oboe::Result::OK) { 49 return (int32_t) result; 50 } 51 52 AAudioExtensions::getInstance().setMMapEnabled(wasMMapEnabled); 53 return (int32_t) mStream->requestStart(); 54 } 55 stop()56int32_t TestRoutingCrash::stop() { 57 oboe::Result result1 = mStream->requestStop(); 58 oboe::Result result2 = mStream->close(); 59 return (int32_t)((result1 != oboe::Result::OK) ? result1 : result2); 60 } 61 62 // Callback that sleeps then touches the audio buffer. onAudioReady(AudioStream * audioStream,void * audioData,int32_t numFrames)63DataCallbackResult TestRoutingCrash::MyDataCallback::onAudioReady( 64 AudioStream *audioStream, 65 void *audioData, 66 int32_t numFrames) { 67 float *floatData = (float *) audioData; 68 69 // If I call getTimestamp() here it does NOT crash! 70 71 // Simulate the timing of a heavy workload by sleeping. 72 // Otherwise the window for the crash is very narrow. 73 const double kDutyCycle = 0.7; 74 const double bufferTimeNanos = 1.0e9 * numFrames / (double) audioStream->getSampleRate(); 75 const int64_t targetDurationNanos = (int64_t) (bufferTimeNanos * kDutyCycle); 76 if (targetDurationNanos > 0) { 77 AudioClock::sleepForNanos(targetDurationNanos); 78 } 79 const double kFilterCoefficient = 0.95; // low pass IIR filter 80 const double sleepMicros = targetDurationNanos * 0.0001; 81 mParent->averageSleepTimeMicros = ((1.0 - kFilterCoefficient) * sleepMicros) 82 + (kFilterCoefficient * mParent->averageSleepTimeMicros); 83 84 // If I call getTimestamp() here it crashes. 85 audioStream->getTimestamp(CLOCK_MONOTONIC); // Trigger a restoreTrack_l() in framework. 86 87 const int numSamples = numFrames * kChannelCount; 88 if (audioStream->getDirection() == oboe::Direction::Input) { 89 // Read buffer and write sum of samples to a member variable. 90 // We just want to touch the memory and not get optimized away by the compiler. 91 float sum = 0.0f; 92 for (int i = 0; i < numSamples; i++) { 93 sum += *floatData++; 94 } 95 mInputSum = sum; 96 } else { 97 // Fill mono buffer with a sine wave. 98 // If the routing occurred then the buffer may be dead and 99 // we may be writing into unallocated memory. 100 for (int i = 0; i < numSamples; i++) { 101 *floatData++ = sinf(mPhase) * 0.2f; 102 mPhase += kPhaseIncrement; 103 // Wrap the phase around in a circle. 104 if (mPhase >= M_PI) mPhase -= 2 * M_PI; 105 } 106 } 107 108 // If I call getTimestamp() here it does NOT crash! 109 110 return oboe::DataCallbackResult::Continue; 111 } 112