Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 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 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 17 | #define LOG_TAG "AAudio" |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 18 | //#define LOG_NDEBUG 0 |
| 19 | #include <utils/Log.h> |
| 20 | |
| 21 | #include <cassert> |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 22 | #include <aaudio/AAudio.h> |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 23 | |
| 24 | #include "AudioEndpointParcelable.h" |
| 25 | #include "AudioEndpoint.h" |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 26 | #include "AAudioServiceMessage.h" |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 27 | |
| 28 | using namespace android; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 29 | using namespace aaudio; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 30 | |
Phil Burk | 5204d31 | 2017-05-04 17:16:13 -0700 | [diff] [blame] | 31 | #define RIDICULOUSLY_LARGE_BUFFER_CAPACITY (256 * 1024) |
| 32 | #define RIDICULOUSLY_LARGE_FRAME_SIZE 4096 |
| 33 | |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 34 | AudioEndpoint::AudioEndpoint() |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 35 | : mFreeRunning(false) |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 36 | , mDataReadCounter(0) |
| 37 | , mDataWriteCounter(0) |
| 38 | { |
| 39 | } |
| 40 | |
| 41 | AudioEndpoint::~AudioEndpoint() |
| 42 | { |
| 43 | } |
| 44 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 45 | static aaudio_result_t AudioEndpoint_validateQueueDescriptor(const char *type, |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 46 | const RingBufferDescriptor *descriptor) { |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 47 | if (descriptor == nullptr) { |
| 48 | ALOGE("AudioEndpoint_validateQueueDescriptor() NULL descriptor"); |
| 49 | return AAUDIO_ERROR_NULL; |
| 50 | } |
Phil Burk | 5204d31 | 2017-05-04 17:16:13 -0700 | [diff] [blame] | 51 | |
| 52 | if (descriptor->capacityInFrames < 1 |
| 53 | || descriptor->capacityInFrames > RIDICULOUSLY_LARGE_BUFFER_CAPACITY) { |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 54 | ALOGE("AudioEndpoint_validateQueueDescriptor() bad capacityInFrames = %d", |
| 55 | descriptor->capacityInFrames); |
| 56 | return AAUDIO_ERROR_OUT_OF_RANGE; |
| 57 | } |
Phil Burk | 5204d31 | 2017-05-04 17:16:13 -0700 | [diff] [blame] | 58 | |
| 59 | // Reject extreme values to catch bugs and prevent numeric overflows. |
| 60 | if (descriptor->bytesPerFrame < 1 |
| 61 | || descriptor->bytesPerFrame > RIDICULOUSLY_LARGE_FRAME_SIZE) { |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 62 | ALOGE("AudioEndpoint_validateQueueDescriptor() bad bytesPerFrame = %d", |
| 63 | descriptor->bytesPerFrame); |
| 64 | return AAUDIO_ERROR_OUT_OF_RANGE; |
| 65 | } |
Phil Burk | 5204d31 | 2017-05-04 17:16:13 -0700 | [diff] [blame] | 66 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 67 | if (descriptor->dataAddress == nullptr) { |
| 68 | ALOGE("AudioEndpoint_validateQueueDescriptor() NULL dataAddress"); |
| 69 | return AAUDIO_ERROR_NULL; |
| 70 | } |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 71 | ALOGV("AudioEndpoint_validateQueueDescriptor %s, dataAddress at %p ====================", |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 72 | type, |
| 73 | descriptor->dataAddress); |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 74 | ALOGV("AudioEndpoint_validateQueueDescriptor readCounter at %p, writeCounter at %p", |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 75 | descriptor->readCounterAddress, |
| 76 | descriptor->writeCounterAddress); |
| 77 | |
| 78 | // Try to READ from the data area. |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 79 | // This code will crash if the mmap failed. |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 80 | uint8_t value = descriptor->dataAddress[0]; |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 81 | ALOGV("AudioEndpoint_validateQueueDescriptor() dataAddress[0] = %d, then try to write", |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 82 | (int) value); |
| 83 | // Try to WRITE to the data area. |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 84 | descriptor->dataAddress[0] = value * 3; |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 85 | ALOGV("AudioEndpoint_validateQueueDescriptor() wrote successfully"); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 86 | |
| 87 | if (descriptor->readCounterAddress) { |
| 88 | fifo_counter_t counter = *descriptor->readCounterAddress; |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 89 | ALOGV("AudioEndpoint_validateQueueDescriptor() *readCounterAddress = %d, now write", |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 90 | (int) counter); |
| 91 | *descriptor->readCounterAddress = counter; |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 92 | ALOGV("AudioEndpoint_validateQueueDescriptor() wrote readCounterAddress successfully"); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 93 | } |
Phil Burk | 5204d31 | 2017-05-04 17:16:13 -0700 | [diff] [blame] | 94 | |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 95 | if (descriptor->writeCounterAddress) { |
| 96 | fifo_counter_t counter = *descriptor->writeCounterAddress; |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 97 | ALOGV("AudioEndpoint_validateQueueDescriptor() *writeCounterAddress = %d, now write", |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 98 | (int) counter); |
| 99 | *descriptor->writeCounterAddress = counter; |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 100 | ALOGV("AudioEndpoint_validateQueueDescriptor() wrote writeCounterAddress successfully"); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 101 | } |
Phil Burk | 5204d31 | 2017-05-04 17:16:13 -0700 | [diff] [blame] | 102 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 103 | return AAUDIO_OK; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 104 | } |
| 105 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 106 | aaudio_result_t AudioEndpoint_validateDescriptor(const EndpointDescriptor *pEndpointDescriptor) { |
| 107 | aaudio_result_t result = AudioEndpoint_validateQueueDescriptor("messages", |
| 108 | &pEndpointDescriptor->upMessageQueueDescriptor); |
| 109 | if (result == AAUDIO_OK) { |
| 110 | result = AudioEndpoint_validateQueueDescriptor("data", |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 111 | &pEndpointDescriptor->dataQueueDescriptor); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 112 | } |
| 113 | return result; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 114 | } |
| 115 | |
Phil Burk | fd34a93 | 2017-07-19 07:03:52 -0700 | [diff] [blame] | 116 | aaudio_result_t AudioEndpoint::configure(const EndpointDescriptor *pEndpointDescriptor, |
| 117 | aaudio_direction_t direction) |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 118 | { |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 119 | aaudio_result_t result = AudioEndpoint_validateDescriptor(pEndpointDescriptor); |
| 120 | if (result != AAUDIO_OK) { |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 121 | ALOGE("AudioEndpoint_validateQueueDescriptor returned %d %s", |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 122 | result, AAudio_convertResultToText(result)); |
| 123 | return result; |
| 124 | } |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 125 | |
Phil Burk | 5204d31 | 2017-05-04 17:16:13 -0700 | [diff] [blame] | 126 | // ============================ up message queue ============================= |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 127 | const RingBufferDescriptor *descriptor = &pEndpointDescriptor->upMessageQueueDescriptor; |
Phil Burk | 5204d31 | 2017-05-04 17:16:13 -0700 | [diff] [blame] | 128 | if(descriptor->bytesPerFrame != sizeof(AAudioServiceMessage)) { |
| 129 | ALOGE("AudioEndpoint::configure() bytesPerFrame != sizeof(AAudioServiceMessage) = %d", |
| 130 | descriptor->bytesPerFrame); |
| 131 | return AAUDIO_ERROR_INTERNAL; |
| 132 | } |
| 133 | |
| 134 | if(descriptor->readCounterAddress == nullptr || descriptor->writeCounterAddress == nullptr) { |
| 135 | ALOGE("AudioEndpoint_validateQueueDescriptor() NULL counter address"); |
| 136 | return AAUDIO_ERROR_NULL; |
| 137 | } |
| 138 | |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 139 | mUpCommandQueue = new FifoBuffer( |
| 140 | descriptor->bytesPerFrame, |
| 141 | descriptor->capacityInFrames, |
| 142 | descriptor->readCounterAddress, |
| 143 | descriptor->writeCounterAddress, |
| 144 | descriptor->dataAddress |
| 145 | ); |
Phil Burk | 5204d31 | 2017-05-04 17:16:13 -0700 | [diff] [blame] | 146 | |
Phil Burk | fd34a93 | 2017-07-19 07:03:52 -0700 | [diff] [blame] | 147 | // ============================ data queue ============================= |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 148 | descriptor = &pEndpointDescriptor->dataQueueDescriptor; |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 149 | ALOGV("AudioEndpoint::configure() data framesPerBurst = %d", descriptor->framesPerBurst); |
Phil Burk | fd34a93 | 2017-07-19 07:03:52 -0700 | [diff] [blame] | 150 | ALOGV("AudioEndpoint::configure() data readCounterAddress = %p", |
| 151 | descriptor->readCounterAddress); |
| 152 | |
| 153 | // An example of free running is when the other side is read or written by hardware DMA |
| 154 | // or a DSP. It does not update its counter so we have to update it. |
| 155 | int64_t *remoteCounter = (direction == AAUDIO_DIRECTION_OUTPUT) |
| 156 | ? descriptor->readCounterAddress // read by other side |
| 157 | : descriptor->writeCounterAddress; // written by other side |
| 158 | mFreeRunning = (remoteCounter == nullptr); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 159 | ALOGV("AudioEndpoint::configure() mFreeRunning = %d", mFreeRunning ? 1 : 0); |
Phil Burk | fd34a93 | 2017-07-19 07:03:52 -0700 | [diff] [blame] | 160 | |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 161 | int64_t *readCounterAddress = (descriptor->readCounterAddress == nullptr) |
| 162 | ? &mDataReadCounter |
| 163 | : descriptor->readCounterAddress; |
| 164 | int64_t *writeCounterAddress = (descriptor->writeCounterAddress == nullptr) |
| 165 | ? &mDataWriteCounter |
| 166 | : descriptor->writeCounterAddress; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 167 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 168 | mDataQueue = new FifoBuffer( |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 169 | descriptor->bytesPerFrame, |
| 170 | descriptor->capacityInFrames, |
| 171 | readCounterAddress, |
| 172 | writeCounterAddress, |
| 173 | descriptor->dataAddress |
| 174 | ); |
| 175 | uint32_t threshold = descriptor->capacityInFrames / 2; |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 176 | mDataQueue->setThreshold(threshold); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 177 | return result; |
| 178 | } |
| 179 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 180 | aaudio_result_t AudioEndpoint::readUpCommand(AAudioServiceMessage *commandPtr) |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 181 | { |
| 182 | return mUpCommandQueue->read(commandPtr, 1); |
| 183 | } |
| 184 | |
Phil Burk | fd34a93 | 2017-07-19 07:03:52 -0700 | [diff] [blame] | 185 | int32_t AudioEndpoint::getEmptyFramesAvailable(WrappingBuffer *wrappingBuffer) { |
| 186 | return mDataQueue->getEmptyRoomAvailable(wrappingBuffer); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 187 | } |
| 188 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 189 | int32_t AudioEndpoint::getEmptyFramesAvailable() |
| 190 | { |
| 191 | return mDataQueue->getFifoControllerBase()->getEmptyFramesAvailable(); |
| 192 | } |
| 193 | |
Phil Burk | fd34a93 | 2017-07-19 07:03:52 -0700 | [diff] [blame] | 194 | int32_t AudioEndpoint::getFullFramesAvailable(WrappingBuffer *wrappingBuffer) |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 195 | { |
| 196 | return mDataQueue->getFullDataAvailable(wrappingBuffer); |
Phil Burk | 4485d41 | 2017-05-09 15:55:02 -0700 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | int32_t AudioEndpoint::getFullFramesAvailable() |
| 200 | { |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 201 | return mDataQueue->getFifoControllerBase()->getFullFramesAvailable(); |
Phil Burk | 4485d41 | 2017-05-09 15:55:02 -0700 | [diff] [blame] | 202 | } |
| 203 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 204 | void AudioEndpoint::advanceWriteIndex(int32_t deltaFrames) { |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 205 | mDataQueue->getFifoControllerBase()->advanceWriteIndex(deltaFrames); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 206 | } |
| 207 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 208 | void AudioEndpoint::advanceReadIndex(int32_t deltaFrames) { |
| 209 | mDataQueue->getFifoControllerBase()->advanceReadIndex(deltaFrames); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 210 | } |
| 211 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 212 | void AudioEndpoint::setDataReadCounter(fifo_counter_t framesRead) |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 213 | { |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 214 | mDataQueue->setReadCounter(framesRead); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 215 | } |
| 216 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 217 | fifo_counter_t AudioEndpoint::getDataReadCounter() |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 218 | { |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 219 | return mDataQueue->getReadCounter(); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 220 | } |
| 221 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 222 | void AudioEndpoint::setDataWriteCounter(fifo_counter_t framesRead) |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 223 | { |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 224 | mDataQueue->setWriteCounter(framesRead); |
| 225 | } |
| 226 | |
| 227 | fifo_counter_t AudioEndpoint::getDataWriteCounter() |
| 228 | { |
| 229 | return mDataQueue->getWriteCounter(); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 230 | } |
| 231 | |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 232 | int32_t AudioEndpoint::setBufferSizeInFrames(int32_t requestedFrames, |
| 233 | int32_t *actualFrames) |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 234 | { |
| 235 | if (requestedFrames < ENDPOINT_DATA_QUEUE_SIZE_MIN) { |
| 236 | requestedFrames = ENDPOINT_DATA_QUEUE_SIZE_MIN; |
| 237 | } |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 238 | mDataQueue->setThreshold(requestedFrames); |
| 239 | *actualFrames = mDataQueue->getThreshold(); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 240 | return AAUDIO_OK; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | int32_t AudioEndpoint::getBufferSizeInFrames() const |
| 244 | { |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 245 | return mDataQueue->getThreshold(); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | int32_t AudioEndpoint::getBufferCapacityInFrames() const |
| 249 | { |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 250 | return (int32_t)mDataQueue->getBufferCapacityInFrames(); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 251 | } |
| 252 | |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 253 | void AudioEndpoint::dump() const { |
| 254 | ALOGD("AudioEndpoint: data readCounter = %lld", (long long) mDataQueue->getReadCounter()); |
| 255 | ALOGD("AudioEndpoint: data writeCounter = %lld", (long long) mDataQueue->getWriteCounter()); |
| 256 | } |