1 /* 2 * Copyright (C) 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 #include <common/all-versions/IncludeGuard.h> 18 19 #include <mutex> 20 21 #include <android/hidl/memory/1.0/IMemory.h> 22 #include <system/audio_effect.h> 23 #include <utils/KeyedVector.h> 24 #include <utils/RefBase.h> 25 #include <utils/Singleton.h> 26 27 using ::android::hardware::audio::effect::AUDIO_HAL_VERSION::AudioBuffer; 28 using ::android::hidl::memory::V1_0::IMemory; 29 30 namespace android { 31 namespace hardware { 32 namespace audio { 33 namespace effect { 34 namespace AUDIO_HAL_VERSION { 35 namespace implementation { 36 37 class AudioBufferWrapper : public RefBase { 38 public: 39 explicit AudioBufferWrapper(const AudioBuffer& buffer); 40 virtual ~AudioBufferWrapper(); 41 bool init(); 42 audio_buffer_t* getHalBuffer() { return &mHalBuffer; } 43 44 private: 45 AudioBufferWrapper(const AudioBufferWrapper&) = delete; 46 void operator=(AudioBufferWrapper) = delete; 47 48 AudioBuffer mHidlBuffer; 49 sp<IMemory> mHidlMemory; 50 audio_buffer_t mHalBuffer; 51 }; 52 53 } // namespace implementation 54 } // namespace AUDIO_HAL_VERSION 55 } // namespace effect 56 } // namespace audio 57 } // namespace hardware 58 } // namespace android 59 60 using ::android::hardware::audio::effect::AUDIO_HAL_VERSION::implementation::AudioBufferWrapper; 61 62 namespace android { 63 64 // This class needs to be in 'android' ns because Singleton macros require that. 65 class AudioBufferManager : public Singleton<AudioBufferManager> { 66 public: 67 bool wrap(const AudioBuffer& buffer, sp<AudioBufferWrapper>* wrapper); 68 69 private: 70 friend class hardware::audio::effect::AUDIO_HAL_VERSION::implementation::AudioBufferWrapper; 71 72 // Called by AudioBufferWrapper. 73 void removeEntry(uint64_t id); 74 75 std::mutex mLock; 76 KeyedVector<uint64_t, wp<AudioBufferWrapper>> mBuffers; 77 }; 78 79 } // namespace android 80