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