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