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