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