blob: efcc9d633b4a56a040009d40160fd112201322e9 [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 Burk7f6b40d2017-02-09 13:18:38 -080021#include "binding/RingBufferParcelable.h"
22#include "binding/AudioEndpointParcelable.h"
Phil Burk2355edb2016-12-26 13:54:02 -080023
24#include "SharedRingBuffer.h"
25
26using namespace android;
Phil Burk5ed503c2017-02-01 09:38:15 -080027using namespace aaudio;
Phil Burk2355edb2016-12-26 13:54:02 -080028
29SharedRingBuffer::~SharedRingBuffer()
30{
31 if (mSharedMemory != nullptr) {
32 delete mFifoBuffer;
33 munmap(mSharedMemory, mSharedMemorySizeInBytes);
34 close(mFileDescriptor);
35 mSharedMemory = nullptr;
36 }
37}
38
Phil Burk5ed503c2017-02-01 09:38:15 -080039aaudio_result_t SharedRingBuffer::allocate(fifo_frames_t bytesPerFrame,
Phil Burk2355edb2016-12-26 13:54:02 -080040 fifo_frames_t capacityInFrames) {
41 mCapacityInFrames = capacityInFrames;
42
43 // Create shared memory large enough to hold the data and the read and write counters.
44 mDataMemorySizeInBytes = bytesPerFrame * capacityInFrames;
45 mSharedMemorySizeInBytes = mDataMemorySizeInBytes + (2 * (sizeof(fifo_counter_t)));
Phil Burk5ed503c2017-02-01 09:38:15 -080046 mFileDescriptor = ashmem_create_region("AAudioSharedRingBuffer", mSharedMemorySizeInBytes);
Phil Burk2355edb2016-12-26 13:54:02 -080047 if (mFileDescriptor < 0) {
48 ALOGE("SharedRingBuffer::allocate() ashmem_create_region() failed %d", errno);
Phil Burk5ed503c2017-02-01 09:38:15 -080049 return AAUDIO_ERROR_INTERNAL;
Phil Burk2355edb2016-12-26 13:54:02 -080050 }
51 int err = ashmem_set_prot_region(mFileDescriptor, PROT_READ|PROT_WRITE); // TODO error handling?
52 if (err < 0) {
53 ALOGE("SharedRingBuffer::allocate() ashmem_set_prot_region() failed %d", errno);
54 close(mFileDescriptor);
Phil Burk5ed503c2017-02-01 09:38:15 -080055 return AAUDIO_ERROR_INTERNAL; // TODO convert errno to a better AAUDIO_ERROR;
Phil Burk2355edb2016-12-26 13:54:02 -080056 }
57
58 // Map the fd to memory addresses.
59 mSharedMemory = (uint8_t *) mmap(0, mSharedMemorySizeInBytes,
60 PROT_READ|PROT_WRITE,
61 MAP_SHARED,
62 mFileDescriptor, 0);
63 if (mSharedMemory == MAP_FAILED) {
64 ALOGE("SharedRingBuffer::allocate() mmap() failed %d", errno);
65 close(mFileDescriptor);
Phil Burk5ed503c2017-02-01 09:38:15 -080066 return AAUDIO_ERROR_INTERNAL; // TODO convert errno to a better AAUDIO_ERROR;
Phil Burk2355edb2016-12-26 13:54:02 -080067 }
68
69 // Get addresses for our counters and data from the shared memory.
70 fifo_counter_t *readCounterAddress =
71 (fifo_counter_t *) &mSharedMemory[SHARED_RINGBUFFER_READ_OFFSET];
72 fifo_counter_t *writeCounterAddress =
73 (fifo_counter_t *) &mSharedMemory[SHARED_RINGBUFFER_WRITE_OFFSET];
74 uint8_t *dataAddress = &mSharedMemory[SHARED_RINGBUFFER_DATA_OFFSET];
75
76 mFifoBuffer = new(std::nothrow) FifoBuffer(bytesPerFrame, capacityInFrames,
77 readCounterAddress, writeCounterAddress, dataAddress);
Phil Burk5ed503c2017-02-01 09:38:15 -080078 return (mFifoBuffer == nullptr) ? AAUDIO_ERROR_NO_MEMORY : AAUDIO_OK;
Phil Burk2355edb2016-12-26 13:54:02 -080079}
80
81void SharedRingBuffer::fillParcelable(AudioEndpointParcelable &endpointParcelable,
82 RingBufferParcelable &ringBufferParcelable) {
83 int fdIndex = endpointParcelable.addFileDescriptor(mFileDescriptor, mSharedMemorySizeInBytes);
84 ringBufferParcelable.setupMemory(fdIndex,
85 SHARED_RINGBUFFER_DATA_OFFSET,
86 mDataMemorySizeInBytes,
87 SHARED_RINGBUFFER_READ_OFFSET,
88 SHARED_RINGBUFFER_WRITE_OFFSET,
89 sizeof(fifo_counter_t));
90 ringBufferParcelable.setBytesPerFrame(mFifoBuffer->getBytesPerFrame());
91 ringBufferParcelable.setFramesPerBurst(1);
92 ringBufferParcelable.setCapacityInFrames(mCapacityInFrames);
93}