blob: c43ed2255403c22bccd08aa0c7ccd62d1292ef3e [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>
23
Phil Burka4eb0d82017-04-12 15:44:06 -070024#include <aaudio/AAudio.h>
Phil Burkc0c70e32017-02-09 13:18:38 -080025#include "SharedMemoryProxy.h"
26
Phil Burkc0c70e32017-02-09 13:18:38 -080027using namespace aaudio;
28
29SharedMemoryProxy::~SharedMemoryProxy()
30{
31 if (mOriginalSharedMemory != nullptr) {
32 munmap(mOriginalSharedMemory, mSharedMemorySizeInBytes);
33 mOriginalSharedMemory = nullptr;
34 }
35 if (mProxySharedMemory != nullptr) {
36 munmap(mProxySharedMemory, mSharedMemorySizeInBytes);
37 close(mProxyFileDescriptor);
38 mProxySharedMemory = nullptr;
39 }
40}
41
42aaudio_result_t SharedMemoryProxy::open(int originalFD, int32_t capacityInBytes) {
43 mOriginalFileDescriptor = originalFD;
44 mSharedMemorySizeInBytes = capacityInBytes;
45
46 mProxyFileDescriptor = ashmem_create_region("AAudioProxyDataBuffer", mSharedMemorySizeInBytes);
47 if (mProxyFileDescriptor < 0) {
Phil Burkfbf031e2017-10-12 15:58:31 -070048 ALOGE("open() ashmem_create_region() failed %d", errno);
Phil Burkc0c70e32017-02-09 13:18:38 -080049 return AAUDIO_ERROR_INTERNAL;
50 }
51 int err = ashmem_set_prot_region(mProxyFileDescriptor, PROT_READ|PROT_WRITE);
52 if (err < 0) {
Phil Burkfbf031e2017-10-12 15:58:31 -070053 ALOGE("open() ashmem_set_prot_region() failed %d", errno);
Phil Burkc0c70e32017-02-09 13:18:38 -080054 close(mProxyFileDescriptor);
55 mProxyFileDescriptor = -1;
56 return AAUDIO_ERROR_INTERNAL; // TODO convert errno to a better AAUDIO_ERROR;
57 }
58
59 // Get original memory address.
60 mOriginalSharedMemory = (uint8_t *) mmap(0, mSharedMemorySizeInBytes,
61 PROT_READ|PROT_WRITE,
62 MAP_SHARED,
63 mOriginalFileDescriptor, 0);
64 if (mOriginalSharedMemory == MAP_FAILED) {
Phil Burkfbf031e2017-10-12 15:58:31 -070065 ALOGE("open() original mmap(%d) failed %d (%s)",
Phil Burkc0c70e32017-02-09 13:18:38 -080066 mOriginalFileDescriptor, errno, strerror(errno));
67 return AAUDIO_ERROR_INTERNAL; // TODO convert errno to a better AAUDIO_ERROR;
68 }
69
70 // Map the fd to the same memory addresses.
71 mProxySharedMemory = (uint8_t *) mmap(mOriginalSharedMemory, mSharedMemorySizeInBytes,
72 PROT_READ|PROT_WRITE,
73 MAP_SHARED,
74 mProxyFileDescriptor, 0);
75 if (mProxySharedMemory != mOriginalSharedMemory) {
Phil Burkfbf031e2017-10-12 15:58:31 -070076 ALOGE("open() proxy mmap(%d) failed %d", mProxyFileDescriptor, errno);
Phil Burkc0c70e32017-02-09 13:18:38 -080077 munmap(mOriginalSharedMemory, mSharedMemorySizeInBytes);
78 mOriginalSharedMemory = nullptr;
79 close(mProxyFileDescriptor);
80 mProxyFileDescriptor = -1;
81 return AAUDIO_ERROR_INTERNAL; // TODO convert errno to a better AAUDIO_ERROR;
82 }
83
84 return AAUDIO_OK;
85}