blob: 366cc8720979245af9e321b956dadeffab1e8418 [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 Burk0127c1b2018-03-29 13:48:06 -070023#include <audio_utils/primitives.h>
Phil Burk87c9f642017-05-17 07:22:39 -070024#include <aaudio/AAudio.h>
25
26#include "client/AudioStreamInternalCapture.h"
27#include "utility/AudioClock.h"
28
Phil Burkfd34a932017-07-19 07:03:52 -070029#define ATRACE_TAG ATRACE_TAG_AUDIO
30#include <utils/Trace.h>
31
Phil Burk87c9f642017-05-17 07:22:39 -070032using android::WrappingBuffer;
33
34using namespace aaudio;
35
36AudioStreamInternalCapture::AudioStreamInternalCapture(AAudioServiceInterface &serviceInterface,
37 bool inService)
38 : AudioStreamInternal(serviceInterface, inService) {
39
40}
41
42AudioStreamInternalCapture::~AudioStreamInternalCapture() {}
43
Phil Burkbcc36742017-08-31 17:24:51 -070044void AudioStreamInternalCapture::advanceClientToMatchServerPosition() {
45 int64_t readCounter = mAudioEndpoint.getDataReadCounter();
46 int64_t writeCounter = mAudioEndpoint.getDataWriteCounter();
47
48 // Bump offset so caller does not see the retrograde motion in getFramesRead().
49 int64_t offset = readCounter - writeCounter;
50 mFramesOffsetFromService += offset;
51 ALOGD("advanceClientToMatchServerPosition() readN = %lld, writeN = %lld, offset = %lld",
52 (long long)readCounter, (long long)writeCounter, (long long)mFramesOffsetFromService);
53
54 // Force readCounter to match writeCounter.
55 // This is because we cannot change the write counter in the hardware.
56 mAudioEndpoint.setDataReadCounter(writeCounter);
57}
58
Phil Burk87c9f642017-05-17 07:22:39 -070059// Write the data, block if needed and timeoutMillis > 0
60aaudio_result_t AudioStreamInternalCapture::read(void *buffer, int32_t numFrames,
61 int64_t timeoutNanoseconds)
62{
63 return processData(buffer, numFrames, timeoutNanoseconds);
64}
65
66// Read as much data as we can without blocking.
67aaudio_result_t AudioStreamInternalCapture::processDataNow(void *buffer, int32_t numFrames,
68 int64_t currentNanoTime, int64_t *wakeTimePtr) {
69 aaudio_result_t result = processCommands();
70 if (result != AAUDIO_OK) {
71 return result;
72 }
73
Phil Burkfd34a932017-07-19 07:03:52 -070074 const char *traceName = "aaRdNow";
75 ATRACE_BEGIN(traceName);
76
Phil Burkbcc36742017-08-31 17:24:51 -070077 if (mClockModel.isStarting()) {
78 // Still haven't got any timestamps from server.
79 // Keep waiting until we get some valid timestamps then start writing to the
80 // current buffer position.
81 ALOGD("processDataNow() wait for valid timestamps");
82 // Sleep very briefly and hope we get a timestamp soon.
83 *wakeTimePtr = currentNanoTime + (2000 * AAUDIO_NANOS_PER_MICROSECOND);
84 ATRACE_END();
85 return 0;
86 }
87 // If we have gotten this far then we have at least one timestamp from server.
88
Phil Burk87c9f642017-05-17 07:22:39 -070089 if (mAudioEndpoint.isFreeRunning()) {
90 //ALOGD("AudioStreamInternalCapture::processDataNow() - update remote counter");
91 // Update data queue based on the timing model.
Phil Burkfceeee72019-06-14 11:18:45 -070092 // Jitter in the DSP can cause late writes to the FIFO.
93 // This might be caused by resampling.
94 // We want to read the FIFO after the latest possible time
95 // that the DSP could have written the data.
96 int64_t estimatedRemoteCounter = mClockModel.convertLatestTimeToPosition(currentNanoTime);
Phil Burk87c9f642017-05-17 07:22:39 -070097 // TODO refactor, maybe use setRemoteCounter()
98 mAudioEndpoint.setDataWriteCounter(estimatedRemoteCounter);
99 }
100
Phil Burkbcc36742017-08-31 17:24:51 -0700101 // This code assumes that we have already received valid timestamps.
102 if (mNeedCatchUp.isRequested()) {
103 // Catch an MMAP pointer that is already advancing.
104 // This will avoid initial underruns caused by a slow cold start.
105 advanceClientToMatchServerPosition();
106 mNeedCatchUp.acknowledge();
107 }
108
Phil Burk87c9f642017-05-17 07:22:39 -0700109 // If the write index passed the read index then consider it an overrun.
Phil Burk23296382017-11-20 15:45:11 -0800110 // For shared streams, the xRunCount is passed up from the service.
111 if (mAudioEndpoint.isFreeRunning() && mAudioEndpoint.getEmptyFramesAvailable() < 0) {
Phil Burk87c9f642017-05-17 07:22:39 -0700112 mXRunCount++;
Phil Burkfd34a932017-07-19 07:03:52 -0700113 if (ATRACE_ENABLED()) {
114 ATRACE_INT("aaOverRuns", mXRunCount);
115 }
Phil Burk87c9f642017-05-17 07:22:39 -0700116 }
117
118 // Read some data from the buffer.
119 //ALOGD("AudioStreamInternalCapture::processDataNow() - readNowWithConversion(%d)", numFrames);
120 int32_t framesProcessed = readNowWithConversion(buffer, numFrames);
121 //ALOGD("AudioStreamInternalCapture::processDataNow() - tried to read %d frames, read %d",
122 // numFrames, framesProcessed);
Phil Burkfd34a932017-07-19 07:03:52 -0700123 if (ATRACE_ENABLED()) {
124 ATRACE_INT("aaRead", framesProcessed);
125 }
Phil Burk87c9f642017-05-17 07:22:39 -0700126
127 // Calculate an ideal time to wake up.
128 if (wakeTimePtr != nullptr && framesProcessed >= 0) {
129 // By default wake up a few milliseconds from now. // TODO review
130 int64_t wakeTime = currentNanoTime + (1 * AAUDIO_NANOS_PER_MILLISECOND);
131 aaudio_stream_state_t state = getState();
132 //ALOGD("AudioStreamInternalCapture::processDataNow() - wakeTime based on %s",
133 // AAudio_convertStreamStateToText(state));
134 switch (state) {
135 case AAUDIO_STREAM_STATE_OPEN:
136 case AAUDIO_STREAM_STATE_STARTING:
137 break;
Phil Burkfd34a932017-07-19 07:03:52 -0700138 case AAUDIO_STREAM_STATE_STARTED:
Phil Burk87c9f642017-05-17 07:22:39 -0700139 {
Phil Burkfd34a932017-07-19 07:03:52 -0700140 // When do we expect the next write burst to occur?
Phil Burk87c9f642017-05-17 07:22:39 -0700141
Phil Burkfd34a932017-07-19 07:03:52 -0700142 // Calculate frame position based off of the readCounter because
143 // the writeCounter might have just advanced in the background,
144 // causing us to sleep until a later burst.
Phil Burkbcc36742017-08-31 17:24:51 -0700145 int64_t nextPosition = mAudioEndpoint.getDataReadCounter() + mFramesPerBurst;
Phil Burkfceeee72019-06-14 11:18:45 -0700146 wakeTime = mClockModel.convertPositionToLatestTime(nextPosition);
Phil Burk87c9f642017-05-17 07:22:39 -0700147 }
148 break;
149 default:
150 break;
151 }
152 *wakeTimePtr = wakeTime;
153
154 }
Phil Burkfd34a932017-07-19 07:03:52 -0700155
156 ATRACE_END();
Phil Burk87c9f642017-05-17 07:22:39 -0700157 return framesProcessed;
158}
159
160aaudio_result_t AudioStreamInternalCapture::readNowWithConversion(void *buffer,
161 int32_t numFrames) {
Phil Burkfbf031e2017-10-12 15:58:31 -0700162 // ALOGD("readNowWithConversion(%p, %d)",
Phil Burk87c9f642017-05-17 07:22:39 -0700163 // buffer, numFrames);
164 WrappingBuffer wrappingBuffer;
165 uint8_t *destination = (uint8_t *) buffer;
166 int32_t framesLeft = numFrames;
167
168 mAudioEndpoint.getFullFramesAvailable(&wrappingBuffer);
169
170 // Read data in one or two parts.
171 for (int partIndex = 0; framesLeft > 0 && partIndex < WrappingBuffer::SIZE; partIndex++) {
172 int32_t framesToProcess = framesLeft;
Phil Burk0127c1b2018-03-29 13:48:06 -0700173 const int32_t framesAvailable = wrappingBuffer.numFrames[partIndex];
Phil Burk87c9f642017-05-17 07:22:39 -0700174 if (framesAvailable <= 0) break;
175
176 if (framesToProcess > framesAvailable) {
177 framesToProcess = framesAvailable;
178 }
179
Phil Burk0127c1b2018-03-29 13:48:06 -0700180 const int32_t numBytes = getBytesPerFrame() * framesToProcess;
181 const int32_t numSamples = framesToProcess * getSamplesPerFrame();
Phil Burk87c9f642017-05-17 07:22:39 -0700182
Phil Burk0127c1b2018-03-29 13:48:06 -0700183 const audio_format_t sourceFormat = getDeviceFormat();
184 const audio_format_t destinationFormat = getFormat();
Phil Burk87c9f642017-05-17 07:22:39 -0700185 // TODO factor this out into a utility function
Phil Burk0127c1b2018-03-29 13:48:06 -0700186 if (sourceFormat == destinationFormat) {
Phil Burk87c9f642017-05-17 07:22:39 -0700187 memcpy(destination, wrappingBuffer.data[partIndex], numBytes);
Phil Burk0127c1b2018-03-29 13:48:06 -0700188 } else if (sourceFormat == AUDIO_FORMAT_PCM_16_BIT
189 && destinationFormat == AUDIO_FORMAT_PCM_FLOAT) {
190 memcpy_to_float_from_i16(
Phil Burk87c9f642017-05-17 07:22:39 -0700191 (float *) destination,
Phil Burk0127c1b2018-03-29 13:48:06 -0700192 (const int16_t *) wrappingBuffer.data[partIndex],
193 numSamples);
194 } else if (sourceFormat == AUDIO_FORMAT_PCM_FLOAT
195 && destinationFormat == AUDIO_FORMAT_PCM_16_BIT) {
196 memcpy_to_i16_from_float(
Phil Burk87c9f642017-05-17 07:22:39 -0700197 (int16_t *) destination,
Phil Burk0127c1b2018-03-29 13:48:06 -0700198 (const float *) wrappingBuffer.data[partIndex],
199 numSamples);
Phil Burk87c9f642017-05-17 07:22:39 -0700200 } else {
Phil Burk0127c1b2018-03-29 13:48:06 -0700201 ALOGE("%s() - Format conversion not supported! audio_format_t source = %u, dest = %u",
202 __func__, sourceFormat, destinationFormat);
Phil Burk87c9f642017-05-17 07:22:39 -0700203 return AAUDIO_ERROR_INVALID_FORMAT;
204 }
205 destination += numBytes;
206 framesLeft -= framesToProcess;
207 }
208
209 int32_t framesProcessed = numFrames - framesLeft;
210 mAudioEndpoint.advanceReadIndex(framesProcessed);
Phil Burk87c9f642017-05-17 07:22:39 -0700211
Phil Burkfbf031e2017-10-12 15:58:31 -0700212 //ALOGD("readNowWithConversion() returns %d", framesProcessed);
Phil Burk87c9f642017-05-17 07:22:39 -0700213 return framesProcessed;
214}
215
Phil Burkec89b2e2017-06-20 15:05:06 -0700216int64_t AudioStreamInternalCapture::getFramesWritten() {
Phil Burk377c1c22018-12-12 16:06:54 -0800217 const int64_t framesWrittenHardware = isClockModelInControl()
218 ? mClockModel.convertTimeToPosition(AudioClock::getNanoseconds())
219 : mAudioEndpoint.getDataWriteCounter();
220 // Add service offset and prevent retrograde motion.
Phil Burkec89b2e2017-06-20 15:05:06 -0700221 mLastFramesWritten = std::max(mLastFramesWritten,
222 framesWrittenHardware + mFramesOffsetFromService);
Phil Burkec89b2e2017-06-20 15:05:06 -0700223 return mLastFramesWritten;
Phil Burk87c9f642017-05-17 07:22:39 -0700224}
225
Phil Burkec89b2e2017-06-20 15:05:06 -0700226int64_t AudioStreamInternalCapture::getFramesRead() {
Phil Burkbcc36742017-08-31 17:24:51 -0700227 int64_t frames = mAudioEndpoint.getDataReadCounter() + mFramesOffsetFromService;
Phil Burkfbf031e2017-10-12 15:58:31 -0700228 //ALOGD("getFramesRead() returns %lld", (long long)frames);
Phil Burk87c9f642017-05-17 07:22:39 -0700229 return frames;
230}
231
232// Read data from the stream and pass it to the callback for processing.
233void *AudioStreamInternalCapture::callbackLoop() {
234 aaudio_result_t result = AAUDIO_OK;
235 aaudio_data_callback_result_t callbackResult = AAUDIO_CALLBACK_RESULT_CONTINUE;
Phil Burk134f1972017-12-08 13:06:11 -0800236 if (!isDataCallbackSet()) return NULL;
Phil Burk87c9f642017-05-17 07:22:39 -0700237
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 }
Phil Burk134f1972017-12-08 13:06:11 -0800252 maybeCallErrorCallback(result);
Phil Burk87c9f642017-05-17 07:22:39 -0700253 break;
254 }
255
256 // Call application using the AAudio callback interface.
Phil Burk134f1972017-12-08 13:06:11 -0800257 callbackResult = maybeCallDataCallback(mCallbackBuffer, mCallbackFrames);
Phil Burk87c9f642017-05-17 07:22:39 -0700258
259 if (callbackResult == AAUDIO_CALLBACK_RESULT_STOP) {
Phil Burk762365c2018-12-10 16:02:16 -0800260 ALOGD("%s(): callback returned AAUDIO_CALLBACK_RESULT_STOP", __func__);
Phil Burk1e83bee2018-12-17 14:15:20 -0800261 result = systemStopFromCallback();
Phil Burk87c9f642017-05-17 07:22:39 -0700262 break;
263 }
264 }
265
Phil Burkfbf031e2017-10-12 15:58:31 -0700266 ALOGD("callbackLoop() exiting, result = %d, isActive() = %d",
Phil Burk87c9f642017-05-17 07:22:39 -0700267 result, (int) isActive());
268 return NULL;
269}