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