• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 #ifndef NATIVEOBOE_OBOESTREAMCALLBACKPROXY_H
18 #define NATIVEOBOE_OBOESTREAMCALLBACKPROXY_H
19 
20 #include <unistd.h>
21 #include <sys/types.h>
22 
23 #include "oboe/Oboe.h"
24 
25 class OboeStreamCallbackProxy : public oboe::AudioStreamDataCallback {
26 public:
27 
setCallback(oboe::AudioStreamCallback * callback)28     void setCallback(oboe::AudioStreamCallback *callback) {
29         mCallback = callback;
30         setCallbackCount(0);
31     }
32 
setCallbackReturnStop(bool b)33     static void setCallbackReturnStop(bool b) {
34         mCallbackReturnStop = b;
35     }
36 
getCallbackCount()37     int64_t getCallbackCount() {
38         return mCallbackCount;
39     }
40 
setCallbackCount(int64_t count)41     void setCallbackCount(int64_t count) {
42         mCallbackCount = count;
43     }
44 
getFramesPerCallback()45     int32_t getFramesPerCallback() {
46         return mFramesPerCallback.load();
47     }
48 
49     /**
50      * Called when the stream is ready to process audio.
51      */
52     oboe::DataCallbackResult onAudioReady(
53             oboe::AudioStream *audioStream,
54             void *audioData,
55             int numFrames) override;
56 
57     /**
58      * Specify the amount of artificial workload that will waste CPU cycles
59      * and increase the CPU load.
60      * @param workload typically ranges from 0.0 to 100.0
61      */
setWorkload(double workload)62     void setWorkload(double workload) {
63         mWorkload = std::max(0.0, workload);
64     }
65 
getWorkload()66     double getWorkload() const {
67         return mWorkload;
68     }
69 
getCpuLoad()70     double getCpuLoad() const {
71         return mCpuLoad;
72     }
73 
74     static int64_t getNanoseconds(clockid_t clockId = CLOCK_MONOTONIC);
75 
76 private:
77     static constexpr int32_t   kWorkloadScaler = 500;
78     double                     mWorkload = 0.0;
79     std::atomic<double>        mCpuLoad{0};
80 
81     oboe::AudioStreamCallback *mCallback = nullptr;
82     static bool                mCallbackReturnStop;
83     int64_t                    mCallbackCount = 0;
84     std::atomic<int32_t>       mFramesPerCallback{0};
85 };
86 
87 
88 #endif //NATIVEOBOE_OBOESTREAMCALLBACKPROXY_H
89