blob: fffcda016e18534aa453455ad3f2f2adc715f2e1 [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 Burkbcc36742017-08-31 17:24:51 -070065 , mAtomicTimestamp()
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 Burk5ed503c2017-02-01 09:38:15 -080079 AAudioStreamRequest request;
Phil Burk99306c82017-08-14 12:38:58 -070080 AAudioStreamConfiguration configurationOutput;
Phil Burk204a1632017-01-03 17:23:43 -080081
Phil Burk99306c82017-08-14 12:38:58 -070082 if (getState() != AAUDIO_STREAM_STATE_UNINITIALIZED) {
Phil Burkfbf031e2017-10-12 15:58:31 -070083 ALOGE("%s - already open! state = %d", __func__, getState());
Phil Burk99306c82017-08-14 12:38:58 -070084 return AAUDIO_ERROR_INVALID_STATE;
85 }
86
87 // Copy requested parameters to the stream.
Phil Burk204a1632017-01-03 17:23:43 -080088 result = AudioStream::open(builder);
89 if (result < 0) {
90 return result;
91 }
92
Phil Burkc0c70e32017-02-09 13:18:38 -080093 // We have to do volume scaling. So we prefer FLOAT format.
Phil Burk0127c1b2018-03-29 13:48:06 -070094 if (getFormat() == AUDIO_FORMAT_DEFAULT) {
95 setFormat(AUDIO_FORMAT_PCM_FLOAT);
Phil Burkc0c70e32017-02-09 13:18:38 -080096 }
Phil Burk71f35bb2017-04-13 16:05:07 -070097 // Request FLOAT for the shared mixer.
Phil Burk0127c1b2018-03-29 13:48:06 -070098 request.getConfiguration().setFormat(AUDIO_FORMAT_PCM_FLOAT);
Phil Burkc0c70e32017-02-09 13:18:38 -080099
Phil Burkdec33ab2017-01-17 14:48:16 -0800100 // Build the request to send to the server.
Phil Burk204a1632017-01-03 17:23:43 -0800101 request.setUserId(getuid());
102 request.setProcessId(getpid());
Phil Burk71f35bb2017-04-13 16:05:07 -0700103 request.setSharingModeMatchRequired(isSharingModeMatchRequired());
Phil Burk41f19d82018-02-13 14:59:10 -0800104 request.setInService(isInService());
Phil Burkc0c70e32017-02-09 13:18:38 -0800105
Phil Burk204a1632017-01-03 17:23:43 -0800106 request.getConfiguration().setDeviceId(getDeviceId());
107 request.getConfiguration().setSampleRate(getSampleRate());
108 request.getConfiguration().setSamplesPerFrame(getSamplesPerFrame());
Phil Burk39f02dd2017-08-04 09:13:31 -0700109 request.getConfiguration().setDirection(getDirection());
Phil Burk71f35bb2017-04-13 16:05:07 -0700110 request.getConfiguration().setSharingMode(getSharingMode());
111
Phil Burka62fb952018-01-16 12:44:06 -0800112 request.getConfiguration().setUsage(getUsage());
113 request.getConfiguration().setContentType(getContentType());
114 request.getConfiguration().setInputPreset(getInputPreset());
115
Phil Burk3df348f2017-02-08 11:41:55 -0800116 request.getConfiguration().setBufferCapacity(builder.getBufferCapacity());
Phil Burk204a1632017-01-03 17:23:43 -0800117
Phil Burk41f19d82018-02-13 14:59:10 -0800118 mDeviceChannelCount = getSamplesPerFrame(); // Assume it will be the same. Update if not.
119
Phil Burk99306c82017-08-14 12:38:58 -0700120 mServiceStreamHandle = mServiceInterface.openStream(request, configurationOutput);
Phil Burk41f19d82018-02-13 14:59:10 -0800121 if (mServiceStreamHandle < 0
122 && request.getConfiguration().getSamplesPerFrame() == 1 // mono?
123 && getDirection() == AAUDIO_DIRECTION_OUTPUT
124 && !isInService()) {
125 // if that failed then try switching from mono to stereo if OUTPUT.
126 // Only do this in the client. Otherwise we end up with a mono mixer in the service
127 // that writes to a stereo MMAP stream.
Phil Burk0127c1b2018-03-29 13:48:06 -0700128 ALOGD("%s() - openStream() returned %d, try switching from MONO to STEREO",
Phil Burk41f19d82018-02-13 14:59:10 -0800129 __func__, mServiceStreamHandle);
130 request.getConfiguration().setSamplesPerFrame(2); // stereo
131 mServiceStreamHandle = mServiceInterface.openStream(request, configurationOutput);
132 }
Phil Burk204a1632017-01-03 17:23:43 -0800133 if (mServiceStreamHandle < 0) {
Phil Burk41f19d82018-02-13 14:59:10 -0800134 return mServiceStreamHandle;
Phil Burk204a1632017-01-03 17:23:43 -0800135 }
Phil Burk99306c82017-08-14 12:38:58 -0700136
137 result = configurationOutput.validate();
138 if (result != AAUDIO_OK) {
139 goto error;
140 }
141 // Save results of the open.
Phil Burk41f19d82018-02-13 14:59:10 -0800142 if (getSamplesPerFrame() == AAUDIO_UNSPECIFIED) {
143 setSamplesPerFrame(configurationOutput.getSamplesPerFrame());
144 }
145 mDeviceChannelCount = configurationOutput.getSamplesPerFrame();
146
Phil Burk99306c82017-08-14 12:38:58 -0700147 setSampleRate(configurationOutput.getSampleRate());
Phil Burk99306c82017-08-14 12:38:58 -0700148 setDeviceId(configurationOutput.getDeviceId());
Phil Burk4e1af9f2018-01-03 15:54:35 -0800149 setSessionId(configurationOutput.getSessionId());
Phil Burk99306c82017-08-14 12:38:58 -0700150 setSharingMode(configurationOutput.getSharingMode());
151
Phil Burka62fb952018-01-16 12:44:06 -0800152 setUsage(configurationOutput.getUsage());
153 setContentType(configurationOutput.getContentType());
154 setInputPreset(configurationOutput.getInputPreset());
155
Phil Burk99306c82017-08-14 12:38:58 -0700156 // Save device format so we can do format conversion and volume scaling together.
Phil Burk3d786cb2018-04-09 11:58:09 -0700157 setDeviceFormat(configurationOutput.getFormat());
Phil Burk99306c82017-08-14 12:38:58 -0700158
159 result = mServiceInterface.getStreamDescription(mServiceStreamHandle, mEndPointParcelable);
160 if (result != AAUDIO_OK) {
161 goto error;
162 }
163
164 // Resolve parcelable into a descriptor.
165 result = mEndPointParcelable.resolve(&mEndpointDescriptor);
166 if (result != AAUDIO_OK) {
167 goto error;
168 }
169
170 // Configure endpoint based on descriptor.
171 result = mAudioEndpoint.configure(&mEndpointDescriptor, getDirection());
172 if (result != AAUDIO_OK) {
173 goto error;
174 }
175
Phil Burk99306c82017-08-14 12:38:58 -0700176 // Validate result from server.
Phil Burk6479d502017-11-20 09:32:52 -0800177 framesPerBurst = mEndpointDescriptor.dataQueueDescriptor.framesPerBurst;
178 if (framesPerBurst < MIN_FRAMES_PER_BURST || framesPerBurst > MAX_FRAMES_PER_BURST) {
179 ALOGE("%s - framesPerBurst out of range = %d", __func__, framesPerBurst);
Phil Burk99306c82017-08-14 12:38:58 -0700180 result = AAUDIO_ERROR_OUT_OF_RANGE;
181 goto error;
182 }
Phil Burk6479d502017-11-20 09:32:52 -0800183 mFramesPerBurst = framesPerBurst; // only save good value
184
185 capacity = mEndpointDescriptor.dataQueueDescriptor.capacityInFrames;
186 if (capacity < mFramesPerBurst || capacity > MAX_BUFFER_CAPACITY_IN_FRAMES) {
Phil Burkfbf031e2017-10-12 15:58:31 -0700187 ALOGE("%s - bufferCapacity out of range = %d", __func__, capacity);
Phil Burk99306c82017-08-14 12:38:58 -0700188 result = AAUDIO_ERROR_OUT_OF_RANGE;
189 goto error;
190 }
191
192 mClockModel.setSampleRate(getSampleRate());
193 mClockModel.setFramesPerBurst(mFramesPerBurst);
194
Phil Burk134f1972017-12-08 13:06:11 -0800195 if (isDataCallbackSet()) {
Phil Burk99306c82017-08-14 12:38:58 -0700196 mCallbackFrames = builder.getFramesPerDataCallback();
197 if (mCallbackFrames > getBufferCapacity() / 2) {
Phil Burkfbf031e2017-10-12 15:58:31 -0700198 ALOGE("%s - framesPerCallback too big = %d, capacity = %d",
199 __func__, mCallbackFrames, getBufferCapacity());
Phil Burk99306c82017-08-14 12:38:58 -0700200 result = AAUDIO_ERROR_OUT_OF_RANGE;
201 goto error;
202
203 } else if (mCallbackFrames < 0) {
Phil Burkfbf031e2017-10-12 15:58:31 -0700204 ALOGE("%s - framesPerCallback negative", __func__);
Phil Burk99306c82017-08-14 12:38:58 -0700205 result = AAUDIO_ERROR_OUT_OF_RANGE;
206 goto error;
207
208 }
209 if (mCallbackFrames == AAUDIO_UNSPECIFIED) {
210 mCallbackFrames = mFramesPerBurst;
211 }
212
Phil Burk0127c1b2018-03-29 13:48:06 -0700213 const int32_t callbackBufferSize = mCallbackFrames * getBytesPerFrame();
Phil Burk99306c82017-08-14 12:38:58 -0700214 mCallbackBuffer = new uint8_t[callbackBufferSize];
215 }
216
217 setState(AAUDIO_STREAM_STATE_OPEN);
Phil Burk99306c82017-08-14 12:38:58 -0700218
219 return result;
220
221error:
222 close();
Phil Burk204a1632017-01-03 17:23:43 -0800223 return result;
224}
225
Phil Burk5ed503c2017-02-01 09:38:15 -0800226aaudio_result_t AudioStreamInternal::close() {
Phil Burk965650e2017-09-07 21:00:09 -0700227 aaudio_result_t result = AAUDIO_OK;
Phil Burk19e990e2018-03-22 13:59:34 -0700228 ALOGD("%s(): mServiceStreamHandle = 0x%08X", __func__, mServiceStreamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800229 if (mServiceStreamHandle != AAUDIO_HANDLE_INVALID) {
Phil Burk4485d412017-05-09 15:55:02 -0700230 // Don't close a stream while it is running.
231 aaudio_stream_state_t currentState = getState();
Phil Burk87c9f642017-05-17 07:22:39 -0700232 if (isActive()) {
Phil Burk4485d412017-05-09 15:55:02 -0700233 requestStop();
234 aaudio_stream_state_t nextState;
235 int64_t timeoutNanoseconds = MIN_TIMEOUT_NANOS;
Phil Burk965650e2017-09-07 21:00:09 -0700236 result = waitForStateChange(currentState, &nextState,
Phil Burk4485d412017-05-09 15:55:02 -0700237 timeoutNanoseconds);
238 if (result != AAUDIO_OK) {
Phil Burk19e990e2018-03-22 13:59:34 -0700239 ALOGE("%s() waitForStateChange() returned %d %s",
240 __func__, result, AAudio_convertResultToText(result));
Phil Burk4485d412017-05-09 15:55:02 -0700241 }
242 }
Phil Burkec89b2e2017-06-20 15:05:06 -0700243 setState(AAUDIO_STREAM_STATE_CLOSING);
Phil Burk5ed503c2017-02-01 09:38:15 -0800244 aaudio_handle_t serviceStreamHandle = mServiceStreamHandle;
245 mServiceStreamHandle = AAUDIO_HANDLE_INVALID;
Phil Burkc0c70e32017-02-09 13:18:38 -0800246
247 mServiceInterface.closeStream(serviceStreamHandle);
Phil Burke4d7bb42017-03-28 11:32:39 -0700248 delete[] mCallbackBuffer;
Phil Burk4485d412017-05-09 15:55:02 -0700249 mCallbackBuffer = nullptr;
Phil Burk965650e2017-09-07 21:00:09 -0700250
Phil Burkec89b2e2017-06-20 15:05:06 -0700251 setState(AAUDIO_STREAM_STATE_CLOSED);
Phil Burk965650e2017-09-07 21:00:09 -0700252 result = mEndPointParcelable.close();
253 aaudio_result_t result2 = AudioStream::close();
254 return (result != AAUDIO_OK) ? result : result2;
Phil Burk204a1632017-01-03 17:23:43 -0800255 } else {
Phil Burk5ed503c2017-02-01 09:38:15 -0800256 return AAUDIO_ERROR_INVALID_HANDLE;
Phil Burk204a1632017-01-03 17:23:43 -0800257 }
258}
259
Phil Burke4d7bb42017-03-28 11:32:39 -0700260static void *aaudio_callback_thread_proc(void *context)
261{
262 AudioStreamInternal *stream = (AudioStreamInternal *)context;
Phil Burkfbf031e2017-10-12 15:58:31 -0700263 //LOGD("oboe_callback_thread, stream = %p", stream);
Phil Burke4d7bb42017-03-28 11:32:39 -0700264 if (stream != NULL) {
265 return stream->callbackLoop();
266 } else {
267 return NULL;
268 }
269}
270
Phil Burkbcc36742017-08-31 17:24:51 -0700271/*
272 * It normally takes about 20-30 msec to start a stream on the server.
273 * But the first time can take as much as 200-300 msec. The HW
274 * starts right away so by the time the client gets a chance to write into
275 * the buffer, it is already in a deep underflow state. That can cause the
276 * XRunCount to be non-zero, which could lead an app to tune its latency higher.
277 * To avoid this problem, we set a request for the processing code to start the
278 * client stream at the same position as the server stream.
279 * The processing code will then save the current offset
280 * between client and server and apply that to any position given to the app.
281 */
Phil Burk5ed503c2017-02-01 09:38:15 -0800282aaudio_result_t AudioStreamInternal::requestStart()
Phil Burk204a1632017-01-03 17:23:43 -0800283{
Phil Burk3316d5e2017-02-15 11:23:01 -0800284 int64_t startTime;
Phil Burk5ed503c2017-02-01 09:38:15 -0800285 if (mServiceStreamHandle == AAUDIO_HANDLE_INVALID) {
Phil Burkbcc36742017-08-31 17:24:51 -0700286 ALOGE("requestStart() mServiceStreamHandle invalid");
Phil Burk5ed503c2017-02-01 09:38:15 -0800287 return AAUDIO_ERROR_INVALID_STATE;
Phil Burk204a1632017-01-03 17:23:43 -0800288 }
Phil Burkec89b2e2017-06-20 15:05:06 -0700289 if (isActive()) {
Phil Burkbcc36742017-08-31 17:24:51 -0700290 ALOGE("requestStart() already active");
Phil Burkec89b2e2017-06-20 15:05:06 -0700291 return AAUDIO_ERROR_INVALID_STATE;
292 }
Phil Burkec89b2e2017-06-20 15:05:06 -0700293
Phil Burkbcc36742017-08-31 17:24:51 -0700294 aaudio_stream_state_t originalState = getState();
295 if (originalState == AAUDIO_STREAM_STATE_DISCONNECTED) {
296 ALOGE("requestStart() but DISCONNECTED");
297 return AAUDIO_ERROR_DISCONNECTED;
298 }
Phil Burkec89b2e2017-06-20 15:05:06 -0700299 setState(AAUDIO_STREAM_STATE_STARTING);
Phil Burkbcc36742017-08-31 17:24:51 -0700300
301 // Clear any stale timestamps from the previous run.
302 drainTimestampsFromService();
303
Phil Burk965650e2017-09-07 21:00:09 -0700304 aaudio_result_t result = mServiceInterface.startStream(mServiceStreamHandle);
Phil Burkc0c70e32017-02-09 13:18:38 -0800305
Phil Burk3316d5e2017-02-15 11:23:01 -0800306 startTime = AudioClock::getNanoseconds();
Phil Burk204a1632017-01-03 17:23:43 -0800307 mClockModel.start(startTime);
Phil Burkbcc36742017-08-31 17:24:51 -0700308 mNeedCatchUp.request(); // Ask data processing code to catch up when first timestamp received.
Phil Burke4d7bb42017-03-28 11:32:39 -0700309
Phil Burk965650e2017-09-07 21:00:09 -0700310 // Start data callback thread.
Phil Burk134f1972017-12-08 13:06:11 -0800311 if (result == AAUDIO_OK && isDataCallbackSet()) {
Phil Burke4d7bb42017-03-28 11:32:39 -0700312 // Launch the callback loop thread.
313 int64_t periodNanos = mCallbackFrames
314 * AAUDIO_NANOS_PER_SECOND
315 / getSampleRate();
316 mCallbackEnabled.store(true);
317 result = createThread(periodNanos, aaudio_callback_thread_proc, this);
318 }
Phil Burkec89b2e2017-06-20 15:05:06 -0700319 if (result != AAUDIO_OK) {
320 setState(originalState);
321 }
Phil Burke4d7bb42017-03-28 11:32:39 -0700322 return result;
Phil Burk204a1632017-01-03 17:23:43 -0800323}
324
Phil Burke4d7bb42017-03-28 11:32:39 -0700325int64_t AudioStreamInternal::calculateReasonableTimeout(int32_t framesPerOperation) {
326
327 // Wait for at least a second or some number of callbacks to join the thread.
Phil Burk71f35bb2017-04-13 16:05:07 -0700328 int64_t timeoutNanoseconds = (MIN_TIMEOUT_OPERATIONS
329 * framesPerOperation
330 * AAUDIO_NANOS_PER_SECOND)
331 / getSampleRate();
Phil Burke4d7bb42017-03-28 11:32:39 -0700332 if (timeoutNanoseconds < MIN_TIMEOUT_NANOS) { // arbitrary number of seconds
333 timeoutNanoseconds = MIN_TIMEOUT_NANOS;
334 }
335 return timeoutNanoseconds;
336}
337
Phil Burk87c9f642017-05-17 07:22:39 -0700338int64_t AudioStreamInternal::calculateReasonableTimeout() {
339 return calculateReasonableTimeout(getFramesPerBurst());
340}
341
Phil Burke4d7bb42017-03-28 11:32:39 -0700342aaudio_result_t AudioStreamInternal::stopCallback()
343{
344 if (isDataCallbackActive()) {
345 mCallbackEnabled.store(false);
Phil Burk87c9f642017-05-17 07:22:39 -0700346 return joinThread(NULL);
Phil Burke4d7bb42017-03-28 11:32:39 -0700347 } else {
348 return AAUDIO_OK;
349 }
350}
351
Phil Burk5cc83c32017-11-28 15:43:18 -0800352aaudio_result_t AudioStreamInternal::requestStop()
Phil Burk71f35bb2017-04-13 16:05:07 -0700353{
Phil Burk5cc83c32017-11-28 15:43:18 -0800354 aaudio_result_t result = stopCallback();
355 if (result != AAUDIO_OK) {
356 return result;
357 }
358
Phil Burk71f35bb2017-04-13 16:05:07 -0700359 if (mServiceStreamHandle == AAUDIO_HANDLE_INVALID) {
Phil Burkbcc36742017-08-31 17:24:51 -0700360 ALOGE("requestStopInternal() mServiceStreamHandle invalid = 0x%08X",
Phil Burk71f35bb2017-04-13 16:05:07 -0700361 mServiceStreamHandle);
362 return AAUDIO_ERROR_INVALID_STATE;
363 }
364
365 mClockModel.stop(AudioClock::getNanoseconds());
366 setState(AAUDIO_STREAM_STATE_STOPPING);
Phil Burkbcc36742017-08-31 17:24:51 -0700367 mAtomicTimestamp.clear();
Phil Burk965650e2017-09-07 21:00:09 -0700368
369 return mServiceInterface.stopStream(mServiceStreamHandle);
Phil Burk71f35bb2017-04-13 16:05:07 -0700370}
371
Phil Burk5ed503c2017-02-01 09:38:15 -0800372aaudio_result_t AudioStreamInternal::registerThread() {
Phil Burk5ed503c2017-02-01 09:38:15 -0800373 if (mServiceStreamHandle == AAUDIO_HANDLE_INVALID) {
Phil Burkbcc36742017-08-31 17:24:51 -0700374 ALOGE("registerThread() mServiceStreamHandle invalid");
Phil Burk5ed503c2017-02-01 09:38:15 -0800375 return AAUDIO_ERROR_INVALID_STATE;
Phil Burk204a1632017-01-03 17:23:43 -0800376 }
Phil Burkc0c70e32017-02-09 13:18:38 -0800377 return mServiceInterface.registerAudioThread(mServiceStreamHandle,
Phil Burkc0c70e32017-02-09 13:18:38 -0800378 gettid(),
379 getPeriodNanoseconds());
Phil Burk204a1632017-01-03 17:23:43 -0800380}
381
Phil Burk5ed503c2017-02-01 09:38:15 -0800382aaudio_result_t AudioStreamInternal::unregisterThread() {
Phil Burk5ed503c2017-02-01 09:38:15 -0800383 if (mServiceStreamHandle == AAUDIO_HANDLE_INVALID) {
Phil Burkbcc36742017-08-31 17:24:51 -0700384 ALOGE("unregisterThread() mServiceStreamHandle invalid");
Phil Burk5ed503c2017-02-01 09:38:15 -0800385 return AAUDIO_ERROR_INVALID_STATE;
Phil Burk204a1632017-01-03 17:23:43 -0800386 }
Phil Burk2ac035f2017-06-23 14:51:14 -0700387 return mServiceInterface.unregisterAudioThread(mServiceStreamHandle, gettid());
Phil Burk204a1632017-01-03 17:23:43 -0800388}
389
Eric Laurentcb4dae22017-07-01 19:39:32 -0700390aaudio_result_t AudioStreamInternal::startClient(const android::AudioClient& client,
Phil Burkbbd52862018-04-13 11:37:42 -0700391 audio_port_handle_t *portHandle) {
392 ALOGV("%s() called", __func__);
Eric Laurentcb4dae22017-07-01 19:39:32 -0700393 if (mServiceStreamHandle == AAUDIO_HANDLE_INVALID) {
394 return AAUDIO_ERROR_INVALID_STATE;
395 }
Phil Burkbbd52862018-04-13 11:37:42 -0700396 aaudio_result_t result = mServiceInterface.startClient(mServiceStreamHandle,
397 client, portHandle);
398 ALOGV("%s(%d) returning %d", __func__, *portHandle, result);
399 return result;
Eric Laurentcb4dae22017-07-01 19:39:32 -0700400}
401
Phil Burkbbd52862018-04-13 11:37:42 -0700402aaudio_result_t AudioStreamInternal::stopClient(audio_port_handle_t portHandle) {
403 ALOGV("%s(%d) called", __func__, portHandle);
Eric Laurentcb4dae22017-07-01 19:39:32 -0700404 if (mServiceStreamHandle == AAUDIO_HANDLE_INVALID) {
405 return AAUDIO_ERROR_INVALID_STATE;
406 }
Phil Burkbbd52862018-04-13 11:37:42 -0700407 aaudio_result_t result = mServiceInterface.stopClient(mServiceStreamHandle, portHandle);
408 ALOGV("%s(%d) returning %d", __func__, portHandle, result);
409 return result;
Eric Laurentcb4dae22017-07-01 19:39:32 -0700410}
411
Phil Burk5ed503c2017-02-01 09:38:15 -0800412aaudio_result_t AudioStreamInternal::getTimestamp(clockid_t clockId,
Phil Burk3316d5e2017-02-15 11:23:01 -0800413 int64_t *framePosition,
414 int64_t *timeNanoseconds) {
Phil Burk97350f92017-07-21 15:59:44 -0700415 // Generated in server and passed to client. Return latest.
416 if (mAtomicTimestamp.isValid()) {
417 Timestamp timestamp = mAtomicTimestamp.read();
Phil Burkbcc36742017-08-31 17:24:51 -0700418 int64_t position = timestamp.getPosition() + mFramesOffsetFromService;
419 if (position >= 0) {
420 *framePosition = position;
421 *timeNanoseconds = timestamp.getNanoseconds();
422 return AAUDIO_OK;
423 }
Phil Burk97350f92017-07-21 15:59:44 -0700424 }
Phil Burkc75d97f2017-09-08 15:48:36 -0700425 return AAUDIO_ERROR_INVALID_STATE;
Phil Burk204a1632017-01-03 17:23:43 -0800426}
427
Phil Burk0befec62017-07-28 15:12:13 -0700428aaudio_result_t AudioStreamInternal::updateStateMachine() {
Phil Burke4d7bb42017-03-28 11:32:39 -0700429 if (isDataCallbackActive()) {
430 return AAUDIO_OK; // state is getting updated by the callback thread read/write call
431 }
Phil Burk204a1632017-01-03 17:23:43 -0800432 return processCommands();
433}
434
Phil Burkec89b2e2017-06-20 15:05:06 -0700435void AudioStreamInternal::logTimestamp(AAudioServiceMessage &command) {
Phil Burk204a1632017-01-03 17:23:43 -0800436 static int64_t oldPosition = 0;
Phil Burk3316d5e2017-02-15 11:23:01 -0800437 static int64_t oldTime = 0;
Phil Burk204a1632017-01-03 17:23:43 -0800438 int64_t framePosition = command.timestamp.position;
Phil Burk3316d5e2017-02-15 11:23:01 -0800439 int64_t nanoTime = command.timestamp.timestamp;
Phil Burkbcc36742017-08-31 17:24:51 -0700440 ALOGD("logTimestamp: timestamp says framePosition = %8lld at nanoTime %lld",
Phil Burk204a1632017-01-03 17:23:43 -0800441 (long long) framePosition,
442 (long long) nanoTime);
443 int64_t nanosDelta = nanoTime - oldTime;
444 if (nanosDelta > 0 && oldTime > 0) {
445 int64_t framesDelta = framePosition - oldPosition;
Phil Burk5ed503c2017-02-01 09:38:15 -0800446 int64_t rate = (framesDelta * AAUDIO_NANOS_PER_SECOND) / nanosDelta;
Phil Burkbcc36742017-08-31 17:24:51 -0700447 ALOGD("logTimestamp: framesDelta = %8lld, nanosDelta = %8lld, rate = %lld",
Phil Burkec89b2e2017-06-20 15:05:06 -0700448 (long long) framesDelta, (long long) nanosDelta, (long long) rate);
Phil Burk204a1632017-01-03 17:23:43 -0800449 }
450 oldPosition = framePosition;
451 oldTime = nanoTime;
452}
Phil Burk204a1632017-01-03 17:23:43 -0800453
Phil Burk97350f92017-07-21 15:59:44 -0700454aaudio_result_t AudioStreamInternal::onTimestampService(AAudioServiceMessage *message) {
Phil Burk204a1632017-01-03 17:23:43 -0800455#if LOG_TIMESTAMPS
Phil Burkec89b2e2017-06-20 15:05:06 -0700456 logTimestamp(*message);
Phil Burk204a1632017-01-03 17:23:43 -0800457#endif
Phil Burk87c9f642017-05-17 07:22:39 -0700458 processTimestamp(message->timestamp.position, message->timestamp.timestamp);
Phil Burk5ed503c2017-02-01 09:38:15 -0800459 return AAUDIO_OK;
Phil Burk204a1632017-01-03 17:23:43 -0800460}
461
Phil Burk97350f92017-07-21 15:59:44 -0700462aaudio_result_t AudioStreamInternal::onTimestampHardware(AAudioServiceMessage *message) {
463 Timestamp timestamp(message->timestamp.position, message->timestamp.timestamp);
464 mAtomicTimestamp.write(timestamp);
465 return AAUDIO_OK;
466}
467
Phil Burk5ed503c2017-02-01 09:38:15 -0800468aaudio_result_t AudioStreamInternal::onEventFromServer(AAudioServiceMessage *message) {
469 aaudio_result_t result = AAUDIO_OK;
Phil Burk204a1632017-01-03 17:23:43 -0800470 switch (message->event.event) {
Phil Burk5ed503c2017-02-01 09:38:15 -0800471 case AAUDIO_SERVICE_EVENT_STARTED:
Phil Burkfbf031e2017-10-12 15:58:31 -0700472 ALOGD("%s - got AAUDIO_SERVICE_EVENT_STARTED", __func__);
Phil Burk87c9f642017-05-17 07:22:39 -0700473 if (getState() == AAUDIO_STREAM_STATE_STARTING) {
474 setState(AAUDIO_STREAM_STATE_STARTED);
475 }
Phil Burk204a1632017-01-03 17:23:43 -0800476 break;
Phil Burk5ed503c2017-02-01 09:38:15 -0800477 case AAUDIO_SERVICE_EVENT_PAUSED:
Phil Burkfbf031e2017-10-12 15:58:31 -0700478 ALOGD("%s - got AAUDIO_SERVICE_EVENT_PAUSED", __func__);
Phil Burk87c9f642017-05-17 07:22:39 -0700479 if (getState() == AAUDIO_STREAM_STATE_PAUSING) {
480 setState(AAUDIO_STREAM_STATE_PAUSED);
481 }
Phil Burk204a1632017-01-03 17:23:43 -0800482 break;
Phil Burk71f35bb2017-04-13 16:05:07 -0700483 case AAUDIO_SERVICE_EVENT_STOPPED:
Phil Burkfbf031e2017-10-12 15:58:31 -0700484 ALOGD("%s - got AAUDIO_SERVICE_EVENT_STOPPED", __func__);
Phil Burk87c9f642017-05-17 07:22:39 -0700485 if (getState() == AAUDIO_STREAM_STATE_STOPPING) {
486 setState(AAUDIO_STREAM_STATE_STOPPED);
487 }
Phil Burk71f35bb2017-04-13 16:05:07 -0700488 break;
Phil Burk5ed503c2017-02-01 09:38:15 -0800489 case AAUDIO_SERVICE_EVENT_FLUSHED:
Phil Burkfbf031e2017-10-12 15:58:31 -0700490 ALOGD("%s - got AAUDIO_SERVICE_EVENT_FLUSHED", __func__);
Phil Burk87c9f642017-05-17 07:22:39 -0700491 if (getState() == AAUDIO_STREAM_STATE_FLUSHING) {
492 setState(AAUDIO_STREAM_STATE_FLUSHED);
493 onFlushFromServer();
494 }
Phil Burk204a1632017-01-03 17:23:43 -0800495 break;
Phil Burk5ed503c2017-02-01 09:38:15 -0800496 case AAUDIO_SERVICE_EVENT_DISCONNECTED:
Phil Burkea04d972017-08-07 12:30:44 -0700497 // Prevent hardware from looping on old data and making buzzing sounds.
498 if (getDirection() == AAUDIO_DIRECTION_OUTPUT) {
499 mAudioEndpoint.eraseDataMemory();
500 }
Phil Burk5ed503c2017-02-01 09:38:15 -0800501 result = AAUDIO_ERROR_DISCONNECTED;
Phil Burkc0c70e32017-02-09 13:18:38 -0800502 setState(AAUDIO_STREAM_STATE_DISCONNECTED);
Phil Burkfbf031e2017-10-12 15:58:31 -0700503 ALOGW("%s - AAUDIO_SERVICE_EVENT_DISCONNECTED - FIFO cleared", __func__);
Phil Burk204a1632017-01-03 17:23:43 -0800504 break;
Phil Burkc0c70e32017-02-09 13:18:38 -0800505 case AAUDIO_SERVICE_EVENT_VOLUME:
Phil Burk55e5eab2018-04-10 15:16:38 -0700506 ALOGD("%s - AAUDIO_SERVICE_EVENT_VOLUME %lf", __func__, message->event.dataDouble);
Eric Laurenta2f296e2017-06-21 18:51:47 -0700507 mStreamVolume = (float)message->event.dataDouble;
508 doSetVolume();
Phil Burkc0c70e32017-02-09 13:18:38 -0800509 break;
Phil Burk23296382017-11-20 15:45:11 -0800510 case AAUDIO_SERVICE_EVENT_XRUN:
511 mXRunCount = static_cast<int32_t>(message->event.dataLong);
512 break;
Phil Burk204a1632017-01-03 17:23:43 -0800513 default:
Phil Burkfbf031e2017-10-12 15:58:31 -0700514 ALOGE("%s - Unrecognized event = %d", __func__, (int) message->event.event);
Phil Burk204a1632017-01-03 17:23:43 -0800515 break;
516 }
517 return result;
518}
519
Phil Burkbcc36742017-08-31 17:24:51 -0700520aaudio_result_t AudioStreamInternal::drainTimestampsFromService() {
521 aaudio_result_t result = AAUDIO_OK;
522
523 while (result == AAUDIO_OK) {
524 AAudioServiceMessage message;
525 if (mAudioEndpoint.readUpCommand(&message) != 1) {
526 break; // no command this time, no problem
527 }
528 switch (message.what) {
529 // ignore most messages
530 case AAudioServiceMessage::code::TIMESTAMP_SERVICE:
531 case AAudioServiceMessage::code::TIMESTAMP_HARDWARE:
532 break;
533
534 case AAudioServiceMessage::code::EVENT:
535 result = onEventFromServer(&message);
536 break;
537
538 default:
Phil Burkfbf031e2017-10-12 15:58:31 -0700539 ALOGE("%s - unrecognized message.what = %d", __func__, (int) message.what);
Phil Burkbcc36742017-08-31 17:24:51 -0700540 result = AAUDIO_ERROR_INTERNAL;
541 break;
542 }
543 }
544 return result;
545}
546
Phil Burk204a1632017-01-03 17:23:43 -0800547// Process all the commands coming from the server.
Phil Burk5ed503c2017-02-01 09:38:15 -0800548aaudio_result_t AudioStreamInternal::processCommands() {
549 aaudio_result_t result = AAUDIO_OK;
Phil Burk204a1632017-01-03 17:23:43 -0800550
Phil Burk5ed503c2017-02-01 09:38:15 -0800551 while (result == AAUDIO_OK) {
552 AAudioServiceMessage message;
Phil Burk204a1632017-01-03 17:23:43 -0800553 if (mAudioEndpoint.readUpCommand(&message) != 1) {
554 break; // no command this time, no problem
555 }
556 switch (message.what) {
Phil Burk97350f92017-07-21 15:59:44 -0700557 case AAudioServiceMessage::code::TIMESTAMP_SERVICE:
558 result = onTimestampService(&message);
559 break;
560
561 case AAudioServiceMessage::code::TIMESTAMP_HARDWARE:
562 result = onTimestampHardware(&message);
Phil Burk204a1632017-01-03 17:23:43 -0800563 break;
564
Phil Burk5ed503c2017-02-01 09:38:15 -0800565 case AAudioServiceMessage::code::EVENT:
Phil Burk204a1632017-01-03 17:23:43 -0800566 result = onEventFromServer(&message);
567 break;
568
569 default:
Phil Burkfbf031e2017-10-12 15:58:31 -0700570 ALOGE("%s - unrecognized message.what = %d", __func__, (int) message.what);
Phil Burk17fff382017-05-16 14:06:45 -0700571 result = AAUDIO_ERROR_INTERNAL;
Phil Burk204a1632017-01-03 17:23:43 -0800572 break;
573 }
574 }
575 return result;
576}
577
Phil Burk87c9f642017-05-17 07:22:39 -0700578// Read or write the data, block if needed and timeoutMillis > 0
579aaudio_result_t AudioStreamInternal::processData(void *buffer, int32_t numFrames,
580 int64_t timeoutNanoseconds)
Phil Burk204a1632017-01-03 17:23:43 -0800581{
Phil Burkfd34a932017-07-19 07:03:52 -0700582 const char * traceName = "aaProc";
583 const char * fifoName = "aaRdy";
Phil Burk4485d412017-05-09 15:55:02 -0700584 ATRACE_BEGIN(traceName);
Phil Burk4485d412017-05-09 15:55:02 -0700585 if (ATRACE_ENABLED()) {
Phil Burkfd34a932017-07-19 07:03:52 -0700586 int32_t fullFrames = mAudioEndpoint.getFullFramesAvailable();
587 ATRACE_INT(fifoName, fullFrames);
Phil Burk4485d412017-05-09 15:55:02 -0700588 }
589
Phil Burkec89b2e2017-06-20 15:05:06 -0700590 aaudio_result_t result = AAUDIO_OK;
591 int32_t loopCount = 0;
592 uint8_t* audioData = (uint8_t*)buffer;
593 int64_t currentTimeNanos = AudioClock::getNanoseconds();
594 const int64_t entryTimeNanos = currentTimeNanos;
595 const int64_t deadlineNanos = currentTimeNanos + timeoutNanoseconds;
596 int32_t framesLeft = numFrames;
597
Phil Burk87c9f642017-05-17 07:22:39 -0700598 // Loop until all the data has been processed or until a timeout occurs.
Phil Burk204a1632017-01-03 17:23:43 -0800599 while (framesLeft > 0) {
Phil Burkec89b2e2017-06-20 15:05:06 -0700600 // The call to processDataNow() will not block. It will just process as much as it can.
Phil Burk3316d5e2017-02-15 11:23:01 -0800601 int64_t wakeTimeNanos = 0;
Phil Burk87c9f642017-05-17 07:22:39 -0700602 aaudio_result_t framesProcessed = processDataNow(audioData, framesLeft,
603 currentTimeNanos, &wakeTimeNanos);
604 if (framesProcessed < 0) {
Phil Burk87c9f642017-05-17 07:22:39 -0700605 result = framesProcessed;
Phil Burk204a1632017-01-03 17:23:43 -0800606 break;
607 }
Phil Burk87c9f642017-05-17 07:22:39 -0700608 framesLeft -= (int32_t) framesProcessed;
609 audioData += framesProcessed * getBytesPerFrame();
Phil Burk204a1632017-01-03 17:23:43 -0800610
611 // Should we block?
612 if (timeoutNanoseconds == 0) {
613 break; // don't block
614 } else if (framesLeft > 0) {
Phil Burkfd34a932017-07-19 07:03:52 -0700615 if (!mAudioEndpoint.isFreeRunning()) {
616 // If there is software on the other end of the FIFO then it may get delayed.
617 // So wake up just a little after we expect it to be ready.
618 wakeTimeNanos += mWakeupDelayNanos;
Phil Burk204a1632017-01-03 17:23:43 -0800619 }
Phil Burkfd34a932017-07-19 07:03:52 -0700620
Phil Burk2bc7c182017-08-28 11:45:01 -0700621 currentTimeNanos = AudioClock::getNanoseconds();
622 int64_t earliestWakeTime = currentTimeNanos + mMinimumSleepNanos;
623 // Guarantee a minimum sleep time.
624 if (wakeTimeNanos < earliestWakeTime) {
625 wakeTimeNanos = earliestWakeTime;
626 }
627
Phil Burk204a1632017-01-03 17:23:43 -0800628 if (wakeTimeNanos > deadlineNanos) {
629 // If we time out, just return the framesWritten so far.
Phil Burkcf5f6d22017-05-26 12:35:07 -0700630 // TODO remove after we fix the deadline bug
Phil Burkfbf031e2017-10-12 15:58:31 -0700631 ALOGW("processData(): entered at %lld nanos, currently %lld",
Phil Burkec89b2e2017-06-20 15:05:06 -0700632 (long long) entryTimeNanos, (long long) currentTimeNanos);
Phil Burkfbf031e2017-10-12 15:58:31 -0700633 ALOGW("processData(): TIMEOUT after %lld nanos",
Phil Burkc0c70e32017-02-09 13:18:38 -0800634 (long long) timeoutNanoseconds);
Phil Burkfbf031e2017-10-12 15:58:31 -0700635 ALOGW("processData(): wakeTime = %lld, deadline = %lld nanos",
Phil Burk87c9f642017-05-17 07:22:39 -0700636 (long long) wakeTimeNanos, (long long) deadlineNanos);
Phil Burkfbf031e2017-10-12 15:58:31 -0700637 ALOGW("processData(): past deadline by %d micros",
Phil Burk87c9f642017-05-17 07:22:39 -0700638 (int)((wakeTimeNanos - deadlineNanos) / AAUDIO_NANOS_PER_MICROSECOND));
Phil Burkec89b2e2017-06-20 15:05:06 -0700639 mClockModel.dump();
640 mAudioEndpoint.dump();
Phil Burk204a1632017-01-03 17:23:43 -0800641 break;
642 }
643
Phil Burkfd34a932017-07-19 07:03:52 -0700644 if (ATRACE_ENABLED()) {
645 int32_t fullFrames = mAudioEndpoint.getFullFramesAvailable();
646 ATRACE_INT(fifoName, fullFrames);
647 int64_t sleepForNanos = wakeTimeNanos - currentTimeNanos;
648 ATRACE_INT("aaSlpNs", (int32_t)sleepForNanos);
649 }
650
651 AudioClock::sleepUntilNanoTime(wakeTimeNanos);
Phil Burk204a1632017-01-03 17:23:43 -0800652 currentTimeNanos = AudioClock::getNanoseconds();
653 }
654 }
655
Phil Burkfd34a932017-07-19 07:03:52 -0700656 if (ATRACE_ENABLED()) {
657 int32_t fullFrames = mAudioEndpoint.getFullFramesAvailable();
658 ATRACE_INT(fifoName, fullFrames);
659 }
660
Phil Burk87c9f642017-05-17 07:22:39 -0700661 // return error or framesProcessed
Phil Burkc0c70e32017-02-09 13:18:38 -0800662 (void) loopCount;
Phil Burk4485d412017-05-09 15:55:02 -0700663 ATRACE_END();
Phil Burk204a1632017-01-03 17:23:43 -0800664 return (result < 0) ? result : numFrames - framesLeft;
665}
666
Phil Burk3316d5e2017-02-15 11:23:01 -0800667void AudioStreamInternal::processTimestamp(uint64_t position, int64_t time) {
Phil Burk87c9f642017-05-17 07:22:39 -0700668 mClockModel.processTimestamp(position, time);
Phil Burk204a1632017-01-03 17:23:43 -0800669}
670
Phil Burk3316d5e2017-02-15 11:23:01 -0800671aaudio_result_t AudioStreamInternal::setBufferSize(int32_t requestedFrames) {
Phil Burk6479d502017-11-20 09:32:52 -0800672 int32_t adjustedFrames = requestedFrames;
Phil Burk3316d5e2017-02-15 11:23:01 -0800673 int32_t actualFrames = 0;
Phil Burk6479d502017-11-20 09:32:52 -0800674 int32_t maximumSize = getBufferCapacity();
675
676 // Clip to minimum size so that rounding up will work better.
677 if (adjustedFrames < 1) {
678 adjustedFrames = 1;
Phil Burk71f35bb2017-04-13 16:05:07 -0700679 }
680
Phil Burk6479d502017-11-20 09:32:52 -0800681 if (adjustedFrames > maximumSize) {
682 // Clip to maximum size.
683 adjustedFrames = maximumSize;
684 } else {
685 // Round to the next highest burst size.
686 int32_t numBursts = (adjustedFrames + mFramesPerBurst - 1) / mFramesPerBurst;
687 adjustedFrames = numBursts * mFramesPerBurst;
688 // Rounding may have gone above maximum.
689 if (adjustedFrames > maximumSize) {
690 adjustedFrames = maximumSize;
691 }
692 }
693
694 aaudio_result_t result = mAudioEndpoint.setBufferSizeInFrames(adjustedFrames, &actualFrames);
Phil Burk3316d5e2017-02-15 11:23:01 -0800695 if (result < 0) {
696 return result;
697 } else {
698 return (aaudio_result_t) actualFrames;
699 }
Phil Burk204a1632017-01-03 17:23:43 -0800700}
701
Phil Burk87c9f642017-05-17 07:22:39 -0700702int32_t AudioStreamInternal::getBufferSize() const {
Phil Burk204a1632017-01-03 17:23:43 -0800703 return mAudioEndpoint.getBufferSizeInFrames();
704}
705
Phil Burk87c9f642017-05-17 07:22:39 -0700706int32_t AudioStreamInternal::getBufferCapacity() const {
Phil Burk204a1632017-01-03 17:23:43 -0800707 return mAudioEndpoint.getBufferCapacityInFrames();
708}
709
Phil Burk87c9f642017-05-17 07:22:39 -0700710int32_t AudioStreamInternal::getFramesPerBurst() const {
Phil Burk6479d502017-11-20 09:32:52 -0800711 return mFramesPerBurst;
Phil Burk204a1632017-01-03 17:23:43 -0800712}
713
Phil Burk87c9f642017-05-17 07:22:39 -0700714aaudio_result_t AudioStreamInternal::joinThread(void** returnArg) {
715 return AudioStream::joinThread(returnArg, calculateReasonableTimeout(getFramesPerBurst()));
Phil Burk4c5129b2017-04-28 15:17:32 -0700716}