blob: 5d4c3d41c177e1bb630693c1cbcdf0ac8d784c13 [file] [log] [blame]
Phil Burk87c9f642017-05-17 07:22:39 -07001/*
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 Burk87c9f642017-05-17 07:22:39 -070017//#define LOG_NDEBUG 0
18#include <utils/Log.h>
19
Phil Burkfd34a932017-07-19 07:03:52 -070020#define ATRACE_TAG ATRACE_TAG_AUDIO
21
jiabin97247ea2021-04-07 00:33:38 +000022#include <media/MediaMetricsItem.h>
Phil Burkfd34a932017-07-19 07:03:52 -070023#include <utils/Trace.h>
24
Phil Burk87c9f642017-05-17 07:22:39 -070025#include "client/AudioStreamInternalPlay.h"
26#include "utility/AudioClock.h"
27
Phil Burk58f5ce12020-08-12 14:29:10 +000028// 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-Tsvic5f45872020-08-18 10:39:44 -070036using android::status_t;
Phil Burk87c9f642017-05-17 07:22:39 -070037using android::WrappingBuffer;
38
39using namespace aaudio;
40
41AudioStreamInternalPlay::AudioStreamInternalPlay(AAudioServiceInterface &serviceInterface,
42 bool inService)
43 : AudioStreamInternal(serviceInterface, inService) {
44
45}
46
Phil Burk02fec702018-02-16 18:25:55 -080047constexpr int kRampMSec = 10; // time to apply a change in volume
48
49aaudio_result_t AudioStreamInternalPlay::open(const AudioStreamBuilder &builder) {
50 aaudio_result_t result = AudioStreamInternal::open(builder);
Robert Wud559ba52023-06-29 00:08:51 +000051 const bool useVolumeRamps = (getSharingMode() == AAUDIO_SHARING_MODE_EXCLUSIVE);
Phil Burk02fec702018-02-16 18:25:55 -080052 if (result == AAUDIO_OK) {
Phil Burk0127c1b2018-03-29 13:48:06 -070053 result = mFlowGraph.configure(getFormat(),
54 getSamplesPerFrame(),
Robert Wud559ba52023-06-29 00:08:51 +000055 getSampleRate(),
Phil Burk0127c1b2018-03-29 13:48:06 -070056 getDeviceFormat(),
Robert Wue8b58962023-07-21 19:48:56 +000057 getDeviceSamplesPerFrame(),
Robert Wud559ba52023-06-29 00:08:51 +000058 getDeviceSampleRate(),
Robert Wu8393bed2021-12-08 02:08:48 +000059 getRequireMonoBlend(),
Robert Wud559ba52023-06-29 00:08:51 +000060 useVolumeRamps,
Robert Wub7e30fa2021-12-09 01:00:16 +000061 getAudioBalance(),
Robert Wud559ba52023-06-29 00:08:51 +000062 aaudio::resampler::MultiChannelResampler::Quality::Medium);
Phil Burk0127c1b2018-03-29 13:48:06 -070063
64 if (result != AAUDIO_OK) {
Phil Burkdd582922020-10-15 20:29:51 +000065 safeReleaseClose();
Phil Burk0127c1b2018-03-29 13:48:06 -070066 }
Phil Burk02fec702018-02-16 18:25:55 -080067 // Sample rate is constrained to common values by now and should not overflow.
68 int32_t numFrames = kRampMSec * getSampleRate() / AAUDIO_MILLIS_PER_SECOND;
Phil Burk0127c1b2018-03-29 13:48:06 -070069 mFlowGraph.setRampLengthInFrames(numFrames);
Phil Burk02fec702018-02-16 18:25:55 -080070 }
71 return result;
72}
73
Phil Burk13d3d832019-06-10 14:36:48 -070074// This must be called under mStreamLock.
Phil Burkdd582922020-10-15 20:29:51 +000075aaudio_result_t AudioStreamInternalPlay::requestPause_l()
Phil Burkb336e892017-07-05 15:35:43 -070076{
Phil Burkdd582922020-10-15 20:29:51 +000077 aaudio_result_t result = stopCallback_l();
Phil Burk5cc83c32017-11-28 15:43:18 -080078 if (result != AAUDIO_OK) {
79 return result;
80 }
jiabin5f787812023-03-02 20:42:43 +000081 if (getServiceHandle() == AAUDIO_HANDLE_INVALID) {
Phil Burk29ccc292019-04-15 08:58:08 -070082 ALOGW("%s() mServiceStreamHandle invalid", __func__);
Phil Burkb336e892017-07-05 15:35:43 -070083 return AAUDIO_ERROR_INVALID_STATE;
84 }
85
86 mClockModel.stop(AudioClock::getNanoseconds());
87 setState(AAUDIO_STREAM_STATE_PAUSING);
Phil Burka53ffa62018-10-10 16:21:37 -070088 mAtomicInternalTimestamp.clear();
jiabin5f787812023-03-02 20:42:43 +000089 return mServiceInterface.pauseStream(mServiceStreamHandleInfo);
Phil Burkb336e892017-07-05 15:35:43 -070090}
91
Phil Burkdd582922020-10-15 20:29:51 +000092aaudio_result_t AudioStreamInternalPlay::requestFlush_l() {
jiabin5f787812023-03-02 20:42:43 +000093 if (getServiceHandle() == AAUDIO_HANDLE_INVALID) {
Phil Burk29ccc292019-04-15 08:58:08 -070094 ALOGW("%s() mServiceStreamHandle invalid", __func__);
Phil Burkb336e892017-07-05 15:35:43 -070095 return AAUDIO_ERROR_INVALID_STATE;
96 }
97
98 setState(AAUDIO_STREAM_STATE_FLUSHING);
jiabin5f787812023-03-02 20:42:43 +000099 return mServiceInterface.flushStream(mServiceStreamHandleInfo);
Phil Burkb336e892017-07-05 15:35:43 -0700100}
101
Phil Burkec8ca522020-05-19 10:05:58 -0700102void AudioStreamInternalPlay::prepareBuffersForStart() {
Phil Burk48abde82024-01-03 00:58:40 +0000103 // Reset volume ramps to avoid a starting noise.
104 // This was called here instead of AudioStreamInternal so that
105 // it will be easier to backport.
106 mFlowGraph.reset();
Phil Burkec8ca522020-05-19 10:05:58 -0700107 // Prevent stale data from being played.
108 mAudioEndpoint->eraseDataMemory();
109}
110
111void AudioStreamInternalPlay::advanceClientToMatchServerPosition(int32_t serverMargin) {
112 int64_t readCounter = mAudioEndpoint->getDataReadCounter() + serverMargin;
Phil Burk5edc4ea2020-04-17 08:15:42 -0700113 int64_t writeCounter = mAudioEndpoint->getDataWriteCounter();
Phil Burkb336e892017-07-05 15:35:43 -0700114
115 // Bump offset so caller does not see the retrograde motion in getFramesRead().
Phil Burkbcc36742017-08-31 17:24:51 -0700116 int64_t offset = writeCounter - readCounter;
117 mFramesOffsetFromService += offset;
Phil Burk19e990e2018-03-22 13:59:34 -0700118 ALOGV("%s() readN = %lld, writeN = %lld, offset = %lld", __func__,
Phil Burkb336e892017-07-05 15:35:43 -0700119 (long long)readCounter, (long long)writeCounter, (long long)mFramesOffsetFromService);
120
Phil Burkbcc36742017-08-31 17:24:51 -0700121 // Force writeCounter to match readCounter.
122 // This is because we cannot change the read counter in the hardware.
Phil Burk5edc4ea2020-04-17 08:15:42 -0700123 mAudioEndpoint->setDataWriteCounter(readCounter);
Phil Burkb336e892017-07-05 15:35:43 -0700124}
125
Phil Burkbcc36742017-08-31 17:24:51 -0700126void AudioStreamInternalPlay::onFlushFromServer() {
jiabind5bd06a2021-04-27 22:04:08 +0000127 advanceClientToMatchServerPosition(0 /*serverMargin*/);
Phil Burkbcc36742017-08-31 17:24:51 -0700128}
129
Phil Burk87c9f642017-05-17 07:22:39 -0700130// Write the data, block if needed and timeoutMillis > 0
131aaudio_result_t AudioStreamInternalPlay::write(const void *buffer, int32_t numFrames,
Phil Burk19e990e2018-03-22 13:59:34 -0700132 int64_t timeoutNanoseconds) {
Phil Burk87c9f642017-05-17 07:22:39 -0700133 return processData((void *)buffer, numFrames, timeoutNanoseconds);
134}
135
136// Write as much data as we can without blocking.
137aaudio_result_t AudioStreamInternalPlay::processDataNow(void *buffer, int32_t numFrames,
138 int64_t currentNanoTime, int64_t *wakeTimePtr) {
139 aaudio_result_t result = processCommands();
140 if (result != AAUDIO_OK) {
141 return result;
142 }
143
Phil Burkfd34a932017-07-19 07:03:52 -0700144 const char *traceName = "aaWrNow";
145 ATRACE_BEGIN(traceName);
146
Phil Burkbcc36742017-08-31 17:24:51 -0700147 if (mClockModel.isStarting()) {
148 // Still haven't got any timestamps from server.
149 // Keep waiting until we get some valid timestamps then start writing to the
150 // current buffer position.
Phil Burk55e5eab2018-04-10 15:16:38 -0700151 ALOGV("%s() wait for valid timestamps", __func__);
Phil Burkbcc36742017-08-31 17:24:51 -0700152 // Sleep very briefly and hope we get a timestamp soon.
153 *wakeTimePtr = currentNanoTime + (2000 * AAUDIO_NANOS_PER_MICROSECOND);
154 ATRACE_END();
155 return 0;
156 }
157 // If we have gotten this far then we have at least one timestamp from server.
158
Phil Burkfd34a932017-07-19 07:03:52 -0700159 // If a DMA channel or DSP is reading the other end then we have to update the readCounter.
Phil Burk5edc4ea2020-04-17 08:15:42 -0700160 if (mAudioEndpoint->isFreeRunning()) {
Phil Burk87c9f642017-05-17 07:22:39 -0700161 // Update data queue based on the timing model.
162 int64_t estimatedReadCounter = mClockModel.convertTimeToPosition(currentNanoTime);
Phil Burkec89b2e2017-06-20 15:05:06 -0700163 // ALOGD("AudioStreamInternal::processDataNow() - estimatedReadCounter = %d", (int)estimatedReadCounter);
Phil Burk5edc4ea2020-04-17 08:15:42 -0700164 mAudioEndpoint->setDataReadCounter(estimatedReadCounter);
Phil Burk87c9f642017-05-17 07:22:39 -0700165 }
Phil Burk87c9f642017-05-17 07:22:39 -0700166
Phil Burkbcc36742017-08-31 17:24:51 -0700167 if (mNeedCatchUp.isRequested()) {
168 // Catch an MMAP pointer that is already advancing.
169 // This will avoid initial underruns caused by a slow cold start.
Phil Burkec8ca522020-05-19 10:05:58 -0700170 // We add a one burst margin in case the DSP advances before we can write the data.
171 // This can help prevent the beginning of the stream from being skipped.
172 advanceClientToMatchServerPosition(getFramesPerBurst());
Phil Burkbcc36742017-08-31 17:24:51 -0700173 mNeedCatchUp.acknowledge();
174 }
175
Phil Burk87c9f642017-05-17 07:22:39 -0700176 // If the read index passed the write index then consider it an underrun.
Phil Burk23296382017-11-20 15:45:11 -0800177 // For shared streams, the xRunCount is passed up from the service.
Phil Burk5edc4ea2020-04-17 08:15:42 -0700178 if (mAudioEndpoint->isFreeRunning() && mAudioEndpoint->getFullFramesAvailable() < 0) {
Phil Burk87c9f642017-05-17 07:22:39 -0700179 mXRunCount++;
Phil Burkfd34a932017-07-19 07:03:52 -0700180 if (ATRACE_ENABLED()) {
181 ATRACE_INT("aaUnderRuns", mXRunCount);
182 }
Phil Burk87c9f642017-05-17 07:22:39 -0700183 }
184
185 // Write some data to the buffer.
186 //ALOGD("AudioStreamInternal::processDataNow() - writeNowWithConversion(%d)", numFrames);
187 int32_t framesWritten = writeNowWithConversion(buffer, numFrames);
188 //ALOGD("AudioStreamInternal::processDataNow() - tried to write %d frames, wrote %d",
189 // numFrames, framesWritten);
Phil Burkfd34a932017-07-19 07:03:52 -0700190 if (ATRACE_ENABLED()) {
191 ATRACE_INT("aaWrote", framesWritten);
192 }
Phil Burk87c9f642017-05-17 07:22:39 -0700193
Phil Burk8d4f0062019-10-03 15:55:41 -0700194 // Sleep if there is too much data in the buffer.
Phil Burk87c9f642017-05-17 07:22:39 -0700195 // Calculate an ideal time to wake up.
Phil Burk8d4f0062019-10-03 15:55:41 -0700196 if (wakeTimePtr != nullptr
Robert Wud559ba52023-06-29 00:08:51 +0000197 && (mAudioEndpoint->getFullFramesAvailable() >= getDeviceBufferSize())) {
Phil Burk87c9f642017-05-17 07:22:39 -0700198 // By default wake up a few milliseconds from now. // TODO review
199 int64_t wakeTime = currentNanoTime + (1 * AAUDIO_NANOS_PER_MILLISECOND);
200 aaudio_stream_state_t state = getState();
201 //ALOGD("AudioStreamInternal::processDataNow() - wakeTime based on %s",
202 // AAudio_convertStreamStateToText(state));
203 switch (state) {
204 case AAUDIO_STREAM_STATE_OPEN:
205 case AAUDIO_STREAM_STATE_STARTING:
206 if (framesWritten != 0) {
207 // Don't wait to write more data. Just prime the buffer.
208 wakeTime = currentNanoTime;
209 }
210 break;
Phil Burkfd34a932017-07-19 07:03:52 -0700211 case AAUDIO_STREAM_STATE_STARTED:
Phil Burk87c9f642017-05-17 07:22:39 -0700212 {
Phil Burkec21f2b2022-04-19 18:52:03 +0000213 // Calculate when there will be room available to write to the buffer.
214 // If the appBufferSize is smaller than the endpointBufferSize then
215 // we will have room to write data beyond the appBufferSize.
216 // That is a technique used to reduce glitches without adding latency.
Robert Wud559ba52023-06-29 00:08:51 +0000217 const int64_t appBufferSize = getDeviceBufferSize();
Phil Burkec21f2b2022-04-19 18:52:03 +0000218 // The endpoint buffer size is set to the maximum that can be written.
219 // If we use it then we must carve out some room to write data when we wake up.
Robert Wud559ba52023-06-29 00:08:51 +0000220 const int64_t endBufferSize = mAudioEndpoint->getBufferSizeInFrames()
221 - getDeviceFramesPerBurst();
222 const int64_t bestBufferSize = std::min(appBufferSize, endBufferSize);
Phil Burkec21f2b2022-04-19 18:52:03 +0000223 int64_t targetReadPosition = mAudioEndpoint->getDataWriteCounter() - bestBufferSize;
224 wakeTime = mClockModel.convertPositionToTime(targetReadPosition);
Phil Burk87c9f642017-05-17 07:22:39 -0700225 }
226 break;
227 default:
228 break;
229 }
230 *wakeTimePtr = wakeTime;
231
232 }
Phil Burkfd34a932017-07-19 07:03:52 -0700233
234 ATRACE_END();
Phil Burk87c9f642017-05-17 07:22:39 -0700235 return framesWritten;
236}
237
238
239aaudio_result_t AudioStreamInternalPlay::writeNowWithConversion(const void *buffer,
240 int32_t numFrames) {
Phil Burk87c9f642017-05-17 07:22:39 -0700241 WrappingBuffer wrappingBuffer;
Phil Burk41f19d82018-02-13 14:59:10 -0800242 uint8_t *byteBuffer = (uint8_t *) buffer;
Robert Wud559ba52023-06-29 00:08:51 +0000243 int32_t framesLeftInByteBuffer = numFrames;
Phil Burk87c9f642017-05-17 07:22:39 -0700244
Phil Burk5edc4ea2020-04-17 08:15:42 -0700245 mAudioEndpoint->getEmptyFramesAvailable(&wrappingBuffer);
Phil Burk87c9f642017-05-17 07:22:39 -0700246
Phil Burkfd34a932017-07-19 07:03:52 -0700247 // Write data in one or two parts.
Phil Burk87c9f642017-05-17 07:22:39 -0700248 int partIndex = 0;
Robert Wud559ba52023-06-29 00:08:51 +0000249 int framesWrittenToAudioEndpoint = 0;
250 while (framesLeftInByteBuffer > 0 && partIndex < WrappingBuffer::SIZE) {
251 int32_t framesAvailableInWrappingBuffer = wrappingBuffer.numFrames[partIndex];
252 uint8_t *currentWrappingBuffer = (uint8_t *) wrappingBuffer.data[partIndex];
Phil Burk41f19d82018-02-13 14:59:10 -0800253
Robert Wud559ba52023-06-29 00:08:51 +0000254 if (framesAvailableInWrappingBuffer > 0) {
255 // Pull data from the flowgraph in case there is residual data.
256 const int32_t framesActuallyWrittenToWrappingBuffer = mFlowGraph.pull(
257 (void*) currentWrappingBuffer,
258 framesAvailableInWrappingBuffer);
Phil Burk41f19d82018-02-13 14:59:10 -0800259
Robert Wud559ba52023-06-29 00:08:51 +0000260 const int32_t numBytesActuallyWrittenToWrappingBuffer =
261 framesActuallyWrittenToWrappingBuffer * getBytesPerDeviceFrame();
262 currentWrappingBuffer += numBytesActuallyWrittenToWrappingBuffer;
263 framesAvailableInWrappingBuffer -= framesActuallyWrittenToWrappingBuffer;
264 framesWrittenToAudioEndpoint += framesActuallyWrittenToWrappingBuffer;
Robert Wu6641f9d2023-11-11 00:30:56 +0000265 } else {
266 break;
Robert Wud559ba52023-06-29 00:08:51 +0000267 }
Phil Burk41f19d82018-02-13 14:59:10 -0800268
Robert Wud559ba52023-06-29 00:08:51 +0000269 // Put data from byteBuffer into the flowgraph one buffer (8 frames) at a time.
270 // Continuously pull as much data as possible from the flowgraph into the wrapping buffer.
271 // The return value of mFlowGraph.process is the number of frames actually pulled.
272 while (framesAvailableInWrappingBuffer > 0 && framesLeftInByteBuffer > 0) {
Robert Wu6641f9d2023-11-11 00:30:56 +0000273 int32_t framesToWriteFromByteBuffer = std::min(flowgraph::kDefaultBufferSize,
Robert Wud559ba52023-06-29 00:08:51 +0000274 framesLeftInByteBuffer);
Robert Wu6641f9d2023-11-11 00:30:56 +0000275 // If the wrapping buffer is running low, write one frame at a time.
276 if (framesAvailableInWrappingBuffer < flowgraph::kDefaultBufferSize) {
277 framesToWriteFromByteBuffer = 1;
278 }
Robert Wud559ba52023-06-29 00:08:51 +0000279
280 const int32_t numBytesToWriteFromByteBuffer = getBytesPerFrame() *
281 framesToWriteFromByteBuffer;
282
283 //ALOGD("%s() framesLeftInByteBuffer %d, framesAvailableInWrappingBuffer %d"
284 // "framesToWriteFromByteBuffer %d, numBytesToWriteFromByteBuffer %d"
285 // , __func__, framesLeftInByteBuffer, framesAvailableInWrappingBuffer,
286 // framesToWriteFromByteBuffer, numBytesToWriteFromByteBuffer);
287
288 const int32_t framesActuallyWrittenToWrappingBuffer = mFlowGraph.process(
289 (void *)byteBuffer,
290 framesToWriteFromByteBuffer,
291 (void *)currentWrappingBuffer,
292 framesAvailableInWrappingBuffer);
293
294 byteBuffer += numBytesToWriteFromByteBuffer;
295 framesLeftInByteBuffer -= framesToWriteFromByteBuffer;
296 const int32_t numBytesActuallyWrittenToWrappingBuffer =
297 framesActuallyWrittenToWrappingBuffer * getBytesPerDeviceFrame();
298 currentWrappingBuffer += numBytesActuallyWrittenToWrappingBuffer;
299 framesAvailableInWrappingBuffer -= framesActuallyWrittenToWrappingBuffer;
300 framesWrittenToAudioEndpoint += framesActuallyWrittenToWrappingBuffer;
301
302 //ALOGD("%s() numBytesActuallyWrittenToWrappingBuffer %d, framesLeftInByteBuffer %d"
303 // "framesActuallyWrittenToWrappingBuffer %d, numBytesToWriteFromByteBuffer %d"
304 // "framesWrittenToAudioEndpoint %d"
305 // , __func__, numBytesActuallyWrittenToWrappingBuffer, framesLeftInByteBuffer,
306 // framesActuallyWrittenToWrappingBuffer, numBytesToWriteFromByteBuffer,
307 // framesWrittenToAudioEndpoint);
Phil Burk87c9f642017-05-17 07:22:39 -0700308 }
309 partIndex++;
310 }
Robert Wud559ba52023-06-29 00:08:51 +0000311 //ALOGD("%s() framesWrittenToAudioEndpoint %d, numFrames %d"
312 // "framesLeftInByteBuffer %d"
313 // , __func__, framesWrittenToAudioEndpoint, numFrames,
314 // framesLeftInByteBuffer);
Phil Burk87c9f642017-05-17 07:22:39 -0700315
Robert Wud559ba52023-06-29 00:08:51 +0000316 // The audio endpoint should reference the number of frames written to the wrapping buffer.
317 mAudioEndpoint->advanceWriteIndex(framesWrittenToAudioEndpoint);
318
319 // The internal code should use the number of frames read from the app.
320 return numFrames - framesLeftInByteBuffer;
Phil Burk87c9f642017-05-17 07:22:39 -0700321}
322
Phil Burk377c1c22018-12-12 16:06:54 -0800323int64_t AudioStreamInternalPlay::getFramesRead() {
Phil Burk5edc4ea2020-04-17 08:15:42 -0700324 if (mAudioEndpoint) {
325 const int64_t framesReadHardware = isClockModelInControl()
326 ? mClockModel.convertTimeToPosition(AudioClock::getNanoseconds())
327 : mAudioEndpoint->getDataReadCounter();
328 // Add service offset and prevent retrograde motion.
329 mLastFramesRead = std::max(mLastFramesRead, framesReadHardware + mFramesOffsetFromService);
330 }
Phil Burk377c1c22018-12-12 16:06:54 -0800331 return mLastFramesRead;
Phil Burk87c9f642017-05-17 07:22:39 -0700332}
333
Phil Burk377c1c22018-12-12 16:06:54 -0800334int64_t AudioStreamInternalPlay::getFramesWritten() {
Phil Burk5edc4ea2020-04-17 08:15:42 -0700335 if (mAudioEndpoint) {
jiabinf86a0042023-12-08 00:15:51 +0000336 mLastFramesWritten = std::max(
337 mLastFramesWritten,
338 mAudioEndpoint->getDataWriteCounter() + mFramesOffsetFromService);
Phil Burk5edc4ea2020-04-17 08:15:42 -0700339 }
340 return mLastFramesWritten;
Phil Burk87c9f642017-05-17 07:22:39 -0700341}
342
Phil Burk87c9f642017-05-17 07:22:39 -0700343// Render audio in the application callback and then write the data to the stream.
344void *AudioStreamInternalPlay::callbackLoop() {
Phil Burk19e990e2018-03-22 13:59:34 -0700345 ALOGD("%s() entering >>>>>>>>>>>>>>>", __func__);
Phil Burk87c9f642017-05-17 07:22:39 -0700346 aaudio_result_t result = AAUDIO_OK;
347 aaudio_data_callback_result_t callbackResult = AAUDIO_CALLBACK_RESULT_CONTINUE;
jiabind5bd06a2021-04-27 22:04:08 +0000348 if (!isDataCallbackSet()) return nullptr;
Phil Burkfd34a932017-07-19 07:03:52 -0700349 int64_t timeoutNanos = calculateReasonableTimeout(mCallbackFrames);
Phil Burk87c9f642017-05-17 07:22:39 -0700350
351 // result might be a frame count
352 while (mCallbackEnabled.load() && isActive() && (result >= 0)) {
353 // Call application using the AAudio callback interface.
Phil Burkbf821e22020-04-17 11:51:43 -0700354 callbackResult = maybeCallDataCallback(mCallbackBuffer.get(), mCallbackFrames);
Phil Burk87c9f642017-05-17 07:22:39 -0700355
356 if (callbackResult == AAUDIO_CALLBACK_RESULT_CONTINUE) {
Phil Burkfd34a932017-07-19 07:03:52 -0700357 // Write audio data to stream. This is a BLOCKING WRITE!
Phil Burkbf821e22020-04-17 11:51:43 -0700358 result = write(mCallbackBuffer.get(), mCallbackFrames, timeoutNanos);
Phil Burk87c9f642017-05-17 07:22:39 -0700359 if ((result != mCallbackFrames)) {
Phil Burk87c9f642017-05-17 07:22:39 -0700360 if (result >= 0) {
jiabind7ff88a2023-12-04 18:40:26 +0000361 // Only wrote some of the frames requested. The stream can be disconnected
362 // or timed out.
363 processCommands();
364 result = isDisconnected() ? AAUDIO_ERROR_DISCONNECTED : AAUDIO_ERROR_TIMEOUT;
Phil Burk87c9f642017-05-17 07:22:39 -0700365 }
Phil Burk134f1972017-12-08 13:06:11 -0800366 maybeCallErrorCallback(result);
Phil Burk87c9f642017-05-17 07:22:39 -0700367 break;
368 }
369 } else if (callbackResult == AAUDIO_CALLBACK_RESULT_STOP) {
Phil Burk762365c2018-12-10 16:02:16 -0800370 ALOGD("%s(): callback returned AAUDIO_CALLBACK_RESULT_STOP", __func__);
Phil Burk5ff3b952021-04-02 17:29:11 +0000371 result = systemStopInternal();
Phil Burk87c9f642017-05-17 07:22:39 -0700372 break;
373 }
374 }
375
Phil Burk19e990e2018-03-22 13:59:34 -0700376 ALOGD("%s() exiting, result = %d, isActive() = %d <<<<<<<<<<<<<<",
377 __func__, result, (int) isActive());
jiabind5bd06a2021-04-27 22:04:08 +0000378 return nullptr;
Phil Burk87c9f642017-05-17 07:22:39 -0700379}
Phil Burk965650e2017-09-07 21:00:09 -0700380
381//------------------------------------------------------------------------------
382// Implementation of PlayerBase
383status_t AudioStreamInternalPlay::doSetVolume() {
Phil Burk55e5eab2018-04-10 15:16:38 -0700384 float combinedVolume = mStreamVolume * getDuckAndMuteVolume();
385 ALOGD("%s() mStreamVolume * duckAndMuteVolume = %f * %f = %f",
386 __func__, mStreamVolume, getDuckAndMuteVolume(), combinedVolume);
Phil Burk0127c1b2018-03-29 13:48:06 -0700387 mFlowGraph.setTargetVolume(combinedVolume);
Phil Burk965650e2017-09-07 21:00:09 -0700388 return android::NO_ERROR;
389}