blob: 78d48848e9036ea9e02ab8658fc2c0bac4b99baa [file] [log] [blame]
Phil Burkc0c70e32017-02-09 13:18:38 -08001/*
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
Phil Burkfbf031e2017-10-12 15:58:31 -070017#define LOG_TAG "SharedMemoryProxy"
Phil Burkc0c70e32017-02-09 13:18:38 -080018//#define LOG_NDEBUG 0
Tom Cherry357552e2017-07-12 13:48:26 -070019#include <log/log.h>
Phil Burkc0c70e32017-02-09 13:18:38 -080020
Dan Albert36967fb2017-10-11 12:43:41 -070021#include <errno.h>
22#include <string.h>
Tom Cherry7ae78252020-04-13 15:20:50 -070023#include <unistd.h>
Dan Albert36967fb2017-10-11 12:43:41 -070024
Phil Burka4eb0d82017-04-12 15:44:06 -070025#include <aaudio/AAudio.h>
Phil Burkc0c70e32017-02-09 13:18:38 -080026#include "SharedMemoryProxy.h"
27
Phil Burkc0c70e32017-02-09 13:18:38 -080028using namespace aaudio;
29
30SharedMemoryProxy::~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
43aaudio_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) {
Phil Burkfbf031e2017-10-12 15:58:31 -070049 ALOGE("open() ashmem_create_region() failed %d", errno);
Phil Burkc0c70e32017-02-09 13:18:38 -080050 return AAUDIO_ERROR_INTERNAL;
51 }
52 int err = ashmem_set_prot_region(mProxyFileDescriptor, PROT_READ|PROT_WRITE);
53 if (err < 0) {
Phil Burkfbf031e2017-10-12 15:58:31 -070054 ALOGE("open() ashmem_set_prot_region() failed %d", errno);
Phil Burkc0c70e32017-02-09 13:18:38 -080055 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) {
Phil Burkfbf031e2017-10-12 15:58:31 -070066 ALOGE("open() original mmap(%d) failed %d (%s)",
Phil Burkc0c70e32017-02-09 13:18:38 -080067 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) {
Phil Burkfbf031e2017-10-12 15:58:31 -070077 ALOGE("open() proxy mmap(%d) failed %d", mProxyFileDescriptor, errno);
Phil Burkc0c70e32017-02-09 13:18:38 -080078 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}