SharedRingBuffer.cpp revision c0c70e3c7dd10bc2c0caffcab1f3f5fb406b35fb
1/* 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 17#define LOG_TAG "AAudioService" 18//#define LOG_NDEBUG 0 19#include <utils/Log.h> 20 21#include "binding/RingBufferParcelable.h" 22#include "binding/AudioEndpointParcelable.h" 23 24#include "SharedRingBuffer.h" 25 26using namespace android; 27using namespace aaudio; 28 29SharedRingBuffer::~SharedRingBuffer() 30{ 31 if (mSharedMemory != nullptr) { 32 delete mFifoBuffer; 33 munmap(mSharedMemory, mSharedMemorySizeInBytes); 34 close(mFileDescriptor); 35 mSharedMemory = nullptr; 36 } 37} 38 39aaudio_result_t SharedRingBuffer::allocate(fifo_frames_t bytesPerFrame, 40 fifo_frames_t capacityInFrames) { 41 mCapacityInFrames = capacityInFrames; 42 43 // Create shared memory large enough to hold the data and the read and write counters. 44 mDataMemorySizeInBytes = bytesPerFrame * capacityInFrames; 45 mSharedMemorySizeInBytes = mDataMemorySizeInBytes + (2 * (sizeof(fifo_counter_t))); 46 mFileDescriptor = ashmem_create_region("AAudioSharedRingBuffer", mSharedMemorySizeInBytes); 47 if (mFileDescriptor < 0) { 48 ALOGE("SharedRingBuffer::allocate() ashmem_create_region() failed %d", errno); 49 return AAUDIO_ERROR_INTERNAL; 50 } 51 int err = ashmem_set_prot_region(mFileDescriptor, PROT_READ|PROT_WRITE); // TODO error handling? 52 if (err < 0) { 53 ALOGE("SharedRingBuffer::allocate() ashmem_set_prot_region() failed %d", errno); 54 close(mFileDescriptor); 55 return AAUDIO_ERROR_INTERNAL; // TODO convert errno to a better AAUDIO_ERROR; 56 } 57 58 // Map the fd to memory addresses. 59 mSharedMemory = (uint8_t *) mmap(0, mSharedMemorySizeInBytes, 60 PROT_READ|PROT_WRITE, 61 MAP_SHARED, 62 mFileDescriptor, 0); 63 if (mSharedMemory == MAP_FAILED) { 64 ALOGE("SharedRingBuffer::allocate() mmap() failed %d", errno); 65 close(mFileDescriptor); 66 return AAUDIO_ERROR_INTERNAL; // TODO convert errno to a better AAUDIO_ERROR; 67 } 68 69 // Get addresses for our counters and data from the shared memory. 70 fifo_counter_t *readCounterAddress = 71 (fifo_counter_t *) &mSharedMemory[SHARED_RINGBUFFER_READ_OFFSET]; 72 fifo_counter_t *writeCounterAddress = 73 (fifo_counter_t *) &mSharedMemory[SHARED_RINGBUFFER_WRITE_OFFSET]; 74 uint8_t *dataAddress = &mSharedMemory[SHARED_RINGBUFFER_DATA_OFFSET]; 75 76 mFifoBuffer = new(std::nothrow) FifoBuffer(bytesPerFrame, capacityInFrames, 77 readCounterAddress, writeCounterAddress, dataAddress); 78 return (mFifoBuffer == nullptr) ? AAUDIO_ERROR_NO_MEMORY : AAUDIO_OK; 79} 80 81void SharedRingBuffer::fillParcelable(AudioEndpointParcelable &endpointParcelable, 82 RingBufferParcelable &ringBufferParcelable) { 83 int fdIndex = endpointParcelable.addFileDescriptor(mFileDescriptor, mSharedMemorySizeInBytes); 84 ringBufferParcelable.setupMemory(fdIndex, 85 SHARED_RINGBUFFER_DATA_OFFSET, 86 mDataMemorySizeInBytes, 87 SHARED_RINGBUFFER_READ_OFFSET, 88 SHARED_RINGBUFFER_WRITE_OFFSET, 89 sizeof(fifo_counter_t)); 90 ringBufferParcelable.setBytesPerFrame(mFifoBuffer->getBytesPerFrame()); 91 ringBufferParcelable.setFramesPerBurst(1); 92 ringBufferParcelable.setCapacityInFrames(mCapacityInFrames); 93} 94