blob: 03bc2201e3c1ff3a6f296bcaf588b3ffe2703596 [file] [log] [blame]
Phil Burk0433d8f2018-11-21 16:41:25 -08001/*
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 Burk71d97e92019-03-08 12:50:01 -080017#include "common/OboeDebug.h"
Phil Burk0433d8f2018-11-21 16:41:25 -080018#include "OboeStreamCallbackProxy.h"
19
Phil Burk0f94ca12019-12-21 15:13:40 -080020// Linear congruential random number generator.
21static uint32_t s_random16() {
22 static uint32_t seed = 1234;
23 seed = ((seed * 31421) + 6927) & 0x0FFFF;
24 return seed;
Phil Burk0433d8f2018-11-21 16:41:25 -080025}
26
Phil Burk0f94ca12019-12-21 15:13:40 -080027/**
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 */
33static 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
43bool OboeStreamCallbackProxy::mCallbackReturnStop = false;
44
Phil Burk0433d8f2018-11-21 16:41:25 -080045oboe::DataCallbackResult OboeStreamCallbackProxy::onAudioReady(
46 oboe::AudioStream *audioStream,
47 void *audioData,
48 int numFrames) {
Phil Burk6fb1d802018-12-21 15:28:07 -080049 mCallbackCount++;
50 if (mCallbackReturnStop) {
51 return oboe::DataCallbackResult::Stop;
52 }
Phil Burk0f94ca12019-12-21 15:13:40 -080053
54 //
55 s_burnCPU((int32_t)(mWorkload * kWorkloadScaler * numFrames));
56
Phil Burk0433d8f2018-11-21 16:41:25 -080057 if (mCallback != nullptr) {
58 return mCallback->onAudioReady(audioStream, audioData, numFrames);
59 }
Phil Burk0433d8f2018-11-21 16:41:25 -080060 return oboe::DataCallbackResult::Stop;
61}
62
63void OboeStreamCallbackProxy::onErrorBeforeClose(oboe::AudioStream *audioStream, oboe::Result error) {
64 if (mCallback != nullptr) {
65 return mCallback->onErrorBeforeClose(audioStream, error);
66 }
67}
68
69void OboeStreamCallbackProxy::onErrorAfterClose(oboe::AudioStream *audioStream, oboe::Result error) {
70 if (mCallback != nullptr) {
71 return mCallback->onErrorAfterClose(audioStream, error);
72 }
73}