blob: 245444612faf838d3afacb8741a20b8e57d06b19 [file] [log] [blame]
Phil Burk2355edb2016-12-26 13:54:02 -08001/*
2 * Copyright (C) 2016 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 "SharedRingBuffer"
Phil Burk2355edb2016-12-26 13:54:02 -080018//#define LOG_NDEBUG 0
19#include <utils/Log.h>
20
Phil Burk478d5df2017-05-01 11:34:11 -070021#include <sys/mman.h>
22
Phil Burkc0c70e32017-02-09 13:18:38 -080023#include "binding/RingBufferParcelable.h"
24#include "binding/AudioEndpointParcelable.h"
Phil Burk2355edb2016-12-26 13:54:02 -080025
26#include "SharedRingBuffer.h"
27
28using namespace android;
Phil Burk5ed503c2017-02-01 09:38:15 -080029using namespace aaudio;
Phil Burk2355edb2016-12-26 13:54:02 -080030
31SharedRingBuffer::~SharedRingBuffer()
32{
33 if (mSharedMemory != nullptr) {
34 delete mFifoBuffer;
35 munmap(mSharedMemory, mSharedMemorySizeInBytes);
Phil Burk2355edb2016-12-26 13:54:02 -080036 mSharedMemory = nullptr;
37 }
38}
39
Phil Burk5ed503c2017-02-01 09:38:15 -080040aaudio_result_t SharedRingBuffer::allocate(fifo_frames_t bytesPerFrame,
Phil Burk2355edb2016-12-26 13:54:02 -080041 fifo_frames_t capacityInFrames) {
42 mCapacityInFrames = capacityInFrames;
43
44 // Create shared memory large enough to hold the data and the read and write counters.
45 mDataMemorySizeInBytes = bytesPerFrame * capacityInFrames;
46 mSharedMemorySizeInBytes = mDataMemorySizeInBytes + (2 * (sizeof(fifo_counter_t)));
Phil Burke72481c2017-08-08 13:20:45 -070047 mFileDescriptor.reset(ashmem_create_region("AAudioSharedRingBuffer", mSharedMemorySizeInBytes));
48 if (mFileDescriptor.get() == -1) {
Phil Burkfbf031e2017-10-12 15:58:31 -070049 ALOGE("allocate() ashmem_create_region() failed %d", errno);
Phil Burk5ed503c2017-02-01 09:38:15 -080050 return AAUDIO_ERROR_INTERNAL;
Phil Burk2355edb2016-12-26 13:54:02 -080051 }
Phil Burkfbf031e2017-10-12 15:58:31 -070052 ALOGV("allocate() mFileDescriptor = %d\n", mFileDescriptor.get());
Phil Burk942bdc02017-05-03 11:36:50 -070053
Phil Burke72481c2017-08-08 13:20:45 -070054 int err = ashmem_set_prot_region(mFileDescriptor.get(), PROT_READ|PROT_WRITE); // TODO error handling?
Phil Burk2355edb2016-12-26 13:54:02 -080055 if (err < 0) {
Phil Burkfbf031e2017-10-12 15:58:31 -070056 ALOGE("allocate() ashmem_set_prot_region() failed %d", errno);
Phil Burke72481c2017-08-08 13:20:45 -070057 mFileDescriptor.reset();
Phil Burk5ed503c2017-02-01 09:38:15 -080058 return AAUDIO_ERROR_INTERNAL; // TODO convert errno to a better AAUDIO_ERROR;
Phil Burk2355edb2016-12-26 13:54:02 -080059 }
60
61 // Map the fd to memory addresses.
62 mSharedMemory = (uint8_t *) mmap(0, mSharedMemorySizeInBytes,
63 PROT_READ|PROT_WRITE,
64 MAP_SHARED,
Phil Burke72481c2017-08-08 13:20:45 -070065 mFileDescriptor.get(), 0);
Phil Burk2355edb2016-12-26 13:54:02 -080066 if (mSharedMemory == MAP_FAILED) {
Phil Burkfbf031e2017-10-12 15:58:31 -070067 ALOGE("allocate() mmap() failed %d", errno);
Phil Burke72481c2017-08-08 13:20:45 -070068 mFileDescriptor.reset();
Phil Burk5ed503c2017-02-01 09:38:15 -080069 return AAUDIO_ERROR_INTERNAL; // TODO convert errno to a better AAUDIO_ERROR;
Phil Burk2355edb2016-12-26 13:54:02 -080070 }
71
72 // Get addresses for our counters and data from the shared memory.
73 fifo_counter_t *readCounterAddress =
74 (fifo_counter_t *) &mSharedMemory[SHARED_RINGBUFFER_READ_OFFSET];
75 fifo_counter_t *writeCounterAddress =
76 (fifo_counter_t *) &mSharedMemory[SHARED_RINGBUFFER_WRITE_OFFSET];
77 uint8_t *dataAddress = &mSharedMemory[SHARED_RINGBUFFER_DATA_OFFSET];
78
Phil Burk942bdc02017-05-03 11:36:50 -070079 mFifoBuffer = new FifoBuffer(bytesPerFrame, capacityInFrames,
Phil Burk2355edb2016-12-26 13:54:02 -080080 readCounterAddress, writeCounterAddress, dataAddress);
Phil Burk942bdc02017-05-03 11:36:50 -070081 return AAUDIO_OK;
Phil Burk2355edb2016-12-26 13:54:02 -080082}
83
84void SharedRingBuffer::fillParcelable(AudioEndpointParcelable &endpointParcelable,
85 RingBufferParcelable &ringBufferParcelable) {
86 int fdIndex = endpointParcelable.addFileDescriptor(mFileDescriptor, mSharedMemorySizeInBytes);
87 ringBufferParcelable.setupMemory(fdIndex,
88 SHARED_RINGBUFFER_DATA_OFFSET,
89 mDataMemorySizeInBytes,
90 SHARED_RINGBUFFER_READ_OFFSET,
91 SHARED_RINGBUFFER_WRITE_OFFSET,
92 sizeof(fifo_counter_t));
93 ringBufferParcelable.setBytesPerFrame(mFifoBuffer->getBytesPerFrame());
94 ringBufferParcelable.setFramesPerBurst(1);
95 ringBufferParcelable.setCapacityInFrames(mCapacityInFrames);
96}