Mikhail Naganov | a331de1 | 2017-01-04 16:33:55 -0800 | [diff] [blame] | 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 <atomic> |
| 18 | |
| 19 | #include <hidlmemory/mapping.h> |
| 20 | |
| 21 | #include "AudioBufferManager.h" |
| 22 | |
| 23 | namespace android { |
| 24 | |
| 25 | ANDROID_SINGLETON_STATIC_INSTANCE(AudioBufferManager); |
| 26 | |
| 27 | bool AudioBufferManager::wrap(const AudioBuffer& buffer, sp<AudioBufferWrapper>* wrapper) { |
| 28 | // Check if we have this buffer already |
| 29 | std::lock_guard<std::mutex> lock(mLock); |
| 30 | ssize_t idx = mBuffers.indexOfKey(buffer.id); |
| 31 | if (idx >= 0) { |
| 32 | *wrapper = mBuffers[idx].promote(); |
| 33 | if (*wrapper != nullptr) return true; |
| 34 | mBuffers.removeItemsAt(idx); |
| 35 | } |
| 36 | // Need to create and init a new AudioBufferWrapper. |
| 37 | sp<AudioBufferWrapper> tempBuffer(new AudioBufferWrapper(buffer)); |
| 38 | if (!tempBuffer->init()) return false; |
| 39 | *wrapper = tempBuffer; |
| 40 | mBuffers.add(buffer.id, *wrapper); |
| 41 | return true; |
| 42 | } |
| 43 | |
| 44 | void AudioBufferManager::removeEntry(uint64_t id) { |
| 45 | std::lock_guard<std::mutex> lock(mLock); |
| 46 | ssize_t idx = mBuffers.indexOfKey(id); |
| 47 | if (idx >= 0) mBuffers.removeItemsAt(idx); |
| 48 | } |
| 49 | |
| 50 | namespace hardware { |
| 51 | namespace audio { |
| 52 | namespace effect { |
| 53 | namespace V2_0 { |
| 54 | namespace implementation { |
| 55 | |
| 56 | AudioBufferWrapper::AudioBufferWrapper(const AudioBuffer& buffer) : |
| 57 | mHidlBuffer(buffer), mHalBuffer{ 0, { nullptr } } { |
| 58 | } |
| 59 | |
| 60 | AudioBufferWrapper::~AudioBufferWrapper() { |
| 61 | AudioBufferManager::getInstance().removeEntry(mHidlBuffer.id); |
| 62 | } |
| 63 | |
| 64 | bool AudioBufferWrapper::init() { |
| 65 | if (mHalBuffer.raw != nullptr) { |
| 66 | ALOGE("An attempt to init AudioBufferWrapper twice"); |
| 67 | return false; |
| 68 | } |
| 69 | mHidlMemory = mapMemory(mHidlBuffer.data); |
| 70 | if (mHidlMemory == nullptr) { |
| 71 | ALOGE("Could not map HIDL memory to IMemory"); |
| 72 | return false; |
| 73 | } |
| 74 | mHalBuffer.raw = static_cast<void*>(mHidlMemory->getPointer()); |
| 75 | if (mHalBuffer.raw == nullptr) { |
| 76 | ALOGE("IMemory buffer pointer is null"); |
| 77 | return false; |
| 78 | } |
| 79 | mHalBuffer.frameCount = mHidlBuffer.frameCount; |
| 80 | return true; |
| 81 | } |
| 82 | |
| 83 | } // namespace implementation |
| 84 | } // namespace V2_0 |
| 85 | } // namespace effect |
| 86 | } // namespace audio |
| 87 | } // namespace hardware |
| 88 | } // namespace android |