Phil Burk | 0433d8f | 2018-11-21 16:41:25 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 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 | |
Phil Burk | 71d97e9 | 2019-03-08 12:50:01 -0800 | [diff] [blame] | 17 | #include "common/OboeDebug.h" |
Phil Burk | 0433d8f | 2018-11-21 16:41:25 -0800 | [diff] [blame] | 18 | #include "OboeStreamCallbackProxy.h" |
| 19 | |
Phil Burk | 0f94ca1 | 2019-12-21 15:13:40 -0800 | [diff] [blame^] | 20 | // Linear congruential random number generator. |
| 21 | static uint32_t s_random16() { |
| 22 | static uint32_t seed = 1234; |
| 23 | seed = ((seed * 31421) + 6927) & 0x0FFFF; |
| 24 | return seed; |
Phil Burk | 0433d8f | 2018-11-21 16:41:25 -0800 | [diff] [blame] | 25 | } |
| 26 | |
Phil Burk | 0f94ca1 | 2019-12-21 15:13:40 -0800 | [diff] [blame^] | 27 | /** |
| 28 | * The random number generator is good for burning CPU because the compiler cannot |
| 29 | * easily optimize away the computation. |
| 30 | * @param workload number of times to execute the loop |
| 31 | * @return a white noise value between -1.0 and +1.0 |
| 32 | */ |
| 33 | static float s_burnCPU(int32_t workload) { |
| 34 | uint32_t random = 0; |
| 35 | for (int32_t i = 0; i < workload; i++) { |
| 36 | for (int32_t j = 0; j < 10; j++) { |
| 37 | random = random ^ s_random16(); |
| 38 | } |
| 39 | } |
| 40 | return (random - 32768) * (1.0 / 32768); |
| 41 | } |
| 42 | |
| 43 | bool OboeStreamCallbackProxy::mCallbackReturnStop = false; |
| 44 | |
Phil Burk | 0433d8f | 2018-11-21 16:41:25 -0800 | [diff] [blame] | 45 | oboe::DataCallbackResult OboeStreamCallbackProxy::onAudioReady( |
| 46 | oboe::AudioStream *audioStream, |
| 47 | void *audioData, |
| 48 | int numFrames) { |
Phil Burk | 6fb1d80 | 2018-12-21 15:28:07 -0800 | [diff] [blame] | 49 | mCallbackCount++; |
| 50 | if (mCallbackReturnStop) { |
| 51 | return oboe::DataCallbackResult::Stop; |
| 52 | } |
Phil Burk | 0f94ca1 | 2019-12-21 15:13:40 -0800 | [diff] [blame^] | 53 | |
| 54 | // |
| 55 | s_burnCPU((int32_t)(mWorkload * kWorkloadScaler * numFrames)); |
| 56 | |
Phil Burk | 0433d8f | 2018-11-21 16:41:25 -0800 | [diff] [blame] | 57 | if (mCallback != nullptr) { |
| 58 | return mCallback->onAudioReady(audioStream, audioData, numFrames); |
| 59 | } |
Phil Burk | 0433d8f | 2018-11-21 16:41:25 -0800 | [diff] [blame] | 60 | return oboe::DataCallbackResult::Stop; |
| 61 | } |
| 62 | |
| 63 | void OboeStreamCallbackProxy::onErrorBeforeClose(oboe::AudioStream *audioStream, oboe::Result error) { |
| 64 | if (mCallback != nullptr) { |
| 65 | return mCallback->onErrorBeforeClose(audioStream, error); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | void OboeStreamCallbackProxy::onErrorAfterClose(oboe::AudioStream *audioStream, oboe::Result error) { |
| 70 | if (mCallback != nullptr) { |
| 71 | return mCallback->onErrorAfterClose(audioStream, error); |
| 72 | } |
| 73 | } |