blob: 03c160dfe529c11c57f1f8fa6d63090d5a725cb8 [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);
36 close(mFileDescriptor);
37 mSharedMemory = nullptr;
38 }
39}
40
Phil Burk5ed503c2017-02-01 09:38:15 -080041aaudio_result_t SharedRingBuffer::allocate(fifo_frames_t bytesPerFrame,
Phil Burk2355edb2016-12-26 13:54:02 -080042 fifo_frames_t capacityInFrames) {
43 mCapacityInFrames = capacityInFrames;
44
45 // Create shared memory large enough to hold the data and the read and write counters.
46 mDataMemorySizeInBytes = bytesPerFrame * capacityInFrames;
47 mSharedMemorySizeInBytes = mDataMemorySizeInBytes + (2 * (sizeof(fifo_counter_t)));
Phil Burk5ed503c2017-02-01 09:38:15 -080048 mFileDescriptor = ashmem_create_region("AAudioSharedRingBuffer", mSharedMemorySizeInBytes);
Phil Burk2355edb2016-12-26 13:54:02 -080049 if (mFileDescriptor < 0) {
50 ALOGE("SharedRingBuffer::allocate() ashmem_create_region() failed %d", errno);
Phil Burk5ed503c2017-02-01 09:38:15 -080051 return AAUDIO_ERROR_INTERNAL;
Phil Burk2355edb2016-12-26 13:54:02 -080052 }
53 int err = ashmem_set_prot_region(mFileDescriptor, PROT_READ|PROT_WRITE); // TODO error handling?
54 if (err < 0) {
55 ALOGE("SharedRingBuffer::allocate() ashmem_set_prot_region() failed %d", errno);
56 close(mFileDescriptor);
Phil Burk5ed503c2017-02-01 09:38:15 -080057 return AAUDIO_ERROR_INTERNAL; // TODO convert errno to a better AAUDIO_ERROR;
Phil Burk2355edb2016-12-26 13:54:02 -080058 }
59
60 // Map the fd to memory addresses.
61 mSharedMemory = (uint8_t *) mmap(0, mSharedMemorySizeInBytes,
62 PROT_READ|PROT_WRITE,
63 MAP_SHARED,
64 mFileDescriptor, 0);
65 if (mSharedMemory == MAP_FAILED) {
66 ALOGE("SharedRingBuffer::allocate() mmap() failed %d", errno);
67 close(mFileDescriptor);
Phil Burk5ed503c2017-02-01 09:38:15 -080068 return AAUDIO_ERROR_INTERNAL; // TODO convert errno to a better AAUDIO_ERROR;
Phil Burk2355edb2016-12-26 13:54:02 -080069 }
70
71 // Get addresses for our counters and data from the shared memory.
72 fifo_counter_t *readCounterAddress =
73 (fifo_counter_t *) &mSharedMemory[SHARED_RINGBUFFER_READ_OFFSET];
74 fifo_counter_t *writeCounterAddress =
75 (fifo_counter_t *) &mSharedMemory[SHARED_RINGBUFFER_WRITE_OFFSET];
76 uint8_t *dataAddress = &mSharedMemory[SHARED_RINGBUFFER_DATA_OFFSET];
77
78 mFifoBuffer = new(std::nothrow) FifoBuffer(bytesPerFrame, capacityInFrames,
79 readCounterAddress, writeCounterAddress, dataAddress);
Phil Burk5ed503c2017-02-01 09:38:15 -080080 return (mFifoBuffer == nullptr) ? AAUDIO_ERROR_NO_MEMORY : AAUDIO_OK;
Phil Burk2355edb2016-12-26 13:54:02 -080081}
82
83void SharedRingBuffer::fillParcelable(AudioEndpointParcelable &endpointParcelable,
84 RingBufferParcelable &ringBufferParcelable) {
85 int fdIndex = endpointParcelable.addFileDescriptor(mFileDescriptor, mSharedMemorySizeInBytes);
86 ringBufferParcelable.setupMemory(fdIndex,
87 SHARED_RINGBUFFER_DATA_OFFSET,
88 mDataMemorySizeInBytes,
89 SHARED_RINGBUFFER_READ_OFFSET,
90 SHARED_RINGBUFFER_WRITE_OFFSET,
91 sizeof(fifo_counter_t));
92 ringBufferParcelable.setBytesPerFrame(mFifoBuffer->getBytesPerFrame());
93 ringBufferParcelable.setFramesPerBurst(1);
94 ringBufferParcelable.setCapacityInFrames(mCapacityInFrames);
95}