Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 17 | //#define LOG_NDEBUG 0 |
| 18 | #include <utils/Log.h> |
| 19 | |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 20 | #include <algorithm> |
Phil Burk | 0127c1b | 2018-03-29 13:48:06 -0700 | [diff] [blame] | 21 | #include <audio_utils/primitives.h> |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 22 | #include <aaudio/AAudio.h> |
| 23 | |
| 24 | #include "client/AudioStreamInternalCapture.h" |
| 25 | #include "utility/AudioClock.h" |
| 26 | |
Phil Burk | fd34a93 | 2017-07-19 07:03:52 -0700 | [diff] [blame] | 27 | #define ATRACE_TAG ATRACE_TAG_AUDIO |
| 28 | #include <utils/Trace.h> |
| 29 | |
Phil Burk | 79224ca | 2020-08-12 14:29:10 +0000 | [diff] [blame] | 30 | // We do this after the #includes because if a header uses ALOG. |
| 31 | // it would fail on the reference to mInService. |
| 32 | #undef LOG_TAG |
| 33 | // This file is used in both client and server processes. |
| 34 | // This is needed to make sense of the logs more easily. |
| 35 | #define LOG_TAG (mInService ? "AudioStreamInternalCapture_Service" \ |
| 36 | : "AudioStreamInternalCapture_Client") |
| 37 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 38 | using android::WrappingBuffer; |
| 39 | |
| 40 | using namespace aaudio; |
| 41 | |
| 42 | AudioStreamInternalCapture::AudioStreamInternalCapture(AAudioServiceInterface &serviceInterface, |
| 43 | bool inService) |
| 44 | : AudioStreamInternal(serviceInterface, inService) { |
| 45 | |
| 46 | } |
| 47 | |
| 48 | AudioStreamInternalCapture::~AudioStreamInternalCapture() {} |
| 49 | |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 50 | void AudioStreamInternalCapture::advanceClientToMatchServerPosition() { |
Phil Burk | 5edc4ea | 2020-04-17 08:15:42 -0700 | [diff] [blame] | 51 | int64_t readCounter = mAudioEndpoint->getDataReadCounter(); |
| 52 | int64_t writeCounter = mAudioEndpoint->getDataWriteCounter(); |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 53 | |
| 54 | // Bump offset so caller does not see the retrograde motion in getFramesRead(). |
| 55 | int64_t offset = readCounter - writeCounter; |
| 56 | mFramesOffsetFromService += offset; |
| 57 | ALOGD("advanceClientToMatchServerPosition() readN = %lld, writeN = %lld, offset = %lld", |
| 58 | (long long)readCounter, (long long)writeCounter, (long long)mFramesOffsetFromService); |
| 59 | |
| 60 | // Force readCounter to match writeCounter. |
| 61 | // This is because we cannot change the write counter in the hardware. |
Phil Burk | 5edc4ea | 2020-04-17 08:15:42 -0700 | [diff] [blame] | 62 | mAudioEndpoint->setDataReadCounter(writeCounter); |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 63 | } |
| 64 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 65 | // Write the data, block if needed and timeoutMillis > 0 |
| 66 | aaudio_result_t AudioStreamInternalCapture::read(void *buffer, int32_t numFrames, |
| 67 | int64_t timeoutNanoseconds) |
| 68 | { |
| 69 | return processData(buffer, numFrames, timeoutNanoseconds); |
| 70 | } |
| 71 | |
| 72 | // Read as much data as we can without blocking. |
| 73 | aaudio_result_t AudioStreamInternalCapture::processDataNow(void *buffer, int32_t numFrames, |
| 74 | int64_t currentNanoTime, int64_t *wakeTimePtr) { |
| 75 | aaudio_result_t result = processCommands(); |
| 76 | if (result != AAUDIO_OK) { |
| 77 | return result; |
| 78 | } |
| 79 | |
Phil Burk | fd34a93 | 2017-07-19 07:03:52 -0700 | [diff] [blame] | 80 | const char *traceName = "aaRdNow"; |
| 81 | ATRACE_BEGIN(traceName); |
| 82 | |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 83 | if (mClockModel.isStarting()) { |
| 84 | // Still haven't got any timestamps from server. |
| 85 | // Keep waiting until we get some valid timestamps then start writing to the |
| 86 | // current buffer position. |
| 87 | ALOGD("processDataNow() wait for valid timestamps"); |
| 88 | // Sleep very briefly and hope we get a timestamp soon. |
| 89 | *wakeTimePtr = currentNanoTime + (2000 * AAUDIO_NANOS_PER_MICROSECOND); |
| 90 | ATRACE_END(); |
| 91 | return 0; |
| 92 | } |
| 93 | // If we have gotten this far then we have at least one timestamp from server. |
| 94 | |
Phil Burk | 5edc4ea | 2020-04-17 08:15:42 -0700 | [diff] [blame] | 95 | if (mAudioEndpoint->isFreeRunning()) { |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 96 | //ALOGD("AudioStreamInternalCapture::processDataNow() - update remote counter"); |
| 97 | // Update data queue based on the timing model. |
Phil Burk | fceeee7 | 2019-06-14 11:18:45 -0700 | [diff] [blame] | 98 | // Jitter in the DSP can cause late writes to the FIFO. |
| 99 | // This might be caused by resampling. |
| 100 | // We want to read the FIFO after the latest possible time |
| 101 | // that the DSP could have written the data. |
| 102 | int64_t estimatedRemoteCounter = mClockModel.convertLatestTimeToPosition(currentNanoTime); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 103 | // TODO refactor, maybe use setRemoteCounter() |
Phil Burk | 5edc4ea | 2020-04-17 08:15:42 -0700 | [diff] [blame] | 104 | mAudioEndpoint->setDataWriteCounter(estimatedRemoteCounter); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 107 | // This code assumes that we have already received valid timestamps. |
| 108 | if (mNeedCatchUp.isRequested()) { |
| 109 | // Catch an MMAP pointer that is already advancing. |
| 110 | // This will avoid initial underruns caused by a slow cold start. |
| 111 | advanceClientToMatchServerPosition(); |
| 112 | mNeedCatchUp.acknowledge(); |
| 113 | } |
| 114 | |
Phil Burk | a10bd51 | 2019-09-27 11:49:17 -0700 | [diff] [blame] | 115 | // If the capture buffer is full beyond capacity then consider it an overrun. |
Phil Burk | 2329638 | 2017-11-20 15:45:11 -0800 | [diff] [blame] | 116 | // For shared streams, the xRunCount is passed up from the service. |
Phil Burk | 5edc4ea | 2020-04-17 08:15:42 -0700 | [diff] [blame] | 117 | if (mAudioEndpoint->isFreeRunning() |
| 118 | && mAudioEndpoint->getFullFramesAvailable() > mAudioEndpoint->getBufferCapacityInFrames()) { |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 119 | mXRunCount++; |
Phil Burk | fd34a93 | 2017-07-19 07:03:52 -0700 | [diff] [blame] | 120 | if (ATRACE_ENABLED()) { |
| 121 | ATRACE_INT("aaOverRuns", mXRunCount); |
| 122 | } |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | // Read some data from the buffer. |
| 126 | //ALOGD("AudioStreamInternalCapture::processDataNow() - readNowWithConversion(%d)", numFrames); |
| 127 | int32_t framesProcessed = readNowWithConversion(buffer, numFrames); |
| 128 | //ALOGD("AudioStreamInternalCapture::processDataNow() - tried to read %d frames, read %d", |
| 129 | // numFrames, framesProcessed); |
Phil Burk | fd34a93 | 2017-07-19 07:03:52 -0700 | [diff] [blame] | 130 | if (ATRACE_ENABLED()) { |
| 131 | ATRACE_INT("aaRead", framesProcessed); |
| 132 | } |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 133 | |
| 134 | // Calculate an ideal time to wake up. |
| 135 | if (wakeTimePtr != nullptr && framesProcessed >= 0) { |
| 136 | // By default wake up a few milliseconds from now. // TODO review |
| 137 | int64_t wakeTime = currentNanoTime + (1 * AAUDIO_NANOS_PER_MILLISECOND); |
| 138 | aaudio_stream_state_t state = getState(); |
| 139 | //ALOGD("AudioStreamInternalCapture::processDataNow() - wakeTime based on %s", |
| 140 | // AAudio_convertStreamStateToText(state)); |
| 141 | switch (state) { |
| 142 | case AAUDIO_STREAM_STATE_OPEN: |
| 143 | case AAUDIO_STREAM_STATE_STARTING: |
| 144 | break; |
Phil Burk | fd34a93 | 2017-07-19 07:03:52 -0700 | [diff] [blame] | 145 | case AAUDIO_STREAM_STATE_STARTED: |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 146 | { |
Phil Burk | fd34a93 | 2017-07-19 07:03:52 -0700 | [diff] [blame] | 147 | // When do we expect the next write burst to occur? |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 148 | |
Phil Burk | fd34a93 | 2017-07-19 07:03:52 -0700 | [diff] [blame] | 149 | // Calculate frame position based off of the readCounter because |
| 150 | // the writeCounter might have just advanced in the background, |
| 151 | // causing us to sleep until a later burst. |
Phil Burk | 5edc4ea | 2020-04-17 08:15:42 -0700 | [diff] [blame] | 152 | int64_t nextPosition = mAudioEndpoint->getDataReadCounter() + mFramesPerBurst; |
Phil Burk | fceeee7 | 2019-06-14 11:18:45 -0700 | [diff] [blame] | 153 | wakeTime = mClockModel.convertPositionToLatestTime(nextPosition); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 154 | } |
| 155 | break; |
| 156 | default: |
| 157 | break; |
| 158 | } |
| 159 | *wakeTimePtr = wakeTime; |
| 160 | |
| 161 | } |
Phil Burk | fd34a93 | 2017-07-19 07:03:52 -0700 | [diff] [blame] | 162 | |
| 163 | ATRACE_END(); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 164 | return framesProcessed; |
| 165 | } |
| 166 | |
| 167 | aaudio_result_t AudioStreamInternalCapture::readNowWithConversion(void *buffer, |
| 168 | int32_t numFrames) { |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 169 | // ALOGD("readNowWithConversion(%p, %d)", |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 170 | // buffer, numFrames); |
| 171 | WrappingBuffer wrappingBuffer; |
| 172 | uint8_t *destination = (uint8_t *) buffer; |
| 173 | int32_t framesLeft = numFrames; |
| 174 | |
Phil Burk | 5edc4ea | 2020-04-17 08:15:42 -0700 | [diff] [blame] | 175 | mAudioEndpoint->getFullFramesAvailable(&wrappingBuffer); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 176 | |
| 177 | // Read data in one or two parts. |
| 178 | for (int partIndex = 0; framesLeft > 0 && partIndex < WrappingBuffer::SIZE; partIndex++) { |
| 179 | int32_t framesToProcess = framesLeft; |
Phil Burk | 0127c1b | 2018-03-29 13:48:06 -0700 | [diff] [blame] | 180 | const int32_t framesAvailable = wrappingBuffer.numFrames[partIndex]; |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 181 | if (framesAvailable <= 0) break; |
| 182 | |
| 183 | if (framesToProcess > framesAvailable) { |
| 184 | framesToProcess = framesAvailable; |
| 185 | } |
| 186 | |
Phil Burk | 0127c1b | 2018-03-29 13:48:06 -0700 | [diff] [blame] | 187 | const int32_t numBytes = getBytesPerFrame() * framesToProcess; |
| 188 | const int32_t numSamples = framesToProcess * getSamplesPerFrame(); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 189 | |
Phil Burk | 0127c1b | 2018-03-29 13:48:06 -0700 | [diff] [blame] | 190 | const audio_format_t sourceFormat = getDeviceFormat(); |
| 191 | const audio_format_t destinationFormat = getFormat(); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 192 | // TODO factor this out into a utility function |
Phil Burk | 0127c1b | 2018-03-29 13:48:06 -0700 | [diff] [blame] | 193 | if (sourceFormat == destinationFormat) { |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 194 | memcpy(destination, wrappingBuffer.data[partIndex], numBytes); |
Phil Burk | 0127c1b | 2018-03-29 13:48:06 -0700 | [diff] [blame] | 195 | } else if (sourceFormat == AUDIO_FORMAT_PCM_16_BIT |
| 196 | && destinationFormat == AUDIO_FORMAT_PCM_FLOAT) { |
| 197 | memcpy_to_float_from_i16( |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 198 | (float *) destination, |
Phil Burk | 0127c1b | 2018-03-29 13:48:06 -0700 | [diff] [blame] | 199 | (const int16_t *) wrappingBuffer.data[partIndex], |
| 200 | numSamples); |
| 201 | } else if (sourceFormat == AUDIO_FORMAT_PCM_FLOAT |
| 202 | && destinationFormat == AUDIO_FORMAT_PCM_16_BIT) { |
| 203 | memcpy_to_i16_from_float( |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 204 | (int16_t *) destination, |
Phil Burk | 0127c1b | 2018-03-29 13:48:06 -0700 | [diff] [blame] | 205 | (const float *) wrappingBuffer.data[partIndex], |
| 206 | numSamples); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 207 | } else { |
Phil Burk | 0127c1b | 2018-03-29 13:48:06 -0700 | [diff] [blame] | 208 | ALOGE("%s() - Format conversion not supported! audio_format_t source = %u, dest = %u", |
| 209 | __func__, sourceFormat, destinationFormat); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 210 | return AAUDIO_ERROR_INVALID_FORMAT; |
| 211 | } |
| 212 | destination += numBytes; |
| 213 | framesLeft -= framesToProcess; |
| 214 | } |
| 215 | |
| 216 | int32_t framesProcessed = numFrames - framesLeft; |
Phil Burk | 5edc4ea | 2020-04-17 08:15:42 -0700 | [diff] [blame] | 217 | mAudioEndpoint->advanceReadIndex(framesProcessed); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 218 | |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 219 | //ALOGD("readNowWithConversion() returns %d", framesProcessed); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 220 | return framesProcessed; |
| 221 | } |
| 222 | |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 223 | int64_t AudioStreamInternalCapture::getFramesWritten() { |
Phil Burk | 5edc4ea | 2020-04-17 08:15:42 -0700 | [diff] [blame] | 224 | if (mAudioEndpoint) { |
| 225 | const int64_t framesWrittenHardware = isClockModelInControl() |
| 226 | ? mClockModel.convertTimeToPosition(AudioClock::getNanoseconds()) |
| 227 | : mAudioEndpoint->getDataWriteCounter(); |
| 228 | // Add service offset and prevent retrograde motion. |
| 229 | mLastFramesWritten = std::max(mLastFramesWritten, |
| 230 | framesWrittenHardware + mFramesOffsetFromService); |
| 231 | } |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 232 | return mLastFramesWritten; |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 233 | } |
| 234 | |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 235 | int64_t AudioStreamInternalCapture::getFramesRead() { |
Phil Burk | 5edc4ea | 2020-04-17 08:15:42 -0700 | [diff] [blame] | 236 | if (mAudioEndpoint) { |
| 237 | mLastFramesRead = mAudioEndpoint->getDataReadCounter() + mFramesOffsetFromService; |
| 238 | } |
| 239 | return mLastFramesRead; |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | // Read data from the stream and pass it to the callback for processing. |
| 243 | void *AudioStreamInternalCapture::callbackLoop() { |
| 244 | aaudio_result_t result = AAUDIO_OK; |
| 245 | aaudio_data_callback_result_t callbackResult = AAUDIO_CALLBACK_RESULT_CONTINUE; |
Phil Burk | 134f197 | 2017-12-08 13:06:11 -0800 | [diff] [blame] | 246 | if (!isDataCallbackSet()) return NULL; |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 247 | |
| 248 | // result might be a frame count |
| 249 | while (mCallbackEnabled.load() && isActive() && (result >= 0)) { |
| 250 | |
| 251 | // Read audio data from stream. |
| 252 | int64_t timeoutNanos = calculateReasonableTimeout(mCallbackFrames); |
| 253 | |
| 254 | // This is a BLOCKING READ! |
Phil Burk | bf821e2 | 2020-04-17 11:51:43 -0700 | [diff] [blame] | 255 | result = read(mCallbackBuffer.get(), mCallbackFrames, timeoutNanos); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 256 | if ((result != mCallbackFrames)) { |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 257 | ALOGE("callbackLoop: read() returned %d", result); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 258 | if (result >= 0) { |
| 259 | // Only read some of the frames requested. Must have timed out. |
| 260 | result = AAUDIO_ERROR_TIMEOUT; |
| 261 | } |
Phil Burk | 134f197 | 2017-12-08 13:06:11 -0800 | [diff] [blame] | 262 | maybeCallErrorCallback(result); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 263 | break; |
| 264 | } |
| 265 | |
| 266 | // Call application using the AAudio callback interface. |
Phil Burk | bf821e2 | 2020-04-17 11:51:43 -0700 | [diff] [blame] | 267 | callbackResult = maybeCallDataCallback(mCallbackBuffer.get(), mCallbackFrames); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 268 | |
| 269 | if (callbackResult == AAUDIO_CALLBACK_RESULT_STOP) { |
Phil Burk | 762365c | 2018-12-10 16:02:16 -0800 | [diff] [blame] | 270 | ALOGD("%s(): callback returned AAUDIO_CALLBACK_RESULT_STOP", __func__); |
Phil Burk | 1e83bee | 2018-12-17 14:15:20 -0800 | [diff] [blame] | 271 | result = systemStopFromCallback(); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 272 | break; |
| 273 | } |
| 274 | } |
| 275 | |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 276 | ALOGD("callbackLoop() exiting, result = %d, isActive() = %d", |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 277 | result, (int) isActive()); |
| 278 | return NULL; |
| 279 | } |