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 "SharedRingBuffer"
18 //#define LOG_NDEBUG 0
19 #include <utils/Log.h>
20
21 #include <iomanip>
22 #include <iostream>
23 #include <sys/mman.h>
24
25 #include "binding/RingBufferParcelable.h"
26 #include "binding/AudioEndpointParcelable.h"
27
28 #include "SharedRingBuffer.h"
29
30 using namespace android;
31 using namespace aaudio;
32
~SharedRingBuffer()33 SharedRingBuffer::~SharedRingBuffer()
34 {
35 mFifoBuffer.reset(); // uses mSharedMemory
36 if (mSharedMemory != nullptr) {
37 munmap(mSharedMemory, mSharedMemorySizeInBytes);
38 mSharedMemory = nullptr;
39 }
40 }
41
allocate(fifo_frames_t bytesPerFrame,fifo_frames_t capacityInFrames)42 aaudio_result_t SharedRingBuffer::allocate(fifo_frames_t bytesPerFrame,
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)));
49 mFileDescriptor.reset(ashmem_create_region("AAudioSharedRingBuffer", mSharedMemorySizeInBytes));
50 if (mFileDescriptor.get() == -1) {
51 ALOGE("allocate() ashmem_create_region() failed %d", errno);
52 return AAUDIO_ERROR_INTERNAL;
53 }
54 ALOGV("allocate() mFileDescriptor = %d\n", mFileDescriptor.get());
55
56 int err = ashmem_set_prot_region(mFileDescriptor.get(), PROT_READ|PROT_WRITE); // TODO error handling?
57 if (err < 0) {
58 ALOGE("allocate() ashmem_set_prot_region() failed %d", errno);
59 mFileDescriptor.reset();
60 return AAUDIO_ERROR_INTERNAL; // TODO convert errno to a better AAUDIO_ERROR;
61 }
62
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 auto tmpPtr = (uint8_t *) mmap(nullptr, mSharedMemorySizeInBytes,
66 PROT_READ|PROT_WRITE,
67 MAP_SHARED,
68 mFileDescriptor.get(), 0);
69 if (tmpPtr == MAP_FAILED) {
70 ALOGE("allocate() mmap() failed %d", errno);
71 mFileDescriptor.reset();
72 return AAUDIO_ERROR_INTERNAL; // TODO convert errno to a better AAUDIO_ERROR;
73 }
74 mSharedMemory = tmpPtr;
75
76 // Get addresses for our counters and data from the shared memory.
77 auto readCounterAddress = (fifo_counter_t *) &mSharedMemory[SHARED_RINGBUFFER_READ_OFFSET];
78 auto writeCounterAddress = (fifo_counter_t *) &mSharedMemory[SHARED_RINGBUFFER_WRITE_OFFSET];
79 uint8_t *dataAddress = &mSharedMemory[SHARED_RINGBUFFER_DATA_OFFSET];
80
81 mFifoBuffer = std::make_shared<FifoBufferIndirect>(bytesPerFrame, capacityInFrames,
82 readCounterAddress, writeCounterAddress, dataAddress);
83 return AAUDIO_OK;
84 }
85
fillParcelable(AudioEndpointParcelable * endpointParcelable,RingBufferParcelable & ringBufferParcelable)86 void SharedRingBuffer::fillParcelable(AudioEndpointParcelable* endpointParcelable,
87 RingBufferParcelable &ringBufferParcelable) {
88 int fdIndex = endpointParcelable->addFileDescriptor(mFileDescriptor, mSharedMemorySizeInBytes);
89 ringBufferParcelable.setupMemory(fdIndex,
90 SHARED_RINGBUFFER_DATA_OFFSET,
91 mDataMemorySizeInBytes,
92 SHARED_RINGBUFFER_READ_OFFSET,
93 SHARED_RINGBUFFER_WRITE_OFFSET,
94 sizeof(fifo_counter_t));
95 ringBufferParcelable.setBytesPerFrame(mFifoBuffer->getBytesPerFrame());
96 ringBufferParcelable.setFramesPerBurst(1);
97 ringBufferParcelable.setCapacityInFrames(mCapacityInFrames);
98 }
99
getFractionalFullness() const100 double SharedRingBuffer::getFractionalFullness() const {
101 int32_t framesAvailable = mFifoBuffer->getFullFramesAvailable();
102 int32_t capacity = mFifoBuffer->getBufferCapacityInFrames();
103 return framesAvailable / (double) capacity;
104 }
105
dump() const106 std::string SharedRingBuffer::dump() const {
107 std::stringstream result;
108 int32_t readCounter = mFifoBuffer->getReadCounter();
109 int32_t writeCounter = mFifoBuffer->getWriteCounter();
110 result << std::setw(10) << writeCounter;
111 result << std::setw(10) << readCounter;
112 result << std::setw(8) << (writeCounter - readCounter);
113 return result.str();
114 }
115