blob: 9a638fda2c96907aa882b5a8a177ba5166d1f5bc (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
|
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "AudioBufferManager.h"
#include <atomic>
#include <hidlmemory/mapping.h>
namespace android {
ANDROID_SINGLETON_STATIC_INSTANCE(AudioBufferManager);
bool AudioBufferManager::wrap(const AudioBuffer& buffer, sp<AudioBufferWrapper>* wrapper) {
// Check if we have this buffer already
std::lock_guard<std::mutex> lock(mLock);
ssize_t idx = mBuffers.indexOfKey(buffer.id);
if (idx >= 0) {
*wrapper = mBuffers[idx].promote();
if (*wrapper != nullptr) {
(*wrapper)->getHalBuffer()->frameCount = buffer.frameCount;
return true;
}
mBuffers.removeItemsAt(idx);
}
// Need to create and init a new AudioBufferWrapper.
sp<AudioBufferWrapper> tempBuffer(new AudioBufferWrapper(buffer));
if (!tempBuffer->init()) return false;
*wrapper = tempBuffer;
mBuffers.add(buffer.id, *wrapper);
return true;
}
void AudioBufferManager::removeEntry(uint64_t id) {
std::lock_guard<std::mutex> lock(mLock);
ssize_t idx = mBuffers.indexOfKey(id);
if (idx >= 0) mBuffers.removeItemsAt(idx);
}
namespace hardware {
namespace audio {
namespace effect {
namespace CPP_VERSION {
namespace implementation {
AudioBufferWrapper::AudioBufferWrapper(const AudioBuffer& buffer)
: mHidlBuffer(buffer), mHalBuffer{0, {nullptr}} {}
AudioBufferWrapper::~AudioBufferWrapper() {
AudioBufferManager::getInstance().removeEntry(mHidlBuffer.id);
}
bool AudioBufferWrapper::init() {
if (mHalBuffer.raw != nullptr) {
ALOGE("An attempt to init AudioBufferWrapper twice");
return false;
}
mHidlMemory = mapMemory(mHidlBuffer.data);
if (mHidlMemory == nullptr) {
ALOGE("Could not map HIDL memory to IMemory");
return false;
}
mHalBuffer.raw = static_cast<void*>(mHidlMemory->getPointer());
if (mHalBuffer.raw == nullptr) {
ALOGE("IMemory buffer pointer is null");
return false;
}
mHalBuffer.frameCount = mHidlBuffer.frameCount;
return true;
}
} // namespace implementation
} // namespace CPP_VERSION
} // namespace effect
} // namespace audio
} // namespace hardware
} // namespace android
|