1 /* 2 * Copyright 2019 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 SAMPLES_DEFAULT_DATA_CALLBACK_H 18 #define SAMPLES_DEFAULT_DATA_CALLBACK_H 19 20 21 #include <vector> 22 #include <oboe/AudioStreamCallback.h> 23 #include <logging_macros.h> 24 25 #include "IRenderableAudio.h" 26 #include "IRestartable.h" 27 28 /** 29 * This is a callback object which will render data from an `IRenderableAudio` source. 30 */ 31 class DefaultDataCallback : public oboe::AudioStreamDataCallback { 32 public: DefaultDataCallback()33 DefaultDataCallback() {} 34 virtual ~DefaultDataCallback() = default; 35 36 virtual oboe::DataCallbackResult onAudioReady(oboe::AudioStream * oboeStream,void * audioData,int32_t numFrames)37 onAudioReady(oboe::AudioStream *oboeStream, void *audioData, int32_t numFrames) override { 38 39 if (mIsThreadAffinityEnabled && !mIsThreadAffinitySet) { 40 setThreadAffinity(); 41 mIsThreadAffinitySet = true; 42 } 43 44 float *outputBuffer = static_cast<float *>(audioData); 45 46 std::shared_ptr<IRenderableAudio> localRenderable = mRenderable; 47 if (!localRenderable) { 48 LOGE("Renderable source not set!"); 49 return oboe::DataCallbackResult::Stop; 50 } 51 localRenderable->renderAudio(outputBuffer, numFrames); 52 return oboe::DataCallbackResult::Continue; 53 } 54 setSource(std::shared_ptr<IRenderableAudio> renderable)55 void setSource(std::shared_ptr<IRenderableAudio> renderable) { 56 mRenderable = renderable; 57 } 58 59 /** 60 * Reset the callback to its initial state. 61 */ reset()62 void reset(){ 63 mIsThreadAffinitySet = false; 64 } 65 getSource()66 std::shared_ptr<IRenderableAudio> getSource() { 67 return mRenderable; 68 } 69 70 /** 71 * Set the CPU IDs to bind the audio callback thread to 72 * 73 * @param mCpuIds - the CPU IDs to bind to 74 */ setCpuIds(std::vector<int> cpuIds)75 void setCpuIds(std::vector<int> cpuIds){ 76 mCpuIds = std::move(cpuIds); 77 } 78 79 /** 80 * Enable or disable binding the audio callback thread to specific CPU cores. The CPU core IDs 81 * can be specified using @see setCpuIds. If no CPU IDs are specified the initial core which the 82 * audio thread is called on will be used. 83 * 84 * @param isEnabled - whether the audio callback thread should be bound to specific CPU core(s) 85 */ setThreadAffinityEnabled(bool isEnabled)86 void setThreadAffinityEnabled(bool isEnabled){ 87 mIsThreadAffinityEnabled = isEnabled; 88 LOGD("Thread affinity enabled: %s", (isEnabled) ? "true" : "false"); 89 } 90 91 private: 92 std::shared_ptr<IRenderableAudio> mRenderable; 93 std::vector<int> mCpuIds; // IDs of CPU cores which the audio callback should be bound to 94 std::atomic<bool> mIsThreadAffinityEnabled { false }; 95 std::atomic<bool> mIsThreadAffinitySet { false }; 96 97 /** 98 * Set the thread affinity for the current thread to mCpuIds. This can be useful to call on the 99 * audio thread to avoid underruns caused by CPU core migrations to slower CPU cores. 100 */ setThreadAffinity()101 void setThreadAffinity() { 102 103 pid_t current_thread_id = gettid(); 104 cpu_set_t cpu_set; 105 CPU_ZERO(&cpu_set); 106 107 // If the callback cpu ids aren't specified then bind to the current cpu 108 if (mCpuIds.empty()) { 109 int current_cpu_id = sched_getcpu(); 110 LOGD("Binding to current CPU ID %d", current_cpu_id); 111 CPU_SET(current_cpu_id, &cpu_set); 112 } else { 113 LOGD("Binding to %d CPU IDs", static_cast<int>(mCpuIds.size())); 114 for (size_t i = 0; i < mCpuIds.size(); i++) { 115 int cpu_id = mCpuIds.at(i); 116 LOGD("CPU ID %d added to cores set", cpu_id); 117 CPU_SET(cpu_id, &cpu_set); 118 } 119 } 120 121 int result = sched_setaffinity(current_thread_id, sizeof(cpu_set_t), &cpu_set); 122 if (result == 0) { 123 LOGV("Thread affinity set"); 124 } else { 125 LOGW("Error setting thread affinity. Error no: %d", result); 126 } 127 128 mIsThreadAffinitySet = true; 129 } 130 131 }; 132 133 #endif //SAMPLES_DEFAULT_DATA_CALLBACK_H 134