blob: c1d4e16b4e9777760ccf19aacf382e98659b0e91 [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 Burk8f4fe502020-07-15 23:54:50 +000021#include <iomanip>
22#include <iostream>
Phil Burk478d5df2017-05-01 11:34:11 -070023#include <sys/mman.h>
24
Phil Burkc0c70e32017-02-09 13:18:38 -080025#include "binding/RingBufferParcelable.h"
26#include "binding/AudioEndpointParcelable.h"
Phil Burk2355edb2016-12-26 13:54:02 -080027
28#include "SharedRingBuffer.h"
29
30using namespace android;
Phil Burk5ed503c2017-02-01 09:38:15 -080031using namespace aaudio;
Phil Burk2355edb2016-12-26 13:54:02 -080032
33SharedRingBuffer::~SharedRingBuffer()
34{
Phil Burk8f4fe502020-07-15 23:54:50 +000035 mFifoBuffer.reset(); // uses mSharedMemory
Phil Burk2355edb2016-12-26 13:54:02 -080036 if (mSharedMemory != nullptr) {
Phil Burk2355edb2016-12-26 13:54:02 -080037 munmap(mSharedMemory, mSharedMemorySizeInBytes);
Phil Burk2355edb2016-12-26 13:54:02 -080038 mSharedMemory = nullptr;
39 }
40}
41
Phil Burk5ed503c2017-02-01 09:38:15 -080042aaudio_result_t SharedRingBuffer::allocate(fifo_frames_t bytesPerFrame,
Phil Burk2355edb2016-12-26 13:54:02 -080043 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 Burke72481c2017-08-08 13:20:45 -070049 mFileDescriptor.reset(ashmem_create_region("AAudioSharedRingBuffer", mSharedMemorySizeInBytes));
50 if (mFileDescriptor.get() == -1) {
Phil Burkfbf031e2017-10-12 15:58:31 -070051 ALOGE("allocate() ashmem_create_region() failed %d", errno);
Phil Burk5ed503c2017-02-01 09:38:15 -080052 return AAUDIO_ERROR_INTERNAL;
Phil Burk2355edb2016-12-26 13:54:02 -080053 }
Phil Burkfbf031e2017-10-12 15:58:31 -070054 ALOGV("allocate() mFileDescriptor = %d\n", mFileDescriptor.get());
Phil Burk942bdc02017-05-03 11:36:50 -070055
Phil Burke72481c2017-08-08 13:20:45 -070056 int err = ashmem_set_prot_region(mFileDescriptor.get(), PROT_READ|PROT_WRITE); // TODO error handling?
Phil Burk2355edb2016-12-26 13:54:02 -080057 if (err < 0) {
Phil Burkfbf031e2017-10-12 15:58:31 -070058 ALOGE("allocate() ashmem_set_prot_region() failed %d", errno);
Phil Burke72481c2017-08-08 13:20:45 -070059 mFileDescriptor.reset();
Phil Burk5ed503c2017-02-01 09:38:15 -080060 return AAUDIO_ERROR_INTERNAL; // TODO convert errno to a better AAUDIO_ERROR;
Phil Burk2355edb2016-12-26 13:54:02 -080061 }
62
jiabin664d6f02020-08-11 17:15:50 -070063 // 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 Burk2355edb2016-12-26 13:54:02 -080066 PROT_READ|PROT_WRITE,
67 MAP_SHARED,
Phil Burke72481c2017-08-08 13:20:45 -070068 mFileDescriptor.get(), 0);
jiabin664d6f02020-08-11 17:15:50 -070069 if (tmpPtr == MAP_FAILED) {
Phil Burkfbf031e2017-10-12 15:58:31 -070070 ALOGE("allocate() mmap() failed %d", errno);
Phil Burke72481c2017-08-08 13:20:45 -070071 mFileDescriptor.reset();
Phil Burk5ed503c2017-02-01 09:38:15 -080072 return AAUDIO_ERROR_INTERNAL; // TODO convert errno to a better AAUDIO_ERROR;
Phil Burk2355edb2016-12-26 13:54:02 -080073 }
jiabin664d6f02020-08-11 17:15:50 -070074 mSharedMemory = tmpPtr;
Phil Burk2355edb2016-12-26 13:54:02 -080075
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 Burk8f4fe502020-07-15 23:54:50 +000083 mFifoBuffer = std::make_shared<FifoBufferIndirect>(bytesPerFrame, capacityInFrames,
Phil Burk2355edb2016-12-26 13:54:02 -080084 readCounterAddress, writeCounterAddress, dataAddress);
Phil Burk942bdc02017-05-03 11:36:50 -070085 return AAUDIO_OK;
Phil Burk2355edb2016-12-26 13:54:02 -080086}
87
88void 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 Burk8f4fe502020-07-15 23:54:50 +0000101
102double SharedRingBuffer::getFractionalFullness() const {
103 int32_t framesAvailable = mFifoBuffer->getFullFramesAvailable();
104 int32_t capacity = mFifoBuffer->getBufferCapacityInFrames();
105 return framesAvailable / (double) capacity;
106}
107
108std::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}