blob: fb276c2ae773b2d6ad46525e4b56a30c5c25f296 [file] [log] [blame]
Phil Burk204a1632017-01-03 17:23:43 -08001/*
2 * Copyright (C) 2016 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 Burkcf5f6d22017-05-26 12:35:07 -070017// This file is used in both client and server processes.
18// This is needed to make sense of the logs more easily.
Eric Laurentcb4dae22017-07-01 19:39:32 -070019#define LOG_TAG (mInService ? "AudioStreamInternal_Service" : "AudioStreamInternal_Client")
Phil Burk204a1632017-01-03 17:23:43 -080020//#define LOG_NDEBUG 0
21#include <utils/Log.h>
22
Phil Burk4485d412017-05-09 15:55:02 -070023#define ATRACE_TAG ATRACE_TAG_AUDIO
24
Phil Burkc0c70e32017-02-09 13:18:38 -080025#include <stdint.h>
Phil Burk204a1632017-01-03 17:23:43 -080026
27#include <binder/IServiceManager.h>
28
Phil Burk5ed503c2017-02-01 09:38:15 -080029#include <aaudio/AAudio.h>
Phil Burkfd34a932017-07-19 07:03:52 -070030#include <cutils/properties.h>
Phil Burke4d7bb42017-03-28 11:32:39 -070031#include <utils/String16.h>
Phil Burk4485d412017-05-09 15:55:02 -070032#include <utils/Trace.h>
Phil Burk204a1632017-01-03 17:23:43 -080033
Phil Burkc0c70e32017-02-09 13:18:38 -080034#include "AudioEndpointParcelable.h"
35#include "binding/AAudioStreamRequest.h"
36#include "binding/AAudioStreamConfiguration.h"
37#include "binding/IAAudioService.h"
Phil Burk5ed503c2017-02-01 09:38:15 -080038#include "binding/AAudioServiceMessage.h"
dimitry76ee1ff2019-07-17 13:55:16 +020039#include "core/AudioGlobal.h"
Phil Burk3df348f2017-02-08 11:41:55 -080040#include "core/AudioStreamBuilder.h"
Phil Burke572f462017-04-20 13:03:19 -070041#include "fifo/FifoBuffer.h"
Phil Burkfd34a932017-07-19 07:03:52 -070042#include "utility/AudioClock.h"
Phil Burke572f462017-04-20 13:03:19 -070043
Phil Burkc0c70e32017-02-09 13:18:38 -080044#include "AudioStreamInternal.h"
Phil Burk204a1632017-01-03 17:23:43 -080045
Phil Burk204a1632017-01-03 17:23:43 -080046using android::String16;
Phil Burkdec33ab2017-01-17 14:48:16 -080047using android::Mutex;
Phil Burkc0c70e32017-02-09 13:18:38 -080048using android::WrappingBuffer;
Phil Burk204a1632017-01-03 17:23:43 -080049
Phil Burk5ed503c2017-02-01 09:38:15 -080050using namespace aaudio;
Phil Burk204a1632017-01-03 17:23:43 -080051
Phil Burke4d7bb42017-03-28 11:32:39 -070052#define MIN_TIMEOUT_NANOS (1000 * AAUDIO_NANOS_PER_MILLISECOND)
53
54// Wait at least this many times longer than the operation should take.
55#define MIN_TIMEOUT_OPERATIONS 4
56
Phil Burkbcc36742017-08-31 17:24:51 -070057#define LOG_TIMESTAMPS 0
Phil Burk87c9f642017-05-17 07:22:39 -070058
Phil Burkc0c70e32017-02-09 13:18:38 -080059AudioStreamInternal::AudioStreamInternal(AAudioServiceInterface &serviceInterface, bool inService)
Phil Burk204a1632017-01-03 17:23:43 -080060 : AudioStream()
61 , mClockModel()
62 , mAudioEndpoint()
Phil Burk5ed503c2017-02-01 09:38:15 -080063 , mServiceStreamHandle(AAUDIO_HANDLE_INVALID)
Phil Burkec89b2e2017-06-20 15:05:06 -070064 , mInService(inService)
Phil Burkfd34a932017-07-19 07:03:52 -070065 , mServiceInterface(serviceInterface)
Phil Burka53ffa62018-10-10 16:21:37 -070066 , mAtomicInternalTimestamp()
Phil Burkfd34a932017-07-19 07:03:52 -070067 , mWakeupDelayNanos(AAudioProperty_getWakeupDelayMicros() * AAUDIO_NANOS_PER_MICROSECOND)
68 , mMinimumSleepNanos(AAudioProperty_getMinimumSleepMicros() * AAUDIO_NANOS_PER_MICROSECOND)
69 {
Phil Burk204a1632017-01-03 17:23:43 -080070}
71
72AudioStreamInternal::~AudioStreamInternal() {
73}
74
Phil Burk5ed503c2017-02-01 09:38:15 -080075aaudio_result_t AudioStreamInternal::open(const AudioStreamBuilder &builder) {
Phil Burk204a1632017-01-03 17:23:43 -080076
Phil Burk5ed503c2017-02-01 09:38:15 -080077 aaudio_result_t result = AAUDIO_OK;
Phil Burk99306c82017-08-14 12:38:58 -070078 int32_t capacity;
Phil Burk6479d502017-11-20 09:32:52 -080079 int32_t framesPerBurst;
Phil Burk3c4e6b52019-01-22 15:53:36 -080080 int32_t framesPerHardwareBurst;
Phil Burk5ed503c2017-02-01 09:38:15 -080081 AAudioStreamRequest request;
Phil Burk99306c82017-08-14 12:38:58 -070082 AAudioStreamConfiguration configurationOutput;
Phil Burk204a1632017-01-03 17:23:43 -080083
Phil Burk99306c82017-08-14 12:38:58 -070084 if (getState() != AAUDIO_STREAM_STATE_UNINITIALIZED) {
Phil Burkfbf031e2017-10-12 15:58:31 -070085 ALOGE("%s - already open! state = %d", __func__, getState());
Phil Burk99306c82017-08-14 12:38:58 -070086 return AAUDIO_ERROR_INVALID_STATE;
87 }
88
89 // Copy requested parameters to the stream.
Phil Burk204a1632017-01-03 17:23:43 -080090 result = AudioStream::open(builder);
91 if (result < 0) {
92 return result;
93 }
94
Phil Burk3c4e6b52019-01-22 15:53:36 -080095 const int32_t burstMinMicros = AAudioProperty_getHardwareBurstMinMicros();
96 int32_t burstMicros = 0;
97
Phil Burkc0c70e32017-02-09 13:18:38 -080098 // We have to do volume scaling. So we prefer FLOAT format.
Phil Burk0127c1b2018-03-29 13:48:06 -070099 if (getFormat() == AUDIO_FORMAT_DEFAULT) {
100 setFormat(AUDIO_FORMAT_PCM_FLOAT);
Phil Burkc0c70e32017-02-09 13:18:38 -0800101 }
Phil Burk71f35bb2017-04-13 16:05:07 -0700102 // Request FLOAT for the shared mixer.
Phil Burk0127c1b2018-03-29 13:48:06 -0700103 request.getConfiguration().setFormat(AUDIO_FORMAT_PCM_FLOAT);
Phil Burkc0c70e32017-02-09 13:18:38 -0800104
Phil Burkdec33ab2017-01-17 14:48:16 -0800105 // Build the request to send to the server.
Phil Burk204a1632017-01-03 17:23:43 -0800106 request.setUserId(getuid());
107 request.setProcessId(getpid());
Phil Burk71f35bb2017-04-13 16:05:07 -0700108 request.setSharingModeMatchRequired(isSharingModeMatchRequired());
Phil Burk41f19d82018-02-13 14:59:10 -0800109 request.setInService(isInService());
Phil Burkc0c70e32017-02-09 13:18:38 -0800110
Phil Burk204a1632017-01-03 17:23:43 -0800111 request.getConfiguration().setDeviceId(getDeviceId());
112 request.getConfiguration().setSampleRate(getSampleRate());
113 request.getConfiguration().setSamplesPerFrame(getSamplesPerFrame());
Phil Burk39f02dd2017-08-04 09:13:31 -0700114 request.getConfiguration().setDirection(getDirection());
Phil Burk71f35bb2017-04-13 16:05:07 -0700115 request.getConfiguration().setSharingMode(getSharingMode());
116
Phil Burka62fb952018-01-16 12:44:06 -0800117 request.getConfiguration().setUsage(getUsage());
118 request.getConfiguration().setContentType(getContentType());
119 request.getConfiguration().setInputPreset(getInputPreset());
120
Phil Burk3df348f2017-02-08 11:41:55 -0800121 request.getConfiguration().setBufferCapacity(builder.getBufferCapacity());
Phil Burk204a1632017-01-03 17:23:43 -0800122
Phil Burk41f19d82018-02-13 14:59:10 -0800123 mDeviceChannelCount = getSamplesPerFrame(); // Assume it will be the same. Update if not.
124
Phil Burk99306c82017-08-14 12:38:58 -0700125 mServiceStreamHandle = mServiceInterface.openStream(request, configurationOutput);
Phil Burk41f19d82018-02-13 14:59:10 -0800126 if (mServiceStreamHandle < 0
127 && request.getConfiguration().getSamplesPerFrame() == 1 // mono?
128 && getDirection() == AAUDIO_DIRECTION_OUTPUT
129 && !isInService()) {
130 // if that failed then try switching from mono to stereo if OUTPUT.
131 // Only do this in the client. Otherwise we end up with a mono mixer in the service
132 // that writes to a stereo MMAP stream.
Phil Burk0127c1b2018-03-29 13:48:06 -0700133 ALOGD("%s() - openStream() returned %d, try switching from MONO to STEREO",
Phil Burk41f19d82018-02-13 14:59:10 -0800134 __func__, mServiceStreamHandle);
135 request.getConfiguration().setSamplesPerFrame(2); // stereo
136 mServiceStreamHandle = mServiceInterface.openStream(request, configurationOutput);
137 }
Phil Burk204a1632017-01-03 17:23:43 -0800138 if (mServiceStreamHandle < 0) {
Phil Burk41f19d82018-02-13 14:59:10 -0800139 return mServiceStreamHandle;
Phil Burk204a1632017-01-03 17:23:43 -0800140 }
Phil Burk99306c82017-08-14 12:38:58 -0700141
142 result = configurationOutput.validate();
143 if (result != AAUDIO_OK) {
144 goto error;
145 }
146 // Save results of the open.
Phil Burk41f19d82018-02-13 14:59:10 -0800147 if (getSamplesPerFrame() == AAUDIO_UNSPECIFIED) {
148 setSamplesPerFrame(configurationOutput.getSamplesPerFrame());
149 }
150 mDeviceChannelCount = configurationOutput.getSamplesPerFrame();
151
Phil Burk99306c82017-08-14 12:38:58 -0700152 setSampleRate(configurationOutput.getSampleRate());
Phil Burk99306c82017-08-14 12:38:58 -0700153 setDeviceId(configurationOutput.getDeviceId());
Phil Burk4e1af9f2018-01-03 15:54:35 -0800154 setSessionId(configurationOutput.getSessionId());
Phil Burk99306c82017-08-14 12:38:58 -0700155 setSharingMode(configurationOutput.getSharingMode());
156
Phil Burka62fb952018-01-16 12:44:06 -0800157 setUsage(configurationOutput.getUsage());
158 setContentType(configurationOutput.getContentType());
159 setInputPreset(configurationOutput.getInputPreset());
160
Phil Burk99306c82017-08-14 12:38:58 -0700161 // Save device format so we can do format conversion and volume scaling together.
Phil Burk3d786cb2018-04-09 11:58:09 -0700162 setDeviceFormat(configurationOutput.getFormat());
Phil Burk99306c82017-08-14 12:38:58 -0700163
164 result = mServiceInterface.getStreamDescription(mServiceStreamHandle, mEndPointParcelable);
165 if (result != AAUDIO_OK) {
166 goto error;
167 }
168
169 // Resolve parcelable into a descriptor.
170 result = mEndPointParcelable.resolve(&mEndpointDescriptor);
171 if (result != AAUDIO_OK) {
172 goto error;
173 }
174
175 // Configure endpoint based on descriptor.
176 result = mAudioEndpoint.configure(&mEndpointDescriptor, getDirection());
177 if (result != AAUDIO_OK) {
178 goto error;
179 }
180
Phil Burk3c4e6b52019-01-22 15:53:36 -0800181 framesPerHardwareBurst = mEndpointDescriptor.dataQueueDescriptor.framesPerBurst;
182
183 // Scale up the burst size to meet the minimum equivalent in microseconds.
184 // This is to avoid waking the CPU too often when the HW burst is very small
185 // or at high sample rates.
186 framesPerBurst = framesPerHardwareBurst;
187 do {
188 if (burstMicros > 0) { // skip first loop
189 framesPerBurst *= 2;
190 }
191 burstMicros = framesPerBurst * static_cast<int64_t>(1000000) / getSampleRate();
192 } while (burstMicros < burstMinMicros);
193 ALOGD("%s() original HW burst = %d, minMicros = %d => SW burst = %d\n",
194 __func__, framesPerHardwareBurst, burstMinMicros, framesPerBurst);
195
196 // Validate final burst size.
Phil Burk6479d502017-11-20 09:32:52 -0800197 if (framesPerBurst < MIN_FRAMES_PER_BURST || framesPerBurst > MAX_FRAMES_PER_BURST) {
198 ALOGE("%s - framesPerBurst out of range = %d", __func__, framesPerBurst);
Phil Burk99306c82017-08-14 12:38:58 -0700199 result = AAUDIO_ERROR_OUT_OF_RANGE;
200 goto error;
201 }
Phil Burk6479d502017-11-20 09:32:52 -0800202 mFramesPerBurst = framesPerBurst; // only save good value
203
204 capacity = mEndpointDescriptor.dataQueueDescriptor.capacityInFrames;
205 if (capacity < mFramesPerBurst || capacity > MAX_BUFFER_CAPACITY_IN_FRAMES) {
Phil Burkfbf031e2017-10-12 15:58:31 -0700206 ALOGE("%s - bufferCapacity out of range = %d", __func__, capacity);
Phil Burk99306c82017-08-14 12:38:58 -0700207 result = AAUDIO_ERROR_OUT_OF_RANGE;
208 goto error;
209 }
210
211 mClockModel.setSampleRate(getSampleRate());
Phil Burk3c4e6b52019-01-22 15:53:36 -0800212 mClockModel.setFramesPerBurst(framesPerHardwareBurst);
Phil Burk99306c82017-08-14 12:38:58 -0700213
Phil Burk134f1972017-12-08 13:06:11 -0800214 if (isDataCallbackSet()) {
Phil Burk99306c82017-08-14 12:38:58 -0700215 mCallbackFrames = builder.getFramesPerDataCallback();
216 if (mCallbackFrames > getBufferCapacity() / 2) {
Phil Burk29ccc292019-04-15 08:58:08 -0700217 ALOGW("%s - framesPerCallback too big = %d, capacity = %d",
Phil Burkfbf031e2017-10-12 15:58:31 -0700218 __func__, mCallbackFrames, getBufferCapacity());
Phil Burk99306c82017-08-14 12:38:58 -0700219 result = AAUDIO_ERROR_OUT_OF_RANGE;
220 goto error;
221
222 } else if (mCallbackFrames < 0) {
Phil Burk29ccc292019-04-15 08:58:08 -0700223 ALOGW("%s - framesPerCallback negative", __func__);
Phil Burk99306c82017-08-14 12:38:58 -0700224 result = AAUDIO_ERROR_OUT_OF_RANGE;
225 goto error;
226
227 }
228 if (mCallbackFrames == AAUDIO_UNSPECIFIED) {
229 mCallbackFrames = mFramesPerBurst;
230 }
231
Phil Burk0127c1b2018-03-29 13:48:06 -0700232 const int32_t callbackBufferSize = mCallbackFrames * getBytesPerFrame();
Phil Burk99306c82017-08-14 12:38:58 -0700233 mCallbackBuffer = new uint8_t[callbackBufferSize];
234 }
235
236 setState(AAUDIO_STREAM_STATE_OPEN);
Phil Burk99306c82017-08-14 12:38:58 -0700237
238 return result;
239
240error:
241 close();
Phil Burk204a1632017-01-03 17:23:43 -0800242 return result;
243}
244
Phil Burk13d3d832019-06-10 14:36:48 -0700245// This must be called under mStreamLock.
Phil Burk5ed503c2017-02-01 09:38:15 -0800246aaudio_result_t AudioStreamInternal::close() {
Phil Burk965650e2017-09-07 21:00:09 -0700247 aaudio_result_t result = AAUDIO_OK;
Phil Burk29ccc292019-04-15 08:58:08 -0700248 ALOGV("%s(): mServiceStreamHandle = 0x%08X", __func__, mServiceStreamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800249 if (mServiceStreamHandle != AAUDIO_HANDLE_INVALID) {
Phil Burk4485d412017-05-09 15:55:02 -0700250 // Don't close a stream while it is running.
251 aaudio_stream_state_t currentState = getState();
Phil Burk13d3d832019-06-10 14:36:48 -0700252 // Don't close a stream while it is running. Stop it first.
253 // If DISCONNECTED then we should still try to stop in case the
254 // error callback is still running.
255 if (isActive() || currentState == AAUDIO_STREAM_STATE_DISCONNECTED) {
Phil Burk4485d412017-05-09 15:55:02 -0700256 requestStop();
Phil Burk4485d412017-05-09 15:55:02 -0700257 }
Phil Burkec89b2e2017-06-20 15:05:06 -0700258 setState(AAUDIO_STREAM_STATE_CLOSING);
Phil Burk5ed503c2017-02-01 09:38:15 -0800259 aaudio_handle_t serviceStreamHandle = mServiceStreamHandle;
260 mServiceStreamHandle = AAUDIO_HANDLE_INVALID;
Phil Burkc0c70e32017-02-09 13:18:38 -0800261
262 mServiceInterface.closeStream(serviceStreamHandle);
Phil Burke4d7bb42017-03-28 11:32:39 -0700263 delete[] mCallbackBuffer;
Phil Burk4485d412017-05-09 15:55:02 -0700264 mCallbackBuffer = nullptr;
Phil Burk965650e2017-09-07 21:00:09 -0700265
Phil Burkec89b2e2017-06-20 15:05:06 -0700266 setState(AAUDIO_STREAM_STATE_CLOSED);
Phil Burk965650e2017-09-07 21:00:09 -0700267 result = mEndPointParcelable.close();
268 aaudio_result_t result2 = AudioStream::close();
269 return (result != AAUDIO_OK) ? result : result2;
Phil Burk204a1632017-01-03 17:23:43 -0800270 } else {
Phil Burk5ed503c2017-02-01 09:38:15 -0800271 return AAUDIO_ERROR_INVALID_HANDLE;
Phil Burk204a1632017-01-03 17:23:43 -0800272 }
273}
274
Phil Burke4d7bb42017-03-28 11:32:39 -0700275static void *aaudio_callback_thread_proc(void *context)
276{
277 AudioStreamInternal *stream = (AudioStreamInternal *)context;
Phil Burkfbf031e2017-10-12 15:58:31 -0700278 //LOGD("oboe_callback_thread, stream = %p", stream);
Phil Burke4d7bb42017-03-28 11:32:39 -0700279 if (stream != NULL) {
280 return stream->callbackLoop();
281 } else {
282 return NULL;
283 }
284}
285
Phil Burkbcc36742017-08-31 17:24:51 -0700286/*
287 * It normally takes about 20-30 msec to start a stream on the server.
288 * But the first time can take as much as 200-300 msec. The HW
289 * starts right away so by the time the client gets a chance to write into
290 * the buffer, it is already in a deep underflow state. That can cause the
291 * XRunCount to be non-zero, which could lead an app to tune its latency higher.
292 * To avoid this problem, we set a request for the processing code to start the
293 * client stream at the same position as the server stream.
294 * The processing code will then save the current offset
295 * between client and server and apply that to any position given to the app.
296 */
Phil Burk5ed503c2017-02-01 09:38:15 -0800297aaudio_result_t AudioStreamInternal::requestStart()
Phil Burk204a1632017-01-03 17:23:43 -0800298{
Phil Burk3316d5e2017-02-15 11:23:01 -0800299 int64_t startTime;
Phil Burk5ed503c2017-02-01 09:38:15 -0800300 if (mServiceStreamHandle == AAUDIO_HANDLE_INVALID) {
Phil Burk29ccc292019-04-15 08:58:08 -0700301 ALOGD("requestStart() mServiceStreamHandle invalid");
Phil Burk5ed503c2017-02-01 09:38:15 -0800302 return AAUDIO_ERROR_INVALID_STATE;
Phil Burk204a1632017-01-03 17:23:43 -0800303 }
Phil Burkec89b2e2017-06-20 15:05:06 -0700304 if (isActive()) {
Phil Burk29ccc292019-04-15 08:58:08 -0700305 ALOGD("requestStart() already active");
Phil Burkec89b2e2017-06-20 15:05:06 -0700306 return AAUDIO_ERROR_INVALID_STATE;
307 }
Phil Burkec89b2e2017-06-20 15:05:06 -0700308
Phil Burkbcc36742017-08-31 17:24:51 -0700309 aaudio_stream_state_t originalState = getState();
310 if (originalState == AAUDIO_STREAM_STATE_DISCONNECTED) {
Phil Burk29ccc292019-04-15 08:58:08 -0700311 ALOGD("requestStart() but DISCONNECTED");
Phil Burkbcc36742017-08-31 17:24:51 -0700312 return AAUDIO_ERROR_DISCONNECTED;
313 }
Phil Burkec89b2e2017-06-20 15:05:06 -0700314 setState(AAUDIO_STREAM_STATE_STARTING);
Phil Burkbcc36742017-08-31 17:24:51 -0700315
316 // Clear any stale timestamps from the previous run.
317 drainTimestampsFromService();
318
Phil Burk965650e2017-09-07 21:00:09 -0700319 aaudio_result_t result = mServiceInterface.startStream(mServiceStreamHandle);
Phil Burkc0c70e32017-02-09 13:18:38 -0800320
Phil Burk3316d5e2017-02-15 11:23:01 -0800321 startTime = AudioClock::getNanoseconds();
Phil Burk204a1632017-01-03 17:23:43 -0800322 mClockModel.start(startTime);
Phil Burkbcc36742017-08-31 17:24:51 -0700323 mNeedCatchUp.request(); // Ask data processing code to catch up when first timestamp received.
Phil Burke4d7bb42017-03-28 11:32:39 -0700324
Phil Burk965650e2017-09-07 21:00:09 -0700325 // Start data callback thread.
Phil Burk134f1972017-12-08 13:06:11 -0800326 if (result == AAUDIO_OK && isDataCallbackSet()) {
Phil Burke4d7bb42017-03-28 11:32:39 -0700327 // Launch the callback loop thread.
328 int64_t periodNanos = mCallbackFrames
329 * AAUDIO_NANOS_PER_SECOND
330 / getSampleRate();
331 mCallbackEnabled.store(true);
332 result = createThread(periodNanos, aaudio_callback_thread_proc, this);
333 }
Phil Burkec89b2e2017-06-20 15:05:06 -0700334 if (result != AAUDIO_OK) {
335 setState(originalState);
336 }
Phil Burke4d7bb42017-03-28 11:32:39 -0700337 return result;
Phil Burk204a1632017-01-03 17:23:43 -0800338}
339
Phil Burke4d7bb42017-03-28 11:32:39 -0700340int64_t AudioStreamInternal::calculateReasonableTimeout(int32_t framesPerOperation) {
341
342 // Wait for at least a second or some number of callbacks to join the thread.
Phil Burk71f35bb2017-04-13 16:05:07 -0700343 int64_t timeoutNanoseconds = (MIN_TIMEOUT_OPERATIONS
344 * framesPerOperation
345 * AAUDIO_NANOS_PER_SECOND)
346 / getSampleRate();
Phil Burke4d7bb42017-03-28 11:32:39 -0700347 if (timeoutNanoseconds < MIN_TIMEOUT_NANOS) { // arbitrary number of seconds
348 timeoutNanoseconds = MIN_TIMEOUT_NANOS;
349 }
350 return timeoutNanoseconds;
351}
352
Phil Burk87c9f642017-05-17 07:22:39 -0700353int64_t AudioStreamInternal::calculateReasonableTimeout() {
354 return calculateReasonableTimeout(getFramesPerBurst());
355}
356
Phil Burk13d3d832019-06-10 14:36:48 -0700357// This must be called under mStreamLock.
Phil Burke4d7bb42017-03-28 11:32:39 -0700358aaudio_result_t AudioStreamInternal::stopCallback()
359{
Phil Burk13d3d832019-06-10 14:36:48 -0700360 if (isDataCallbackSet()
361 && (isActive() || getState() == AAUDIO_STREAM_STATE_DISCONNECTED)) {
Phil Burke4d7bb42017-03-28 11:32:39 -0700362 mCallbackEnabled.store(false);
Phil Burk13d3d832019-06-10 14:36:48 -0700363 return joinThread(NULL); // may temporarily unlock mStreamLock
Phil Burke4d7bb42017-03-28 11:32:39 -0700364 } else {
365 return AAUDIO_OK;
366 }
367}
368
Phil Burk13d3d832019-06-10 14:36:48 -0700369// This must be called under mStreamLock.
Phil Burk1e83bee2018-12-17 14:15:20 -0800370aaudio_result_t AudioStreamInternal::requestStop() {
Phil Burk5cc83c32017-11-28 15:43:18 -0800371 aaudio_result_t result = stopCallback();
372 if (result != AAUDIO_OK) {
373 return result;
374 }
Phil Burk13d3d832019-06-10 14:36:48 -0700375 // The stream may have been unlocked temporarily to let a callback finish
376 // and the callback may have stopped the stream.
377 // Check to make sure the stream still needs to be stopped.
378 // See also AudioStream::safeStop().
379 if (!(isActive() || getState() == AAUDIO_STREAM_STATE_DISCONNECTED)) {
380 return AAUDIO_OK;
381 }
Phil Burk5cc83c32017-11-28 15:43:18 -0800382
Phil Burk71f35bb2017-04-13 16:05:07 -0700383 if (mServiceStreamHandle == AAUDIO_HANDLE_INVALID) {
Phil Burk29ccc292019-04-15 08:58:08 -0700384 ALOGW("%s() mServiceStreamHandle invalid = 0x%08X",
385 __func__, mServiceStreamHandle);
Phil Burk71f35bb2017-04-13 16:05:07 -0700386 return AAUDIO_ERROR_INVALID_STATE;
387 }
388
389 mClockModel.stop(AudioClock::getNanoseconds());
390 setState(AAUDIO_STREAM_STATE_STOPPING);
Phil Burka53ffa62018-10-10 16:21:37 -0700391 mAtomicInternalTimestamp.clear();
Phil Burk965650e2017-09-07 21:00:09 -0700392
393 return mServiceInterface.stopStream(mServiceStreamHandle);
Phil Burk71f35bb2017-04-13 16:05:07 -0700394}
395
Phil Burk5ed503c2017-02-01 09:38:15 -0800396aaudio_result_t AudioStreamInternal::registerThread() {
Phil Burk5ed503c2017-02-01 09:38:15 -0800397 if (mServiceStreamHandle == AAUDIO_HANDLE_INVALID) {
Phil Burk29ccc292019-04-15 08:58:08 -0700398 ALOGW("%s() mServiceStreamHandle invalid", __func__);
Phil Burk5ed503c2017-02-01 09:38:15 -0800399 return AAUDIO_ERROR_INVALID_STATE;
Phil Burk204a1632017-01-03 17:23:43 -0800400 }
Phil Burkc0c70e32017-02-09 13:18:38 -0800401 return mServiceInterface.registerAudioThread(mServiceStreamHandle,
Phil Burkc0c70e32017-02-09 13:18:38 -0800402 gettid(),
403 getPeriodNanoseconds());
Phil Burk204a1632017-01-03 17:23:43 -0800404}
405
Phil Burk5ed503c2017-02-01 09:38:15 -0800406aaudio_result_t AudioStreamInternal::unregisterThread() {
Phil Burk5ed503c2017-02-01 09:38:15 -0800407 if (mServiceStreamHandle == AAUDIO_HANDLE_INVALID) {
Phil Burk29ccc292019-04-15 08:58:08 -0700408 ALOGW("%s() mServiceStreamHandle invalid", __func__);
Phil Burk5ed503c2017-02-01 09:38:15 -0800409 return AAUDIO_ERROR_INVALID_STATE;
Phil Burk204a1632017-01-03 17:23:43 -0800410 }
Phil Burk2ac035f2017-06-23 14:51:14 -0700411 return mServiceInterface.unregisterAudioThread(mServiceStreamHandle, gettid());
Phil Burk204a1632017-01-03 17:23:43 -0800412}
413
Eric Laurentcb4dae22017-07-01 19:39:32 -0700414aaudio_result_t AudioStreamInternal::startClient(const android::AudioClient& client,
Phil Burkbbd52862018-04-13 11:37:42 -0700415 audio_port_handle_t *portHandle) {
416 ALOGV("%s() called", __func__);
Eric Laurentcb4dae22017-07-01 19:39:32 -0700417 if (mServiceStreamHandle == AAUDIO_HANDLE_INVALID) {
418 return AAUDIO_ERROR_INVALID_STATE;
419 }
Phil Burkbbd52862018-04-13 11:37:42 -0700420 aaudio_result_t result = mServiceInterface.startClient(mServiceStreamHandle,
421 client, portHandle);
422 ALOGV("%s(%d) returning %d", __func__, *portHandle, result);
423 return result;
Eric Laurentcb4dae22017-07-01 19:39:32 -0700424}
425
Phil Burkbbd52862018-04-13 11:37:42 -0700426aaudio_result_t AudioStreamInternal::stopClient(audio_port_handle_t portHandle) {
427 ALOGV("%s(%d) called", __func__, portHandle);
Eric Laurentcb4dae22017-07-01 19:39:32 -0700428 if (mServiceStreamHandle == AAUDIO_HANDLE_INVALID) {
429 return AAUDIO_ERROR_INVALID_STATE;
430 }
Phil Burkbbd52862018-04-13 11:37:42 -0700431 aaudio_result_t result = mServiceInterface.stopClient(mServiceStreamHandle, portHandle);
432 ALOGV("%s(%d) returning %d", __func__, portHandle, result);
433 return result;
Eric Laurentcb4dae22017-07-01 19:39:32 -0700434}
435
Phil Burk5ed503c2017-02-01 09:38:15 -0800436aaudio_result_t AudioStreamInternal::getTimestamp(clockid_t clockId,
Phil Burk3316d5e2017-02-15 11:23:01 -0800437 int64_t *framePosition,
438 int64_t *timeNanoseconds) {
Phil Burk97350f92017-07-21 15:59:44 -0700439 // Generated in server and passed to client. Return latest.
Phil Burka53ffa62018-10-10 16:21:37 -0700440 if (mAtomicInternalTimestamp.isValid()) {
441 Timestamp timestamp = mAtomicInternalTimestamp.read();
Phil Burkbcc36742017-08-31 17:24:51 -0700442 int64_t position = timestamp.getPosition() + mFramesOffsetFromService;
443 if (position >= 0) {
444 *framePosition = position;
445 *timeNanoseconds = timestamp.getNanoseconds();
446 return AAUDIO_OK;
447 }
Phil Burk97350f92017-07-21 15:59:44 -0700448 }
Phil Burkc75d97f2017-09-08 15:48:36 -0700449 return AAUDIO_ERROR_INVALID_STATE;
Phil Burk204a1632017-01-03 17:23:43 -0800450}
451
Phil Burk0befec62017-07-28 15:12:13 -0700452aaudio_result_t AudioStreamInternal::updateStateMachine() {
Phil Burke4d7bb42017-03-28 11:32:39 -0700453 if (isDataCallbackActive()) {
454 return AAUDIO_OK; // state is getting updated by the callback thread read/write call
455 }
Phil Burk204a1632017-01-03 17:23:43 -0800456 return processCommands();
457}
458
Phil Burkec89b2e2017-06-20 15:05:06 -0700459void AudioStreamInternal::logTimestamp(AAudioServiceMessage &command) {
Phil Burk204a1632017-01-03 17:23:43 -0800460 static int64_t oldPosition = 0;
Phil Burk3316d5e2017-02-15 11:23:01 -0800461 static int64_t oldTime = 0;
Phil Burk204a1632017-01-03 17:23:43 -0800462 int64_t framePosition = command.timestamp.position;
Phil Burk3316d5e2017-02-15 11:23:01 -0800463 int64_t nanoTime = command.timestamp.timestamp;
Phil Burkbcc36742017-08-31 17:24:51 -0700464 ALOGD("logTimestamp: timestamp says framePosition = %8lld at nanoTime %lld",
Phil Burk204a1632017-01-03 17:23:43 -0800465 (long long) framePosition,
466 (long long) nanoTime);
467 int64_t nanosDelta = nanoTime - oldTime;
468 if (nanosDelta > 0 && oldTime > 0) {
469 int64_t framesDelta = framePosition - oldPosition;
Phil Burk5ed503c2017-02-01 09:38:15 -0800470 int64_t rate = (framesDelta * AAUDIO_NANOS_PER_SECOND) / nanosDelta;
Phil Burkbcc36742017-08-31 17:24:51 -0700471 ALOGD("logTimestamp: framesDelta = %8lld, nanosDelta = %8lld, rate = %lld",
Phil Burkec89b2e2017-06-20 15:05:06 -0700472 (long long) framesDelta, (long long) nanosDelta, (long long) rate);
Phil Burk204a1632017-01-03 17:23:43 -0800473 }
474 oldPosition = framePosition;
475 oldTime = nanoTime;
476}
Phil Burk204a1632017-01-03 17:23:43 -0800477
Phil Burk97350f92017-07-21 15:59:44 -0700478aaudio_result_t AudioStreamInternal::onTimestampService(AAudioServiceMessage *message) {
Phil Burk204a1632017-01-03 17:23:43 -0800479#if LOG_TIMESTAMPS
Phil Burkec89b2e2017-06-20 15:05:06 -0700480 logTimestamp(*message);
Phil Burk204a1632017-01-03 17:23:43 -0800481#endif
Phil Burk87c9f642017-05-17 07:22:39 -0700482 processTimestamp(message->timestamp.position, message->timestamp.timestamp);
Phil Burk5ed503c2017-02-01 09:38:15 -0800483 return AAUDIO_OK;
Phil Burk204a1632017-01-03 17:23:43 -0800484}
485
Phil Burk97350f92017-07-21 15:59:44 -0700486aaudio_result_t AudioStreamInternal::onTimestampHardware(AAudioServiceMessage *message) {
487 Timestamp timestamp(message->timestamp.position, message->timestamp.timestamp);
Phil Burka53ffa62018-10-10 16:21:37 -0700488 mAtomicInternalTimestamp.write(timestamp);
Phil Burk97350f92017-07-21 15:59:44 -0700489 return AAUDIO_OK;
490}
491
Phil Burk5ed503c2017-02-01 09:38:15 -0800492aaudio_result_t AudioStreamInternal::onEventFromServer(AAudioServiceMessage *message) {
493 aaudio_result_t result = AAUDIO_OK;
Phil Burk204a1632017-01-03 17:23:43 -0800494 switch (message->event.event) {
Phil Burk5ed503c2017-02-01 09:38:15 -0800495 case AAUDIO_SERVICE_EVENT_STARTED:
Phil Burkfbf031e2017-10-12 15:58:31 -0700496 ALOGD("%s - got AAUDIO_SERVICE_EVENT_STARTED", __func__);
Phil Burk87c9f642017-05-17 07:22:39 -0700497 if (getState() == AAUDIO_STREAM_STATE_STARTING) {
498 setState(AAUDIO_STREAM_STATE_STARTED);
499 }
Phil Burk204a1632017-01-03 17:23:43 -0800500 break;
Phil Burk5ed503c2017-02-01 09:38:15 -0800501 case AAUDIO_SERVICE_EVENT_PAUSED:
Phil Burkfbf031e2017-10-12 15:58:31 -0700502 ALOGD("%s - got AAUDIO_SERVICE_EVENT_PAUSED", __func__);
Phil Burk87c9f642017-05-17 07:22:39 -0700503 if (getState() == AAUDIO_STREAM_STATE_PAUSING) {
504 setState(AAUDIO_STREAM_STATE_PAUSED);
505 }
Phil Burk204a1632017-01-03 17:23:43 -0800506 break;
Phil Burk71f35bb2017-04-13 16:05:07 -0700507 case AAUDIO_SERVICE_EVENT_STOPPED:
Phil Burkfbf031e2017-10-12 15:58:31 -0700508 ALOGD("%s - got AAUDIO_SERVICE_EVENT_STOPPED", __func__);
Phil Burk87c9f642017-05-17 07:22:39 -0700509 if (getState() == AAUDIO_STREAM_STATE_STOPPING) {
510 setState(AAUDIO_STREAM_STATE_STOPPED);
511 }
Phil Burk71f35bb2017-04-13 16:05:07 -0700512 break;
Phil Burk5ed503c2017-02-01 09:38:15 -0800513 case AAUDIO_SERVICE_EVENT_FLUSHED:
Phil Burkfbf031e2017-10-12 15:58:31 -0700514 ALOGD("%s - got AAUDIO_SERVICE_EVENT_FLUSHED", __func__);
Phil Burk87c9f642017-05-17 07:22:39 -0700515 if (getState() == AAUDIO_STREAM_STATE_FLUSHING) {
516 setState(AAUDIO_STREAM_STATE_FLUSHED);
517 onFlushFromServer();
518 }
Phil Burk204a1632017-01-03 17:23:43 -0800519 break;
Phil Burk5ed503c2017-02-01 09:38:15 -0800520 case AAUDIO_SERVICE_EVENT_DISCONNECTED:
Phil Burkea04d972017-08-07 12:30:44 -0700521 // Prevent hardware from looping on old data and making buzzing sounds.
522 if (getDirection() == AAUDIO_DIRECTION_OUTPUT) {
523 mAudioEndpoint.eraseDataMemory();
524 }
Phil Burk5ed503c2017-02-01 09:38:15 -0800525 result = AAUDIO_ERROR_DISCONNECTED;
Phil Burkc0c70e32017-02-09 13:18:38 -0800526 setState(AAUDIO_STREAM_STATE_DISCONNECTED);
Phil Burkfbf031e2017-10-12 15:58:31 -0700527 ALOGW("%s - AAUDIO_SERVICE_EVENT_DISCONNECTED - FIFO cleared", __func__);
Phil Burk204a1632017-01-03 17:23:43 -0800528 break;
Phil Burkc0c70e32017-02-09 13:18:38 -0800529 case AAUDIO_SERVICE_EVENT_VOLUME:
Phil Burk55e5eab2018-04-10 15:16:38 -0700530 ALOGD("%s - AAUDIO_SERVICE_EVENT_VOLUME %lf", __func__, message->event.dataDouble);
Eric Laurenta2f296e2017-06-21 18:51:47 -0700531 mStreamVolume = (float)message->event.dataDouble;
532 doSetVolume();
Phil Burkc0c70e32017-02-09 13:18:38 -0800533 break;
Phil Burk23296382017-11-20 15:45:11 -0800534 case AAUDIO_SERVICE_EVENT_XRUN:
535 mXRunCount = static_cast<int32_t>(message->event.dataLong);
536 break;
Phil Burk204a1632017-01-03 17:23:43 -0800537 default:
Phil Burkfbf031e2017-10-12 15:58:31 -0700538 ALOGE("%s - Unrecognized event = %d", __func__, (int) message->event.event);
Phil Burk204a1632017-01-03 17:23:43 -0800539 break;
540 }
541 return result;
542}
543
Phil Burkbcc36742017-08-31 17:24:51 -0700544aaudio_result_t AudioStreamInternal::drainTimestampsFromService() {
545 aaudio_result_t result = AAUDIO_OK;
546
547 while (result == AAUDIO_OK) {
548 AAudioServiceMessage message;
549 if (mAudioEndpoint.readUpCommand(&message) != 1) {
550 break; // no command this time, no problem
551 }
552 switch (message.what) {
553 // ignore most messages
554 case AAudioServiceMessage::code::TIMESTAMP_SERVICE:
555 case AAudioServiceMessage::code::TIMESTAMP_HARDWARE:
556 break;
557
558 case AAudioServiceMessage::code::EVENT:
559 result = onEventFromServer(&message);
560 break;
561
562 default:
Phil Burkfbf031e2017-10-12 15:58:31 -0700563 ALOGE("%s - unrecognized message.what = %d", __func__, (int) message.what);
Phil Burkbcc36742017-08-31 17:24:51 -0700564 result = AAUDIO_ERROR_INTERNAL;
565 break;
566 }
567 }
568 return result;
569}
570
Phil Burk204a1632017-01-03 17:23:43 -0800571// Process all the commands coming from the server.
Phil Burk5ed503c2017-02-01 09:38:15 -0800572aaudio_result_t AudioStreamInternal::processCommands() {
573 aaudio_result_t result = AAUDIO_OK;
Phil Burk204a1632017-01-03 17:23:43 -0800574
Phil Burk5ed503c2017-02-01 09:38:15 -0800575 while (result == AAUDIO_OK) {
576 AAudioServiceMessage message;
Phil Burk204a1632017-01-03 17:23:43 -0800577 if (mAudioEndpoint.readUpCommand(&message) != 1) {
578 break; // no command this time, no problem
579 }
580 switch (message.what) {
Phil Burk97350f92017-07-21 15:59:44 -0700581 case AAudioServiceMessage::code::TIMESTAMP_SERVICE:
582 result = onTimestampService(&message);
583 break;
584
585 case AAudioServiceMessage::code::TIMESTAMP_HARDWARE:
586 result = onTimestampHardware(&message);
Phil Burk204a1632017-01-03 17:23:43 -0800587 break;
588
Phil Burk5ed503c2017-02-01 09:38:15 -0800589 case AAudioServiceMessage::code::EVENT:
Phil Burk204a1632017-01-03 17:23:43 -0800590 result = onEventFromServer(&message);
591 break;
592
593 default:
Phil Burkfbf031e2017-10-12 15:58:31 -0700594 ALOGE("%s - unrecognized message.what = %d", __func__, (int) message.what);
Phil Burk17fff382017-05-16 14:06:45 -0700595 result = AAUDIO_ERROR_INTERNAL;
Phil Burk204a1632017-01-03 17:23:43 -0800596 break;
597 }
598 }
599 return result;
600}
601
Phil Burk87c9f642017-05-17 07:22:39 -0700602// Read or write the data, block if needed and timeoutMillis > 0
603aaudio_result_t AudioStreamInternal::processData(void *buffer, int32_t numFrames,
604 int64_t timeoutNanoseconds)
Phil Burk204a1632017-01-03 17:23:43 -0800605{
Phil Burkfd34a932017-07-19 07:03:52 -0700606 const char * traceName = "aaProc";
607 const char * fifoName = "aaRdy";
Phil Burk4485d412017-05-09 15:55:02 -0700608 ATRACE_BEGIN(traceName);
Phil Burk4485d412017-05-09 15:55:02 -0700609 if (ATRACE_ENABLED()) {
Phil Burkfd34a932017-07-19 07:03:52 -0700610 int32_t fullFrames = mAudioEndpoint.getFullFramesAvailable();
611 ATRACE_INT(fifoName, fullFrames);
Phil Burk4485d412017-05-09 15:55:02 -0700612 }
613
Phil Burkec89b2e2017-06-20 15:05:06 -0700614 aaudio_result_t result = AAUDIO_OK;
615 int32_t loopCount = 0;
616 uint8_t* audioData = (uint8_t*)buffer;
617 int64_t currentTimeNanos = AudioClock::getNanoseconds();
618 const int64_t entryTimeNanos = currentTimeNanos;
619 const int64_t deadlineNanos = currentTimeNanos + timeoutNanoseconds;
620 int32_t framesLeft = numFrames;
621
Phil Burk87c9f642017-05-17 07:22:39 -0700622 // Loop until all the data has been processed or until a timeout occurs.
Phil Burk204a1632017-01-03 17:23:43 -0800623 while (framesLeft > 0) {
Phil Burkec89b2e2017-06-20 15:05:06 -0700624 // The call to processDataNow() will not block. It will just process as much as it can.
Phil Burk3316d5e2017-02-15 11:23:01 -0800625 int64_t wakeTimeNanos = 0;
Phil Burk87c9f642017-05-17 07:22:39 -0700626 aaudio_result_t framesProcessed = processDataNow(audioData, framesLeft,
627 currentTimeNanos, &wakeTimeNanos);
628 if (framesProcessed < 0) {
Phil Burk87c9f642017-05-17 07:22:39 -0700629 result = framesProcessed;
Phil Burk204a1632017-01-03 17:23:43 -0800630 break;
631 }
Phil Burk87c9f642017-05-17 07:22:39 -0700632 framesLeft -= (int32_t) framesProcessed;
633 audioData += framesProcessed * getBytesPerFrame();
Phil Burk204a1632017-01-03 17:23:43 -0800634
635 // Should we block?
636 if (timeoutNanoseconds == 0) {
637 break; // don't block
638 } else if (framesLeft > 0) {
Phil Burkfd34a932017-07-19 07:03:52 -0700639 if (!mAudioEndpoint.isFreeRunning()) {
640 // If there is software on the other end of the FIFO then it may get delayed.
641 // So wake up just a little after we expect it to be ready.
642 wakeTimeNanos += mWakeupDelayNanos;
Phil Burk204a1632017-01-03 17:23:43 -0800643 }
Phil Burkfd34a932017-07-19 07:03:52 -0700644
Phil Burk2bc7c182017-08-28 11:45:01 -0700645 currentTimeNanos = AudioClock::getNanoseconds();
646 int64_t earliestWakeTime = currentTimeNanos + mMinimumSleepNanos;
647 // Guarantee a minimum sleep time.
648 if (wakeTimeNanos < earliestWakeTime) {
649 wakeTimeNanos = earliestWakeTime;
650 }
651
Phil Burk204a1632017-01-03 17:23:43 -0800652 if (wakeTimeNanos > deadlineNanos) {
653 // If we time out, just return the framesWritten so far.
Phil Burkcf5f6d22017-05-26 12:35:07 -0700654 // TODO remove after we fix the deadline bug
Phil Burkfbf031e2017-10-12 15:58:31 -0700655 ALOGW("processData(): entered at %lld nanos, currently %lld",
Phil Burkec89b2e2017-06-20 15:05:06 -0700656 (long long) entryTimeNanos, (long long) currentTimeNanos);
Phil Burkfbf031e2017-10-12 15:58:31 -0700657 ALOGW("processData(): TIMEOUT after %lld nanos",
Phil Burkc0c70e32017-02-09 13:18:38 -0800658 (long long) timeoutNanoseconds);
Phil Burkfbf031e2017-10-12 15:58:31 -0700659 ALOGW("processData(): wakeTime = %lld, deadline = %lld nanos",
Phil Burk87c9f642017-05-17 07:22:39 -0700660 (long long) wakeTimeNanos, (long long) deadlineNanos);
Phil Burkfbf031e2017-10-12 15:58:31 -0700661 ALOGW("processData(): past deadline by %d micros",
Phil Burk87c9f642017-05-17 07:22:39 -0700662 (int)((wakeTimeNanos - deadlineNanos) / AAUDIO_NANOS_PER_MICROSECOND));
Phil Burkec89b2e2017-06-20 15:05:06 -0700663 mClockModel.dump();
664 mAudioEndpoint.dump();
Phil Burk204a1632017-01-03 17:23:43 -0800665 break;
666 }
667
Phil Burkfd34a932017-07-19 07:03:52 -0700668 if (ATRACE_ENABLED()) {
669 int32_t fullFrames = mAudioEndpoint.getFullFramesAvailable();
670 ATRACE_INT(fifoName, fullFrames);
671 int64_t sleepForNanos = wakeTimeNanos - currentTimeNanos;
672 ATRACE_INT("aaSlpNs", (int32_t)sleepForNanos);
673 }
674
675 AudioClock::sleepUntilNanoTime(wakeTimeNanos);
Phil Burk204a1632017-01-03 17:23:43 -0800676 currentTimeNanos = AudioClock::getNanoseconds();
677 }
678 }
679
Phil Burkfd34a932017-07-19 07:03:52 -0700680 if (ATRACE_ENABLED()) {
681 int32_t fullFrames = mAudioEndpoint.getFullFramesAvailable();
682 ATRACE_INT(fifoName, fullFrames);
683 }
684
Phil Burk87c9f642017-05-17 07:22:39 -0700685 // return error or framesProcessed
Phil Burkc0c70e32017-02-09 13:18:38 -0800686 (void) loopCount;
Phil Burk4485d412017-05-09 15:55:02 -0700687 ATRACE_END();
Phil Burk204a1632017-01-03 17:23:43 -0800688 return (result < 0) ? result : numFrames - framesLeft;
689}
690
Phil Burk3316d5e2017-02-15 11:23:01 -0800691void AudioStreamInternal::processTimestamp(uint64_t position, int64_t time) {
Phil Burk87c9f642017-05-17 07:22:39 -0700692 mClockModel.processTimestamp(position, time);
Phil Burk204a1632017-01-03 17:23:43 -0800693}
694
Phil Burk3316d5e2017-02-15 11:23:01 -0800695aaudio_result_t AudioStreamInternal::setBufferSize(int32_t requestedFrames) {
Phil Burk6479d502017-11-20 09:32:52 -0800696 int32_t adjustedFrames = requestedFrames;
Phil Burk3316d5e2017-02-15 11:23:01 -0800697 int32_t actualFrames = 0;
Phil Burk6479d502017-11-20 09:32:52 -0800698 int32_t maximumSize = getBufferCapacity();
699
700 // Clip to minimum size so that rounding up will work better.
701 if (adjustedFrames < 1) {
702 adjustedFrames = 1;
Phil Burk71f35bb2017-04-13 16:05:07 -0700703 }
704
Phil Burk6479d502017-11-20 09:32:52 -0800705 if (adjustedFrames > maximumSize) {
706 // Clip to maximum size.
707 adjustedFrames = maximumSize;
708 } else {
709 // Round to the next highest burst size.
710 int32_t numBursts = (adjustedFrames + mFramesPerBurst - 1) / mFramesPerBurst;
711 adjustedFrames = numBursts * mFramesPerBurst;
712 // Rounding may have gone above maximum.
713 if (adjustedFrames > maximumSize) {
714 adjustedFrames = maximumSize;
715 }
716 }
717
718 aaudio_result_t result = mAudioEndpoint.setBufferSizeInFrames(adjustedFrames, &actualFrames);
Phil Burk3316d5e2017-02-15 11:23:01 -0800719 if (result < 0) {
720 return result;
721 } else {
722 return (aaudio_result_t) actualFrames;
723 }
Phil Burk204a1632017-01-03 17:23:43 -0800724}
725
Phil Burk87c9f642017-05-17 07:22:39 -0700726int32_t AudioStreamInternal::getBufferSize() const {
Phil Burk204a1632017-01-03 17:23:43 -0800727 return mAudioEndpoint.getBufferSizeInFrames();
728}
729
Phil Burk87c9f642017-05-17 07:22:39 -0700730int32_t AudioStreamInternal::getBufferCapacity() const {
Phil Burk204a1632017-01-03 17:23:43 -0800731 return mAudioEndpoint.getBufferCapacityInFrames();
732}
733
Phil Burk87c9f642017-05-17 07:22:39 -0700734int32_t AudioStreamInternal::getFramesPerBurst() const {
Phil Burk6479d502017-11-20 09:32:52 -0800735 return mFramesPerBurst;
Phil Burk204a1632017-01-03 17:23:43 -0800736}
737
Phil Burk13d3d832019-06-10 14:36:48 -0700738// This must be called under mStreamLock.
Phil Burk87c9f642017-05-17 07:22:39 -0700739aaudio_result_t AudioStreamInternal::joinThread(void** returnArg) {
740 return AudioStream::joinThread(returnArg, calculateReasonableTimeout(getFramesPerBurst()));
Phil Burk4c5129b2017-04-28 15:17:32 -0700741}
Phil Burk377c1c22018-12-12 16:06:54 -0800742
743bool AudioStreamInternal::isClockModelInControl() const {
744 return isActive() && mAudioEndpoint.isFreeRunning() && mClockModel.isRunning();
745}