Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 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 | |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 17 | #define LOG_TAG "SharedRingBuffer" |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 18 | //#define LOG_NDEBUG 0 |
| 19 | #include <utils/Log.h> |
| 20 | |
Phil Burk | 8f4fe50 | 2020-07-15 23:54:50 +0000 | [diff] [blame] | 21 | #include <iomanip> |
| 22 | #include <iostream> |
Phil Burk | 478d5df | 2017-05-01 11:34:11 -0700 | [diff] [blame] | 23 | #include <sys/mman.h> |
| 24 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 25 | #include "binding/RingBufferParcelable.h" |
| 26 | #include "binding/AudioEndpointParcelable.h" |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 27 | |
| 28 | #include "SharedRingBuffer.h" |
| 29 | |
| 30 | using namespace android; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 31 | using namespace aaudio; |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 32 | |
| 33 | SharedRingBuffer::~SharedRingBuffer() |
| 34 | { |
Phil Burk | 8f4fe50 | 2020-07-15 23:54:50 +0000 | [diff] [blame] | 35 | mFifoBuffer.reset(); // uses mSharedMemory |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 36 | if (mSharedMemory != nullptr) { |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 37 | munmap(mSharedMemory, mSharedMemorySizeInBytes); |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 38 | mSharedMemory = nullptr; |
| 39 | } |
| 40 | } |
| 41 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 42 | aaudio_result_t SharedRingBuffer::allocate(fifo_frames_t bytesPerFrame, |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 43 | fifo_frames_t capacityInFrames) { |
| 44 | mCapacityInFrames = capacityInFrames; |
| 45 | |
| 46 | // Create shared memory large enough to hold the data and the read and write counters. |
| 47 | mDataMemorySizeInBytes = bytesPerFrame * capacityInFrames; |
| 48 | mSharedMemorySizeInBytes = mDataMemorySizeInBytes + (2 * (sizeof(fifo_counter_t))); |
Phil Burk | e72481c | 2017-08-08 13:20:45 -0700 | [diff] [blame] | 49 | mFileDescriptor.reset(ashmem_create_region("AAudioSharedRingBuffer", mSharedMemorySizeInBytes)); |
| 50 | if (mFileDescriptor.get() == -1) { |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 51 | ALOGE("allocate() ashmem_create_region() failed %d", errno); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 52 | return AAUDIO_ERROR_INTERNAL; |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 53 | } |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 54 | ALOGV("allocate() mFileDescriptor = %d\n", mFileDescriptor.get()); |
Phil Burk | 942bdc0 | 2017-05-03 11:36:50 -0700 | [diff] [blame] | 55 | |
Phil Burk | e72481c | 2017-08-08 13:20:45 -0700 | [diff] [blame] | 56 | int err = ashmem_set_prot_region(mFileDescriptor.get(), PROT_READ|PROT_WRITE); // TODO error handling? |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 57 | if (err < 0) { |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 58 | ALOGE("allocate() ashmem_set_prot_region() failed %d", errno); |
Phil Burk | e72481c | 2017-08-08 13:20:45 -0700 | [diff] [blame] | 59 | mFileDescriptor.reset(); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 60 | return AAUDIO_ERROR_INTERNAL; // TODO convert errno to a better AAUDIO_ERROR; |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 61 | } |
| 62 | |
jiabin | 664d6f0 | 2020-08-11 17:15:50 -0700 | [diff] [blame] | 63 | // Map the fd to memory addresses. Use a temporary pointer to keep the mmap result and update |
| 64 | // it to `mSharedMemory` only when mmap operate successfully. |
| 65 | uint8_t* tmpPtr = (uint8_t *) mmap(0, mSharedMemorySizeInBytes, |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 66 | PROT_READ|PROT_WRITE, |
| 67 | MAP_SHARED, |
Phil Burk | e72481c | 2017-08-08 13:20:45 -0700 | [diff] [blame] | 68 | mFileDescriptor.get(), 0); |
jiabin | 664d6f0 | 2020-08-11 17:15:50 -0700 | [diff] [blame] | 69 | if (tmpPtr == MAP_FAILED) { |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 70 | ALOGE("allocate() mmap() failed %d", errno); |
Phil Burk | e72481c | 2017-08-08 13:20:45 -0700 | [diff] [blame] | 71 | mFileDescriptor.reset(); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 72 | return AAUDIO_ERROR_INTERNAL; // TODO convert errno to a better AAUDIO_ERROR; |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 73 | } |
jiabin | 664d6f0 | 2020-08-11 17:15:50 -0700 | [diff] [blame] | 74 | mSharedMemory = tmpPtr; |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 75 | |
| 76 | // Get addresses for our counters and data from the shared memory. |
| 77 | fifo_counter_t *readCounterAddress = |
| 78 | (fifo_counter_t *) &mSharedMemory[SHARED_RINGBUFFER_READ_OFFSET]; |
| 79 | fifo_counter_t *writeCounterAddress = |
| 80 | (fifo_counter_t *) &mSharedMemory[SHARED_RINGBUFFER_WRITE_OFFSET]; |
| 81 | uint8_t *dataAddress = &mSharedMemory[SHARED_RINGBUFFER_DATA_OFFSET]; |
| 82 | |
Phil Burk | 8f4fe50 | 2020-07-15 23:54:50 +0000 | [diff] [blame] | 83 | mFifoBuffer = std::make_shared<FifoBufferIndirect>(bytesPerFrame, capacityInFrames, |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 84 | readCounterAddress, writeCounterAddress, dataAddress); |
Phil Burk | 942bdc0 | 2017-05-03 11:36:50 -0700 | [diff] [blame] | 85 | return AAUDIO_OK; |
Phil Burk | 2355edb | 2016-12-26 13:54:02 -0800 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | void SharedRingBuffer::fillParcelable(AudioEndpointParcelable &endpointParcelable, |
| 89 | RingBufferParcelable &ringBufferParcelable) { |
| 90 | int fdIndex = endpointParcelable.addFileDescriptor(mFileDescriptor, mSharedMemorySizeInBytes); |
| 91 | ringBufferParcelable.setupMemory(fdIndex, |
| 92 | SHARED_RINGBUFFER_DATA_OFFSET, |
| 93 | mDataMemorySizeInBytes, |
| 94 | SHARED_RINGBUFFER_READ_OFFSET, |
| 95 | SHARED_RINGBUFFER_WRITE_OFFSET, |
| 96 | sizeof(fifo_counter_t)); |
| 97 | ringBufferParcelable.setBytesPerFrame(mFifoBuffer->getBytesPerFrame()); |
| 98 | ringBufferParcelable.setFramesPerBurst(1); |
| 99 | ringBufferParcelable.setCapacityInFrames(mCapacityInFrames); |
| 100 | } |
Phil Burk | 8f4fe50 | 2020-07-15 23:54:50 +0000 | [diff] [blame] | 101 | |
| 102 | double SharedRingBuffer::getFractionalFullness() const { |
| 103 | int32_t framesAvailable = mFifoBuffer->getFullFramesAvailable(); |
| 104 | int32_t capacity = mFifoBuffer->getBufferCapacityInFrames(); |
| 105 | return framesAvailable / (double) capacity; |
| 106 | } |
| 107 | |
| 108 | std::string SharedRingBuffer::dump() const { |
| 109 | std::stringstream result; |
| 110 | int32_t readCounter = mFifoBuffer->getReadCounter(); |
| 111 | int32_t writeCounter = mFifoBuffer->getWriteCounter(); |
| 112 | result << std::setw(10) << writeCounter; |
| 113 | result << std::setw(10) << readCounter; |
| 114 | result << std::setw(8) << (writeCounter - readCounter); |
| 115 | return result.str(); |
| 116 | } |