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 #ifndef OBOE_STABILIZEDCALLBACK_H 18 #define OBOE_STABILIZEDCALLBACK_H 19 20 #include <cstdint> 21 #include "oboe/AudioStream.h" 22 23 namespace oboe { 24 25 class StabilizedCallback : public AudioStreamCallback { 26 27 public: 28 explicit StabilizedCallback(AudioStreamCallback *callback); 29 30 DataCallbackResult 31 onAudioReady(AudioStream *oboeStream, void *audioData, int32_t numFrames) override; 32 onErrorBeforeClose(AudioStream * oboeStream,Result error)33 void onErrorBeforeClose(AudioStream *oboeStream, Result error) override { 34 return mCallback->onErrorBeforeClose(oboeStream, error); 35 } 36 onErrorAfterClose(AudioStream * oboeStream,Result error)37 void onErrorAfterClose(AudioStream *oboeStream, Result error) override { 38 39 // Reset all fields now that the stream has been closed 40 mFrameCount = 0; 41 mEpochTimeNanos = 0; 42 mOpsPerNano = 1; 43 return mCallback->onErrorAfterClose(oboeStream, error); 44 } 45 46 private: 47 48 AudioStreamCallback *mCallback = nullptr; 49 int64_t mFrameCount = 0; 50 int64_t mEpochTimeNanos = 0; 51 double mOpsPerNano = 1; 52 53 void generateLoad(int64_t durationNanos); 54 }; 55 56 /** 57 * cpu_relax is an architecture specific method of telling the CPU that you don't want it to 58 * do much work. asm volatile keeps the compiler from optimising these instructions out. 59 */ 60 #if defined(__i386__) || defined(__x86_64__) 61 #define cpu_relax() asm volatile("rep; nop" ::: "memory"); 62 63 #elif defined(__arm__) || defined(__mips__) 64 #define cpu_relax() asm volatile("":::"memory") 65 66 #elif defined(__aarch64__) 67 #define cpu_relax() asm volatile("yield" ::: "memory") 68 69 #else 70 #error "cpu_relax is not defined for this architecture" 71 #endif 72 73 } 74 75 #endif //OBOE_STABILIZEDCALLBACK_H 76