blob: 3ee450a861c1f3dc7d9418b2af6c4ec77e24e26d [file] [log] [blame]
Phil Burk204a1632017-01-03 17:23:43 -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 "AAudio"
Phil Burk204a1632017-01-03 17:23:43 -080018//#define LOG_NDEBUG 0
19#include <utils/Log.h>
20
21#include <cassert>
Phil Burke4d7bb42017-03-28 11:32:39 -070022#include <aaudio/AAudio.h>
Phil Burk204a1632017-01-03 17:23:43 -080023
24#include "AudioEndpointParcelable.h"
25#include "AudioEndpoint.h"
Phil Burk5ed503c2017-02-01 09:38:15 -080026#include "AAudioServiceMessage.h"
Phil Burk204a1632017-01-03 17:23:43 -080027
28using namespace android;
Phil Burk5ed503c2017-02-01 09:38:15 -080029using namespace aaudio;
Phil Burk204a1632017-01-03 17:23:43 -080030
Phil Burk5204d312017-05-04 17:16:13 -070031#define RIDICULOUSLY_LARGE_BUFFER_CAPACITY (256 * 1024)
32#define RIDICULOUSLY_LARGE_FRAME_SIZE 4096
33
Phil Burk204a1632017-01-03 17:23:43 -080034AudioEndpoint::AudioEndpoint()
Phil Burk4a8d2602017-08-09 16:53:35 -070035 : mUpCommandQueue(nullptr)
36 , mDataQueue(nullptr)
37 , mFreeRunning(false)
Phil Burk204a1632017-01-03 17:23:43 -080038 , mDataReadCounter(0)
39 , mDataWriteCounter(0)
40{
41}
42
Phil Burk4a8d2602017-08-09 16:53:35 -070043AudioEndpoint::~AudioEndpoint() {
44 delete mDataQueue;
45 delete mUpCommandQueue;
Phil Burk204a1632017-01-03 17:23:43 -080046}
47
Phil Burkc0c70e32017-02-09 13:18:38 -080048static aaudio_result_t AudioEndpoint_validateQueueDescriptor(const char *type,
Phil Burk204a1632017-01-03 17:23:43 -080049 const RingBufferDescriptor *descriptor) {
Phil Burkc0c70e32017-02-09 13:18:38 -080050 if (descriptor == nullptr) {
51 ALOGE("AudioEndpoint_validateQueueDescriptor() NULL descriptor");
52 return AAUDIO_ERROR_NULL;
53 }
Phil Burk5204d312017-05-04 17:16:13 -070054
55 if (descriptor->capacityInFrames < 1
56 || descriptor->capacityInFrames > RIDICULOUSLY_LARGE_BUFFER_CAPACITY) {
Phil Burkc0c70e32017-02-09 13:18:38 -080057 ALOGE("AudioEndpoint_validateQueueDescriptor() bad capacityInFrames = %d",
58 descriptor->capacityInFrames);
59 return AAUDIO_ERROR_OUT_OF_RANGE;
60 }
Phil Burk5204d312017-05-04 17:16:13 -070061
62 // Reject extreme values to catch bugs and prevent numeric overflows.
63 if (descriptor->bytesPerFrame < 1
64 || descriptor->bytesPerFrame > RIDICULOUSLY_LARGE_FRAME_SIZE) {
Phil Burkc0c70e32017-02-09 13:18:38 -080065 ALOGE("AudioEndpoint_validateQueueDescriptor() bad bytesPerFrame = %d",
66 descriptor->bytesPerFrame);
67 return AAUDIO_ERROR_OUT_OF_RANGE;
68 }
Phil Burk5204d312017-05-04 17:16:13 -070069
Phil Burkc0c70e32017-02-09 13:18:38 -080070 if (descriptor->dataAddress == nullptr) {
71 ALOGE("AudioEndpoint_validateQueueDescriptor() NULL dataAddress");
72 return AAUDIO_ERROR_NULL;
73 }
Phil Burk71f35bb2017-04-13 16:05:07 -070074 ALOGV("AudioEndpoint_validateQueueDescriptor %s, dataAddress at %p ====================",
Phil Burk204a1632017-01-03 17:23:43 -080075 type,
76 descriptor->dataAddress);
Phil Burk71f35bb2017-04-13 16:05:07 -070077 ALOGV("AudioEndpoint_validateQueueDescriptor readCounter at %p, writeCounter at %p",
Phil Burk204a1632017-01-03 17:23:43 -080078 descriptor->readCounterAddress,
79 descriptor->writeCounterAddress);
80
81 // Try to READ from the data area.
Phil Burkc0c70e32017-02-09 13:18:38 -080082 // This code will crash if the mmap failed.
Phil Burk204a1632017-01-03 17:23:43 -080083 uint8_t value = descriptor->dataAddress[0];
Phil Burk71f35bb2017-04-13 16:05:07 -070084 ALOGV("AudioEndpoint_validateQueueDescriptor() dataAddress[0] = %d, then try to write",
Phil Burk204a1632017-01-03 17:23:43 -080085 (int) value);
86 // Try to WRITE to the data area.
Phil Burkc0c70e32017-02-09 13:18:38 -080087 descriptor->dataAddress[0] = value * 3;
Phil Burk71f35bb2017-04-13 16:05:07 -070088 ALOGV("AudioEndpoint_validateQueueDescriptor() wrote successfully");
Phil Burk204a1632017-01-03 17:23:43 -080089
90 if (descriptor->readCounterAddress) {
91 fifo_counter_t counter = *descriptor->readCounterAddress;
Phil Burk71f35bb2017-04-13 16:05:07 -070092 ALOGV("AudioEndpoint_validateQueueDescriptor() *readCounterAddress = %d, now write",
Phil Burk204a1632017-01-03 17:23:43 -080093 (int) counter);
94 *descriptor->readCounterAddress = counter;
Phil Burk71f35bb2017-04-13 16:05:07 -070095 ALOGV("AudioEndpoint_validateQueueDescriptor() wrote readCounterAddress successfully");
Phil Burk204a1632017-01-03 17:23:43 -080096 }
Phil Burk5204d312017-05-04 17:16:13 -070097
Phil Burk204a1632017-01-03 17:23:43 -080098 if (descriptor->writeCounterAddress) {
99 fifo_counter_t counter = *descriptor->writeCounterAddress;
Phil Burk71f35bb2017-04-13 16:05:07 -0700100 ALOGV("AudioEndpoint_validateQueueDescriptor() *writeCounterAddress = %d, now write",
Phil Burk204a1632017-01-03 17:23:43 -0800101 (int) counter);
102 *descriptor->writeCounterAddress = counter;
Phil Burk71f35bb2017-04-13 16:05:07 -0700103 ALOGV("AudioEndpoint_validateQueueDescriptor() wrote writeCounterAddress successfully");
Phil Burk204a1632017-01-03 17:23:43 -0800104 }
Phil Burk5204d312017-05-04 17:16:13 -0700105
Phil Burkc0c70e32017-02-09 13:18:38 -0800106 return AAUDIO_OK;
Phil Burk204a1632017-01-03 17:23:43 -0800107}
108
Phil Burkc0c70e32017-02-09 13:18:38 -0800109aaudio_result_t AudioEndpoint_validateDescriptor(const EndpointDescriptor *pEndpointDescriptor) {
110 aaudio_result_t result = AudioEndpoint_validateQueueDescriptor("messages",
111 &pEndpointDescriptor->upMessageQueueDescriptor);
112 if (result == AAUDIO_OK) {
113 result = AudioEndpoint_validateQueueDescriptor("data",
Phil Burk87c9f642017-05-17 07:22:39 -0700114 &pEndpointDescriptor->dataQueueDescriptor);
Phil Burkc0c70e32017-02-09 13:18:38 -0800115 }
116 return result;
Phil Burk204a1632017-01-03 17:23:43 -0800117}
118
Phil Burkfd34a932017-07-19 07:03:52 -0700119aaudio_result_t AudioEndpoint::configure(const EndpointDescriptor *pEndpointDescriptor,
120 aaudio_direction_t direction)
Phil Burk204a1632017-01-03 17:23:43 -0800121{
Phil Burkc0c70e32017-02-09 13:18:38 -0800122 aaudio_result_t result = AudioEndpoint_validateDescriptor(pEndpointDescriptor);
123 if (result != AAUDIO_OK) {
Phil Burk71f35bb2017-04-13 16:05:07 -0700124 ALOGE("AudioEndpoint_validateQueueDescriptor returned %d %s",
Phil Burkc0c70e32017-02-09 13:18:38 -0800125 result, AAudio_convertResultToText(result));
126 return result;
127 }
Phil Burk204a1632017-01-03 17:23:43 -0800128
Phil Burk5204d312017-05-04 17:16:13 -0700129 // ============================ up message queue =============================
Phil Burk204a1632017-01-03 17:23:43 -0800130 const RingBufferDescriptor *descriptor = &pEndpointDescriptor->upMessageQueueDescriptor;
Phil Burk5204d312017-05-04 17:16:13 -0700131 if(descriptor->bytesPerFrame != sizeof(AAudioServiceMessage)) {
132 ALOGE("AudioEndpoint::configure() bytesPerFrame != sizeof(AAudioServiceMessage) = %d",
133 descriptor->bytesPerFrame);
134 return AAUDIO_ERROR_INTERNAL;
135 }
136
137 if(descriptor->readCounterAddress == nullptr || descriptor->writeCounterAddress == nullptr) {
138 ALOGE("AudioEndpoint_validateQueueDescriptor() NULL counter address");
139 return AAUDIO_ERROR_NULL;
140 }
141
Phil Burk204a1632017-01-03 17:23:43 -0800142 mUpCommandQueue = new FifoBuffer(
143 descriptor->bytesPerFrame,
144 descriptor->capacityInFrames,
145 descriptor->readCounterAddress,
146 descriptor->writeCounterAddress,
147 descriptor->dataAddress
148 );
Phil Burk5204d312017-05-04 17:16:13 -0700149
Phil Burkfd34a932017-07-19 07:03:52 -0700150 // ============================ data queue =============================
Phil Burk87c9f642017-05-17 07:22:39 -0700151 descriptor = &pEndpointDescriptor->dataQueueDescriptor;
Phil Burk71f35bb2017-04-13 16:05:07 -0700152 ALOGV("AudioEndpoint::configure() data framesPerBurst = %d", descriptor->framesPerBurst);
Phil Burkfd34a932017-07-19 07:03:52 -0700153 ALOGV("AudioEndpoint::configure() data readCounterAddress = %p",
154 descriptor->readCounterAddress);
155
156 // An example of free running is when the other side is read or written by hardware DMA
157 // or a DSP. It does not update its counter so we have to update it.
158 int64_t *remoteCounter = (direction == AAUDIO_DIRECTION_OUTPUT)
159 ? descriptor->readCounterAddress // read by other side
160 : descriptor->writeCounterAddress; // written by other side
161 mFreeRunning = (remoteCounter == nullptr);
Phil Burk87c9f642017-05-17 07:22:39 -0700162 ALOGV("AudioEndpoint::configure() mFreeRunning = %d", mFreeRunning ? 1 : 0);
Phil Burkfd34a932017-07-19 07:03:52 -0700163
Phil Burk204a1632017-01-03 17:23:43 -0800164 int64_t *readCounterAddress = (descriptor->readCounterAddress == nullptr)
165 ? &mDataReadCounter
166 : descriptor->readCounterAddress;
167 int64_t *writeCounterAddress = (descriptor->writeCounterAddress == nullptr)
168 ? &mDataWriteCounter
169 : descriptor->writeCounterAddress;
Phil Burkc0c70e32017-02-09 13:18:38 -0800170
Phil Burk87c9f642017-05-17 07:22:39 -0700171 mDataQueue = new FifoBuffer(
Phil Burk204a1632017-01-03 17:23:43 -0800172 descriptor->bytesPerFrame,
173 descriptor->capacityInFrames,
174 readCounterAddress,
175 writeCounterAddress,
176 descriptor->dataAddress
177 );
178 uint32_t threshold = descriptor->capacityInFrames / 2;
Phil Burk87c9f642017-05-17 07:22:39 -0700179 mDataQueue->setThreshold(threshold);
Phil Burk204a1632017-01-03 17:23:43 -0800180 return result;
181}
182
Phil Burk5ed503c2017-02-01 09:38:15 -0800183aaudio_result_t AudioEndpoint::readUpCommand(AAudioServiceMessage *commandPtr)
Phil Burk204a1632017-01-03 17:23:43 -0800184{
185 return mUpCommandQueue->read(commandPtr, 1);
186}
187
Phil Burkfd34a932017-07-19 07:03:52 -0700188int32_t AudioEndpoint::getEmptyFramesAvailable(WrappingBuffer *wrappingBuffer) {
189 return mDataQueue->getEmptyRoomAvailable(wrappingBuffer);
Phil Burkc0c70e32017-02-09 13:18:38 -0800190}
191
Phil Burk87c9f642017-05-17 07:22:39 -0700192int32_t AudioEndpoint::getEmptyFramesAvailable()
193{
194 return mDataQueue->getFifoControllerBase()->getEmptyFramesAvailable();
195}
196
Phil Burkfd34a932017-07-19 07:03:52 -0700197int32_t AudioEndpoint::getFullFramesAvailable(WrappingBuffer *wrappingBuffer)
Phil Burk87c9f642017-05-17 07:22:39 -0700198{
199 return mDataQueue->getFullDataAvailable(wrappingBuffer);
Phil Burk4485d412017-05-09 15:55:02 -0700200}
201
202int32_t AudioEndpoint::getFullFramesAvailable()
203{
Phil Burk87c9f642017-05-17 07:22:39 -0700204 return mDataQueue->getFifoControllerBase()->getFullFramesAvailable();
Phil Burk4485d412017-05-09 15:55:02 -0700205}
206
Phil Burkc0c70e32017-02-09 13:18:38 -0800207void AudioEndpoint::advanceWriteIndex(int32_t deltaFrames) {
Phil Burk87c9f642017-05-17 07:22:39 -0700208 mDataQueue->getFifoControllerBase()->advanceWriteIndex(deltaFrames);
Phil Burkc0c70e32017-02-09 13:18:38 -0800209}
210
Phil Burk87c9f642017-05-17 07:22:39 -0700211void AudioEndpoint::advanceReadIndex(int32_t deltaFrames) {
212 mDataQueue->getFifoControllerBase()->advanceReadIndex(deltaFrames);
Phil Burk204a1632017-01-03 17:23:43 -0800213}
214
Phil Burk87c9f642017-05-17 07:22:39 -0700215void AudioEndpoint::setDataReadCounter(fifo_counter_t framesRead)
Phil Burk204a1632017-01-03 17:23:43 -0800216{
Phil Burk87c9f642017-05-17 07:22:39 -0700217 mDataQueue->setReadCounter(framesRead);
Phil Burk204a1632017-01-03 17:23:43 -0800218}
219
Phil Burk87c9f642017-05-17 07:22:39 -0700220fifo_counter_t AudioEndpoint::getDataReadCounter()
Phil Burk204a1632017-01-03 17:23:43 -0800221{
Phil Burk87c9f642017-05-17 07:22:39 -0700222 return mDataQueue->getReadCounter();
Phil Burk204a1632017-01-03 17:23:43 -0800223}
224
Phil Burk87c9f642017-05-17 07:22:39 -0700225void AudioEndpoint::setDataWriteCounter(fifo_counter_t framesRead)
Phil Burk204a1632017-01-03 17:23:43 -0800226{
Phil Burk87c9f642017-05-17 07:22:39 -0700227 mDataQueue->setWriteCounter(framesRead);
228}
229
230fifo_counter_t AudioEndpoint::getDataWriteCounter()
231{
232 return mDataQueue->getWriteCounter();
Phil Burk204a1632017-01-03 17:23:43 -0800233}
234
Phil Burk3316d5e2017-02-15 11:23:01 -0800235int32_t AudioEndpoint::setBufferSizeInFrames(int32_t requestedFrames,
236 int32_t *actualFrames)
Phil Burk204a1632017-01-03 17:23:43 -0800237{
238 if (requestedFrames < ENDPOINT_DATA_QUEUE_SIZE_MIN) {
239 requestedFrames = ENDPOINT_DATA_QUEUE_SIZE_MIN;
240 }
Phil Burk87c9f642017-05-17 07:22:39 -0700241 mDataQueue->setThreshold(requestedFrames);
242 *actualFrames = mDataQueue->getThreshold();
Phil Burk5ed503c2017-02-01 09:38:15 -0800243 return AAUDIO_OK;
Phil Burk204a1632017-01-03 17:23:43 -0800244}
245
246int32_t AudioEndpoint::getBufferSizeInFrames() const
247{
Phil Burk87c9f642017-05-17 07:22:39 -0700248 return mDataQueue->getThreshold();
Phil Burk204a1632017-01-03 17:23:43 -0800249}
250
251int32_t AudioEndpoint::getBufferCapacityInFrames() const
252{
Phil Burk87c9f642017-05-17 07:22:39 -0700253 return (int32_t)mDataQueue->getBufferCapacityInFrames();
Phil Burk204a1632017-01-03 17:23:43 -0800254}
255
Phil Burkec89b2e2017-06-20 15:05:06 -0700256void AudioEndpoint::dump() const {
257 ALOGD("AudioEndpoint: data readCounter = %lld", (long long) mDataQueue->getReadCounter());
258 ALOGD("AudioEndpoint: data writeCounter = %lld", (long long) mDataQueue->getWriteCounter());
259}
Phil Burkea04d972017-08-07 12:30:44 -0700260
261void AudioEndpoint::eraseDataMemory() {
262 mDataQueue->eraseMemory();
263}