blob: 6b3fb4c850ece77dd8de2814ae958324429ea6f0 [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 Burk5ed503c2017-02-01 09:38:15 -080017#define LOG_TAG "AAudioService"
Phil Burk2355edb2016-12-26 13:54:02 -080018//#define LOG_NDEBUG 0
19#include <utils/Log.h>
20
Phil Burk478d5df2017-05-01 11:34:11 -070021#include <sys/mman.h>
22
Phil Burkc0c70e32017-02-09 13:18:38 -080023#include "binding/RingBufferParcelable.h"
24#include "binding/AudioEndpointParcelable.h"
Phil Burk2355edb2016-12-26 13:54:02 -080025
26#include "SharedRingBuffer.h"
27
28using namespace android;
Phil Burk5ed503c2017-02-01 09:38:15 -080029using namespace aaudio;
Phil Burk2355edb2016-12-26 13:54:02 -080030
31SharedRingBuffer::~SharedRingBuffer()
32{
33 if (mSharedMemory != nullptr) {
34 delete mFifoBuffer;
35 munmap(mSharedMemory, mSharedMemorySizeInBytes);
Phil Burk2355edb2016-12-26 13:54:02 -080036 mSharedMemory = nullptr;
37 }
Phil Burk942bdc02017-05-03 11:36:50 -070038 if (mFileDescriptor != -1) {
39 ALOGV("SharedRingBuffer: LEAK? close(mFileDescriptor = %d)\n", mFileDescriptor);
40 close(mFileDescriptor);
41 mFileDescriptor = -1;
42 }
Phil Burk2355edb2016-12-26 13:54:02 -080043}
44
Phil Burk5ed503c2017-02-01 09:38:15 -080045aaudio_result_t SharedRingBuffer::allocate(fifo_frames_t bytesPerFrame,
Phil Burk2355edb2016-12-26 13:54:02 -080046 fifo_frames_t capacityInFrames) {
47 mCapacityInFrames = capacityInFrames;
48
49 // Create shared memory large enough to hold the data and the read and write counters.
50 mDataMemorySizeInBytes = bytesPerFrame * capacityInFrames;
51 mSharedMemorySizeInBytes = mDataMemorySizeInBytes + (2 * (sizeof(fifo_counter_t)));
Phil Burk5ed503c2017-02-01 09:38:15 -080052 mFileDescriptor = ashmem_create_region("AAudioSharedRingBuffer", mSharedMemorySizeInBytes);
Phil Burk942bdc02017-05-03 11:36:50 -070053 ALOGV("SharedRingBuffer::allocate() LEAK? mFileDescriptor = %d\n", mFileDescriptor);
Phil Burk2355edb2016-12-26 13:54:02 -080054 if (mFileDescriptor < 0) {
55 ALOGE("SharedRingBuffer::allocate() ashmem_create_region() failed %d", errno);
Phil Burk5ed503c2017-02-01 09:38:15 -080056 return AAUDIO_ERROR_INTERNAL;
Phil Burk2355edb2016-12-26 13:54:02 -080057 }
Phil Burk942bdc02017-05-03 11:36:50 -070058
Phil Burk2355edb2016-12-26 13:54:02 -080059 int err = ashmem_set_prot_region(mFileDescriptor, PROT_READ|PROT_WRITE); // TODO error handling?
60 if (err < 0) {
61 ALOGE("SharedRingBuffer::allocate() ashmem_set_prot_region() failed %d", errno);
62 close(mFileDescriptor);
Phil Burk5ed503c2017-02-01 09:38:15 -080063 return AAUDIO_ERROR_INTERNAL; // TODO convert errno to a better AAUDIO_ERROR;
Phil Burk2355edb2016-12-26 13:54:02 -080064 }
65
66 // Map the fd to memory addresses.
67 mSharedMemory = (uint8_t *) mmap(0, mSharedMemorySizeInBytes,
68 PROT_READ|PROT_WRITE,
69 MAP_SHARED,
70 mFileDescriptor, 0);
71 if (mSharedMemory == MAP_FAILED) {
72 ALOGE("SharedRingBuffer::allocate() mmap() failed %d", errno);
73 close(mFileDescriptor);
Phil Burk5ed503c2017-02-01 09:38:15 -080074 return AAUDIO_ERROR_INTERNAL; // TODO convert errno to a better AAUDIO_ERROR;
Phil Burk2355edb2016-12-26 13:54:02 -080075 }
76
77 // Get addresses for our counters and data from the shared memory.
78 fifo_counter_t *readCounterAddress =
79 (fifo_counter_t *) &mSharedMemory[SHARED_RINGBUFFER_READ_OFFSET];
80 fifo_counter_t *writeCounterAddress =
81 (fifo_counter_t *) &mSharedMemory[SHARED_RINGBUFFER_WRITE_OFFSET];
82 uint8_t *dataAddress = &mSharedMemory[SHARED_RINGBUFFER_DATA_OFFSET];
83
Phil Burk942bdc02017-05-03 11:36:50 -070084 mFifoBuffer = new FifoBuffer(bytesPerFrame, capacityInFrames,
Phil Burk2355edb2016-12-26 13:54:02 -080085 readCounterAddress, writeCounterAddress, dataAddress);
Phil Burk942bdc02017-05-03 11:36:50 -070086 return AAUDIO_OK;
Phil Burk2355edb2016-12-26 13:54:02 -080087}
88
89void SharedRingBuffer::fillParcelable(AudioEndpointParcelable &endpointParcelable,
90 RingBufferParcelable &ringBufferParcelable) {
91 int fdIndex = endpointParcelable.addFileDescriptor(mFileDescriptor, mSharedMemorySizeInBytes);
92 ringBufferParcelable.setupMemory(fdIndex,
93 SHARED_RINGBUFFER_DATA_OFFSET,
94 mDataMemorySizeInBytes,
95 SHARED_RINGBUFFER_READ_OFFSET,
96 SHARED_RINGBUFFER_WRITE_OFFSET,
97 sizeof(fifo_counter_t));
98 ringBufferParcelable.setBytesPerFrame(mFifoBuffer->getBytesPerFrame());
99 ringBufferParcelable.setFramesPerBurst(1);
100 ringBufferParcelable.setCapacityInFrames(mCapacityInFrames);
101}