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 #define LOG_TAG "AAudioService"
18 //#define LOG_NDEBUG 0
19 #include <log/log.h>
20
21 #include <aaudio/AAudio.h>
22 #include "SharedMemoryProxy.h"
23
24 using namespace aaudio;
25
~SharedMemoryProxy()26 SharedMemoryProxy::~SharedMemoryProxy()
27 {
28 if (mOriginalSharedMemory != nullptr) {
29 munmap(mOriginalSharedMemory, mSharedMemorySizeInBytes);
30 mOriginalSharedMemory = nullptr;
31 }
32 if (mProxySharedMemory != nullptr) {
33 munmap(mProxySharedMemory, mSharedMemorySizeInBytes);
34 close(mProxyFileDescriptor);
35 mProxySharedMemory = nullptr;
36 }
37 }
38
open(int originalFD,int32_t capacityInBytes)39 aaudio_result_t SharedMemoryProxy::open(int originalFD, int32_t capacityInBytes) {
40 mOriginalFileDescriptor = originalFD;
41 mSharedMemorySizeInBytes = capacityInBytes;
42
43 mProxyFileDescriptor = ashmem_create_region("AAudioProxyDataBuffer", mSharedMemorySizeInBytes);
44 if (mProxyFileDescriptor < 0) {
45 ALOGE("SharedMemoryProxy::open() ashmem_create_region() failed %d", errno);
46 return AAUDIO_ERROR_INTERNAL;
47 }
48 int err = ashmem_set_prot_region(mProxyFileDescriptor, PROT_READ|PROT_WRITE);
49 if (err < 0) {
50 ALOGE("SharedMemoryProxy::open() ashmem_set_prot_region() failed %d", errno);
51 close(mProxyFileDescriptor);
52 mProxyFileDescriptor = -1;
53 return AAUDIO_ERROR_INTERNAL; // TODO convert errno to a better AAUDIO_ERROR;
54 }
55
56 // Get original memory address.
57 mOriginalSharedMemory = (uint8_t *) mmap(0, mSharedMemorySizeInBytes,
58 PROT_READ|PROT_WRITE,
59 MAP_SHARED,
60 mOriginalFileDescriptor, 0);
61 if (mOriginalSharedMemory == MAP_FAILED) {
62 ALOGE("SharedMemoryProxy::open() original mmap(%d) failed %d (%s)",
63 mOriginalFileDescriptor, errno, strerror(errno));
64 return AAUDIO_ERROR_INTERNAL; // TODO convert errno to a better AAUDIO_ERROR;
65 }
66
67 // Map the fd to the same memory addresses.
68 mProxySharedMemory = (uint8_t *) mmap(mOriginalSharedMemory, mSharedMemorySizeInBytes,
69 PROT_READ|PROT_WRITE,
70 MAP_SHARED,
71 mProxyFileDescriptor, 0);
72 if (mProxySharedMemory != mOriginalSharedMemory) {
73 ALOGE("SharedMemoryProxy::open() proxy mmap(%d) failed %d", mProxyFileDescriptor, errno);
74 munmap(mOriginalSharedMemory, mSharedMemorySizeInBytes);
75 mOriginalSharedMemory = nullptr;
76 close(mProxyFileDescriptor);
77 mProxyFileDescriptor = -1;
78 return AAUDIO_ERROR_INTERNAL; // TODO convert errno to a better AAUDIO_ERROR;
79 }
80
81 return AAUDIO_OK;
82 }
83