blob: 3e82a887650008cb31fbb4dacfec78733216ec3a [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 Burkfbf031e2017-10-12 15:58:31 -070017#define LOG_TAG (mInService ? "AudioStreamInternalCapture_Service" \
18 : "AudioStreamInternalCapture_Client")
Phil Burk87c9f642017-05-17 07:22:39 -070019//#define LOG_NDEBUG 0
20#include <utils/Log.h>
21
Phil Burkec89b2e2017-06-20 15:05:06 -070022#include <algorithm>
Phil Burk87c9f642017-05-17 07:22:39 -070023#include <aaudio/AAudio.h>
24
25#include "client/AudioStreamInternalCapture.h"
26#include "utility/AudioClock.h"
27
Phil Burkfd34a932017-07-19 07:03:52 -070028#define ATRACE_TAG ATRACE_TAG_AUDIO
29#include <utils/Trace.h>
30
Phil Burk87c9f642017-05-17 07:22:39 -070031using android::WrappingBuffer;
32
33using namespace aaudio;
34
35AudioStreamInternalCapture::AudioStreamInternalCapture(AAudioServiceInterface &serviceInterface,
36 bool inService)
37 : AudioStreamInternal(serviceInterface, inService) {
38
39}
40
41AudioStreamInternalCapture::~AudioStreamInternalCapture() {}
42
Phil Burkbcc36742017-08-31 17:24:51 -070043void AudioStreamInternalCapture::advanceClientToMatchServerPosition() {
44 int64_t readCounter = mAudioEndpoint.getDataReadCounter();
45 int64_t writeCounter = mAudioEndpoint.getDataWriteCounter();
46
47 // Bump offset so caller does not see the retrograde motion in getFramesRead().
48 int64_t offset = readCounter - writeCounter;
49 mFramesOffsetFromService += offset;
50 ALOGD("advanceClientToMatchServerPosition() readN = %lld, writeN = %lld, offset = %lld",
51 (long long)readCounter, (long long)writeCounter, (long long)mFramesOffsetFromService);
52
53 // Force readCounter to match writeCounter.
54 // This is because we cannot change the write counter in the hardware.
55 mAudioEndpoint.setDataReadCounter(writeCounter);
56}
57
Phil Burk87c9f642017-05-17 07:22:39 -070058// Write the data, block if needed and timeoutMillis > 0
59aaudio_result_t AudioStreamInternalCapture::read(void *buffer, int32_t numFrames,
60 int64_t timeoutNanoseconds)
61{
62 return processData(buffer, numFrames, timeoutNanoseconds);
63}
64
65// Read as much data as we can without blocking.
66aaudio_result_t AudioStreamInternalCapture::processDataNow(void *buffer, int32_t numFrames,
67 int64_t currentNanoTime, int64_t *wakeTimePtr) {
68 aaudio_result_t result = processCommands();
69 if (result != AAUDIO_OK) {
70 return result;
71 }
72
Phil Burkfd34a932017-07-19 07:03:52 -070073 const char *traceName = "aaRdNow";
74 ATRACE_BEGIN(traceName);
75
Phil Burkbcc36742017-08-31 17:24:51 -070076 if (mClockModel.isStarting()) {
77 // Still haven't got any timestamps from server.
78 // Keep waiting until we get some valid timestamps then start writing to the
79 // current buffer position.
80 ALOGD("processDataNow() wait for valid timestamps");
81 // Sleep very briefly and hope we get a timestamp soon.
82 *wakeTimePtr = currentNanoTime + (2000 * AAUDIO_NANOS_PER_MICROSECOND);
83 ATRACE_END();
84 return 0;
85 }
86 // If we have gotten this far then we have at least one timestamp from server.
87
Phil Burk87c9f642017-05-17 07:22:39 -070088 if (mAudioEndpoint.isFreeRunning()) {
89 //ALOGD("AudioStreamInternalCapture::processDataNow() - update remote counter");
90 // Update data queue based on the timing model.
91 int64_t estimatedRemoteCounter = mClockModel.convertTimeToPosition(currentNanoTime);
92 // TODO refactor, maybe use setRemoteCounter()
93 mAudioEndpoint.setDataWriteCounter(estimatedRemoteCounter);
94 }
95
Phil Burkbcc36742017-08-31 17:24:51 -070096 // This code assumes that we have already received valid timestamps.
97 if (mNeedCatchUp.isRequested()) {
98 // Catch an MMAP pointer that is already advancing.
99 // This will avoid initial underruns caused by a slow cold start.
100 advanceClientToMatchServerPosition();
101 mNeedCatchUp.acknowledge();
102 }
103
Phil Burk87c9f642017-05-17 07:22:39 -0700104 // If the write index passed the read index then consider it an overrun.
Phil Burk23296382017-11-20 15:45:11 -0800105 // For shared streams, the xRunCount is passed up from the service.
106 if (mAudioEndpoint.isFreeRunning() && mAudioEndpoint.getEmptyFramesAvailable() < 0) {
Phil Burk87c9f642017-05-17 07:22:39 -0700107 mXRunCount++;
Phil Burkfd34a932017-07-19 07:03:52 -0700108 if (ATRACE_ENABLED()) {
109 ATRACE_INT("aaOverRuns", mXRunCount);
110 }
Phil Burk87c9f642017-05-17 07:22:39 -0700111 }
112
113 // Read some data from the buffer.
114 //ALOGD("AudioStreamInternalCapture::processDataNow() - readNowWithConversion(%d)", numFrames);
115 int32_t framesProcessed = readNowWithConversion(buffer, numFrames);
116 //ALOGD("AudioStreamInternalCapture::processDataNow() - tried to read %d frames, read %d",
117 // numFrames, framesProcessed);
Phil Burkfd34a932017-07-19 07:03:52 -0700118 if (ATRACE_ENABLED()) {
119 ATRACE_INT("aaRead", framesProcessed);
120 }
Phil Burk87c9f642017-05-17 07:22:39 -0700121
122 // Calculate an ideal time to wake up.
123 if (wakeTimePtr != nullptr && framesProcessed >= 0) {
124 // By default wake up a few milliseconds from now. // TODO review
125 int64_t wakeTime = currentNanoTime + (1 * AAUDIO_NANOS_PER_MILLISECOND);
126 aaudio_stream_state_t state = getState();
127 //ALOGD("AudioStreamInternalCapture::processDataNow() - wakeTime based on %s",
128 // AAudio_convertStreamStateToText(state));
129 switch (state) {
130 case AAUDIO_STREAM_STATE_OPEN:
131 case AAUDIO_STREAM_STATE_STARTING:
132 break;
Phil Burkfd34a932017-07-19 07:03:52 -0700133 case AAUDIO_STREAM_STATE_STARTED:
Phil Burk87c9f642017-05-17 07:22:39 -0700134 {
Phil Burkfd34a932017-07-19 07:03:52 -0700135 // When do we expect the next write burst to occur?
Phil Burk87c9f642017-05-17 07:22:39 -0700136
Phil Burkfd34a932017-07-19 07:03:52 -0700137 // Calculate frame position based off of the readCounter because
138 // the writeCounter might have just advanced in the background,
139 // causing us to sleep until a later burst.
Phil Burkbcc36742017-08-31 17:24:51 -0700140 int64_t nextPosition = mAudioEndpoint.getDataReadCounter() + mFramesPerBurst;
141 wakeTime = mClockModel.convertPositionToTime(nextPosition);
Phil Burk87c9f642017-05-17 07:22:39 -0700142 }
143 break;
144 default:
145 break;
146 }
147 *wakeTimePtr = wakeTime;
148
149 }
Phil Burkfd34a932017-07-19 07:03:52 -0700150
151 ATRACE_END();
Phil Burk87c9f642017-05-17 07:22:39 -0700152 return framesProcessed;
153}
154
155aaudio_result_t AudioStreamInternalCapture::readNowWithConversion(void *buffer,
156 int32_t numFrames) {
Phil Burkfbf031e2017-10-12 15:58:31 -0700157 // ALOGD("readNowWithConversion(%p, %d)",
Phil Burk87c9f642017-05-17 07:22:39 -0700158 // buffer, numFrames);
159 WrappingBuffer wrappingBuffer;
160 uint8_t *destination = (uint8_t *) buffer;
161 int32_t framesLeft = numFrames;
162
163 mAudioEndpoint.getFullFramesAvailable(&wrappingBuffer);
164
165 // Read data in one or two parts.
166 for (int partIndex = 0; framesLeft > 0 && partIndex < WrappingBuffer::SIZE; partIndex++) {
167 int32_t framesToProcess = framesLeft;
168 int32_t framesAvailable = wrappingBuffer.numFrames[partIndex];
169 if (framesAvailable <= 0) break;
170
171 if (framesToProcess > framesAvailable) {
172 framesToProcess = framesAvailable;
173 }
174
175 int32_t numBytes = getBytesPerFrame() * framesToProcess;
176 int32_t numSamples = framesToProcess * getSamplesPerFrame();
177
178 // TODO factor this out into a utility function
179 if (mDeviceFormat == getFormat()) {
180 memcpy(destination, wrappingBuffer.data[partIndex], numBytes);
181 } else if (mDeviceFormat == AAUDIO_FORMAT_PCM_I16
182 && getFormat() == AAUDIO_FORMAT_PCM_FLOAT) {
183 AAudioConvert_pcm16ToFloat(
184 (const int16_t *) wrappingBuffer.data[partIndex],
185 (float *) destination,
186 numSamples,
187 1.0f);
188 } else if (mDeviceFormat == AAUDIO_FORMAT_PCM_FLOAT
189 && getFormat() == AAUDIO_FORMAT_PCM_I16) {
190 AAudioConvert_floatToPcm16(
191 (const float *) wrappingBuffer.data[partIndex],
192 (int16_t *) destination,
193 numSamples,
194 1.0f);
195 } else {
196 ALOGE("Format conversion not supported!");
197 return AAUDIO_ERROR_INVALID_FORMAT;
198 }
199 destination += numBytes;
200 framesLeft -= framesToProcess;
201 }
202
203 int32_t framesProcessed = numFrames - framesLeft;
204 mAudioEndpoint.advanceReadIndex(framesProcessed);
Phil Burk87c9f642017-05-17 07:22:39 -0700205
Phil Burkfbf031e2017-10-12 15:58:31 -0700206 //ALOGD("readNowWithConversion() returns %d", framesProcessed);
Phil Burk87c9f642017-05-17 07:22:39 -0700207 return framesProcessed;
208}
209
Phil Burkec89b2e2017-06-20 15:05:06 -0700210int64_t AudioStreamInternalCapture::getFramesWritten() {
211 int64_t framesWrittenHardware;
212 if (isActive()) {
213 framesWrittenHardware = mClockModel.convertTimeToPosition(AudioClock::getNanoseconds());
Phil Burk87c9f642017-05-17 07:22:39 -0700214 } else {
Phil Burkec89b2e2017-06-20 15:05:06 -0700215 framesWrittenHardware = mAudioEndpoint.getDataWriteCounter();
Phil Burk87c9f642017-05-17 07:22:39 -0700216 }
Phil Burkec89b2e2017-06-20 15:05:06 -0700217 // Prevent retrograde motion.
218 mLastFramesWritten = std::max(mLastFramesWritten,
219 framesWrittenHardware + mFramesOffsetFromService);
Phil Burkfbf031e2017-10-12 15:58:31 -0700220 //ALOGD("getFramesWritten() returns %lld",
Phil Burkec89b2e2017-06-20 15:05:06 -0700221 // (long long)mLastFramesWritten);
222 return mLastFramesWritten;
Phil Burk87c9f642017-05-17 07:22:39 -0700223}
224
Phil Burkec89b2e2017-06-20 15:05:06 -0700225int64_t AudioStreamInternalCapture::getFramesRead() {
Phil Burkbcc36742017-08-31 17:24:51 -0700226 int64_t frames = mAudioEndpoint.getDataReadCounter() + mFramesOffsetFromService;
Phil Burkfbf031e2017-10-12 15:58:31 -0700227 //ALOGD("getFramesRead() returns %lld", (long long)frames);
Phil Burk87c9f642017-05-17 07:22:39 -0700228 return frames;
229}
230
231// Read data from the stream and pass it to the callback for processing.
232void *AudioStreamInternalCapture::callbackLoop() {
233 aaudio_result_t result = AAUDIO_OK;
234 aaudio_data_callback_result_t callbackResult = AAUDIO_CALLBACK_RESULT_CONTINUE;
235 AAudioStream_dataCallback appCallback = getDataCallbackProc();
236 if (appCallback == nullptr) return NULL;
237
238 // result might be a frame count
239 while (mCallbackEnabled.load() && isActive() && (result >= 0)) {
240
241 // Read audio data from stream.
242 int64_t timeoutNanos = calculateReasonableTimeout(mCallbackFrames);
243
244 // This is a BLOCKING READ!
245 result = read(mCallbackBuffer, mCallbackFrames, timeoutNanos);
246 if ((result != mCallbackFrames)) {
Phil Burkfbf031e2017-10-12 15:58:31 -0700247 ALOGE("callbackLoop: read() returned %d", result);
Phil Burk87c9f642017-05-17 07:22:39 -0700248 if (result >= 0) {
249 // Only read some of the frames requested. Must have timed out.
250 result = AAUDIO_ERROR_TIMEOUT;
251 }
252 AAudioStream_errorCallback errorCallback = getErrorCallbackProc();
253 if (errorCallback != nullptr) {
254 (*errorCallback)(
255 (AAudioStream *) this,
256 getErrorCallbackUserData(),
257 result);
258 }
259 break;
260 }
261
262 // Call application using the AAudio callback interface.
263 callbackResult = (*appCallback)(
264 (AAudioStream *) this,
265 getDataCallbackUserData(),
266 mCallbackBuffer,
267 mCallbackFrames);
268
269 if (callbackResult == AAUDIO_CALLBACK_RESULT_STOP) {
Phil Burkfbf031e2017-10-12 15:58:31 -0700270 ALOGD("callback returned AAUDIO_CALLBACK_RESULT_STOP");
Phil Burk87c9f642017-05-17 07:22:39 -0700271 break;
272 }
273 }
274
Phil Burkfbf031e2017-10-12 15:58:31 -0700275 ALOGD("callbackLoop() exiting, result = %d, isActive() = %d",
Phil Burk87c9f642017-05-17 07:22:39 -0700276 result, (int) isActive());
277 return NULL;
278}