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 uint8_t* tmpPtr = (uint8_t *) mmap(0, 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 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
83 mFifoBuffer = std::make_shared<FifoBufferIndirect>(bytesPerFrame, capacityInFrames,
84 readCounterAddress, writeCounterAddress, dataAddress);
85 return AAUDIO_OK;
86 }
87
fillParcelable(AudioEndpointParcelable & endpointParcelable,RingBufferParcelable & ringBufferParcelable)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 }
101
getFractionalFullness() const102 double SharedRingBuffer::getFractionalFullness() const {
103 int32_t framesAvailable = mFifoBuffer->getFullFramesAvailable();
104 int32_t capacity = mFifoBuffer->getBufferCapacityInFrames();
105 return framesAvailable / (double) capacity;
106 }
107
dump() const108 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 }
117