blob: 52eadd43d01235c36dc057564426a6264431fac4 [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"
Phil Burk3df348f2017-02-08 11:41:55 -080039#include "core/AudioStreamBuilder.h"
Phil Burke572f462017-04-20 13:03:19 -070040#include "fifo/FifoBuffer.h"
Phil Burkfd34a932017-07-19 07:03:52 -070041#include "utility/AudioClock.h"
Phil Burke572f462017-04-20 13:03:19 -070042
Phil Burkc0c70e32017-02-09 13:18:38 -080043#include "AudioStreamInternal.h"
Phil Burk204a1632017-01-03 17:23:43 -080044
Phil Burk204a1632017-01-03 17:23:43 -080045using android::String16;
Phil Burkdec33ab2017-01-17 14:48:16 -080046using android::Mutex;
Phil Burkc0c70e32017-02-09 13:18:38 -080047using android::WrappingBuffer;
Phil Burk204a1632017-01-03 17:23:43 -080048
Phil Burk5ed503c2017-02-01 09:38:15 -080049using namespace aaudio;
Phil Burk204a1632017-01-03 17:23:43 -080050
Phil Burke4d7bb42017-03-28 11:32:39 -070051#define MIN_TIMEOUT_NANOS (1000 * AAUDIO_NANOS_PER_MILLISECOND)
52
53// Wait at least this many times longer than the operation should take.
54#define MIN_TIMEOUT_OPERATIONS 4
55
Phil Burkbcc36742017-08-31 17:24:51 -070056#define LOG_TIMESTAMPS 0
Phil Burk87c9f642017-05-17 07:22:39 -070057
Phil Burkc0c70e32017-02-09 13:18:38 -080058AudioStreamInternal::AudioStreamInternal(AAudioServiceInterface &serviceInterface, bool inService)
Phil Burk204a1632017-01-03 17:23:43 -080059 : AudioStream()
60 , mClockModel()
61 , mAudioEndpoint()
Phil Burk5ed503c2017-02-01 09:38:15 -080062 , mServiceStreamHandle(AAUDIO_HANDLE_INVALID)
Phil Burkec89b2e2017-06-20 15:05:06 -070063 , mInService(inService)
Phil Burkfd34a932017-07-19 07:03:52 -070064 , mServiceInterface(serviceInterface)
Phil Burka53ffa62018-10-10 16:21:37 -070065 , mAtomicInternalTimestamp()
Phil Burkfd34a932017-07-19 07:03:52 -070066 , mWakeupDelayNanos(AAudioProperty_getWakeupDelayMicros() * AAUDIO_NANOS_PER_MICROSECOND)
67 , mMinimumSleepNanos(AAudioProperty_getMinimumSleepMicros() * AAUDIO_NANOS_PER_MICROSECOND)
68 {
Phil Burk204a1632017-01-03 17:23:43 -080069}
70
71AudioStreamInternal::~AudioStreamInternal() {
72}
73
Phil Burk5ed503c2017-02-01 09:38:15 -080074aaudio_result_t AudioStreamInternal::open(const AudioStreamBuilder &builder) {
Phil Burk204a1632017-01-03 17:23:43 -080075
Phil Burk5ed503c2017-02-01 09:38:15 -080076 aaudio_result_t result = AAUDIO_OK;
Phil Burk99306c82017-08-14 12:38:58 -070077 int32_t capacity;
Phil Burk6479d502017-11-20 09:32:52 -080078 int32_t framesPerBurst;
Phil Burk3c4e6b52019-01-22 15:53:36 -080079 int32_t framesPerHardwareBurst;
Phil Burk5ed503c2017-02-01 09:38:15 -080080 AAudioStreamRequest request;
Phil Burk99306c82017-08-14 12:38:58 -070081 AAudioStreamConfiguration configurationOutput;
Phil Burk204a1632017-01-03 17:23:43 -080082
Phil Burk99306c82017-08-14 12:38:58 -070083 if (getState() != AAUDIO_STREAM_STATE_UNINITIALIZED) {
Phil Burkfbf031e2017-10-12 15:58:31 -070084 ALOGE("%s - already open! state = %d", __func__, getState());
Phil Burk99306c82017-08-14 12:38:58 -070085 return AAUDIO_ERROR_INVALID_STATE;
86 }
87
88 // Copy requested parameters to the stream.
Phil Burk204a1632017-01-03 17:23:43 -080089 result = AudioStream::open(builder);
90 if (result < 0) {
91 return result;
92 }
93
Phil Burk3c4e6b52019-01-22 15:53:36 -080094 const int32_t burstMinMicros = AAudioProperty_getHardwareBurstMinMicros();
95 int32_t burstMicros = 0;
96
Phil Burkc0c70e32017-02-09 13:18:38 -080097 // We have to do volume scaling. So we prefer FLOAT format.
Phil Burk0127c1b2018-03-29 13:48:06 -070098 if (getFormat() == AUDIO_FORMAT_DEFAULT) {
99 setFormat(AUDIO_FORMAT_PCM_FLOAT);
Phil Burkc0c70e32017-02-09 13:18:38 -0800100 }
Phil Burk71f35bb2017-04-13 16:05:07 -0700101 // Request FLOAT for the shared mixer.
Phil Burk0127c1b2018-03-29 13:48:06 -0700102 request.getConfiguration().setFormat(AUDIO_FORMAT_PCM_FLOAT);
Phil Burkc0c70e32017-02-09 13:18:38 -0800103
Phil Burkdec33ab2017-01-17 14:48:16 -0800104 // Build the request to send to the server.
Phil Burk204a1632017-01-03 17:23:43 -0800105 request.setUserId(getuid());
106 request.setProcessId(getpid());
Phil Burk71f35bb2017-04-13 16:05:07 -0700107 request.setSharingModeMatchRequired(isSharingModeMatchRequired());
Phil Burk41f19d82018-02-13 14:59:10 -0800108 request.setInService(isInService());
Phil Burkc0c70e32017-02-09 13:18:38 -0800109
Phil Burk204a1632017-01-03 17:23:43 -0800110 request.getConfiguration().setDeviceId(getDeviceId());
111 request.getConfiguration().setSampleRate(getSampleRate());
112 request.getConfiguration().setSamplesPerFrame(getSamplesPerFrame());
Phil Burk39f02dd2017-08-04 09:13:31 -0700113 request.getConfiguration().setDirection(getDirection());
Phil Burk71f35bb2017-04-13 16:05:07 -0700114 request.getConfiguration().setSharingMode(getSharingMode());
115
Phil Burka62fb952018-01-16 12:44:06 -0800116 request.getConfiguration().setUsage(getUsage());
117 request.getConfiguration().setContentType(getContentType());
118 request.getConfiguration().setInputPreset(getInputPreset());
119
Phil Burk3df348f2017-02-08 11:41:55 -0800120 request.getConfiguration().setBufferCapacity(builder.getBufferCapacity());
Phil Burk204a1632017-01-03 17:23:43 -0800121
Phil Burk41f19d82018-02-13 14:59:10 -0800122 mDeviceChannelCount = getSamplesPerFrame(); // Assume it will be the same. Update if not.
123
Phil Burk99306c82017-08-14 12:38:58 -0700124 mServiceStreamHandle = mServiceInterface.openStream(request, configurationOutput);
Phil Burk41f19d82018-02-13 14:59:10 -0800125 if (mServiceStreamHandle < 0
126 && request.getConfiguration().getSamplesPerFrame() == 1 // mono?
127 && getDirection() == AAUDIO_DIRECTION_OUTPUT
128 && !isInService()) {
129 // if that failed then try switching from mono to stereo if OUTPUT.
130 // Only do this in the client. Otherwise we end up with a mono mixer in the service
131 // that writes to a stereo MMAP stream.
Phil Burk0127c1b2018-03-29 13:48:06 -0700132 ALOGD("%s() - openStream() returned %d, try switching from MONO to STEREO",
Phil Burk41f19d82018-02-13 14:59:10 -0800133 __func__, mServiceStreamHandle);
134 request.getConfiguration().setSamplesPerFrame(2); // stereo
135 mServiceStreamHandle = mServiceInterface.openStream(request, configurationOutput);
136 }
Phil Burk204a1632017-01-03 17:23:43 -0800137 if (mServiceStreamHandle < 0) {
Phil Burk41f19d82018-02-13 14:59:10 -0800138 return mServiceStreamHandle;
Phil Burk204a1632017-01-03 17:23:43 -0800139 }
Phil Burk99306c82017-08-14 12:38:58 -0700140
141 result = configurationOutput.validate();
142 if (result != AAUDIO_OK) {
143 goto error;
144 }
145 // Save results of the open.
Phil Burk41f19d82018-02-13 14:59:10 -0800146 if (getSamplesPerFrame() == AAUDIO_UNSPECIFIED) {
147 setSamplesPerFrame(configurationOutput.getSamplesPerFrame());
148 }
149 mDeviceChannelCount = configurationOutput.getSamplesPerFrame();
150
Phil Burk99306c82017-08-14 12:38:58 -0700151 setSampleRate(configurationOutput.getSampleRate());
Phil Burk99306c82017-08-14 12:38:58 -0700152 setDeviceId(configurationOutput.getDeviceId());
Phil Burk4e1af9f2018-01-03 15:54:35 -0800153 setSessionId(configurationOutput.getSessionId());
Phil Burk99306c82017-08-14 12:38:58 -0700154 setSharingMode(configurationOutput.getSharingMode());
155
Phil Burka62fb952018-01-16 12:44:06 -0800156 setUsage(configurationOutput.getUsage());
157 setContentType(configurationOutput.getContentType());
158 setInputPreset(configurationOutput.getInputPreset());
159
Phil Burk99306c82017-08-14 12:38:58 -0700160 // Save device format so we can do format conversion and volume scaling together.
Phil Burk3d786cb2018-04-09 11:58:09 -0700161 setDeviceFormat(configurationOutput.getFormat());
Phil Burk99306c82017-08-14 12:38:58 -0700162
163 result = mServiceInterface.getStreamDescription(mServiceStreamHandle, mEndPointParcelable);
164 if (result != AAUDIO_OK) {
165 goto error;
166 }
167
168 // Resolve parcelable into a descriptor.
169 result = mEndPointParcelable.resolve(&mEndpointDescriptor);
170 if (result != AAUDIO_OK) {
171 goto error;
172 }
173
174 // Configure endpoint based on descriptor.
175 result = mAudioEndpoint.configure(&mEndpointDescriptor, getDirection());
176 if (result != AAUDIO_OK) {
177 goto error;
178 }
179
Phil Burk3c4e6b52019-01-22 15:53:36 -0800180 framesPerHardwareBurst = mEndpointDescriptor.dataQueueDescriptor.framesPerBurst;
181
182 // Scale up the burst size to meet the minimum equivalent in microseconds.
183 // This is to avoid waking the CPU too often when the HW burst is very small
184 // or at high sample rates.
185 framesPerBurst = framesPerHardwareBurst;
186 do {
187 if (burstMicros > 0) { // skip first loop
188 framesPerBurst *= 2;
189 }
190 burstMicros = framesPerBurst * static_cast<int64_t>(1000000) / getSampleRate();
191 } while (burstMicros < burstMinMicros);
192 ALOGD("%s() original HW burst = %d, minMicros = %d => SW burst = %d\n",
193 __func__, framesPerHardwareBurst, burstMinMicros, framesPerBurst);
194
195 // Validate final burst size.
Phil Burk6479d502017-11-20 09:32:52 -0800196 if (framesPerBurst < MIN_FRAMES_PER_BURST || framesPerBurst > MAX_FRAMES_PER_BURST) {
197 ALOGE("%s - framesPerBurst out of range = %d", __func__, framesPerBurst);
Phil Burk99306c82017-08-14 12:38:58 -0700198 result = AAUDIO_ERROR_OUT_OF_RANGE;
199 goto error;
200 }
Phil Burk6479d502017-11-20 09:32:52 -0800201 mFramesPerBurst = framesPerBurst; // only save good value
202
203 capacity = mEndpointDescriptor.dataQueueDescriptor.capacityInFrames;
204 if (capacity < mFramesPerBurst || capacity > MAX_BUFFER_CAPACITY_IN_FRAMES) {
Phil Burkfbf031e2017-10-12 15:58:31 -0700205 ALOGE("%s - bufferCapacity out of range = %d", __func__, capacity);
Phil Burk99306c82017-08-14 12:38:58 -0700206 result = AAUDIO_ERROR_OUT_OF_RANGE;
207 goto error;
208 }
209
210 mClockModel.setSampleRate(getSampleRate());
Phil Burk3c4e6b52019-01-22 15:53:36 -0800211 mClockModel.setFramesPerBurst(framesPerHardwareBurst);
Phil Burk99306c82017-08-14 12:38:58 -0700212
Phil Burk134f1972017-12-08 13:06:11 -0800213 if (isDataCallbackSet()) {
Phil Burk99306c82017-08-14 12:38:58 -0700214 mCallbackFrames = builder.getFramesPerDataCallback();
215 if (mCallbackFrames > getBufferCapacity() / 2) {
Phil Burk29ccc292019-04-15 08:58:08 -0700216 ALOGW("%s - framesPerCallback too big = %d, capacity = %d",
Phil Burkfbf031e2017-10-12 15:58:31 -0700217 __func__, mCallbackFrames, getBufferCapacity());
Phil Burk99306c82017-08-14 12:38:58 -0700218 result = AAUDIO_ERROR_OUT_OF_RANGE;
219 goto error;
220
221 } else if (mCallbackFrames < 0) {
Phil Burk29ccc292019-04-15 08:58:08 -0700222 ALOGW("%s - framesPerCallback negative", __func__);
Phil Burk99306c82017-08-14 12:38:58 -0700223 result = AAUDIO_ERROR_OUT_OF_RANGE;
224 goto error;
225
226 }
227 if (mCallbackFrames == AAUDIO_UNSPECIFIED) {
228 mCallbackFrames = mFramesPerBurst;
229 }
230
Phil Burk0127c1b2018-03-29 13:48:06 -0700231 const int32_t callbackBufferSize = mCallbackFrames * getBytesPerFrame();
Phil Burk99306c82017-08-14 12:38:58 -0700232 mCallbackBuffer = new uint8_t[callbackBufferSize];
233 }
234
235 setState(AAUDIO_STREAM_STATE_OPEN);
Phil Burk99306c82017-08-14 12:38:58 -0700236
237 return result;
238
239error:
240 close();
Phil Burk204a1632017-01-03 17:23:43 -0800241 return result;
242}
243
Phil Burk13d3d832019-06-10 14:36:48 -0700244// This must be called under mStreamLock.
Phil Burk5ed503c2017-02-01 09:38:15 -0800245aaudio_result_t AudioStreamInternal::close() {
Phil Burk965650e2017-09-07 21:00:09 -0700246 aaudio_result_t result = AAUDIO_OK;
Phil Burk29ccc292019-04-15 08:58:08 -0700247 ALOGV("%s(): mServiceStreamHandle = 0x%08X", __func__, mServiceStreamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800248 if (mServiceStreamHandle != AAUDIO_HANDLE_INVALID) {
Phil Burk4485d412017-05-09 15:55:02 -0700249 // Don't close a stream while it is running.
250 aaudio_stream_state_t currentState = getState();
Phil Burk13d3d832019-06-10 14:36:48 -0700251 // Don't close a stream while it is running. Stop it first.
252 // If DISCONNECTED then we should still try to stop in case the
253 // error callback is still running.
254 if (isActive() || currentState == AAUDIO_STREAM_STATE_DISCONNECTED) {
Phil Burk4485d412017-05-09 15:55:02 -0700255 requestStop();
Phil Burk4485d412017-05-09 15:55:02 -0700256 }
Phil Burkec89b2e2017-06-20 15:05:06 -0700257 setState(AAUDIO_STREAM_STATE_CLOSING);
Phil Burk5ed503c2017-02-01 09:38:15 -0800258 aaudio_handle_t serviceStreamHandle = mServiceStreamHandle;
259 mServiceStreamHandle = AAUDIO_HANDLE_INVALID;
Phil Burkc0c70e32017-02-09 13:18:38 -0800260
261 mServiceInterface.closeStream(serviceStreamHandle);
Phil Burke4d7bb42017-03-28 11:32:39 -0700262 delete[] mCallbackBuffer;
Phil Burk4485d412017-05-09 15:55:02 -0700263 mCallbackBuffer = nullptr;
Phil Burk965650e2017-09-07 21:00:09 -0700264
Phil Burkec89b2e2017-06-20 15:05:06 -0700265 setState(AAUDIO_STREAM_STATE_CLOSED);
Phil Burk965650e2017-09-07 21:00:09 -0700266 result = mEndPointParcelable.close();
267 aaudio_result_t result2 = AudioStream::close();
268 return (result != AAUDIO_OK) ? result : result2;
Phil Burk204a1632017-01-03 17:23:43 -0800269 } else {
Phil Burk5ed503c2017-02-01 09:38:15 -0800270 return AAUDIO_ERROR_INVALID_HANDLE;
Phil Burk204a1632017-01-03 17:23:43 -0800271 }
272}
273
Phil Burke4d7bb42017-03-28 11:32:39 -0700274static void *aaudio_callback_thread_proc(void *context)
275{
276 AudioStreamInternal *stream = (AudioStreamInternal *)context;
Phil Burkfbf031e2017-10-12 15:58:31 -0700277 //LOGD("oboe_callback_thread, stream = %p", stream);
Phil Burke4d7bb42017-03-28 11:32:39 -0700278 if (stream != NULL) {
279 return stream->callbackLoop();
280 } else {
281 return NULL;
282 }
283}
284
Phil Burkbcc36742017-08-31 17:24:51 -0700285/*
286 * It normally takes about 20-30 msec to start a stream on the server.
287 * But the first time can take as much as 200-300 msec. The HW
288 * starts right away so by the time the client gets a chance to write into
289 * the buffer, it is already in a deep underflow state. That can cause the
290 * XRunCount to be non-zero, which could lead an app to tune its latency higher.
291 * To avoid this problem, we set a request for the processing code to start the
292 * client stream at the same position as the server stream.
293 * The processing code will then save the current offset
294 * between client and server and apply that to any position given to the app.
295 */
Phil Burk5ed503c2017-02-01 09:38:15 -0800296aaudio_result_t AudioStreamInternal::requestStart()
Phil Burk204a1632017-01-03 17:23:43 -0800297{
Phil Burk3316d5e2017-02-15 11:23:01 -0800298 int64_t startTime;
Phil Burk5ed503c2017-02-01 09:38:15 -0800299 if (mServiceStreamHandle == AAUDIO_HANDLE_INVALID) {
Phil Burk29ccc292019-04-15 08:58:08 -0700300 ALOGD("requestStart() mServiceStreamHandle invalid");
Phil Burk5ed503c2017-02-01 09:38:15 -0800301 return AAUDIO_ERROR_INVALID_STATE;
Phil Burk204a1632017-01-03 17:23:43 -0800302 }
Phil Burkec89b2e2017-06-20 15:05:06 -0700303 if (isActive()) {
Phil Burk29ccc292019-04-15 08:58:08 -0700304 ALOGD("requestStart() already active");
Phil Burkec89b2e2017-06-20 15:05:06 -0700305 return AAUDIO_ERROR_INVALID_STATE;
306 }
Phil Burkec89b2e2017-06-20 15:05:06 -0700307
Phil Burkbcc36742017-08-31 17:24:51 -0700308 aaudio_stream_state_t originalState = getState();
309 if (originalState == AAUDIO_STREAM_STATE_DISCONNECTED) {
Phil Burk29ccc292019-04-15 08:58:08 -0700310 ALOGD("requestStart() but DISCONNECTED");
Phil Burkbcc36742017-08-31 17:24:51 -0700311 return AAUDIO_ERROR_DISCONNECTED;
312 }
Phil Burkec89b2e2017-06-20 15:05:06 -0700313 setState(AAUDIO_STREAM_STATE_STARTING);
Phil Burkbcc36742017-08-31 17:24:51 -0700314
315 // Clear any stale timestamps from the previous run.
316 drainTimestampsFromService();
317
Phil Burk965650e2017-09-07 21:00:09 -0700318 aaudio_result_t result = mServiceInterface.startStream(mServiceStreamHandle);
Phil Burkc0c70e32017-02-09 13:18:38 -0800319
Phil Burk3316d5e2017-02-15 11:23:01 -0800320 startTime = AudioClock::getNanoseconds();
Phil Burk204a1632017-01-03 17:23:43 -0800321 mClockModel.start(startTime);
Phil Burkbcc36742017-08-31 17:24:51 -0700322 mNeedCatchUp.request(); // Ask data processing code to catch up when first timestamp received.
Phil Burke4d7bb42017-03-28 11:32:39 -0700323
Phil Burk965650e2017-09-07 21:00:09 -0700324 // Start data callback thread.
Phil Burk134f1972017-12-08 13:06:11 -0800325 if (result == AAUDIO_OK && isDataCallbackSet()) {
Phil Burke4d7bb42017-03-28 11:32:39 -0700326 // Launch the callback loop thread.
327 int64_t periodNanos = mCallbackFrames
328 * AAUDIO_NANOS_PER_SECOND
329 / getSampleRate();
330 mCallbackEnabled.store(true);
331 result = createThread(periodNanos, aaudio_callback_thread_proc, this);
332 }
Phil Burkec89b2e2017-06-20 15:05:06 -0700333 if (result != AAUDIO_OK) {
334 setState(originalState);
335 }
Phil Burke4d7bb42017-03-28 11:32:39 -0700336 return result;
Phil Burk204a1632017-01-03 17:23:43 -0800337}
338
Phil Burke4d7bb42017-03-28 11:32:39 -0700339int64_t AudioStreamInternal::calculateReasonableTimeout(int32_t framesPerOperation) {
340
341 // Wait for at least a second or some number of callbacks to join the thread.
Phil Burk71f35bb2017-04-13 16:05:07 -0700342 int64_t timeoutNanoseconds = (MIN_TIMEOUT_OPERATIONS
343 * framesPerOperation
344 * AAUDIO_NANOS_PER_SECOND)
345 / getSampleRate();
Phil Burke4d7bb42017-03-28 11:32:39 -0700346 if (timeoutNanoseconds < MIN_TIMEOUT_NANOS) { // arbitrary number of seconds
347 timeoutNanoseconds = MIN_TIMEOUT_NANOS;
348 }
349 return timeoutNanoseconds;
350}
351
Phil Burk87c9f642017-05-17 07:22:39 -0700352int64_t AudioStreamInternal::calculateReasonableTimeout() {
353 return calculateReasonableTimeout(getFramesPerBurst());
354}
355
Phil Burk13d3d832019-06-10 14:36:48 -0700356// This must be called under mStreamLock.
Phil Burke4d7bb42017-03-28 11:32:39 -0700357aaudio_result_t AudioStreamInternal::stopCallback()
358{
Phil Burk13d3d832019-06-10 14:36:48 -0700359 if (isDataCallbackSet()
360 && (isActive() || getState() == AAUDIO_STREAM_STATE_DISCONNECTED)) {
Phil Burke4d7bb42017-03-28 11:32:39 -0700361 mCallbackEnabled.store(false);
Phil Burk13d3d832019-06-10 14:36:48 -0700362 return joinThread(NULL); // may temporarily unlock mStreamLock
Phil Burke4d7bb42017-03-28 11:32:39 -0700363 } else {
364 return AAUDIO_OK;
365 }
366}
367
Phil Burk13d3d832019-06-10 14:36:48 -0700368// This must be called under mStreamLock.
Phil Burk1e83bee2018-12-17 14:15:20 -0800369aaudio_result_t AudioStreamInternal::requestStop() {
Phil Burk5cc83c32017-11-28 15:43:18 -0800370 aaudio_result_t result = stopCallback();
371 if (result != AAUDIO_OK) {
372 return result;
373 }
Phil Burk13d3d832019-06-10 14:36:48 -0700374 // The stream may have been unlocked temporarily to let a callback finish
375 // and the callback may have stopped the stream.
376 // Check to make sure the stream still needs to be stopped.
377 // See also AudioStream::safeStop().
378 if (!(isActive() || getState() == AAUDIO_STREAM_STATE_DISCONNECTED)) {
379 return AAUDIO_OK;
380 }
Phil Burk5cc83c32017-11-28 15:43:18 -0800381
Phil Burk71f35bb2017-04-13 16:05:07 -0700382 if (mServiceStreamHandle == AAUDIO_HANDLE_INVALID) {
Phil Burk29ccc292019-04-15 08:58:08 -0700383 ALOGW("%s() mServiceStreamHandle invalid = 0x%08X",
384 __func__, mServiceStreamHandle);
Phil Burk71f35bb2017-04-13 16:05:07 -0700385 return AAUDIO_ERROR_INVALID_STATE;
386 }
387
388 mClockModel.stop(AudioClock::getNanoseconds());
389 setState(AAUDIO_STREAM_STATE_STOPPING);
Phil Burka53ffa62018-10-10 16:21:37 -0700390 mAtomicInternalTimestamp.clear();
Phil Burk965650e2017-09-07 21:00:09 -0700391
392 return mServiceInterface.stopStream(mServiceStreamHandle);
Phil Burk71f35bb2017-04-13 16:05:07 -0700393}
394
Phil Burk5ed503c2017-02-01 09:38:15 -0800395aaudio_result_t AudioStreamInternal::registerThread() {
Phil Burk5ed503c2017-02-01 09:38:15 -0800396 if (mServiceStreamHandle == AAUDIO_HANDLE_INVALID) {
Phil Burk29ccc292019-04-15 08:58:08 -0700397 ALOGW("%s() mServiceStreamHandle invalid", __func__);
Phil Burk5ed503c2017-02-01 09:38:15 -0800398 return AAUDIO_ERROR_INVALID_STATE;
Phil Burk204a1632017-01-03 17:23:43 -0800399 }
Phil Burkc0c70e32017-02-09 13:18:38 -0800400 return mServiceInterface.registerAudioThread(mServiceStreamHandle,
Phil Burkc0c70e32017-02-09 13:18:38 -0800401 gettid(),
402 getPeriodNanoseconds());
Phil Burk204a1632017-01-03 17:23:43 -0800403}
404
Phil Burk5ed503c2017-02-01 09:38:15 -0800405aaudio_result_t AudioStreamInternal::unregisterThread() {
Phil Burk5ed503c2017-02-01 09:38:15 -0800406 if (mServiceStreamHandle == AAUDIO_HANDLE_INVALID) {
Phil Burk29ccc292019-04-15 08:58:08 -0700407 ALOGW("%s() mServiceStreamHandle invalid", __func__);
Phil Burk5ed503c2017-02-01 09:38:15 -0800408 return AAUDIO_ERROR_INVALID_STATE;
Phil Burk204a1632017-01-03 17:23:43 -0800409 }
Phil Burk2ac035f2017-06-23 14:51:14 -0700410 return mServiceInterface.unregisterAudioThread(mServiceStreamHandle, gettid());
Phil Burk204a1632017-01-03 17:23:43 -0800411}
412
Eric Laurentcb4dae22017-07-01 19:39:32 -0700413aaudio_result_t AudioStreamInternal::startClient(const android::AudioClient& client,
Phil Burkbbd52862018-04-13 11:37:42 -0700414 audio_port_handle_t *portHandle) {
415 ALOGV("%s() called", __func__);
Eric Laurentcb4dae22017-07-01 19:39:32 -0700416 if (mServiceStreamHandle == AAUDIO_HANDLE_INVALID) {
417 return AAUDIO_ERROR_INVALID_STATE;
418 }
Phil Burkbbd52862018-04-13 11:37:42 -0700419 aaudio_result_t result = mServiceInterface.startClient(mServiceStreamHandle,
420 client, portHandle);
421 ALOGV("%s(%d) returning %d", __func__, *portHandle, result);
422 return result;
Eric Laurentcb4dae22017-07-01 19:39:32 -0700423}
424
Phil Burkbbd52862018-04-13 11:37:42 -0700425aaudio_result_t AudioStreamInternal::stopClient(audio_port_handle_t portHandle) {
426 ALOGV("%s(%d) called", __func__, portHandle);
Eric Laurentcb4dae22017-07-01 19:39:32 -0700427 if (mServiceStreamHandle == AAUDIO_HANDLE_INVALID) {
428 return AAUDIO_ERROR_INVALID_STATE;
429 }
Phil Burkbbd52862018-04-13 11:37:42 -0700430 aaudio_result_t result = mServiceInterface.stopClient(mServiceStreamHandle, portHandle);
431 ALOGV("%s(%d) returning %d", __func__, portHandle, result);
432 return result;
Eric Laurentcb4dae22017-07-01 19:39:32 -0700433}
434
Phil Burk5ed503c2017-02-01 09:38:15 -0800435aaudio_result_t AudioStreamInternal::getTimestamp(clockid_t clockId,
Phil Burk3316d5e2017-02-15 11:23:01 -0800436 int64_t *framePosition,
437 int64_t *timeNanoseconds) {
Phil Burk97350f92017-07-21 15:59:44 -0700438 // Generated in server and passed to client. Return latest.
Phil Burka53ffa62018-10-10 16:21:37 -0700439 if (mAtomicInternalTimestamp.isValid()) {
440 Timestamp timestamp = mAtomicInternalTimestamp.read();
Phil Burkbcc36742017-08-31 17:24:51 -0700441 int64_t position = timestamp.getPosition() + mFramesOffsetFromService;
442 if (position >= 0) {
443 *framePosition = position;
444 *timeNanoseconds = timestamp.getNanoseconds();
445 return AAUDIO_OK;
446 }
Phil Burk97350f92017-07-21 15:59:44 -0700447 }
Phil Burkc75d97f2017-09-08 15:48:36 -0700448 return AAUDIO_ERROR_INVALID_STATE;
Phil Burk204a1632017-01-03 17:23:43 -0800449}
450
Phil Burk0befec62017-07-28 15:12:13 -0700451aaudio_result_t AudioStreamInternal::updateStateMachine() {
Phil Burke4d7bb42017-03-28 11:32:39 -0700452 if (isDataCallbackActive()) {
453 return AAUDIO_OK; // state is getting updated by the callback thread read/write call
454 }
Phil Burk204a1632017-01-03 17:23:43 -0800455 return processCommands();
456}
457
Phil Burkec89b2e2017-06-20 15:05:06 -0700458void AudioStreamInternal::logTimestamp(AAudioServiceMessage &command) {
Phil Burk204a1632017-01-03 17:23:43 -0800459 static int64_t oldPosition = 0;
Phil Burk3316d5e2017-02-15 11:23:01 -0800460 static int64_t oldTime = 0;
Phil Burk204a1632017-01-03 17:23:43 -0800461 int64_t framePosition = command.timestamp.position;
Phil Burk3316d5e2017-02-15 11:23:01 -0800462 int64_t nanoTime = command.timestamp.timestamp;
Phil Burkbcc36742017-08-31 17:24:51 -0700463 ALOGD("logTimestamp: timestamp says framePosition = %8lld at nanoTime %lld",
Phil Burk204a1632017-01-03 17:23:43 -0800464 (long long) framePosition,
465 (long long) nanoTime);
466 int64_t nanosDelta = nanoTime - oldTime;
467 if (nanosDelta > 0 && oldTime > 0) {
468 int64_t framesDelta = framePosition - oldPosition;
Phil Burk5ed503c2017-02-01 09:38:15 -0800469 int64_t rate = (framesDelta * AAUDIO_NANOS_PER_SECOND) / nanosDelta;
Phil Burkbcc36742017-08-31 17:24:51 -0700470 ALOGD("logTimestamp: framesDelta = %8lld, nanosDelta = %8lld, rate = %lld",
Phil Burkec89b2e2017-06-20 15:05:06 -0700471 (long long) framesDelta, (long long) nanosDelta, (long long) rate);
Phil Burk204a1632017-01-03 17:23:43 -0800472 }
473 oldPosition = framePosition;
474 oldTime = nanoTime;
475}
Phil Burk204a1632017-01-03 17:23:43 -0800476
Phil Burk97350f92017-07-21 15:59:44 -0700477aaudio_result_t AudioStreamInternal::onTimestampService(AAudioServiceMessage *message) {
Phil Burk204a1632017-01-03 17:23:43 -0800478#if LOG_TIMESTAMPS
Phil Burkec89b2e2017-06-20 15:05:06 -0700479 logTimestamp(*message);
Phil Burk204a1632017-01-03 17:23:43 -0800480#endif
Phil Burk87c9f642017-05-17 07:22:39 -0700481 processTimestamp(message->timestamp.position, message->timestamp.timestamp);
Phil Burk5ed503c2017-02-01 09:38:15 -0800482 return AAUDIO_OK;
Phil Burk204a1632017-01-03 17:23:43 -0800483}
484
Phil Burk97350f92017-07-21 15:59:44 -0700485aaudio_result_t AudioStreamInternal::onTimestampHardware(AAudioServiceMessage *message) {
486 Timestamp timestamp(message->timestamp.position, message->timestamp.timestamp);
Phil Burka53ffa62018-10-10 16:21:37 -0700487 mAtomicInternalTimestamp.write(timestamp);
Phil Burk97350f92017-07-21 15:59:44 -0700488 return AAUDIO_OK;
489}
490
Phil Burk5ed503c2017-02-01 09:38:15 -0800491aaudio_result_t AudioStreamInternal::onEventFromServer(AAudioServiceMessage *message) {
492 aaudio_result_t result = AAUDIO_OK;
Phil Burk204a1632017-01-03 17:23:43 -0800493 switch (message->event.event) {
Phil Burk5ed503c2017-02-01 09:38:15 -0800494 case AAUDIO_SERVICE_EVENT_STARTED:
Phil Burkfbf031e2017-10-12 15:58:31 -0700495 ALOGD("%s - got AAUDIO_SERVICE_EVENT_STARTED", __func__);
Phil Burk87c9f642017-05-17 07:22:39 -0700496 if (getState() == AAUDIO_STREAM_STATE_STARTING) {
497 setState(AAUDIO_STREAM_STATE_STARTED);
498 }
Phil Burk204a1632017-01-03 17:23:43 -0800499 break;
Phil Burk5ed503c2017-02-01 09:38:15 -0800500 case AAUDIO_SERVICE_EVENT_PAUSED:
Phil Burkfbf031e2017-10-12 15:58:31 -0700501 ALOGD("%s - got AAUDIO_SERVICE_EVENT_PAUSED", __func__);
Phil Burk87c9f642017-05-17 07:22:39 -0700502 if (getState() == AAUDIO_STREAM_STATE_PAUSING) {
503 setState(AAUDIO_STREAM_STATE_PAUSED);
504 }
Phil Burk204a1632017-01-03 17:23:43 -0800505 break;
Phil Burk71f35bb2017-04-13 16:05:07 -0700506 case AAUDIO_SERVICE_EVENT_STOPPED:
Phil Burkfbf031e2017-10-12 15:58:31 -0700507 ALOGD("%s - got AAUDIO_SERVICE_EVENT_STOPPED", __func__);
Phil Burk87c9f642017-05-17 07:22:39 -0700508 if (getState() == AAUDIO_STREAM_STATE_STOPPING) {
509 setState(AAUDIO_STREAM_STATE_STOPPED);
510 }
Phil Burk71f35bb2017-04-13 16:05:07 -0700511 break;
Phil Burk5ed503c2017-02-01 09:38:15 -0800512 case AAUDIO_SERVICE_EVENT_FLUSHED:
Phil Burkfbf031e2017-10-12 15:58:31 -0700513 ALOGD("%s - got AAUDIO_SERVICE_EVENT_FLUSHED", __func__);
Phil Burk87c9f642017-05-17 07:22:39 -0700514 if (getState() == AAUDIO_STREAM_STATE_FLUSHING) {
515 setState(AAUDIO_STREAM_STATE_FLUSHED);
516 onFlushFromServer();
517 }
Phil Burk204a1632017-01-03 17:23:43 -0800518 break;
Phil Burk5ed503c2017-02-01 09:38:15 -0800519 case AAUDIO_SERVICE_EVENT_DISCONNECTED:
Phil Burkea04d972017-08-07 12:30:44 -0700520 // Prevent hardware from looping on old data and making buzzing sounds.
521 if (getDirection() == AAUDIO_DIRECTION_OUTPUT) {
522 mAudioEndpoint.eraseDataMemory();
523 }
Phil Burk5ed503c2017-02-01 09:38:15 -0800524 result = AAUDIO_ERROR_DISCONNECTED;
Phil Burkc0c70e32017-02-09 13:18:38 -0800525 setState(AAUDIO_STREAM_STATE_DISCONNECTED);
Phil Burkfbf031e2017-10-12 15:58:31 -0700526 ALOGW("%s - AAUDIO_SERVICE_EVENT_DISCONNECTED - FIFO cleared", __func__);
Phil Burk204a1632017-01-03 17:23:43 -0800527 break;
Phil Burkc0c70e32017-02-09 13:18:38 -0800528 case AAUDIO_SERVICE_EVENT_VOLUME:
Phil Burk55e5eab2018-04-10 15:16:38 -0700529 ALOGD("%s - AAUDIO_SERVICE_EVENT_VOLUME %lf", __func__, message->event.dataDouble);
Eric Laurenta2f296e2017-06-21 18:51:47 -0700530 mStreamVolume = (float)message->event.dataDouble;
531 doSetVolume();
Phil Burkc0c70e32017-02-09 13:18:38 -0800532 break;
Phil Burk23296382017-11-20 15:45:11 -0800533 case AAUDIO_SERVICE_EVENT_XRUN:
534 mXRunCount = static_cast<int32_t>(message->event.dataLong);
535 break;
Phil Burk204a1632017-01-03 17:23:43 -0800536 default:
Phil Burkfbf031e2017-10-12 15:58:31 -0700537 ALOGE("%s - Unrecognized event = %d", __func__, (int) message->event.event);
Phil Burk204a1632017-01-03 17:23:43 -0800538 break;
539 }
540 return result;
541}
542
Phil Burkbcc36742017-08-31 17:24:51 -0700543aaudio_result_t AudioStreamInternal::drainTimestampsFromService() {
544 aaudio_result_t result = AAUDIO_OK;
545
546 while (result == AAUDIO_OK) {
547 AAudioServiceMessage message;
548 if (mAudioEndpoint.readUpCommand(&message) != 1) {
549 break; // no command this time, no problem
550 }
551 switch (message.what) {
552 // ignore most messages
553 case AAudioServiceMessage::code::TIMESTAMP_SERVICE:
554 case AAudioServiceMessage::code::TIMESTAMP_HARDWARE:
555 break;
556
557 case AAudioServiceMessage::code::EVENT:
558 result = onEventFromServer(&message);
559 break;
560
561 default:
Phil Burkfbf031e2017-10-12 15:58:31 -0700562 ALOGE("%s - unrecognized message.what = %d", __func__, (int) message.what);
Phil Burkbcc36742017-08-31 17:24:51 -0700563 result = AAUDIO_ERROR_INTERNAL;
564 break;
565 }
566 }
567 return result;
568}
569
Phil Burk204a1632017-01-03 17:23:43 -0800570// Process all the commands coming from the server.
Phil Burk5ed503c2017-02-01 09:38:15 -0800571aaudio_result_t AudioStreamInternal::processCommands() {
572 aaudio_result_t result = AAUDIO_OK;
Phil Burk204a1632017-01-03 17:23:43 -0800573
Phil Burk5ed503c2017-02-01 09:38:15 -0800574 while (result == AAUDIO_OK) {
575 AAudioServiceMessage message;
Phil Burk204a1632017-01-03 17:23:43 -0800576 if (mAudioEndpoint.readUpCommand(&message) != 1) {
577 break; // no command this time, no problem
578 }
579 switch (message.what) {
Phil Burk97350f92017-07-21 15:59:44 -0700580 case AAudioServiceMessage::code::TIMESTAMP_SERVICE:
581 result = onTimestampService(&message);
582 break;
583
584 case AAudioServiceMessage::code::TIMESTAMP_HARDWARE:
585 result = onTimestampHardware(&message);
Phil Burk204a1632017-01-03 17:23:43 -0800586 break;
587
Phil Burk5ed503c2017-02-01 09:38:15 -0800588 case AAudioServiceMessage::code::EVENT:
Phil Burk204a1632017-01-03 17:23:43 -0800589 result = onEventFromServer(&message);
590 break;
591
592 default:
Phil Burkfbf031e2017-10-12 15:58:31 -0700593 ALOGE("%s - unrecognized message.what = %d", __func__, (int) message.what);
Phil Burk17fff382017-05-16 14:06:45 -0700594 result = AAUDIO_ERROR_INTERNAL;
Phil Burk204a1632017-01-03 17:23:43 -0800595 break;
596 }
597 }
598 return result;
599}
600
Phil Burk87c9f642017-05-17 07:22:39 -0700601// Read or write the data, block if needed and timeoutMillis > 0
602aaudio_result_t AudioStreamInternal::processData(void *buffer, int32_t numFrames,
603 int64_t timeoutNanoseconds)
Phil Burk204a1632017-01-03 17:23:43 -0800604{
Phil Burkfd34a932017-07-19 07:03:52 -0700605 const char * traceName = "aaProc";
606 const char * fifoName = "aaRdy";
Phil Burk4485d412017-05-09 15:55:02 -0700607 ATRACE_BEGIN(traceName);
Phil Burk4485d412017-05-09 15:55:02 -0700608 if (ATRACE_ENABLED()) {
Phil Burkfd34a932017-07-19 07:03:52 -0700609 int32_t fullFrames = mAudioEndpoint.getFullFramesAvailable();
610 ATRACE_INT(fifoName, fullFrames);
Phil Burk4485d412017-05-09 15:55:02 -0700611 }
612
Phil Burkec89b2e2017-06-20 15:05:06 -0700613 aaudio_result_t result = AAUDIO_OK;
614 int32_t loopCount = 0;
615 uint8_t* audioData = (uint8_t*)buffer;
616 int64_t currentTimeNanos = AudioClock::getNanoseconds();
617 const int64_t entryTimeNanos = currentTimeNanos;
618 const int64_t deadlineNanos = currentTimeNanos + timeoutNanoseconds;
619 int32_t framesLeft = numFrames;
620
Phil Burk87c9f642017-05-17 07:22:39 -0700621 // Loop until all the data has been processed or until a timeout occurs.
Phil Burk204a1632017-01-03 17:23:43 -0800622 while (framesLeft > 0) {
Phil Burkec89b2e2017-06-20 15:05:06 -0700623 // The call to processDataNow() will not block. It will just process as much as it can.
Phil Burk3316d5e2017-02-15 11:23:01 -0800624 int64_t wakeTimeNanos = 0;
Phil Burk87c9f642017-05-17 07:22:39 -0700625 aaudio_result_t framesProcessed = processDataNow(audioData, framesLeft,
626 currentTimeNanos, &wakeTimeNanos);
627 if (framesProcessed < 0) {
Phil Burk87c9f642017-05-17 07:22:39 -0700628 result = framesProcessed;
Phil Burk204a1632017-01-03 17:23:43 -0800629 break;
630 }
Phil Burk87c9f642017-05-17 07:22:39 -0700631 framesLeft -= (int32_t) framesProcessed;
632 audioData += framesProcessed * getBytesPerFrame();
Phil Burk204a1632017-01-03 17:23:43 -0800633
634 // Should we block?
635 if (timeoutNanoseconds == 0) {
636 break; // don't block
637 } else if (framesLeft > 0) {
Phil Burkfd34a932017-07-19 07:03:52 -0700638 if (!mAudioEndpoint.isFreeRunning()) {
639 // If there is software on the other end of the FIFO then it may get delayed.
640 // So wake up just a little after we expect it to be ready.
641 wakeTimeNanos += mWakeupDelayNanos;
Phil Burk204a1632017-01-03 17:23:43 -0800642 }
Phil Burkfd34a932017-07-19 07:03:52 -0700643
Phil Burk2bc7c182017-08-28 11:45:01 -0700644 currentTimeNanos = AudioClock::getNanoseconds();
645 int64_t earliestWakeTime = currentTimeNanos + mMinimumSleepNanos;
646 // Guarantee a minimum sleep time.
647 if (wakeTimeNanos < earliestWakeTime) {
648 wakeTimeNanos = earliestWakeTime;
649 }
650
Phil Burk204a1632017-01-03 17:23:43 -0800651 if (wakeTimeNanos > deadlineNanos) {
652 // If we time out, just return the framesWritten so far.
Phil Burkcf5f6d22017-05-26 12:35:07 -0700653 // TODO remove after we fix the deadline bug
Phil Burkfbf031e2017-10-12 15:58:31 -0700654 ALOGW("processData(): entered at %lld nanos, currently %lld",
Phil Burkec89b2e2017-06-20 15:05:06 -0700655 (long long) entryTimeNanos, (long long) currentTimeNanos);
Phil Burkfbf031e2017-10-12 15:58:31 -0700656 ALOGW("processData(): TIMEOUT after %lld nanos",
Phil Burkc0c70e32017-02-09 13:18:38 -0800657 (long long) timeoutNanoseconds);
Phil Burkfbf031e2017-10-12 15:58:31 -0700658 ALOGW("processData(): wakeTime = %lld, deadline = %lld nanos",
Phil Burk87c9f642017-05-17 07:22:39 -0700659 (long long) wakeTimeNanos, (long long) deadlineNanos);
Phil Burkfbf031e2017-10-12 15:58:31 -0700660 ALOGW("processData(): past deadline by %d micros",
Phil Burk87c9f642017-05-17 07:22:39 -0700661 (int)((wakeTimeNanos - deadlineNanos) / AAUDIO_NANOS_PER_MICROSECOND));
Phil Burkec89b2e2017-06-20 15:05:06 -0700662 mClockModel.dump();
663 mAudioEndpoint.dump();
Phil Burk204a1632017-01-03 17:23:43 -0800664 break;
665 }
666
Phil Burkfd34a932017-07-19 07:03:52 -0700667 if (ATRACE_ENABLED()) {
668 int32_t fullFrames = mAudioEndpoint.getFullFramesAvailable();
669 ATRACE_INT(fifoName, fullFrames);
670 int64_t sleepForNanos = wakeTimeNanos - currentTimeNanos;
671 ATRACE_INT("aaSlpNs", (int32_t)sleepForNanos);
672 }
673
674 AudioClock::sleepUntilNanoTime(wakeTimeNanos);
Phil Burk204a1632017-01-03 17:23:43 -0800675 currentTimeNanos = AudioClock::getNanoseconds();
676 }
677 }
678
Phil Burkfd34a932017-07-19 07:03:52 -0700679 if (ATRACE_ENABLED()) {
680 int32_t fullFrames = mAudioEndpoint.getFullFramesAvailable();
681 ATRACE_INT(fifoName, fullFrames);
682 }
683
Phil Burk87c9f642017-05-17 07:22:39 -0700684 // return error or framesProcessed
Phil Burkc0c70e32017-02-09 13:18:38 -0800685 (void) loopCount;
Phil Burk4485d412017-05-09 15:55:02 -0700686 ATRACE_END();
Phil Burk204a1632017-01-03 17:23:43 -0800687 return (result < 0) ? result : numFrames - framesLeft;
688}
689
Phil Burk3316d5e2017-02-15 11:23:01 -0800690void AudioStreamInternal::processTimestamp(uint64_t position, int64_t time) {
Phil Burk87c9f642017-05-17 07:22:39 -0700691 mClockModel.processTimestamp(position, time);
Phil Burk204a1632017-01-03 17:23:43 -0800692}
693
Phil Burk3316d5e2017-02-15 11:23:01 -0800694aaudio_result_t AudioStreamInternal::setBufferSize(int32_t requestedFrames) {
Phil Burk6479d502017-11-20 09:32:52 -0800695 int32_t adjustedFrames = requestedFrames;
Phil Burk3316d5e2017-02-15 11:23:01 -0800696 int32_t actualFrames = 0;
Phil Burk6479d502017-11-20 09:32:52 -0800697 int32_t maximumSize = getBufferCapacity();
698
699 // Clip to minimum size so that rounding up will work better.
700 if (adjustedFrames < 1) {
701 adjustedFrames = 1;
Phil Burk71f35bb2017-04-13 16:05:07 -0700702 }
703
Phil Burk6479d502017-11-20 09:32:52 -0800704 if (adjustedFrames > maximumSize) {
705 // Clip to maximum size.
706 adjustedFrames = maximumSize;
707 } else {
708 // Round to the next highest burst size.
709 int32_t numBursts = (adjustedFrames + mFramesPerBurst - 1) / mFramesPerBurst;
710 adjustedFrames = numBursts * mFramesPerBurst;
711 // Rounding may have gone above maximum.
712 if (adjustedFrames > maximumSize) {
713 adjustedFrames = maximumSize;
714 }
715 }
716
717 aaudio_result_t result = mAudioEndpoint.setBufferSizeInFrames(adjustedFrames, &actualFrames);
Phil Burk3316d5e2017-02-15 11:23:01 -0800718 if (result < 0) {
719 return result;
720 } else {
721 return (aaudio_result_t) actualFrames;
722 }
Phil Burk204a1632017-01-03 17:23:43 -0800723}
724
Phil Burk87c9f642017-05-17 07:22:39 -0700725int32_t AudioStreamInternal::getBufferSize() const {
Phil Burk204a1632017-01-03 17:23:43 -0800726 return mAudioEndpoint.getBufferSizeInFrames();
727}
728
Phil Burk87c9f642017-05-17 07:22:39 -0700729int32_t AudioStreamInternal::getBufferCapacity() const {
Phil Burk204a1632017-01-03 17:23:43 -0800730 return mAudioEndpoint.getBufferCapacityInFrames();
731}
732
Phil Burk87c9f642017-05-17 07:22:39 -0700733int32_t AudioStreamInternal::getFramesPerBurst() const {
Phil Burk6479d502017-11-20 09:32:52 -0800734 return mFramesPerBurst;
Phil Burk204a1632017-01-03 17:23:43 -0800735}
736
Phil Burk13d3d832019-06-10 14:36:48 -0700737// This must be called under mStreamLock.
Phil Burk87c9f642017-05-17 07:22:39 -0700738aaudio_result_t AudioStreamInternal::joinThread(void** returnArg) {
739 return AudioStream::joinThread(returnArg, calculateReasonableTimeout(getFramesPerBurst()));
Phil Burk4c5129b2017-04-28 15:17:32 -0700740}
Phil Burk377c1c22018-12-12 16:06:54 -0800741
742bool AudioStreamInternal::isClockModelInControl() const {
743 return isActive() && mAudioEndpoint.isFreeRunning() && mClockModel.isRunning();
744}