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 | fd34a93 | 2017-07-19 07:03:52 -0700 | [diff] [blame] | 20 | #define ATRACE_TAG ATRACE_TAG_AUDIO |
| 21 | |
| 22 | #include <utils/Trace.h> |
| 23 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 24 | #include "client/AudioStreamInternalPlay.h" |
| 25 | #include "utility/AudioClock.h" |
| 26 | |
Phil Burk | 58f5ce1 | 2020-08-12 14:29:10 +0000 | [diff] [blame] | 27 | // We do this after the #includes because if a header uses ALOG. |
| 28 | // it would fail on the reference to mInService. |
| 29 | #undef LOG_TAG |
| 30 | // This file is used in both client and server processes. |
| 31 | // This is needed to make sense of the logs more easily. |
| 32 | #define LOG_TAG (mInService ? "AudioStreamInternalPlay_Service" \ |
| 33 | : "AudioStreamInternalPlay_Client") |
| 34 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 35 | using android::WrappingBuffer; |
| 36 | |
| 37 | using namespace aaudio; |
| 38 | |
| 39 | AudioStreamInternalPlay::AudioStreamInternalPlay(AAudioServiceInterface &serviceInterface, |
| 40 | bool inService) |
| 41 | : AudioStreamInternal(serviceInterface, inService) { |
| 42 | |
| 43 | } |
| 44 | |
| 45 | AudioStreamInternalPlay::~AudioStreamInternalPlay() {} |
| 46 | |
Phil Burk | 02fec70 | 2018-02-16 18:25:55 -0800 | [diff] [blame] | 47 | constexpr int kRampMSec = 10; // time to apply a change in volume |
| 48 | |
| 49 | aaudio_result_t AudioStreamInternalPlay::open(const AudioStreamBuilder &builder) { |
| 50 | aaudio_result_t result = AudioStreamInternal::open(builder); |
| 51 | if (result == AAUDIO_OK) { |
Phil Burk | 0127c1b | 2018-03-29 13:48:06 -0700 | [diff] [blame] | 52 | result = mFlowGraph.configure(getFormat(), |
| 53 | getSamplesPerFrame(), |
| 54 | getDeviceFormat(), |
| 55 | getDeviceChannelCount()); |
| 56 | |
| 57 | if (result != AAUDIO_OK) { |
Phil Burk | 8b4e05e | 2019-12-17 12:12:09 -0800 | [diff] [blame] | 58 | releaseCloseFinal(); |
Phil Burk | 0127c1b | 2018-03-29 13:48:06 -0700 | [diff] [blame] | 59 | } |
Phil Burk | 02fec70 | 2018-02-16 18:25:55 -0800 | [diff] [blame] | 60 | // Sample rate is constrained to common values by now and should not overflow. |
| 61 | int32_t numFrames = kRampMSec * getSampleRate() / AAUDIO_MILLIS_PER_SECOND; |
Phil Burk | 0127c1b | 2018-03-29 13:48:06 -0700 | [diff] [blame] | 62 | mFlowGraph.setRampLengthInFrames(numFrames); |
Phil Burk | 02fec70 | 2018-02-16 18:25:55 -0800 | [diff] [blame] | 63 | } |
| 64 | return result; |
| 65 | } |
| 66 | |
Phil Burk | 13d3d83 | 2019-06-10 14:36:48 -0700 | [diff] [blame] | 67 | // This must be called under mStreamLock. |
Phil Burk | 5cc83c3 | 2017-11-28 15:43:18 -0800 | [diff] [blame] | 68 | aaudio_result_t AudioStreamInternalPlay::requestPause() |
Phil Burk | b336e89 | 2017-07-05 15:35:43 -0700 | [diff] [blame] | 69 | { |
Phil Burk | 5cc83c3 | 2017-11-28 15:43:18 -0800 | [diff] [blame] | 70 | aaudio_result_t result = stopCallback(); |
| 71 | if (result != AAUDIO_OK) { |
| 72 | return result; |
| 73 | } |
Phil Burk | b336e89 | 2017-07-05 15:35:43 -0700 | [diff] [blame] | 74 | if (mServiceStreamHandle == AAUDIO_HANDLE_INVALID) { |
Phil Burk | 29ccc29 | 2019-04-15 08:58:08 -0700 | [diff] [blame] | 75 | ALOGW("%s() mServiceStreamHandle invalid", __func__); |
Phil Burk | b336e89 | 2017-07-05 15:35:43 -0700 | [diff] [blame] | 76 | return AAUDIO_ERROR_INVALID_STATE; |
| 77 | } |
| 78 | |
| 79 | mClockModel.stop(AudioClock::getNanoseconds()); |
| 80 | setState(AAUDIO_STREAM_STATE_PAUSING); |
Phil Burk | a53ffa6 | 2018-10-10 16:21:37 -0700 | [diff] [blame] | 81 | mAtomicInternalTimestamp.clear(); |
Phil Burk | 965650e | 2017-09-07 21:00:09 -0700 | [diff] [blame] | 82 | return mServiceInterface.pauseStream(mServiceStreamHandle); |
Phil Burk | b336e89 | 2017-07-05 15:35:43 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Phil Burk | b336e89 | 2017-07-05 15:35:43 -0700 | [diff] [blame] | 85 | aaudio_result_t AudioStreamInternalPlay::requestFlush() { |
| 86 | if (mServiceStreamHandle == AAUDIO_HANDLE_INVALID) { |
Phil Burk | 29ccc29 | 2019-04-15 08:58:08 -0700 | [diff] [blame] | 87 | ALOGW("%s() mServiceStreamHandle invalid", __func__); |
Phil Burk | b336e89 | 2017-07-05 15:35:43 -0700 | [diff] [blame] | 88 | return AAUDIO_ERROR_INVALID_STATE; |
| 89 | } |
| 90 | |
| 91 | setState(AAUDIO_STREAM_STATE_FLUSHING); |
| 92 | return mServiceInterface.flushStream(mServiceStreamHandle); |
| 93 | } |
| 94 | |
Phil Burk | ec8ca52 | 2020-05-19 10:05:58 -0700 | [diff] [blame] | 95 | void AudioStreamInternalPlay::prepareBuffersForStart() { |
| 96 | // Prevent stale data from being played. |
| 97 | mAudioEndpoint->eraseDataMemory(); |
| 98 | } |
| 99 | |
| 100 | void AudioStreamInternalPlay::advanceClientToMatchServerPosition(int32_t serverMargin) { |
| 101 | int64_t readCounter = mAudioEndpoint->getDataReadCounter() + serverMargin; |
Phil Burk | 5edc4ea | 2020-04-17 08:15:42 -0700 | [diff] [blame] | 102 | int64_t writeCounter = mAudioEndpoint->getDataWriteCounter(); |
Phil Burk | b336e89 | 2017-07-05 15:35:43 -0700 | [diff] [blame] | 103 | |
| 104 | // Bump offset so caller does not see the retrograde motion in getFramesRead(). |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 105 | int64_t offset = writeCounter - readCounter; |
| 106 | mFramesOffsetFromService += offset; |
Phil Burk | 19e990e | 2018-03-22 13:59:34 -0700 | [diff] [blame] | 107 | ALOGV("%s() readN = %lld, writeN = %lld, offset = %lld", __func__, |
Phil Burk | b336e89 | 2017-07-05 15:35:43 -0700 | [diff] [blame] | 108 | (long long)readCounter, (long long)writeCounter, (long long)mFramesOffsetFromService); |
| 109 | |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 110 | // Force writeCounter to match readCounter. |
| 111 | // This is because we cannot change the read counter in the hardware. |
Phil Burk | 5edc4ea | 2020-04-17 08:15:42 -0700 | [diff] [blame] | 112 | mAudioEndpoint->setDataWriteCounter(readCounter); |
Phil Burk | b336e89 | 2017-07-05 15:35:43 -0700 | [diff] [blame] | 113 | } |
| 114 | |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 115 | void AudioStreamInternalPlay::onFlushFromServer() { |
| 116 | advanceClientToMatchServerPosition(); |
| 117 | } |
| 118 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 119 | // Write the data, block if needed and timeoutMillis > 0 |
| 120 | aaudio_result_t AudioStreamInternalPlay::write(const void *buffer, int32_t numFrames, |
Phil Burk | 19e990e | 2018-03-22 13:59:34 -0700 | [diff] [blame] | 121 | int64_t timeoutNanoseconds) { |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 122 | return processData((void *)buffer, numFrames, timeoutNanoseconds); |
| 123 | } |
| 124 | |
| 125 | // Write as much data as we can without blocking. |
| 126 | aaudio_result_t AudioStreamInternalPlay::processDataNow(void *buffer, int32_t numFrames, |
| 127 | int64_t currentNanoTime, int64_t *wakeTimePtr) { |
| 128 | aaudio_result_t result = processCommands(); |
| 129 | if (result != AAUDIO_OK) { |
| 130 | return result; |
| 131 | } |
| 132 | |
Phil Burk | fd34a93 | 2017-07-19 07:03:52 -0700 | [diff] [blame] | 133 | const char *traceName = "aaWrNow"; |
| 134 | ATRACE_BEGIN(traceName); |
| 135 | |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 136 | if (mClockModel.isStarting()) { |
| 137 | // Still haven't got any timestamps from server. |
| 138 | // Keep waiting until we get some valid timestamps then start writing to the |
| 139 | // current buffer position. |
Phil Burk | 55e5eab | 2018-04-10 15:16:38 -0700 | [diff] [blame] | 140 | ALOGV("%s() wait for valid timestamps", __func__); |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 141 | // Sleep very briefly and hope we get a timestamp soon. |
| 142 | *wakeTimePtr = currentNanoTime + (2000 * AAUDIO_NANOS_PER_MICROSECOND); |
| 143 | ATRACE_END(); |
| 144 | return 0; |
| 145 | } |
| 146 | // If we have gotten this far then we have at least one timestamp from server. |
| 147 | |
Phil Burk | fd34a93 | 2017-07-19 07:03:52 -0700 | [diff] [blame] | 148 | // If a DMA channel or DSP is reading the other end then we have to update the readCounter. |
Phil Burk | 5edc4ea | 2020-04-17 08:15:42 -0700 | [diff] [blame] | 149 | if (mAudioEndpoint->isFreeRunning()) { |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 150 | // Update data queue based on the timing model. |
| 151 | int64_t estimatedReadCounter = mClockModel.convertTimeToPosition(currentNanoTime); |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 152 | // ALOGD("AudioStreamInternal::processDataNow() - estimatedReadCounter = %d", (int)estimatedReadCounter); |
Phil Burk | 5edc4ea | 2020-04-17 08:15:42 -0700 | [diff] [blame] | 153 | mAudioEndpoint->setDataReadCounter(estimatedReadCounter); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 154 | } |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 155 | |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 156 | if (mNeedCatchUp.isRequested()) { |
| 157 | // Catch an MMAP pointer that is already advancing. |
| 158 | // This will avoid initial underruns caused by a slow cold start. |
Phil Burk | ec8ca52 | 2020-05-19 10:05:58 -0700 | [diff] [blame] | 159 | // We add a one burst margin in case the DSP advances before we can write the data. |
| 160 | // This can help prevent the beginning of the stream from being skipped. |
| 161 | advanceClientToMatchServerPosition(getFramesPerBurst()); |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 162 | mNeedCatchUp.acknowledge(); |
| 163 | } |
| 164 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 165 | // If the read index passed the write index then consider it an underrun. |
Phil Burk | 2329638 | 2017-11-20 15:45:11 -0800 | [diff] [blame] | 166 | // For shared streams, the xRunCount is passed up from the service. |
Phil Burk | 5edc4ea | 2020-04-17 08:15:42 -0700 | [diff] [blame] | 167 | if (mAudioEndpoint->isFreeRunning() && mAudioEndpoint->getFullFramesAvailable() < 0) { |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 168 | mXRunCount++; |
Phil Burk | fd34a93 | 2017-07-19 07:03:52 -0700 | [diff] [blame] | 169 | if (ATRACE_ENABLED()) { |
| 170 | ATRACE_INT("aaUnderRuns", mXRunCount); |
| 171 | } |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | // Write some data to the buffer. |
| 175 | //ALOGD("AudioStreamInternal::processDataNow() - writeNowWithConversion(%d)", numFrames); |
| 176 | int32_t framesWritten = writeNowWithConversion(buffer, numFrames); |
| 177 | //ALOGD("AudioStreamInternal::processDataNow() - tried to write %d frames, wrote %d", |
| 178 | // numFrames, framesWritten); |
Phil Burk | fd34a93 | 2017-07-19 07:03:52 -0700 | [diff] [blame] | 179 | if (ATRACE_ENABLED()) { |
| 180 | ATRACE_INT("aaWrote", framesWritten); |
| 181 | } |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 182 | |
Phil Burk | 8d4f006 | 2019-10-03 15:55:41 -0700 | [diff] [blame] | 183 | // Sleep if there is too much data in the buffer. |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 184 | // Calculate an ideal time to wake up. |
Phil Burk | 8d4f006 | 2019-10-03 15:55:41 -0700 | [diff] [blame] | 185 | if (wakeTimePtr != nullptr |
Phil Burk | 5edc4ea | 2020-04-17 08:15:42 -0700 | [diff] [blame] | 186 | && (mAudioEndpoint->getFullFramesAvailable() >= getBufferSize())) { |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 187 | // By default wake up a few milliseconds from now. // TODO review |
| 188 | int64_t wakeTime = currentNanoTime + (1 * AAUDIO_NANOS_PER_MILLISECOND); |
| 189 | aaudio_stream_state_t state = getState(); |
| 190 | //ALOGD("AudioStreamInternal::processDataNow() - wakeTime based on %s", |
| 191 | // AAudio_convertStreamStateToText(state)); |
| 192 | switch (state) { |
| 193 | case AAUDIO_STREAM_STATE_OPEN: |
| 194 | case AAUDIO_STREAM_STATE_STARTING: |
| 195 | if (framesWritten != 0) { |
| 196 | // Don't wait to write more data. Just prime the buffer. |
| 197 | wakeTime = currentNanoTime; |
| 198 | } |
| 199 | break; |
Phil Burk | fd34a93 | 2017-07-19 07:03:52 -0700 | [diff] [blame] | 200 | case AAUDIO_STREAM_STATE_STARTED: |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 201 | { |
Phil Burk | 8d4f006 | 2019-10-03 15:55:41 -0700 | [diff] [blame] | 202 | // Sleep until the readCounter catches up and we only have |
| 203 | // the getBufferSize() frames of data sitting in the buffer. |
Phil Burk | 5edc4ea | 2020-04-17 08:15:42 -0700 | [diff] [blame] | 204 | int64_t nextReadPosition = mAudioEndpoint->getDataWriteCounter() - getBufferSize(); |
Phil Burk | 8d4f006 | 2019-10-03 15:55:41 -0700 | [diff] [blame] | 205 | wakeTime = mClockModel.convertPositionToTime(nextReadPosition); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 206 | } |
| 207 | break; |
| 208 | default: |
| 209 | break; |
| 210 | } |
| 211 | *wakeTimePtr = wakeTime; |
| 212 | |
| 213 | } |
Phil Burk | fd34a93 | 2017-07-19 07:03:52 -0700 | [diff] [blame] | 214 | |
| 215 | ATRACE_END(); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 216 | return framesWritten; |
| 217 | } |
| 218 | |
| 219 | |
| 220 | aaudio_result_t AudioStreamInternalPlay::writeNowWithConversion(const void *buffer, |
| 221 | int32_t numFrames) { |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 222 | WrappingBuffer wrappingBuffer; |
Phil Burk | 41f19d8 | 2018-02-13 14:59:10 -0800 | [diff] [blame] | 223 | uint8_t *byteBuffer = (uint8_t *) buffer; |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 224 | int32_t framesLeft = numFrames; |
| 225 | |
Phil Burk | 5edc4ea | 2020-04-17 08:15:42 -0700 | [diff] [blame] | 226 | mAudioEndpoint->getEmptyFramesAvailable(&wrappingBuffer); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 227 | |
Phil Burk | fd34a93 | 2017-07-19 07:03:52 -0700 | [diff] [blame] | 228 | // Write data in one or two parts. |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 229 | int partIndex = 0; |
| 230 | while (framesLeft > 0 && partIndex < WrappingBuffer::SIZE) { |
| 231 | int32_t framesToWrite = framesLeft; |
| 232 | int32_t framesAvailable = wrappingBuffer.numFrames[partIndex]; |
| 233 | if (framesAvailable > 0) { |
| 234 | if (framesToWrite > framesAvailable) { |
| 235 | framesToWrite = framesAvailable; |
| 236 | } |
Phil Burk | 41f19d8 | 2018-02-13 14:59:10 -0800 | [diff] [blame] | 237 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 238 | int32_t numBytes = getBytesPerFrame() * framesToWrite; |
Phil Burk | 41f19d8 | 2018-02-13 14:59:10 -0800 | [diff] [blame] | 239 | |
Phil Burk | 0127c1b | 2018-03-29 13:48:06 -0700 | [diff] [blame] | 240 | mFlowGraph.process((void *)byteBuffer, |
| 241 | wrappingBuffer.data[partIndex], |
| 242 | framesToWrite); |
Phil Burk | 41f19d8 | 2018-02-13 14:59:10 -0800 | [diff] [blame] | 243 | |
| 244 | byteBuffer += numBytes; |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 245 | framesLeft -= framesToWrite; |
| 246 | } else { |
| 247 | break; |
| 248 | } |
| 249 | partIndex++; |
| 250 | } |
| 251 | int32_t framesWritten = numFrames - framesLeft; |
Phil Burk | 5edc4ea | 2020-04-17 08:15:42 -0700 | [diff] [blame] | 252 | mAudioEndpoint->advanceWriteIndex(framesWritten); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 253 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 254 | return framesWritten; |
| 255 | } |
| 256 | |
Phil Burk | 377c1c2 | 2018-12-12 16:06:54 -0800 | [diff] [blame] | 257 | int64_t AudioStreamInternalPlay::getFramesRead() { |
Phil Burk | 5edc4ea | 2020-04-17 08:15:42 -0700 | [diff] [blame] | 258 | if (mAudioEndpoint) { |
| 259 | const int64_t framesReadHardware = isClockModelInControl() |
| 260 | ? mClockModel.convertTimeToPosition(AudioClock::getNanoseconds()) |
| 261 | : mAudioEndpoint->getDataReadCounter(); |
| 262 | // Add service offset and prevent retrograde motion. |
| 263 | mLastFramesRead = std::max(mLastFramesRead, framesReadHardware + mFramesOffsetFromService); |
| 264 | } |
Phil Burk | 377c1c2 | 2018-12-12 16:06:54 -0800 | [diff] [blame] | 265 | return mLastFramesRead; |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 266 | } |
| 267 | |
Phil Burk | 377c1c2 | 2018-12-12 16:06:54 -0800 | [diff] [blame] | 268 | int64_t AudioStreamInternalPlay::getFramesWritten() { |
Phil Burk | 5edc4ea | 2020-04-17 08:15:42 -0700 | [diff] [blame] | 269 | if (mAudioEndpoint) { |
| 270 | mLastFramesWritten = mAudioEndpoint->getDataWriteCounter() |
| 271 | + mFramesOffsetFromService; |
| 272 | } |
| 273 | return mLastFramesWritten; |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | |
| 277 | // Render audio in the application callback and then write the data to the stream. |
| 278 | void *AudioStreamInternalPlay::callbackLoop() { |
Phil Burk | 19e990e | 2018-03-22 13:59:34 -0700 | [diff] [blame] | 279 | ALOGD("%s() entering >>>>>>>>>>>>>>>", __func__); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 280 | aaudio_result_t result = AAUDIO_OK; |
| 281 | aaudio_data_callback_result_t callbackResult = AAUDIO_CALLBACK_RESULT_CONTINUE; |
Phil Burk | 134f197 | 2017-12-08 13:06:11 -0800 | [diff] [blame] | 282 | if (!isDataCallbackSet()) return NULL; |
Phil Burk | fd34a93 | 2017-07-19 07:03:52 -0700 | [diff] [blame] | 283 | int64_t timeoutNanos = calculateReasonableTimeout(mCallbackFrames); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 284 | |
| 285 | // result might be a frame count |
| 286 | while (mCallbackEnabled.load() && isActive() && (result >= 0)) { |
| 287 | // Call application using the AAudio callback interface. |
Phil Burk | bf821e2 | 2020-04-17 11:51:43 -0700 | [diff] [blame] | 288 | callbackResult = maybeCallDataCallback(mCallbackBuffer.get(), mCallbackFrames); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 289 | |
| 290 | if (callbackResult == AAUDIO_CALLBACK_RESULT_CONTINUE) { |
Phil Burk | fd34a93 | 2017-07-19 07:03:52 -0700 | [diff] [blame] | 291 | // Write audio data to stream. This is a BLOCKING WRITE! |
Phil Burk | bf821e2 | 2020-04-17 11:51:43 -0700 | [diff] [blame] | 292 | result = write(mCallbackBuffer.get(), mCallbackFrames, timeoutNanos); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 293 | if ((result != mCallbackFrames)) { |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 294 | if (result >= 0) { |
| 295 | // Only wrote some of the frames requested. Must have timed out. |
| 296 | result = AAUDIO_ERROR_TIMEOUT; |
| 297 | } |
Phil Burk | 134f197 | 2017-12-08 13:06:11 -0800 | [diff] [blame] | 298 | maybeCallErrorCallback(result); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 299 | break; |
| 300 | } |
| 301 | } else if (callbackResult == AAUDIO_CALLBACK_RESULT_STOP) { |
Phil Burk | 762365c | 2018-12-10 16:02:16 -0800 | [diff] [blame] | 302 | ALOGD("%s(): callback returned AAUDIO_CALLBACK_RESULT_STOP", __func__); |
Phil Burk | 1e83bee | 2018-12-17 14:15:20 -0800 | [diff] [blame] | 303 | result = systemStopFromCallback(); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 304 | break; |
| 305 | } |
| 306 | } |
| 307 | |
Phil Burk | 19e990e | 2018-03-22 13:59:34 -0700 | [diff] [blame] | 308 | ALOGD("%s() exiting, result = %d, isActive() = %d <<<<<<<<<<<<<<", |
| 309 | __func__, result, (int) isActive()); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 310 | return NULL; |
| 311 | } |
Phil Burk | 965650e | 2017-09-07 21:00:09 -0700 | [diff] [blame] | 312 | |
| 313 | //------------------------------------------------------------------------------ |
| 314 | // Implementation of PlayerBase |
| 315 | status_t AudioStreamInternalPlay::doSetVolume() { |
Phil Burk | 55e5eab | 2018-04-10 15:16:38 -0700 | [diff] [blame] | 316 | float combinedVolume = mStreamVolume * getDuckAndMuteVolume(); |
| 317 | ALOGD("%s() mStreamVolume * duckAndMuteVolume = %f * %f = %f", |
| 318 | __func__, mStreamVolume, getDuckAndMuteVolume(), combinedVolume); |
Phil Burk | 0127c1b | 2018-03-29 13:48:06 -0700 | [diff] [blame] | 319 | mFlowGraph.setTargetVolume(combinedVolume); |
Phil Burk | 965650e | 2017-09-07 21:00:09 -0700 | [diff] [blame] | 320 | return android::NO_ERROR; |
| 321 | } |