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