• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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 <algorithm>
18 #include <cstdint>
19 #include <cstring>
20 #include <sys/mman.h>
21 #define LOG_TAG "EffectBufferHalAidl"
22 //#define LOG_NDEBUG 0
23 
24 #include <cutils/ashmem.h>
25 #include <utils/Log.h>
26 
27 #include "EffectBufferHalAidl.h"
28 
29 using ndk::ScopedFileDescriptor;
30 
31 namespace android {
32 namespace effect {
33 
34 // static
allocate(size_t size,sp<EffectBufferHalInterface> * buffer)35 status_t EffectBufferHalAidl::allocate(size_t size, sp<EffectBufferHalInterface>* buffer) {
36     return mirror(nullptr, size, buffer);
37 }
38 
mirror(void * external,size_t size,sp<EffectBufferHalInterface> * buffer)39 status_t EffectBufferHalAidl::mirror(void* external, size_t size,
40                                      sp<EffectBufferHalInterface>* buffer) {
41     sp<EffectBufferHalAidl> tempBuffer = new EffectBufferHalAidl(size);
42     status_t status = tempBuffer.get()->init();
43     if (status != OK) {
44         ALOGE("%s init failed %d", __func__, status);
45         return status;
46     }
47 
48     tempBuffer->setExternalData(external);
49     *buffer = tempBuffer;
50     return OK;
51 }
52 
EffectBufferHalAidl(size_t size)53 EffectBufferHalAidl::EffectBufferHalAidl(size_t size)
54     : mBufferSize(size),
55       mFrameCountChanged(false),
56       mExternalData(nullptr),
57       mAudioBuffer{0, {nullptr}} {
58 }
59 
~EffectBufferHalAidl()60 EffectBufferHalAidl::~EffectBufferHalAidl() {
61 }
62 
init()63 status_t EffectBufferHalAidl::init() {
64     int fd = ashmem_create_region("audioEffectAidl", mBufferSize);
65     if (fd < 0) {
66         ALOGE("%s create ashmem failed %d", __func__, fd);
67         return fd;
68     }
69 
70     ScopedFileDescriptor tempFd(fd);
71     mAudioBuffer.raw = mmap(nullptr /* address */, mBufferSize /* length */, PROT_READ | PROT_WRITE,
72                             MAP_SHARED, fd, 0 /* offset */);
73     if (mAudioBuffer.raw == MAP_FAILED) {
74         ALOGE("mmap failed for fd %d", fd);
75         mAudioBuffer.raw = nullptr;
76         return INVALID_OPERATION;
77     }
78 
79     mMemory = {std::move(tempFd), static_cast<int64_t>(mBufferSize)};
80     return OK;
81 }
82 
audioBuffer()83 audio_buffer_t* EffectBufferHalAidl::audioBuffer() {
84     return &mAudioBuffer;
85 }
86 
externalData() const87 void* EffectBufferHalAidl::externalData() const {
88     return mExternalData;
89 }
90 
setFrameCount(size_t frameCount)91 void EffectBufferHalAidl::setFrameCount(size_t frameCount) {
92     mAudioBuffer.frameCount = frameCount;
93     mFrameCountChanged = true;
94 }
95 
checkFrameCountChange()96 bool EffectBufferHalAidl::checkFrameCountChange() {
97     bool result = mFrameCountChanged;
98     mFrameCountChanged = false;
99     return result;
100 }
101 
setExternalData(void * external)102 void EffectBufferHalAidl::setExternalData(void* external) {
103     mExternalData = external;
104 }
105 
update()106 void EffectBufferHalAidl::update() {
107     update(mBufferSize);
108 }
109 
commit()110 void EffectBufferHalAidl::commit() {
111     commit(mBufferSize);
112 }
113 
copy(void * dst,const void * src,size_t n) const114 void EffectBufferHalAidl::copy(void* dst, const void* src, size_t n) const {
115     if (!dst || !src) {
116         return;
117     }
118     std::memcpy(dst, src, std::min(n, mBufferSize));
119 }
120 
update(size_t n)121 void EffectBufferHalAidl::update(size_t n) {
122     copy(mAudioBuffer.raw, mExternalData, n);
123 }
124 
commit(size_t n)125 void EffectBufferHalAidl::commit(size_t n) {
126     copy(mExternalData, mAudioBuffer.raw, n);
127 }
128 
129 } // namespace effect
130 } // namespace android
131