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