blob: 6cfd539d506724f60b500b09032d6a5c6c395209 [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"
dimitryd81a84a2019-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#include "utility/LinearRamp.h"
44
Phil Burkc0c70e32017-02-09 13:18:38 -080045#include "AudioStreamInternal.h"
Phil Burk204a1632017-01-03 17:23:43 -080046
Phil Burk204a1632017-01-03 17:23:43 -080047using android::String16;
Phil Burkdec33ab2017-01-17 14:48:16 -080048using android::Mutex;
Phil Burkc0c70e32017-02-09 13:18:38 -080049using android::WrappingBuffer;
Phil Burk204a1632017-01-03 17:23:43 -080050
Phil Burk5ed503c2017-02-01 09:38:15 -080051using namespace aaudio;
Phil Burk204a1632017-01-03 17:23:43 -080052
Phil Burke4d7bb42017-03-28 11:32:39 -070053#define MIN_TIMEOUT_NANOS (1000 * AAUDIO_NANOS_PER_MILLISECOND)
54
55// Wait at least this many times longer than the operation should take.
56#define MIN_TIMEOUT_OPERATIONS 4
57
Phil Burkbcc36742017-08-31 17:24:51 -070058#define LOG_TIMESTAMPS 0
Phil Burk87c9f642017-05-17 07:22:39 -070059
Phil Burkc0c70e32017-02-09 13:18:38 -080060AudioStreamInternal::AudioStreamInternal(AAudioServiceInterface &serviceInterface, bool inService)
Phil Burk204a1632017-01-03 17:23:43 -080061 : AudioStream()
62 , mClockModel()
63 , mAudioEndpoint()
Phil Burk5ed503c2017-02-01 09:38:15 -080064 , mServiceStreamHandle(AAUDIO_HANDLE_INVALID)
Phil Burkec89b2e2017-06-20 15:05:06 -070065 , mInService(inService)
Phil Burkfd34a932017-07-19 07:03:52 -070066 , mServiceInterface(serviceInterface)
Phil Burkbcc36742017-08-31 17:24:51 -070067 , mAtomicTimestamp()
Phil Burkfd34a932017-07-19 07:03:52 -070068 , mWakeupDelayNanos(AAudioProperty_getWakeupDelayMicros() * AAUDIO_NANOS_PER_MICROSECOND)
69 , mMinimumSleepNanos(AAudioProperty_getMinimumSleepMicros() * AAUDIO_NANOS_PER_MICROSECOND)
70 {
Phil Burk204a1632017-01-03 17:23:43 -080071}
72
73AudioStreamInternal::~AudioStreamInternal() {
74}
75
Phil Burk5ed503c2017-02-01 09:38:15 -080076aaudio_result_t AudioStreamInternal::open(const AudioStreamBuilder &builder) {
Phil Burk204a1632017-01-03 17:23:43 -080077
Phil Burk5ed503c2017-02-01 09:38:15 -080078 aaudio_result_t result = AAUDIO_OK;
Phil Burk99306c82017-08-14 12:38:58 -070079 int32_t capacity;
Phil Burk6479d502017-11-20 09:32:52 -080080 int32_t framesPerBurst;
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 Burkc0c70e32017-02-09 13:18:38 -080095 // We have to do volume scaling. So we prefer FLOAT format.
Glenn Kasten37a466a2017-05-30 15:53:14 -070096 if (getFormat() == AAUDIO_FORMAT_UNSPECIFIED) {
Phil Burkc0c70e32017-02-09 13:18:38 -080097 setFormat(AAUDIO_FORMAT_PCM_FLOAT);
98 }
Phil Burk71f35bb2017-04-13 16:05:07 -070099 // Request FLOAT for the shared mixer.
jiabin901f65d2017-07-12 17:56:35 -0700100 request.getConfiguration().setFormat(AAUDIO_FORMAT_PCM_FLOAT);
Phil Burkc0c70e32017-02-09 13:18:38 -0800101
Phil Burkdec33ab2017-01-17 14:48:16 -0800102 // Build the request to send to the server.
Phil Burk204a1632017-01-03 17:23:43 -0800103 request.setUserId(getuid());
104 request.setProcessId(getpid());
Phil Burk71f35bb2017-04-13 16:05:07 -0700105 request.setSharingModeMatchRequired(isSharingModeMatchRequired());
Phil Burk41f19d82018-02-13 14:59:10 -0800106 request.setInService(isInService());
Phil Burkc0c70e32017-02-09 13:18:38 -0800107
Phil Burk204a1632017-01-03 17:23:43 -0800108 request.getConfiguration().setDeviceId(getDeviceId());
109 request.getConfiguration().setSampleRate(getSampleRate());
110 request.getConfiguration().setSamplesPerFrame(getSamplesPerFrame());
Phil Burk39f02dd2017-08-04 09:13:31 -0700111 request.getConfiguration().setDirection(getDirection());
Phil Burk71f35bb2017-04-13 16:05:07 -0700112 request.getConfiguration().setSharingMode(getSharingMode());
113
Phil Burka62fb952018-01-16 12:44:06 -0800114 request.getConfiguration().setUsage(getUsage());
115 request.getConfiguration().setContentType(getContentType());
116 request.getConfiguration().setInputPreset(getInputPreset());
117
Phil Burk3df348f2017-02-08 11:41:55 -0800118 request.getConfiguration().setBufferCapacity(builder.getBufferCapacity());
Phil Burk204a1632017-01-03 17:23:43 -0800119
Phil Burk41f19d82018-02-13 14:59:10 -0800120 mDeviceChannelCount = getSamplesPerFrame(); // Assume it will be the same. Update if not.
121
Phil Burk99306c82017-08-14 12:38:58 -0700122 mServiceStreamHandle = mServiceInterface.openStream(request, configurationOutput);
Phil Burk41f19d82018-02-13 14:59:10 -0800123 if (mServiceStreamHandle < 0
124 && request.getConfiguration().getSamplesPerFrame() == 1 // mono?
125 && getDirection() == AAUDIO_DIRECTION_OUTPUT
126 && !isInService()) {
127 // if that failed then try switching from mono to stereo if OUTPUT.
128 // Only do this in the client. Otherwise we end up with a mono mixer in the service
129 // that writes to a stereo MMAP stream.
130 ALOGD("%s - openStream() returned %d, try switching from MONO to STEREO",
131 __func__, mServiceStreamHandle);
132 request.getConfiguration().setSamplesPerFrame(2); // stereo
133 mServiceStreamHandle = mServiceInterface.openStream(request, configurationOutput);
134 }
Phil Burk204a1632017-01-03 17:23:43 -0800135 if (mServiceStreamHandle < 0) {
Phil Burk41f19d82018-02-13 14:59:10 -0800136 ALOGE("%s - openStream() returned %d", __func__, mServiceStreamHandle);
137 return mServiceStreamHandle;
Phil Burk204a1632017-01-03 17:23:43 -0800138 }
Phil Burk99306c82017-08-14 12:38:58 -0700139
140 result = configurationOutput.validate();
141 if (result != AAUDIO_OK) {
142 goto error;
143 }
144 // Save results of the open.
Phil Burk41f19d82018-02-13 14:59:10 -0800145 if (getSamplesPerFrame() == AAUDIO_UNSPECIFIED) {
146 setSamplesPerFrame(configurationOutput.getSamplesPerFrame());
147 }
148 mDeviceChannelCount = configurationOutput.getSamplesPerFrame();
149
Phil Burk99306c82017-08-14 12:38:58 -0700150 setSampleRate(configurationOutput.getSampleRate());
Phil Burk99306c82017-08-14 12:38:58 -0700151 setDeviceId(configurationOutput.getDeviceId());
Phil Burk4e1af9f2018-01-03 15:54:35 -0800152 setSessionId(configurationOutput.getSessionId());
Phil Burk99306c82017-08-14 12:38:58 -0700153 setSharingMode(configurationOutput.getSharingMode());
154
Phil Burka62fb952018-01-16 12:44:06 -0800155 setUsage(configurationOutput.getUsage());
156 setContentType(configurationOutput.getContentType());
157 setInputPreset(configurationOutput.getInputPreset());
158
Phil Burk99306c82017-08-14 12:38:58 -0700159 // Save device format so we can do format conversion and volume scaling together.
Phil Burk3d786cb2018-04-09 11:58:09 -0700160 setDeviceFormat(configurationOutput.getFormat());
Phil Burk99306c82017-08-14 12:38:58 -0700161
162 result = mServiceInterface.getStreamDescription(mServiceStreamHandle, mEndPointParcelable);
163 if (result != AAUDIO_OK) {
164 goto error;
165 }
166
167 // Resolve parcelable into a descriptor.
168 result = mEndPointParcelable.resolve(&mEndpointDescriptor);
169 if (result != AAUDIO_OK) {
170 goto error;
171 }
172
173 // Configure endpoint based on descriptor.
174 result = mAudioEndpoint.configure(&mEndpointDescriptor, getDirection());
175 if (result != AAUDIO_OK) {
176 goto error;
177 }
178
Phil Burk99306c82017-08-14 12:38:58 -0700179 // Validate result from server.
Phil Burk6479d502017-11-20 09:32:52 -0800180 framesPerBurst = mEndpointDescriptor.dataQueueDescriptor.framesPerBurst;
181 if (framesPerBurst < MIN_FRAMES_PER_BURST || framesPerBurst > MAX_FRAMES_PER_BURST) {
182 ALOGE("%s - framesPerBurst out of range = %d", __func__, framesPerBurst);
Phil Burk99306c82017-08-14 12:38:58 -0700183 result = AAUDIO_ERROR_OUT_OF_RANGE;
184 goto error;
185 }
Phil Burk6479d502017-11-20 09:32:52 -0800186 mFramesPerBurst = framesPerBurst; // only save good value
187
188 capacity = mEndpointDescriptor.dataQueueDescriptor.capacityInFrames;
189 if (capacity < mFramesPerBurst || capacity > MAX_BUFFER_CAPACITY_IN_FRAMES) {
Phil Burkfbf031e2017-10-12 15:58:31 -0700190 ALOGE("%s - bufferCapacity out of range = %d", __func__, capacity);
Phil Burk99306c82017-08-14 12:38:58 -0700191 result = AAUDIO_ERROR_OUT_OF_RANGE;
192 goto error;
193 }
194
195 mClockModel.setSampleRate(getSampleRate());
196 mClockModel.setFramesPerBurst(mFramesPerBurst);
197
Phil Burk134f1972017-12-08 13:06:11 -0800198 if (isDataCallbackSet()) {
Phil Burk99306c82017-08-14 12:38:58 -0700199 mCallbackFrames = builder.getFramesPerDataCallback();
200 if (mCallbackFrames > getBufferCapacity() / 2) {
Phil Burkfbf031e2017-10-12 15:58:31 -0700201 ALOGE("%s - framesPerCallback too big = %d, capacity = %d",
202 __func__, mCallbackFrames, getBufferCapacity());
Phil Burk99306c82017-08-14 12:38:58 -0700203 result = AAUDIO_ERROR_OUT_OF_RANGE;
204 goto error;
205
206 } else if (mCallbackFrames < 0) {
Phil Burkfbf031e2017-10-12 15:58:31 -0700207 ALOGE("%s - framesPerCallback negative", __func__);
Phil Burk99306c82017-08-14 12:38:58 -0700208 result = AAUDIO_ERROR_OUT_OF_RANGE;
209 goto error;
210
211 }
212 if (mCallbackFrames == AAUDIO_UNSPECIFIED) {
213 mCallbackFrames = mFramesPerBurst;
214 }
215
216 int32_t bytesPerFrame = getSamplesPerFrame()
217 * AAudioConvert_formatToSizeInBytes(getFormat());
218 int32_t callbackBufferSize = mCallbackFrames * bytesPerFrame;
219 mCallbackBuffer = new uint8_t[callbackBufferSize];
220 }
221
222 setState(AAUDIO_STREAM_STATE_OPEN);
Phil Burk99306c82017-08-14 12:38:58 -0700223
224 return result;
225
226error:
227 close();
Phil Burk204a1632017-01-03 17:23:43 -0800228 return result;
229}
230
Phil Burk5ed503c2017-02-01 09:38:15 -0800231aaudio_result_t AudioStreamInternal::close() {
Phil Burk965650e2017-09-07 21:00:09 -0700232 aaudio_result_t result = AAUDIO_OK;
Phil Burk19e990e2018-03-22 13:59:34 -0700233 ALOGD("%s(): mServiceStreamHandle = 0x%08X", __func__, mServiceStreamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800234 if (mServiceStreamHandle != AAUDIO_HANDLE_INVALID) {
Phil Burk4485d412017-05-09 15:55:02 -0700235 // Don't close a stream while it is running.
236 aaudio_stream_state_t currentState = getState();
Phil Burk87c9f642017-05-17 07:22:39 -0700237 if (isActive()) {
Phil Burk4485d412017-05-09 15:55:02 -0700238 requestStop();
239 aaudio_stream_state_t nextState;
240 int64_t timeoutNanoseconds = MIN_TIMEOUT_NANOS;
Phil Burk965650e2017-09-07 21:00:09 -0700241 result = waitForStateChange(currentState, &nextState,
Phil Burk4485d412017-05-09 15:55:02 -0700242 timeoutNanoseconds);
243 if (result != AAUDIO_OK) {
Phil Burk19e990e2018-03-22 13:59:34 -0700244 ALOGE("%s() waitForStateChange() returned %d %s",
dimitryd81a84a2019-07-17 13:55:16 +0200245 __func__, result, AudioGlobal_convertResultToText(result));
Phil Burk4485d412017-05-09 15:55:02 -0700246 }
247 }
Phil Burkec89b2e2017-06-20 15:05:06 -0700248 setState(AAUDIO_STREAM_STATE_CLOSING);
Phil Burk5ed503c2017-02-01 09:38:15 -0800249 aaudio_handle_t serviceStreamHandle = mServiceStreamHandle;
250 mServiceStreamHandle = AAUDIO_HANDLE_INVALID;
Phil Burkc0c70e32017-02-09 13:18:38 -0800251
252 mServiceInterface.closeStream(serviceStreamHandle);
Phil Burke4d7bb42017-03-28 11:32:39 -0700253 delete[] mCallbackBuffer;
Phil Burk4485d412017-05-09 15:55:02 -0700254 mCallbackBuffer = nullptr;
Phil Burk965650e2017-09-07 21:00:09 -0700255
Phil Burkec89b2e2017-06-20 15:05:06 -0700256 setState(AAUDIO_STREAM_STATE_CLOSED);
Phil Burk965650e2017-09-07 21:00:09 -0700257 result = mEndPointParcelable.close();
258 aaudio_result_t result2 = AudioStream::close();
259 return (result != AAUDIO_OK) ? result : result2;
Phil Burk204a1632017-01-03 17:23:43 -0800260 } else {
Phil Burk5ed503c2017-02-01 09:38:15 -0800261 return AAUDIO_ERROR_INVALID_HANDLE;
Phil Burk204a1632017-01-03 17:23:43 -0800262 }
263}
264
Phil Burke4d7bb42017-03-28 11:32:39 -0700265static void *aaudio_callback_thread_proc(void *context)
266{
267 AudioStreamInternal *stream = (AudioStreamInternal *)context;
Phil Burkfbf031e2017-10-12 15:58:31 -0700268 //LOGD("oboe_callback_thread, stream = %p", stream);
Phil Burke4d7bb42017-03-28 11:32:39 -0700269 if (stream != NULL) {
270 return stream->callbackLoop();
271 } else {
272 return NULL;
273 }
274}
275
Phil Burkbcc36742017-08-31 17:24:51 -0700276/*
277 * It normally takes about 20-30 msec to start a stream on the server.
278 * But the first time can take as much as 200-300 msec. The HW
279 * starts right away so by the time the client gets a chance to write into
280 * the buffer, it is already in a deep underflow state. That can cause the
281 * XRunCount to be non-zero, which could lead an app to tune its latency higher.
282 * To avoid this problem, we set a request for the processing code to start the
283 * client stream at the same position as the server stream.
284 * The processing code will then save the current offset
285 * between client and server and apply that to any position given to the app.
286 */
Phil Burk5ed503c2017-02-01 09:38:15 -0800287aaudio_result_t AudioStreamInternal::requestStart()
Phil Burk204a1632017-01-03 17:23:43 -0800288{
Phil Burk3316d5e2017-02-15 11:23:01 -0800289 int64_t startTime;
Phil Burk5ed503c2017-02-01 09:38:15 -0800290 if (mServiceStreamHandle == AAUDIO_HANDLE_INVALID) {
Phil Burkbcc36742017-08-31 17:24:51 -0700291 ALOGE("requestStart() mServiceStreamHandle invalid");
Phil Burk5ed503c2017-02-01 09:38:15 -0800292 return AAUDIO_ERROR_INVALID_STATE;
Phil Burk204a1632017-01-03 17:23:43 -0800293 }
Phil Burkec89b2e2017-06-20 15:05:06 -0700294 if (isActive()) {
Phil Burkbcc36742017-08-31 17:24:51 -0700295 ALOGE("requestStart() already active");
Phil Burkec89b2e2017-06-20 15:05:06 -0700296 return AAUDIO_ERROR_INVALID_STATE;
297 }
Phil Burkec89b2e2017-06-20 15:05:06 -0700298
Phil Burkbcc36742017-08-31 17:24:51 -0700299 aaudio_stream_state_t originalState = getState();
300 if (originalState == AAUDIO_STREAM_STATE_DISCONNECTED) {
301 ALOGE("requestStart() but DISCONNECTED");
302 return AAUDIO_ERROR_DISCONNECTED;
303 }
Phil Burkec89b2e2017-06-20 15:05:06 -0700304 setState(AAUDIO_STREAM_STATE_STARTING);
Phil Burkbcc36742017-08-31 17:24:51 -0700305
306 // Clear any stale timestamps from the previous run.
307 drainTimestampsFromService();
308
Phil Burk965650e2017-09-07 21:00:09 -0700309 aaudio_result_t result = mServiceInterface.startStream(mServiceStreamHandle);
Phil Burkc0c70e32017-02-09 13:18:38 -0800310
Phil Burk3316d5e2017-02-15 11:23:01 -0800311 startTime = AudioClock::getNanoseconds();
Phil Burk204a1632017-01-03 17:23:43 -0800312 mClockModel.start(startTime);
Phil Burkbcc36742017-08-31 17:24:51 -0700313 mNeedCatchUp.request(); // Ask data processing code to catch up when first timestamp received.
Phil Burke4d7bb42017-03-28 11:32:39 -0700314
Phil Burk965650e2017-09-07 21:00:09 -0700315 // Start data callback thread.
Phil Burk134f1972017-12-08 13:06:11 -0800316 if (result == AAUDIO_OK && isDataCallbackSet()) {
Phil Burke4d7bb42017-03-28 11:32:39 -0700317 // Launch the callback loop thread.
318 int64_t periodNanos = mCallbackFrames
319 * AAUDIO_NANOS_PER_SECOND
320 / getSampleRate();
321 mCallbackEnabled.store(true);
322 result = createThread(periodNanos, aaudio_callback_thread_proc, this);
323 }
Phil Burkec89b2e2017-06-20 15:05:06 -0700324 if (result != AAUDIO_OK) {
325 setState(originalState);
326 }
Phil Burke4d7bb42017-03-28 11:32:39 -0700327 return result;
Phil Burk204a1632017-01-03 17:23:43 -0800328}
329
Phil Burke4d7bb42017-03-28 11:32:39 -0700330int64_t AudioStreamInternal::calculateReasonableTimeout(int32_t framesPerOperation) {
331
332 // Wait for at least a second or some number of callbacks to join the thread.
Phil Burk71f35bb2017-04-13 16:05:07 -0700333 int64_t timeoutNanoseconds = (MIN_TIMEOUT_OPERATIONS
334 * framesPerOperation
335 * AAUDIO_NANOS_PER_SECOND)
336 / getSampleRate();
Phil Burke4d7bb42017-03-28 11:32:39 -0700337 if (timeoutNanoseconds < MIN_TIMEOUT_NANOS) { // arbitrary number of seconds
338 timeoutNanoseconds = MIN_TIMEOUT_NANOS;
339 }
340 return timeoutNanoseconds;
341}
342
Phil Burk87c9f642017-05-17 07:22:39 -0700343int64_t AudioStreamInternal::calculateReasonableTimeout() {
344 return calculateReasonableTimeout(getFramesPerBurst());
345}
346
Phil Burke4d7bb42017-03-28 11:32:39 -0700347aaudio_result_t AudioStreamInternal::stopCallback()
348{
349 if (isDataCallbackActive()) {
350 mCallbackEnabled.store(false);
Phil Burk87c9f642017-05-17 07:22:39 -0700351 return joinThread(NULL);
Phil Burke4d7bb42017-03-28 11:32:39 -0700352 } else {
353 return AAUDIO_OK;
354 }
355}
356
Phil Burk5cc83c32017-11-28 15:43:18 -0800357aaudio_result_t AudioStreamInternal::requestStop()
Phil Burk71f35bb2017-04-13 16:05:07 -0700358{
Phil Burk5cc83c32017-11-28 15:43:18 -0800359 aaudio_result_t result = stopCallback();
360 if (result != AAUDIO_OK) {
361 return result;
362 }
363
Phil Burk71f35bb2017-04-13 16:05:07 -0700364 if (mServiceStreamHandle == AAUDIO_HANDLE_INVALID) {
Phil Burkbcc36742017-08-31 17:24:51 -0700365 ALOGE("requestStopInternal() mServiceStreamHandle invalid = 0x%08X",
Phil Burk71f35bb2017-04-13 16:05:07 -0700366 mServiceStreamHandle);
367 return AAUDIO_ERROR_INVALID_STATE;
368 }
369
370 mClockModel.stop(AudioClock::getNanoseconds());
371 setState(AAUDIO_STREAM_STATE_STOPPING);
Phil Burkbcc36742017-08-31 17:24:51 -0700372 mAtomicTimestamp.clear();
Phil Burk965650e2017-09-07 21:00:09 -0700373
374 return mServiceInterface.stopStream(mServiceStreamHandle);
Phil Burk71f35bb2017-04-13 16:05:07 -0700375}
376
Phil Burk5ed503c2017-02-01 09:38:15 -0800377aaudio_result_t AudioStreamInternal::registerThread() {
Phil Burk5ed503c2017-02-01 09:38:15 -0800378 if (mServiceStreamHandle == AAUDIO_HANDLE_INVALID) {
Phil Burkbcc36742017-08-31 17:24:51 -0700379 ALOGE("registerThread() mServiceStreamHandle invalid");
Phil Burk5ed503c2017-02-01 09:38:15 -0800380 return AAUDIO_ERROR_INVALID_STATE;
Phil Burk204a1632017-01-03 17:23:43 -0800381 }
Phil Burkc0c70e32017-02-09 13:18:38 -0800382 return mServiceInterface.registerAudioThread(mServiceStreamHandle,
Phil Burkc0c70e32017-02-09 13:18:38 -0800383 gettid(),
384 getPeriodNanoseconds());
Phil Burk204a1632017-01-03 17:23:43 -0800385}
386
Phil Burk5ed503c2017-02-01 09:38:15 -0800387aaudio_result_t AudioStreamInternal::unregisterThread() {
Phil Burk5ed503c2017-02-01 09:38:15 -0800388 if (mServiceStreamHandle == AAUDIO_HANDLE_INVALID) {
Phil Burkbcc36742017-08-31 17:24:51 -0700389 ALOGE("unregisterThread() mServiceStreamHandle invalid");
Phil Burk5ed503c2017-02-01 09:38:15 -0800390 return AAUDIO_ERROR_INVALID_STATE;
Phil Burk204a1632017-01-03 17:23:43 -0800391 }
Phil Burk2ac035f2017-06-23 14:51:14 -0700392 return mServiceInterface.unregisterAudioThread(mServiceStreamHandle, gettid());
Phil Burk204a1632017-01-03 17:23:43 -0800393}
394
Eric Laurentcb4dae22017-07-01 19:39:32 -0700395aaudio_result_t AudioStreamInternal::startClient(const android::AudioClient& client,
Phil Burkbbd52862018-04-13 11:37:42 -0700396 audio_port_handle_t *portHandle) {
397 ALOGV("%s() called", __func__);
Eric Laurentcb4dae22017-07-01 19:39:32 -0700398 if (mServiceStreamHandle == AAUDIO_HANDLE_INVALID) {
399 return AAUDIO_ERROR_INVALID_STATE;
400 }
Phil Burkbbd52862018-04-13 11:37:42 -0700401 aaudio_result_t result = mServiceInterface.startClient(mServiceStreamHandle,
402 client, portHandle);
403 ALOGV("%s(%d) returning %d", __func__, *portHandle, result);
404 return result;
Eric Laurentcb4dae22017-07-01 19:39:32 -0700405}
406
Phil Burkbbd52862018-04-13 11:37:42 -0700407aaudio_result_t AudioStreamInternal::stopClient(audio_port_handle_t portHandle) {
408 ALOGV("%s(%d) called", __func__, portHandle);
Eric Laurentcb4dae22017-07-01 19:39:32 -0700409 if (mServiceStreamHandle == AAUDIO_HANDLE_INVALID) {
410 return AAUDIO_ERROR_INVALID_STATE;
411 }
Phil Burkbbd52862018-04-13 11:37:42 -0700412 aaudio_result_t result = mServiceInterface.stopClient(mServiceStreamHandle, portHandle);
413 ALOGV("%s(%d) returning %d", __func__, portHandle, result);
414 return result;
Eric Laurentcb4dae22017-07-01 19:39:32 -0700415}
416
Phil Burk5ed503c2017-02-01 09:38:15 -0800417aaudio_result_t AudioStreamInternal::getTimestamp(clockid_t clockId,
Phil Burk3316d5e2017-02-15 11:23:01 -0800418 int64_t *framePosition,
419 int64_t *timeNanoseconds) {
Phil Burk97350f92017-07-21 15:59:44 -0700420 // Generated in server and passed to client. Return latest.
421 if (mAtomicTimestamp.isValid()) {
422 Timestamp timestamp = mAtomicTimestamp.read();
Phil Burkbcc36742017-08-31 17:24:51 -0700423 int64_t position = timestamp.getPosition() + mFramesOffsetFromService;
424 if (position >= 0) {
425 *framePosition = position;
426 *timeNanoseconds = timestamp.getNanoseconds();
427 return AAUDIO_OK;
428 }
Phil Burk97350f92017-07-21 15:59:44 -0700429 }
Phil Burkc75d97f2017-09-08 15:48:36 -0700430 return AAUDIO_ERROR_INVALID_STATE;
Phil Burk204a1632017-01-03 17:23:43 -0800431}
432
Phil Burk0befec62017-07-28 15:12:13 -0700433aaudio_result_t AudioStreamInternal::updateStateMachine() {
Phil Burke4d7bb42017-03-28 11:32:39 -0700434 if (isDataCallbackActive()) {
435 return AAUDIO_OK; // state is getting updated by the callback thread read/write call
436 }
Phil Burk204a1632017-01-03 17:23:43 -0800437 return processCommands();
438}
439
Phil Burkec89b2e2017-06-20 15:05:06 -0700440void AudioStreamInternal::logTimestamp(AAudioServiceMessage &command) {
Phil Burk204a1632017-01-03 17:23:43 -0800441 static int64_t oldPosition = 0;
Phil Burk3316d5e2017-02-15 11:23:01 -0800442 static int64_t oldTime = 0;
Phil Burk204a1632017-01-03 17:23:43 -0800443 int64_t framePosition = command.timestamp.position;
Phil Burk3316d5e2017-02-15 11:23:01 -0800444 int64_t nanoTime = command.timestamp.timestamp;
Phil Burkbcc36742017-08-31 17:24:51 -0700445 ALOGD("logTimestamp: timestamp says framePosition = %8lld at nanoTime %lld",
Phil Burk204a1632017-01-03 17:23:43 -0800446 (long long) framePosition,
447 (long long) nanoTime);
448 int64_t nanosDelta = nanoTime - oldTime;
449 if (nanosDelta > 0 && oldTime > 0) {
450 int64_t framesDelta = framePosition - oldPosition;
Phil Burk5ed503c2017-02-01 09:38:15 -0800451 int64_t rate = (framesDelta * AAUDIO_NANOS_PER_SECOND) / nanosDelta;
Phil Burkbcc36742017-08-31 17:24:51 -0700452 ALOGD("logTimestamp: framesDelta = %8lld, nanosDelta = %8lld, rate = %lld",
Phil Burkec89b2e2017-06-20 15:05:06 -0700453 (long long) framesDelta, (long long) nanosDelta, (long long) rate);
Phil Burk204a1632017-01-03 17:23:43 -0800454 }
455 oldPosition = framePosition;
456 oldTime = nanoTime;
457}
Phil Burk204a1632017-01-03 17:23:43 -0800458
Phil Burk97350f92017-07-21 15:59:44 -0700459aaudio_result_t AudioStreamInternal::onTimestampService(AAudioServiceMessage *message) {
Phil Burk204a1632017-01-03 17:23:43 -0800460#if LOG_TIMESTAMPS
Phil Burkec89b2e2017-06-20 15:05:06 -0700461 logTimestamp(*message);
Phil Burk204a1632017-01-03 17:23:43 -0800462#endif
Phil Burk87c9f642017-05-17 07:22:39 -0700463 processTimestamp(message->timestamp.position, message->timestamp.timestamp);
Phil Burk5ed503c2017-02-01 09:38:15 -0800464 return AAUDIO_OK;
Phil Burk204a1632017-01-03 17:23:43 -0800465}
466
Phil Burk97350f92017-07-21 15:59:44 -0700467aaudio_result_t AudioStreamInternal::onTimestampHardware(AAudioServiceMessage *message) {
468 Timestamp timestamp(message->timestamp.position, message->timestamp.timestamp);
469 mAtomicTimestamp.write(timestamp);
470 return AAUDIO_OK;
471}
472
Phil Burk5ed503c2017-02-01 09:38:15 -0800473aaudio_result_t AudioStreamInternal::onEventFromServer(AAudioServiceMessage *message) {
474 aaudio_result_t result = AAUDIO_OK;
Phil Burk204a1632017-01-03 17:23:43 -0800475 switch (message->event.event) {
Phil Burk5ed503c2017-02-01 09:38:15 -0800476 case AAUDIO_SERVICE_EVENT_STARTED:
Phil Burkfbf031e2017-10-12 15:58:31 -0700477 ALOGD("%s - got AAUDIO_SERVICE_EVENT_STARTED", __func__);
Phil Burk87c9f642017-05-17 07:22:39 -0700478 if (getState() == AAUDIO_STREAM_STATE_STARTING) {
479 setState(AAUDIO_STREAM_STATE_STARTED);
480 }
Phil Burk204a1632017-01-03 17:23:43 -0800481 break;
Phil Burk5ed503c2017-02-01 09:38:15 -0800482 case AAUDIO_SERVICE_EVENT_PAUSED:
Phil Burkfbf031e2017-10-12 15:58:31 -0700483 ALOGD("%s - got AAUDIO_SERVICE_EVENT_PAUSED", __func__);
Phil Burk87c9f642017-05-17 07:22:39 -0700484 if (getState() == AAUDIO_STREAM_STATE_PAUSING) {
485 setState(AAUDIO_STREAM_STATE_PAUSED);
486 }
Phil Burk204a1632017-01-03 17:23:43 -0800487 break;
Phil Burk71f35bb2017-04-13 16:05:07 -0700488 case AAUDIO_SERVICE_EVENT_STOPPED:
Phil Burkfbf031e2017-10-12 15:58:31 -0700489 ALOGD("%s - got AAUDIO_SERVICE_EVENT_STOPPED", __func__);
Phil Burk87c9f642017-05-17 07:22:39 -0700490 if (getState() == AAUDIO_STREAM_STATE_STOPPING) {
491 setState(AAUDIO_STREAM_STATE_STOPPED);
492 }
Phil Burk71f35bb2017-04-13 16:05:07 -0700493 break;
Phil Burk5ed503c2017-02-01 09:38:15 -0800494 case AAUDIO_SERVICE_EVENT_FLUSHED:
Phil Burkfbf031e2017-10-12 15:58:31 -0700495 ALOGD("%s - got AAUDIO_SERVICE_EVENT_FLUSHED", __func__);
Phil Burk87c9f642017-05-17 07:22:39 -0700496 if (getState() == AAUDIO_STREAM_STATE_FLUSHING) {
497 setState(AAUDIO_STREAM_STATE_FLUSHED);
498 onFlushFromServer();
499 }
Phil Burk204a1632017-01-03 17:23:43 -0800500 break;
Phil Burk5ed503c2017-02-01 09:38:15 -0800501 case AAUDIO_SERVICE_EVENT_DISCONNECTED:
Phil Burkea04d972017-08-07 12:30:44 -0700502 // Prevent hardware from looping on old data and making buzzing sounds.
503 if (getDirection() == AAUDIO_DIRECTION_OUTPUT) {
504 mAudioEndpoint.eraseDataMemory();
505 }
Phil Burk5ed503c2017-02-01 09:38:15 -0800506 result = AAUDIO_ERROR_DISCONNECTED;
Phil Burkc0c70e32017-02-09 13:18:38 -0800507 setState(AAUDIO_STREAM_STATE_DISCONNECTED);
Phil Burkfbf031e2017-10-12 15:58:31 -0700508 ALOGW("%s - AAUDIO_SERVICE_EVENT_DISCONNECTED - FIFO cleared", __func__);
Phil Burk204a1632017-01-03 17:23:43 -0800509 break;
Phil Burkc0c70e32017-02-09 13:18:38 -0800510 case AAUDIO_SERVICE_EVENT_VOLUME:
Phil Burk55e5eab2018-04-10 15:16:38 -0700511 ALOGD("%s - AAUDIO_SERVICE_EVENT_VOLUME %lf", __func__, message->event.dataDouble);
Eric Laurenta2f296e2017-06-21 18:51:47 -0700512 mStreamVolume = (float)message->event.dataDouble;
513 doSetVolume();
Phil Burkc0c70e32017-02-09 13:18:38 -0800514 break;
Phil Burk23296382017-11-20 15:45:11 -0800515 case AAUDIO_SERVICE_EVENT_XRUN:
516 mXRunCount = static_cast<int32_t>(message->event.dataLong);
517 break;
Phil Burk204a1632017-01-03 17:23:43 -0800518 default:
Phil Burkfbf031e2017-10-12 15:58:31 -0700519 ALOGE("%s - Unrecognized event = %d", __func__, (int) message->event.event);
Phil Burk204a1632017-01-03 17:23:43 -0800520 break;
521 }
522 return result;
523}
524
Phil Burkbcc36742017-08-31 17:24:51 -0700525aaudio_result_t AudioStreamInternal::drainTimestampsFromService() {
526 aaudio_result_t result = AAUDIO_OK;
527
528 while (result == AAUDIO_OK) {
529 AAudioServiceMessage message;
530 if (mAudioEndpoint.readUpCommand(&message) != 1) {
531 break; // no command this time, no problem
532 }
533 switch (message.what) {
534 // ignore most messages
535 case AAudioServiceMessage::code::TIMESTAMP_SERVICE:
536 case AAudioServiceMessage::code::TIMESTAMP_HARDWARE:
537 break;
538
539 case AAudioServiceMessage::code::EVENT:
540 result = onEventFromServer(&message);
541 break;
542
543 default:
Phil Burkfbf031e2017-10-12 15:58:31 -0700544 ALOGE("%s - unrecognized message.what = %d", __func__, (int) message.what);
Phil Burkbcc36742017-08-31 17:24:51 -0700545 result = AAUDIO_ERROR_INTERNAL;
546 break;
547 }
548 }
549 return result;
550}
551
Phil Burk204a1632017-01-03 17:23:43 -0800552// Process all the commands coming from the server.
Phil Burk5ed503c2017-02-01 09:38:15 -0800553aaudio_result_t AudioStreamInternal::processCommands() {
554 aaudio_result_t result = AAUDIO_OK;
Phil Burk204a1632017-01-03 17:23:43 -0800555
Phil Burk5ed503c2017-02-01 09:38:15 -0800556 while (result == AAUDIO_OK) {
557 AAudioServiceMessage message;
Phil Burk204a1632017-01-03 17:23:43 -0800558 if (mAudioEndpoint.readUpCommand(&message) != 1) {
559 break; // no command this time, no problem
560 }
561 switch (message.what) {
Phil Burk97350f92017-07-21 15:59:44 -0700562 case AAudioServiceMessage::code::TIMESTAMP_SERVICE:
563 result = onTimestampService(&message);
564 break;
565
566 case AAudioServiceMessage::code::TIMESTAMP_HARDWARE:
567 result = onTimestampHardware(&message);
Phil Burk204a1632017-01-03 17:23:43 -0800568 break;
569
Phil Burk5ed503c2017-02-01 09:38:15 -0800570 case AAudioServiceMessage::code::EVENT:
Phil Burk204a1632017-01-03 17:23:43 -0800571 result = onEventFromServer(&message);
572 break;
573
574 default:
Phil Burkfbf031e2017-10-12 15:58:31 -0700575 ALOGE("%s - unrecognized message.what = %d", __func__, (int) message.what);
Phil Burk17fff382017-05-16 14:06:45 -0700576 result = AAUDIO_ERROR_INTERNAL;
Phil Burk204a1632017-01-03 17:23:43 -0800577 break;
578 }
579 }
580 return result;
581}
582
Phil Burk87c9f642017-05-17 07:22:39 -0700583// Read or write the data, block if needed and timeoutMillis > 0
584aaudio_result_t AudioStreamInternal::processData(void *buffer, int32_t numFrames,
585 int64_t timeoutNanoseconds)
Phil Burk204a1632017-01-03 17:23:43 -0800586{
Phil Burkfd34a932017-07-19 07:03:52 -0700587 const char * traceName = "aaProc";
588 const char * fifoName = "aaRdy";
Phil Burk4485d412017-05-09 15:55:02 -0700589 ATRACE_BEGIN(traceName);
Phil Burk4485d412017-05-09 15:55:02 -0700590 if (ATRACE_ENABLED()) {
Phil Burkfd34a932017-07-19 07:03:52 -0700591 int32_t fullFrames = mAudioEndpoint.getFullFramesAvailable();
592 ATRACE_INT(fifoName, fullFrames);
Phil Burk4485d412017-05-09 15:55:02 -0700593 }
594
Phil Burkec89b2e2017-06-20 15:05:06 -0700595 aaudio_result_t result = AAUDIO_OK;
596 int32_t loopCount = 0;
597 uint8_t* audioData = (uint8_t*)buffer;
598 int64_t currentTimeNanos = AudioClock::getNanoseconds();
599 const int64_t entryTimeNanos = currentTimeNanos;
600 const int64_t deadlineNanos = currentTimeNanos + timeoutNanoseconds;
601 int32_t framesLeft = numFrames;
602
Phil Burk87c9f642017-05-17 07:22:39 -0700603 // Loop until all the data has been processed or until a timeout occurs.
Phil Burk204a1632017-01-03 17:23:43 -0800604 while (framesLeft > 0) {
Phil Burkec89b2e2017-06-20 15:05:06 -0700605 // The call to processDataNow() will not block. It will just process as much as it can.
Phil Burk3316d5e2017-02-15 11:23:01 -0800606 int64_t wakeTimeNanos = 0;
Phil Burk87c9f642017-05-17 07:22:39 -0700607 aaudio_result_t framesProcessed = processDataNow(audioData, framesLeft,
608 currentTimeNanos, &wakeTimeNanos);
609 if (framesProcessed < 0) {
Phil Burk87c9f642017-05-17 07:22:39 -0700610 result = framesProcessed;
Phil Burk204a1632017-01-03 17:23:43 -0800611 break;
612 }
Phil Burk87c9f642017-05-17 07:22:39 -0700613 framesLeft -= (int32_t) framesProcessed;
614 audioData += framesProcessed * getBytesPerFrame();
Phil Burk204a1632017-01-03 17:23:43 -0800615
616 // Should we block?
617 if (timeoutNanoseconds == 0) {
618 break; // don't block
619 } else if (framesLeft > 0) {
Phil Burkfd34a932017-07-19 07:03:52 -0700620 if (!mAudioEndpoint.isFreeRunning()) {
621 // If there is software on the other end of the FIFO then it may get delayed.
622 // So wake up just a little after we expect it to be ready.
623 wakeTimeNanos += mWakeupDelayNanos;
Phil Burk204a1632017-01-03 17:23:43 -0800624 }
Phil Burkfd34a932017-07-19 07:03:52 -0700625
Phil Burk2bc7c182017-08-28 11:45:01 -0700626 currentTimeNanos = AudioClock::getNanoseconds();
627 int64_t earliestWakeTime = currentTimeNanos + mMinimumSleepNanos;
628 // Guarantee a minimum sleep time.
629 if (wakeTimeNanos < earliestWakeTime) {
630 wakeTimeNanos = earliestWakeTime;
631 }
632
Phil Burk204a1632017-01-03 17:23:43 -0800633 if (wakeTimeNanos > deadlineNanos) {
634 // If we time out, just return the framesWritten so far.
Phil Burkcf5f6d22017-05-26 12:35:07 -0700635 // TODO remove after we fix the deadline bug
Phil Burkfbf031e2017-10-12 15:58:31 -0700636 ALOGW("processData(): entered at %lld nanos, currently %lld",
Phil Burkec89b2e2017-06-20 15:05:06 -0700637 (long long) entryTimeNanos, (long long) currentTimeNanos);
Phil Burkfbf031e2017-10-12 15:58:31 -0700638 ALOGW("processData(): TIMEOUT after %lld nanos",
Phil Burkc0c70e32017-02-09 13:18:38 -0800639 (long long) timeoutNanoseconds);
Phil Burkfbf031e2017-10-12 15:58:31 -0700640 ALOGW("processData(): wakeTime = %lld, deadline = %lld nanos",
Phil Burk87c9f642017-05-17 07:22:39 -0700641 (long long) wakeTimeNanos, (long long) deadlineNanos);
Phil Burkfbf031e2017-10-12 15:58:31 -0700642 ALOGW("processData(): past deadline by %d micros",
Phil Burk87c9f642017-05-17 07:22:39 -0700643 (int)((wakeTimeNanos - deadlineNanos) / AAUDIO_NANOS_PER_MICROSECOND));
Phil Burkec89b2e2017-06-20 15:05:06 -0700644 mClockModel.dump();
645 mAudioEndpoint.dump();
Phil Burk204a1632017-01-03 17:23:43 -0800646 break;
647 }
648
Phil Burkfd34a932017-07-19 07:03:52 -0700649 if (ATRACE_ENABLED()) {
650 int32_t fullFrames = mAudioEndpoint.getFullFramesAvailable();
651 ATRACE_INT(fifoName, fullFrames);
652 int64_t sleepForNanos = wakeTimeNanos - currentTimeNanos;
653 ATRACE_INT("aaSlpNs", (int32_t)sleepForNanos);
654 }
655
656 AudioClock::sleepUntilNanoTime(wakeTimeNanos);
Phil Burk204a1632017-01-03 17:23:43 -0800657 currentTimeNanos = AudioClock::getNanoseconds();
658 }
659 }
660
Phil Burkfd34a932017-07-19 07:03:52 -0700661 if (ATRACE_ENABLED()) {
662 int32_t fullFrames = mAudioEndpoint.getFullFramesAvailable();
663 ATRACE_INT(fifoName, fullFrames);
664 }
665
Phil Burk87c9f642017-05-17 07:22:39 -0700666 // return error or framesProcessed
Phil Burkc0c70e32017-02-09 13:18:38 -0800667 (void) loopCount;
Phil Burk4485d412017-05-09 15:55:02 -0700668 ATRACE_END();
Phil Burk204a1632017-01-03 17:23:43 -0800669 return (result < 0) ? result : numFrames - framesLeft;
670}
671
Phil Burk3316d5e2017-02-15 11:23:01 -0800672void AudioStreamInternal::processTimestamp(uint64_t position, int64_t time) {
Phil Burk87c9f642017-05-17 07:22:39 -0700673 mClockModel.processTimestamp(position, time);
Phil Burk204a1632017-01-03 17:23:43 -0800674}
675
Phil Burk3316d5e2017-02-15 11:23:01 -0800676aaudio_result_t AudioStreamInternal::setBufferSize(int32_t requestedFrames) {
Phil Burk6479d502017-11-20 09:32:52 -0800677 int32_t adjustedFrames = requestedFrames;
Phil Burk3316d5e2017-02-15 11:23:01 -0800678 int32_t actualFrames = 0;
Phil Burk6479d502017-11-20 09:32:52 -0800679 int32_t maximumSize = getBufferCapacity();
680
681 // Clip to minimum size so that rounding up will work better.
682 if (adjustedFrames < 1) {
683 adjustedFrames = 1;
Phil Burk71f35bb2017-04-13 16:05:07 -0700684 }
685
Phil Burk6479d502017-11-20 09:32:52 -0800686 if (adjustedFrames > maximumSize) {
687 // Clip to maximum size.
688 adjustedFrames = maximumSize;
689 } else {
690 // Round to the next highest burst size.
691 int32_t numBursts = (adjustedFrames + mFramesPerBurst - 1) / mFramesPerBurst;
692 adjustedFrames = numBursts * mFramesPerBurst;
693 // Rounding may have gone above maximum.
694 if (adjustedFrames > maximumSize) {
695 adjustedFrames = maximumSize;
696 }
697 }
698
699 aaudio_result_t result = mAudioEndpoint.setBufferSizeInFrames(adjustedFrames, &actualFrames);
Phil Burkbcc36742017-08-31 17:24:51 -0700700 ALOGD("setBufferSize() req = %d => %d", requestedFrames, actualFrames);
Phil Burk3316d5e2017-02-15 11:23:01 -0800701 if (result < 0) {
702 return result;
703 } else {
704 return (aaudio_result_t) actualFrames;
705 }
Phil Burk204a1632017-01-03 17:23:43 -0800706}
707
Phil Burk87c9f642017-05-17 07:22:39 -0700708int32_t AudioStreamInternal::getBufferSize() const {
Phil Burk204a1632017-01-03 17:23:43 -0800709 return mAudioEndpoint.getBufferSizeInFrames();
710}
711
Phil Burk87c9f642017-05-17 07:22:39 -0700712int32_t AudioStreamInternal::getBufferCapacity() const {
Phil Burk204a1632017-01-03 17:23:43 -0800713 return mAudioEndpoint.getBufferCapacityInFrames();
714}
715
Phil Burk87c9f642017-05-17 07:22:39 -0700716int32_t AudioStreamInternal::getFramesPerBurst() const {
Phil Burk6479d502017-11-20 09:32:52 -0800717 return mFramesPerBurst;
Phil Burk204a1632017-01-03 17:23:43 -0800718}
719
Phil Burk87c9f642017-05-17 07:22:39 -0700720aaudio_result_t AudioStreamInternal::joinThread(void** returnArg) {
721 return AudioStream::joinThread(returnArg, calculateReasonableTimeout(getFramesPerBurst()));
Phil Burk4c5129b2017-04-28 15:17:32 -0700722}