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 | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 17 | #define LOG_TAG (mInService ? "AAudioService" : "AAudio") |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 18 | //#define LOG_NDEBUG 0 |
| 19 | #include <utils/Log.h> |
| 20 | |
| 21 | #include "client/AudioStreamInternalPlay.h" |
| 22 | #include "utility/AudioClock.h" |
| 23 | |
| 24 | using android::WrappingBuffer; |
| 25 | |
| 26 | using namespace aaudio; |
| 27 | |
| 28 | AudioStreamInternalPlay::AudioStreamInternalPlay(AAudioServiceInterface &serviceInterface, |
| 29 | bool inService) |
| 30 | : AudioStreamInternal(serviceInterface, inService) { |
| 31 | |
| 32 | } |
| 33 | |
| 34 | AudioStreamInternalPlay::~AudioStreamInternalPlay() {} |
| 35 | |
| 36 | |
| 37 | // Write the data, block if needed and timeoutMillis > 0 |
| 38 | aaudio_result_t AudioStreamInternalPlay::write(const void *buffer, int32_t numFrames, |
| 39 | int64_t timeoutNanoseconds) |
| 40 | |
| 41 | { |
| 42 | return processData((void *)buffer, numFrames, timeoutNanoseconds); |
| 43 | } |
| 44 | |
| 45 | // Write as much data as we can without blocking. |
| 46 | aaudio_result_t AudioStreamInternalPlay::processDataNow(void *buffer, int32_t numFrames, |
| 47 | int64_t currentNanoTime, int64_t *wakeTimePtr) { |
| 48 | aaudio_result_t result = processCommands(); |
| 49 | if (result != AAUDIO_OK) { |
| 50 | return result; |
| 51 | } |
| 52 | |
| 53 | if (mAudioEndpoint.isFreeRunning()) { |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 54 | // Update data queue based on the timing model. |
| 55 | int64_t estimatedReadCounter = mClockModel.convertTimeToPosition(currentNanoTime); |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 56 | // ALOGD("AudioStreamInternal::processDataNow() - estimatedReadCounter = %d", (int)estimatedReadCounter); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 57 | mAudioEndpoint.setDataReadCounter(estimatedReadCounter); |
| 58 | } |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 59 | |
| 60 | // If the read index passed the write index then consider it an underrun. |
| 61 | if (mAudioEndpoint.getFullFramesAvailable() < 0) { |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 62 | ALOGV("AudioStreamInternal::processDataNow() - XRun! write = %d, read = %d", |
| 63 | (int)mAudioEndpoint.getDataWriteCounter(), |
| 64 | (int)mAudioEndpoint.getDataReadCounter()); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 65 | mXRunCount++; |
| 66 | } |
| 67 | |
| 68 | // Write some data to the buffer. |
| 69 | //ALOGD("AudioStreamInternal::processDataNow() - writeNowWithConversion(%d)", numFrames); |
| 70 | int32_t framesWritten = writeNowWithConversion(buffer, numFrames); |
| 71 | //ALOGD("AudioStreamInternal::processDataNow() - tried to write %d frames, wrote %d", |
| 72 | // numFrames, framesWritten); |
| 73 | |
| 74 | // Calculate an ideal time to wake up. |
| 75 | if (wakeTimePtr != nullptr && framesWritten >= 0) { |
| 76 | // By default wake up a few milliseconds from now. // TODO review |
| 77 | int64_t wakeTime = currentNanoTime + (1 * AAUDIO_NANOS_PER_MILLISECOND); |
| 78 | aaudio_stream_state_t state = getState(); |
| 79 | //ALOGD("AudioStreamInternal::processDataNow() - wakeTime based on %s", |
| 80 | // AAudio_convertStreamStateToText(state)); |
| 81 | switch (state) { |
| 82 | case AAUDIO_STREAM_STATE_OPEN: |
| 83 | case AAUDIO_STREAM_STATE_STARTING: |
| 84 | if (framesWritten != 0) { |
| 85 | // Don't wait to write more data. Just prime the buffer. |
| 86 | wakeTime = currentNanoTime; |
| 87 | } |
| 88 | break; |
| 89 | case AAUDIO_STREAM_STATE_STARTED: // When do we expect the next read burst to occur? |
| 90 | { |
| 91 | uint32_t burstSize = mFramesPerBurst; |
| 92 | if (burstSize < 32) { |
| 93 | burstSize = 32; // TODO review |
| 94 | } |
| 95 | |
| 96 | uint64_t nextReadPosition = mAudioEndpoint.getDataReadCounter() + burstSize; |
| 97 | wakeTime = mClockModel.convertPositionToTime(nextReadPosition); |
| 98 | } |
| 99 | break; |
| 100 | default: |
| 101 | break; |
| 102 | } |
| 103 | *wakeTimePtr = wakeTime; |
| 104 | |
| 105 | } |
| 106 | // ALOGD("AudioStreamInternal::processDataNow finished: now = %llu, read# = %llu, wrote# = %llu", |
| 107 | // (unsigned long long)currentNanoTime, |
| 108 | // (unsigned long long)mAudioEndpoint.getDataReadCounter(), |
| 109 | // (unsigned long long)mAudioEndpoint.getDownDataWriteCounter()); |
| 110 | return framesWritten; |
| 111 | } |
| 112 | |
| 113 | |
| 114 | aaudio_result_t AudioStreamInternalPlay::writeNowWithConversion(const void *buffer, |
| 115 | int32_t numFrames) { |
| 116 | // ALOGD("AudioStreamInternal::writeNowWithConversion(%p, %d)", |
| 117 | // buffer, numFrames); |
| 118 | WrappingBuffer wrappingBuffer; |
| 119 | uint8_t *source = (uint8_t *) buffer; |
| 120 | int32_t framesLeft = numFrames; |
| 121 | |
| 122 | mAudioEndpoint.getEmptyFramesAvailable(&wrappingBuffer); |
| 123 | |
| 124 | // Read data in one or two parts. |
| 125 | int partIndex = 0; |
| 126 | while (framesLeft > 0 && partIndex < WrappingBuffer::SIZE) { |
| 127 | int32_t framesToWrite = framesLeft; |
| 128 | int32_t framesAvailable = wrappingBuffer.numFrames[partIndex]; |
| 129 | if (framesAvailable > 0) { |
| 130 | if (framesToWrite > framesAvailable) { |
| 131 | framesToWrite = framesAvailable; |
| 132 | } |
| 133 | int32_t numBytes = getBytesPerFrame() * framesToWrite; |
| 134 | int32_t numSamples = framesToWrite * getSamplesPerFrame(); |
| 135 | // Data conversion. |
| 136 | float levelFrom; |
| 137 | float levelTo; |
| 138 | bool ramping = mVolumeRamp.nextSegment(framesToWrite * getSamplesPerFrame(), |
| 139 | &levelFrom, &levelTo); |
| 140 | // The formats are validated when the stream is opened so we do not have to |
| 141 | // check for illegal combinations here. |
| 142 | // TODO factor this out into a utility function |
| 143 | if (getFormat() == AAUDIO_FORMAT_PCM_FLOAT) { |
| 144 | if (mDeviceFormat == AAUDIO_FORMAT_PCM_FLOAT) { |
| 145 | AAudio_linearRamp( |
| 146 | (const float *) source, |
| 147 | (float *) wrappingBuffer.data[partIndex], |
| 148 | framesToWrite, |
| 149 | getSamplesPerFrame(), |
| 150 | levelFrom, |
| 151 | levelTo); |
| 152 | } else if (mDeviceFormat == AAUDIO_FORMAT_PCM_I16) { |
| 153 | if (ramping) { |
| 154 | AAudioConvert_floatToPcm16( |
| 155 | (const float *) source, |
| 156 | (int16_t *) wrappingBuffer.data[partIndex], |
| 157 | framesToWrite, |
| 158 | getSamplesPerFrame(), |
| 159 | levelFrom, |
| 160 | levelTo); |
| 161 | } else { |
| 162 | AAudioConvert_floatToPcm16( |
| 163 | (const float *) source, |
| 164 | (int16_t *) wrappingBuffer.data[partIndex], |
| 165 | numSamples, |
| 166 | levelTo); |
| 167 | } |
| 168 | } |
| 169 | } else if (getFormat() == AAUDIO_FORMAT_PCM_I16) { |
| 170 | if (mDeviceFormat == AAUDIO_FORMAT_PCM_FLOAT) { |
| 171 | if (ramping) { |
| 172 | AAudioConvert_pcm16ToFloat( |
| 173 | (const int16_t *) source, |
| 174 | (float *) wrappingBuffer.data[partIndex], |
| 175 | framesToWrite, |
| 176 | getSamplesPerFrame(), |
| 177 | levelFrom, |
| 178 | levelTo); |
| 179 | } else { |
| 180 | AAudioConvert_pcm16ToFloat( |
| 181 | (const int16_t *) source, |
| 182 | (float *) wrappingBuffer.data[partIndex], |
| 183 | numSamples, |
| 184 | levelTo); |
| 185 | } |
| 186 | } else if (mDeviceFormat == AAUDIO_FORMAT_PCM_I16) { |
| 187 | AAudio_linearRamp( |
| 188 | (const int16_t *) source, |
| 189 | (int16_t *) wrappingBuffer.data[partIndex], |
| 190 | framesToWrite, |
| 191 | getSamplesPerFrame(), |
| 192 | levelFrom, |
| 193 | levelTo); |
| 194 | } |
| 195 | } |
| 196 | source += numBytes; |
| 197 | framesLeft -= framesToWrite; |
| 198 | } else { |
| 199 | break; |
| 200 | } |
| 201 | partIndex++; |
| 202 | } |
| 203 | int32_t framesWritten = numFrames - framesLeft; |
| 204 | mAudioEndpoint.advanceWriteIndex(framesWritten); |
| 205 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 206 | // ALOGD("AudioStreamInternal::writeNowWithConversion() returns %d", framesWritten); |
| 207 | return framesWritten; |
| 208 | } |
| 209 | |
| 210 | |
| 211 | int64_t AudioStreamInternalPlay::getFramesRead() |
| 212 | { |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 213 | int64_t framesReadHardware; |
| 214 | if (isActive()) { |
| 215 | framesReadHardware = mClockModel.convertTimeToPosition(AudioClock::getNanoseconds()); |
| 216 | } else { |
| 217 | framesReadHardware = mAudioEndpoint.getDataReadCounter(); |
| 218 | } |
| 219 | int64_t framesRead = framesReadHardware + mFramesOffsetFromService; |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 220 | // Prevent retrograde motion. |
| 221 | if (framesRead < mLastFramesRead) { |
| 222 | framesRead = mLastFramesRead; |
| 223 | } else { |
| 224 | mLastFramesRead = framesRead; |
| 225 | } |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 226 | //ALOGD("AudioStreamInternalPlay::getFramesRead() returns %lld", (long long)framesRead); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 227 | return framesRead; |
| 228 | } |
| 229 | |
| 230 | int64_t AudioStreamInternalPlay::getFramesWritten() |
| 231 | { |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 232 | int64_t framesWritten = mAudioEndpoint.getDataWriteCounter() |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 233 | + mFramesOffsetFromService; |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 234 | //ALOGD("AudioStreamInternalPlay::getFramesWritten() returns %lld", (long long)framesWritten); |
| 235 | return framesWritten; |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | |
| 239 | // Render audio in the application callback and then write the data to the stream. |
| 240 | void *AudioStreamInternalPlay::callbackLoop() { |
| 241 | aaudio_result_t result = AAUDIO_OK; |
| 242 | aaudio_data_callback_result_t callbackResult = AAUDIO_CALLBACK_RESULT_CONTINUE; |
| 243 | AAudioStream_dataCallback appCallback = getDataCallbackProc(); |
| 244 | if (appCallback == nullptr) return NULL; |
| 245 | |
| 246 | // result might be a frame count |
| 247 | while (mCallbackEnabled.load() && isActive() && (result >= 0)) { |
| 248 | // Call application using the AAudio callback interface. |
| 249 | callbackResult = (*appCallback)( |
| 250 | (AAudioStream *) this, |
| 251 | getDataCallbackUserData(), |
| 252 | mCallbackBuffer, |
| 253 | mCallbackFrames); |
| 254 | |
| 255 | if (callbackResult == AAUDIO_CALLBACK_RESULT_CONTINUE) { |
| 256 | // Write audio data to stream. |
| 257 | int64_t timeoutNanos = calculateReasonableTimeout(mCallbackFrames); |
| 258 | |
| 259 | // This is a BLOCKING WRITE! |
| 260 | result = write(mCallbackBuffer, mCallbackFrames, timeoutNanos); |
| 261 | if ((result != mCallbackFrames)) { |
| 262 | ALOGE("AudioStreamInternalPlay(): callbackLoop: write() returned %d", result); |
| 263 | if (result >= 0) { |
| 264 | // Only wrote some of the frames requested. Must have timed out. |
| 265 | result = AAUDIO_ERROR_TIMEOUT; |
| 266 | } |
| 267 | AAudioStream_errorCallback errorCallback = getErrorCallbackProc(); |
| 268 | if (errorCallback != nullptr) { |
| 269 | (*errorCallback)( |
| 270 | (AAudioStream *) this, |
| 271 | getErrorCallbackUserData(), |
| 272 | result); |
| 273 | } |
| 274 | break; |
| 275 | } |
| 276 | } else if (callbackResult == AAUDIO_CALLBACK_RESULT_STOP) { |
| 277 | ALOGD("AudioStreamInternalPlay(): callback returned AAUDIO_CALLBACK_RESULT_STOP"); |
| 278 | break; |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | ALOGD("AudioStreamInternalPlay(): callbackLoop() exiting, result = %d, isActive() = %d", |
| 283 | result, (int) isActive()); |
| 284 | return NULL; |
| 285 | } |