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