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