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