Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 1 | /* |
| 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 Burk | cf5f6d2 | 2017-05-26 12:35:07 -0700 | [diff] [blame] | 17 | // This file is used in both client and server processes. |
| 18 | // This is needed to make sense of the logs more easily. |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 19 | #define LOG_TAG (mInService ? "AudioStreamInternal_Service" : "AudioStreamInternal_Client") |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 20 | //#define LOG_NDEBUG 0 |
| 21 | #include <utils/Log.h> |
| 22 | |
Phil Burk | 4485d41 | 2017-05-09 15:55:02 -0700 | [diff] [blame] | 23 | #define ATRACE_TAG ATRACE_TAG_AUDIO |
| 24 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 25 | #include <stdint.h> |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 26 | |
| 27 | #include <binder/IServiceManager.h> |
| 28 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 29 | #include <aaudio/AAudio.h> |
Phil Burk | fd34a93 | 2017-07-19 07:03:52 -0700 | [diff] [blame] | 30 | #include <cutils/properties.h> |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 31 | #include <utils/String16.h> |
Phil Burk | 4485d41 | 2017-05-09 15:55:02 -0700 | [diff] [blame] | 32 | #include <utils/Trace.h> |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 33 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 34 | #include "AudioEndpointParcelable.h" |
| 35 | #include "binding/AAudioStreamRequest.h" |
| 36 | #include "binding/AAudioStreamConfiguration.h" |
| 37 | #include "binding/IAAudioService.h" |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 38 | #include "binding/AAudioServiceMessage.h" |
Phil Burk | 3df348f | 2017-02-08 11:41:55 -0800 | [diff] [blame] | 39 | #include "core/AudioStreamBuilder.h" |
Phil Burk | e572f46 | 2017-04-20 13:03:19 -0700 | [diff] [blame] | 40 | #include "fifo/FifoBuffer.h" |
Phil Burk | fd34a93 | 2017-07-19 07:03:52 -0700 | [diff] [blame] | 41 | #include "utility/AudioClock.h" |
Phil Burk | e572f46 | 2017-04-20 13:03:19 -0700 | [diff] [blame] | 42 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 43 | #include "AudioStreamInternal.h" |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 44 | |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 45 | using android::String16; |
Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 46 | using android::Mutex; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 47 | using android::WrappingBuffer; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 48 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 49 | using namespace aaudio; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 50 | |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 51 | #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 Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 56 | #define LOG_TIMESTAMPS 0 |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 57 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 58 | AudioStreamInternal::AudioStreamInternal(AAudioServiceInterface &serviceInterface, bool inService) |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 59 | : AudioStream() |
| 60 | , mClockModel() |
| 61 | , mAudioEndpoint() |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 62 | , mServiceStreamHandle(AAUDIO_HANDLE_INVALID) |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 63 | , mInService(inService) |
Phil Burk | fd34a93 | 2017-07-19 07:03:52 -0700 | [diff] [blame] | 64 | , mServiceInterface(serviceInterface) |
Phil Burk | a53ffa6 | 2018-10-10 16:21:37 -0700 | [diff] [blame] | 65 | , mAtomicInternalTimestamp() |
Phil Burk | fd34a93 | 2017-07-19 07:03:52 -0700 | [diff] [blame] | 66 | , mWakeupDelayNanos(AAudioProperty_getWakeupDelayMicros() * AAUDIO_NANOS_PER_MICROSECOND) |
| 67 | , mMinimumSleepNanos(AAudioProperty_getMinimumSleepMicros() * AAUDIO_NANOS_PER_MICROSECOND) |
| 68 | { |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | AudioStreamInternal::~AudioStreamInternal() { |
| 72 | } |
| 73 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 74 | aaudio_result_t AudioStreamInternal::open(const AudioStreamBuilder &builder) { |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 75 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 76 | aaudio_result_t result = AAUDIO_OK; |
Phil Burk | 99306c8 | 2017-08-14 12:38:58 -0700 | [diff] [blame] | 77 | int32_t capacity; |
Phil Burk | 6479d50 | 2017-11-20 09:32:52 -0800 | [diff] [blame] | 78 | int32_t framesPerBurst; |
Phil Burk | 3c4e6b5 | 2019-01-22 15:53:36 -0800 | [diff] [blame] | 79 | int32_t framesPerHardwareBurst; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 80 | AAudioStreamRequest request; |
Phil Burk | 99306c8 | 2017-08-14 12:38:58 -0700 | [diff] [blame] | 81 | AAudioStreamConfiguration configurationOutput; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 82 | |
Phil Burk | 99306c8 | 2017-08-14 12:38:58 -0700 | [diff] [blame] | 83 | if (getState() != AAUDIO_STREAM_STATE_UNINITIALIZED) { |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 84 | ALOGE("%s - already open! state = %d", __func__, getState()); |
Phil Burk | 99306c8 | 2017-08-14 12:38:58 -0700 | [diff] [blame] | 85 | return AAUDIO_ERROR_INVALID_STATE; |
| 86 | } |
| 87 | |
| 88 | // Copy requested parameters to the stream. |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 89 | result = AudioStream::open(builder); |
| 90 | if (result < 0) { |
| 91 | return result; |
| 92 | } |
| 93 | |
Phil Burk | 3c4e6b5 | 2019-01-22 15:53:36 -0800 | [diff] [blame] | 94 | const int32_t burstMinMicros = AAudioProperty_getHardwareBurstMinMicros(); |
| 95 | int32_t burstMicros = 0; |
| 96 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 97 | // We have to do volume scaling. So we prefer FLOAT format. |
Phil Burk | 0127c1b | 2018-03-29 13:48:06 -0700 | [diff] [blame] | 98 | if (getFormat() == AUDIO_FORMAT_DEFAULT) { |
| 99 | setFormat(AUDIO_FORMAT_PCM_FLOAT); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 100 | } |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 101 | // Request FLOAT for the shared mixer. |
Phil Burk | 0127c1b | 2018-03-29 13:48:06 -0700 | [diff] [blame] | 102 | request.getConfiguration().setFormat(AUDIO_FORMAT_PCM_FLOAT); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 103 | |
Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 104 | // Build the request to send to the server. |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 105 | request.setUserId(getuid()); |
| 106 | request.setProcessId(getpid()); |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 107 | request.setSharingModeMatchRequired(isSharingModeMatchRequired()); |
Phil Burk | 41f19d8 | 2018-02-13 14:59:10 -0800 | [diff] [blame] | 108 | request.setInService(isInService()); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 109 | |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 110 | request.getConfiguration().setDeviceId(getDeviceId()); |
| 111 | request.getConfiguration().setSampleRate(getSampleRate()); |
| 112 | request.getConfiguration().setSamplesPerFrame(getSamplesPerFrame()); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 113 | request.getConfiguration().setDirection(getDirection()); |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 114 | request.getConfiguration().setSharingMode(getSharingMode()); |
| 115 | |
Phil Burk | a62fb95 | 2018-01-16 12:44:06 -0800 | [diff] [blame] | 116 | request.getConfiguration().setUsage(getUsage()); |
| 117 | request.getConfiguration().setContentType(getContentType()); |
| 118 | request.getConfiguration().setInputPreset(getInputPreset()); |
| 119 | |
Phil Burk | 3df348f | 2017-02-08 11:41:55 -0800 | [diff] [blame] | 120 | request.getConfiguration().setBufferCapacity(builder.getBufferCapacity()); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 121 | |
Phil Burk | 41f19d8 | 2018-02-13 14:59:10 -0800 | [diff] [blame] | 122 | mDeviceChannelCount = getSamplesPerFrame(); // Assume it will be the same. Update if not. |
| 123 | |
Phil Burk | 99306c8 | 2017-08-14 12:38:58 -0700 | [diff] [blame] | 124 | mServiceStreamHandle = mServiceInterface.openStream(request, configurationOutput); |
Phil Burk | 41f19d8 | 2018-02-13 14:59:10 -0800 | [diff] [blame] | 125 | 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 Burk | 0127c1b | 2018-03-29 13:48:06 -0700 | [diff] [blame] | 132 | ALOGD("%s() - openStream() returned %d, try switching from MONO to STEREO", |
Phil Burk | 41f19d8 | 2018-02-13 14:59:10 -0800 | [diff] [blame] | 133 | __func__, mServiceStreamHandle); |
| 134 | request.getConfiguration().setSamplesPerFrame(2); // stereo |
| 135 | mServiceStreamHandle = mServiceInterface.openStream(request, configurationOutput); |
| 136 | } |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 137 | if (mServiceStreamHandle < 0) { |
Phil Burk | 41f19d8 | 2018-02-13 14:59:10 -0800 | [diff] [blame] | 138 | return mServiceStreamHandle; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 139 | } |
Phil Burk | 99306c8 | 2017-08-14 12:38:58 -0700 | [diff] [blame] | 140 | |
| 141 | result = configurationOutput.validate(); |
| 142 | if (result != AAUDIO_OK) { |
| 143 | goto error; |
| 144 | } |
| 145 | // Save results of the open. |
Phil Burk | 41f19d8 | 2018-02-13 14:59:10 -0800 | [diff] [blame] | 146 | if (getSamplesPerFrame() == AAUDIO_UNSPECIFIED) { |
| 147 | setSamplesPerFrame(configurationOutput.getSamplesPerFrame()); |
| 148 | } |
| 149 | mDeviceChannelCount = configurationOutput.getSamplesPerFrame(); |
| 150 | |
Phil Burk | 99306c8 | 2017-08-14 12:38:58 -0700 | [diff] [blame] | 151 | setSampleRate(configurationOutput.getSampleRate()); |
Phil Burk | 99306c8 | 2017-08-14 12:38:58 -0700 | [diff] [blame] | 152 | setDeviceId(configurationOutput.getDeviceId()); |
Phil Burk | 4e1af9f | 2018-01-03 15:54:35 -0800 | [diff] [blame] | 153 | setSessionId(configurationOutput.getSessionId()); |
Phil Burk | 99306c8 | 2017-08-14 12:38:58 -0700 | [diff] [blame] | 154 | setSharingMode(configurationOutput.getSharingMode()); |
| 155 | |
Phil Burk | a62fb95 | 2018-01-16 12:44:06 -0800 | [diff] [blame] | 156 | setUsage(configurationOutput.getUsage()); |
| 157 | setContentType(configurationOutput.getContentType()); |
| 158 | setInputPreset(configurationOutput.getInputPreset()); |
| 159 | |
Phil Burk | 99306c8 | 2017-08-14 12:38:58 -0700 | [diff] [blame] | 160 | // Save device format so we can do format conversion and volume scaling together. |
Phil Burk | 3d786cb | 2018-04-09 11:58:09 -0700 | [diff] [blame] | 161 | setDeviceFormat(configurationOutput.getFormat()); |
Phil Burk | 99306c8 | 2017-08-14 12:38:58 -0700 | [diff] [blame] | 162 | |
| 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 Burk | 3c4e6b5 | 2019-01-22 15:53:36 -0800 | [diff] [blame] | 180 | 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 Burk | 6479d50 | 2017-11-20 09:32:52 -0800 | [diff] [blame] | 196 | if (framesPerBurst < MIN_FRAMES_PER_BURST || framesPerBurst > MAX_FRAMES_PER_BURST) { |
| 197 | ALOGE("%s - framesPerBurst out of range = %d", __func__, framesPerBurst); |
Phil Burk | 99306c8 | 2017-08-14 12:38:58 -0700 | [diff] [blame] | 198 | result = AAUDIO_ERROR_OUT_OF_RANGE; |
| 199 | goto error; |
| 200 | } |
Phil Burk | 6479d50 | 2017-11-20 09:32:52 -0800 | [diff] [blame] | 201 | mFramesPerBurst = framesPerBurst; // only save good value |
| 202 | |
| 203 | capacity = mEndpointDescriptor.dataQueueDescriptor.capacityInFrames; |
| 204 | if (capacity < mFramesPerBurst || capacity > MAX_BUFFER_CAPACITY_IN_FRAMES) { |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 205 | ALOGE("%s - bufferCapacity out of range = %d", __func__, capacity); |
Phil Burk | 99306c8 | 2017-08-14 12:38:58 -0700 | [diff] [blame] | 206 | result = AAUDIO_ERROR_OUT_OF_RANGE; |
| 207 | goto error; |
| 208 | } |
| 209 | |
| 210 | mClockModel.setSampleRate(getSampleRate()); |
Phil Burk | 3c4e6b5 | 2019-01-22 15:53:36 -0800 | [diff] [blame] | 211 | mClockModel.setFramesPerBurst(framesPerHardwareBurst); |
Phil Burk | 99306c8 | 2017-08-14 12:38:58 -0700 | [diff] [blame] | 212 | |
Phil Burk | 134f197 | 2017-12-08 13:06:11 -0800 | [diff] [blame] | 213 | if (isDataCallbackSet()) { |
Phil Burk | 99306c8 | 2017-08-14 12:38:58 -0700 | [diff] [blame] | 214 | mCallbackFrames = builder.getFramesPerDataCallback(); |
| 215 | if (mCallbackFrames > getBufferCapacity() / 2) { |
Phil Burk | 29ccc29 | 2019-04-15 08:58:08 -0700 | [diff] [blame] | 216 | ALOGW("%s - framesPerCallback too big = %d, capacity = %d", |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 217 | __func__, mCallbackFrames, getBufferCapacity()); |
Phil Burk | 99306c8 | 2017-08-14 12:38:58 -0700 | [diff] [blame] | 218 | result = AAUDIO_ERROR_OUT_OF_RANGE; |
| 219 | goto error; |
| 220 | |
| 221 | } else if (mCallbackFrames < 0) { |
Phil Burk | 29ccc29 | 2019-04-15 08:58:08 -0700 | [diff] [blame] | 222 | ALOGW("%s - framesPerCallback negative", __func__); |
Phil Burk | 99306c8 | 2017-08-14 12:38:58 -0700 | [diff] [blame] | 223 | result = AAUDIO_ERROR_OUT_OF_RANGE; |
| 224 | goto error; |
| 225 | |
| 226 | } |
| 227 | if (mCallbackFrames == AAUDIO_UNSPECIFIED) { |
| 228 | mCallbackFrames = mFramesPerBurst; |
| 229 | } |
| 230 | |
Phil Burk | 0127c1b | 2018-03-29 13:48:06 -0700 | [diff] [blame] | 231 | const int32_t callbackBufferSize = mCallbackFrames * getBytesPerFrame(); |
Phil Burk | 99306c8 | 2017-08-14 12:38:58 -0700 | [diff] [blame] | 232 | mCallbackBuffer = new uint8_t[callbackBufferSize]; |
| 233 | } |
| 234 | |
Phil Burk | b31b66f | 2019-09-30 09:33:41 -0700 | [diff] [blame] | 235 | // For debugging and analyzing the distribution of MMAP timestamps. |
| 236 | // For OUTPUT, use a NEGATIVE offset to move the CPU writes further BEFORE the HW reads. |
| 237 | // For INPUT, use a POSITIVE offset to move the CPU reads further AFTER the HW writes. |
| 238 | // You can use this offset to reduce glitching. |
| 239 | // You can also use this offset to force glitching. By iterating over multiple |
| 240 | // values you can reveal the distribution of the hardware timing jitter. |
| 241 | if (mAudioEndpoint.isFreeRunning()) { // MMAP? |
| 242 | int32_t offsetMicros = (getDirection() == AAUDIO_DIRECTION_OUTPUT) |
| 243 | ? AAudioProperty_getOutputMMapOffsetMicros() |
| 244 | : AAudioProperty_getInputMMapOffsetMicros(); |
| 245 | // This log is used to debug some tricky glitch issues. Please leave. |
| 246 | ALOGD_IF(offsetMicros, "%s() - %s mmap offset = %d micros", |
| 247 | __func__, |
| 248 | (getDirection() == AAUDIO_DIRECTION_OUTPUT) ? "output" : "input", |
| 249 | offsetMicros); |
| 250 | mTimeOffsetNanos = offsetMicros * AAUDIO_NANOS_PER_MICROSECOND; |
| 251 | } |
| 252 | |
Phil Burk | 6c63ae3 | 2019-10-28 10:28:21 -0700 | [diff] [blame] | 253 | setBufferSize(capacity / 2); // Default buffer size to match Q |
| 254 | |
Phil Burk | 99306c8 | 2017-08-14 12:38:58 -0700 | [diff] [blame] | 255 | setState(AAUDIO_STREAM_STATE_OPEN); |
Phil Burk | 99306c8 | 2017-08-14 12:38:58 -0700 | [diff] [blame] | 256 | |
| 257 | return result; |
| 258 | |
| 259 | error: |
| 260 | close(); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 261 | return result; |
| 262 | } |
| 263 | |
Phil Burk | 13d3d83 | 2019-06-10 14:36:48 -0700 | [diff] [blame] | 264 | // This must be called under mStreamLock. |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 265 | aaudio_result_t AudioStreamInternal::close() { |
Phil Burk | 965650e | 2017-09-07 21:00:09 -0700 | [diff] [blame] | 266 | aaudio_result_t result = AAUDIO_OK; |
Phil Burk | 29ccc29 | 2019-04-15 08:58:08 -0700 | [diff] [blame] | 267 | ALOGV("%s(): mServiceStreamHandle = 0x%08X", __func__, mServiceStreamHandle); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 268 | if (mServiceStreamHandle != AAUDIO_HANDLE_INVALID) { |
Phil Burk | 4485d41 | 2017-05-09 15:55:02 -0700 | [diff] [blame] | 269 | // Don't close a stream while it is running. |
| 270 | aaudio_stream_state_t currentState = getState(); |
Phil Burk | 13d3d83 | 2019-06-10 14:36:48 -0700 | [diff] [blame] | 271 | // Don't close a stream while it is running. Stop it first. |
| 272 | // If DISCONNECTED then we should still try to stop in case the |
| 273 | // error callback is still running. |
| 274 | if (isActive() || currentState == AAUDIO_STREAM_STATE_DISCONNECTED) { |
Phil Burk | 4485d41 | 2017-05-09 15:55:02 -0700 | [diff] [blame] | 275 | requestStop(); |
Phil Burk | 4485d41 | 2017-05-09 15:55:02 -0700 | [diff] [blame] | 276 | } |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 277 | setState(AAUDIO_STREAM_STATE_CLOSING); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 278 | aaudio_handle_t serviceStreamHandle = mServiceStreamHandle; |
| 279 | mServiceStreamHandle = AAUDIO_HANDLE_INVALID; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 280 | |
| 281 | mServiceInterface.closeStream(serviceStreamHandle); |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 282 | delete[] mCallbackBuffer; |
Phil Burk | 4485d41 | 2017-05-09 15:55:02 -0700 | [diff] [blame] | 283 | mCallbackBuffer = nullptr; |
Phil Burk | 965650e | 2017-09-07 21:00:09 -0700 | [diff] [blame] | 284 | |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 285 | setState(AAUDIO_STREAM_STATE_CLOSED); |
Phil Burk | 965650e | 2017-09-07 21:00:09 -0700 | [diff] [blame] | 286 | result = mEndPointParcelable.close(); |
| 287 | aaudio_result_t result2 = AudioStream::close(); |
| 288 | return (result != AAUDIO_OK) ? result : result2; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 289 | } else { |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 290 | return AAUDIO_ERROR_INVALID_HANDLE; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 291 | } |
| 292 | } |
| 293 | |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 294 | static void *aaudio_callback_thread_proc(void *context) |
| 295 | { |
| 296 | AudioStreamInternal *stream = (AudioStreamInternal *)context; |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 297 | //LOGD("oboe_callback_thread, stream = %p", stream); |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 298 | if (stream != NULL) { |
| 299 | return stream->callbackLoop(); |
| 300 | } else { |
| 301 | return NULL; |
| 302 | } |
| 303 | } |
| 304 | |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 305 | /* |
| 306 | * It normally takes about 20-30 msec to start a stream on the server. |
| 307 | * But the first time can take as much as 200-300 msec. The HW |
| 308 | * starts right away so by the time the client gets a chance to write into |
| 309 | * the buffer, it is already in a deep underflow state. That can cause the |
| 310 | * XRunCount to be non-zero, which could lead an app to tune its latency higher. |
| 311 | * To avoid this problem, we set a request for the processing code to start the |
| 312 | * client stream at the same position as the server stream. |
| 313 | * The processing code will then save the current offset |
| 314 | * between client and server and apply that to any position given to the app. |
| 315 | */ |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 316 | aaudio_result_t AudioStreamInternal::requestStart() |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 317 | { |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 318 | int64_t startTime; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 319 | if (mServiceStreamHandle == AAUDIO_HANDLE_INVALID) { |
Phil Burk | 29ccc29 | 2019-04-15 08:58:08 -0700 | [diff] [blame] | 320 | ALOGD("requestStart() mServiceStreamHandle invalid"); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 321 | return AAUDIO_ERROR_INVALID_STATE; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 322 | } |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 323 | if (isActive()) { |
Phil Burk | 29ccc29 | 2019-04-15 08:58:08 -0700 | [diff] [blame] | 324 | ALOGD("requestStart() already active"); |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 325 | return AAUDIO_ERROR_INVALID_STATE; |
| 326 | } |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 327 | |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 328 | aaudio_stream_state_t originalState = getState(); |
| 329 | if (originalState == AAUDIO_STREAM_STATE_DISCONNECTED) { |
Phil Burk | 29ccc29 | 2019-04-15 08:58:08 -0700 | [diff] [blame] | 330 | ALOGD("requestStart() but DISCONNECTED"); |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 331 | return AAUDIO_ERROR_DISCONNECTED; |
| 332 | } |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 333 | setState(AAUDIO_STREAM_STATE_STARTING); |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 334 | |
| 335 | // Clear any stale timestamps from the previous run. |
| 336 | drainTimestampsFromService(); |
| 337 | |
Phil Burk | 965650e | 2017-09-07 21:00:09 -0700 | [diff] [blame] | 338 | aaudio_result_t result = mServiceInterface.startStream(mServiceStreamHandle); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 339 | |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 340 | startTime = AudioClock::getNanoseconds(); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 341 | mClockModel.start(startTime); |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 342 | mNeedCatchUp.request(); // Ask data processing code to catch up when first timestamp received. |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 343 | |
Phil Burk | 965650e | 2017-09-07 21:00:09 -0700 | [diff] [blame] | 344 | // Start data callback thread. |
Phil Burk | 134f197 | 2017-12-08 13:06:11 -0800 | [diff] [blame] | 345 | if (result == AAUDIO_OK && isDataCallbackSet()) { |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 346 | // Launch the callback loop thread. |
| 347 | int64_t periodNanos = mCallbackFrames |
| 348 | * AAUDIO_NANOS_PER_SECOND |
| 349 | / getSampleRate(); |
| 350 | mCallbackEnabled.store(true); |
| 351 | result = createThread(periodNanos, aaudio_callback_thread_proc, this); |
| 352 | } |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 353 | if (result != AAUDIO_OK) { |
| 354 | setState(originalState); |
| 355 | } |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 356 | return result; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 357 | } |
| 358 | |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 359 | int64_t AudioStreamInternal::calculateReasonableTimeout(int32_t framesPerOperation) { |
| 360 | |
| 361 | // Wait for at least a second or some number of callbacks to join the thread. |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 362 | int64_t timeoutNanoseconds = (MIN_TIMEOUT_OPERATIONS |
| 363 | * framesPerOperation |
| 364 | * AAUDIO_NANOS_PER_SECOND) |
| 365 | / getSampleRate(); |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 366 | if (timeoutNanoseconds < MIN_TIMEOUT_NANOS) { // arbitrary number of seconds |
| 367 | timeoutNanoseconds = MIN_TIMEOUT_NANOS; |
| 368 | } |
| 369 | return timeoutNanoseconds; |
| 370 | } |
| 371 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 372 | int64_t AudioStreamInternal::calculateReasonableTimeout() { |
| 373 | return calculateReasonableTimeout(getFramesPerBurst()); |
| 374 | } |
| 375 | |
Phil Burk | 13d3d83 | 2019-06-10 14:36:48 -0700 | [diff] [blame] | 376 | // This must be called under mStreamLock. |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 377 | aaudio_result_t AudioStreamInternal::stopCallback() |
| 378 | { |
Phil Burk | 13d3d83 | 2019-06-10 14:36:48 -0700 | [diff] [blame] | 379 | if (isDataCallbackSet() |
| 380 | && (isActive() || getState() == AAUDIO_STREAM_STATE_DISCONNECTED)) { |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 381 | mCallbackEnabled.store(false); |
Phil Burk | 13d3d83 | 2019-06-10 14:36:48 -0700 | [diff] [blame] | 382 | return joinThread(NULL); // may temporarily unlock mStreamLock |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 383 | } else { |
| 384 | return AAUDIO_OK; |
| 385 | } |
| 386 | } |
| 387 | |
Phil Burk | 13d3d83 | 2019-06-10 14:36:48 -0700 | [diff] [blame] | 388 | // This must be called under mStreamLock. |
Phil Burk | 1e83bee | 2018-12-17 14:15:20 -0800 | [diff] [blame] | 389 | aaudio_result_t AudioStreamInternal::requestStop() { |
Phil Burk | 5cc83c3 | 2017-11-28 15:43:18 -0800 | [diff] [blame] | 390 | aaudio_result_t result = stopCallback(); |
| 391 | if (result != AAUDIO_OK) { |
| 392 | return result; |
| 393 | } |
Phil Burk | 13d3d83 | 2019-06-10 14:36:48 -0700 | [diff] [blame] | 394 | // The stream may have been unlocked temporarily to let a callback finish |
| 395 | // and the callback may have stopped the stream. |
| 396 | // Check to make sure the stream still needs to be stopped. |
| 397 | // See also AudioStream::safeStop(). |
| 398 | if (!(isActive() || getState() == AAUDIO_STREAM_STATE_DISCONNECTED)) { |
| 399 | return AAUDIO_OK; |
| 400 | } |
Phil Burk | 5cc83c3 | 2017-11-28 15:43:18 -0800 | [diff] [blame] | 401 | |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 402 | if (mServiceStreamHandle == AAUDIO_HANDLE_INVALID) { |
Phil Burk | 29ccc29 | 2019-04-15 08:58:08 -0700 | [diff] [blame] | 403 | ALOGW("%s() mServiceStreamHandle invalid = 0x%08X", |
| 404 | __func__, mServiceStreamHandle); |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 405 | return AAUDIO_ERROR_INVALID_STATE; |
| 406 | } |
| 407 | |
| 408 | mClockModel.stop(AudioClock::getNanoseconds()); |
| 409 | setState(AAUDIO_STREAM_STATE_STOPPING); |
Phil Burk | a53ffa6 | 2018-10-10 16:21:37 -0700 | [diff] [blame] | 410 | mAtomicInternalTimestamp.clear(); |
Phil Burk | 965650e | 2017-09-07 21:00:09 -0700 | [diff] [blame] | 411 | |
| 412 | return mServiceInterface.stopStream(mServiceStreamHandle); |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 413 | } |
| 414 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 415 | aaudio_result_t AudioStreamInternal::registerThread() { |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 416 | if (mServiceStreamHandle == AAUDIO_HANDLE_INVALID) { |
Phil Burk | 29ccc29 | 2019-04-15 08:58:08 -0700 | [diff] [blame] | 417 | ALOGW("%s() mServiceStreamHandle invalid", __func__); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 418 | return AAUDIO_ERROR_INVALID_STATE; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 419 | } |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 420 | return mServiceInterface.registerAudioThread(mServiceStreamHandle, |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 421 | gettid(), |
| 422 | getPeriodNanoseconds()); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 423 | } |
| 424 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 425 | aaudio_result_t AudioStreamInternal::unregisterThread() { |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 426 | if (mServiceStreamHandle == AAUDIO_HANDLE_INVALID) { |
Phil Burk | 29ccc29 | 2019-04-15 08:58:08 -0700 | [diff] [blame] | 427 | ALOGW("%s() mServiceStreamHandle invalid", __func__); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 428 | return AAUDIO_ERROR_INVALID_STATE; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 429 | } |
Phil Burk | 2ac035f | 2017-06-23 14:51:14 -0700 | [diff] [blame] | 430 | return mServiceInterface.unregisterAudioThread(mServiceStreamHandle, gettid()); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 431 | } |
| 432 | |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 433 | aaudio_result_t AudioStreamInternal::startClient(const android::AudioClient& client, |
Phil Burk | bbd5286 | 2018-04-13 11:37:42 -0700 | [diff] [blame] | 434 | audio_port_handle_t *portHandle) { |
| 435 | ALOGV("%s() called", __func__); |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 436 | if (mServiceStreamHandle == AAUDIO_HANDLE_INVALID) { |
| 437 | return AAUDIO_ERROR_INVALID_STATE; |
| 438 | } |
Phil Burk | bbd5286 | 2018-04-13 11:37:42 -0700 | [diff] [blame] | 439 | aaudio_result_t result = mServiceInterface.startClient(mServiceStreamHandle, |
| 440 | client, portHandle); |
| 441 | ALOGV("%s(%d) returning %d", __func__, *portHandle, result); |
| 442 | return result; |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 443 | } |
| 444 | |
Phil Burk | bbd5286 | 2018-04-13 11:37:42 -0700 | [diff] [blame] | 445 | aaudio_result_t AudioStreamInternal::stopClient(audio_port_handle_t portHandle) { |
| 446 | ALOGV("%s(%d) called", __func__, portHandle); |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 447 | if (mServiceStreamHandle == AAUDIO_HANDLE_INVALID) { |
| 448 | return AAUDIO_ERROR_INVALID_STATE; |
| 449 | } |
Phil Burk | bbd5286 | 2018-04-13 11:37:42 -0700 | [diff] [blame] | 450 | aaudio_result_t result = mServiceInterface.stopClient(mServiceStreamHandle, portHandle); |
| 451 | ALOGV("%s(%d) returning %d", __func__, portHandle, result); |
| 452 | return result; |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 453 | } |
| 454 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 455 | aaudio_result_t AudioStreamInternal::getTimestamp(clockid_t clockId, |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 456 | int64_t *framePosition, |
| 457 | int64_t *timeNanoseconds) { |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 458 | // Generated in server and passed to client. Return latest. |
Phil Burk | a53ffa6 | 2018-10-10 16:21:37 -0700 | [diff] [blame] | 459 | if (mAtomicInternalTimestamp.isValid()) { |
| 460 | Timestamp timestamp = mAtomicInternalTimestamp.read(); |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 461 | int64_t position = timestamp.getPosition() + mFramesOffsetFromService; |
| 462 | if (position >= 0) { |
| 463 | *framePosition = position; |
| 464 | *timeNanoseconds = timestamp.getNanoseconds(); |
| 465 | return AAUDIO_OK; |
| 466 | } |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 467 | } |
Phil Burk | c75d97f | 2017-09-08 15:48:36 -0700 | [diff] [blame] | 468 | return AAUDIO_ERROR_INVALID_STATE; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 469 | } |
| 470 | |
Phil Burk | 0befec6 | 2017-07-28 15:12:13 -0700 | [diff] [blame] | 471 | aaudio_result_t AudioStreamInternal::updateStateMachine() { |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 472 | if (isDataCallbackActive()) { |
| 473 | return AAUDIO_OK; // state is getting updated by the callback thread read/write call |
| 474 | } |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 475 | return processCommands(); |
| 476 | } |
| 477 | |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 478 | void AudioStreamInternal::logTimestamp(AAudioServiceMessage &command) { |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 479 | static int64_t oldPosition = 0; |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 480 | static int64_t oldTime = 0; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 481 | int64_t framePosition = command.timestamp.position; |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 482 | int64_t nanoTime = command.timestamp.timestamp; |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 483 | ALOGD("logTimestamp: timestamp says framePosition = %8lld at nanoTime %lld", |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 484 | (long long) framePosition, |
| 485 | (long long) nanoTime); |
| 486 | int64_t nanosDelta = nanoTime - oldTime; |
| 487 | if (nanosDelta > 0 && oldTime > 0) { |
| 488 | int64_t framesDelta = framePosition - oldPosition; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 489 | int64_t rate = (framesDelta * AAUDIO_NANOS_PER_SECOND) / nanosDelta; |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 490 | ALOGD("logTimestamp: framesDelta = %8lld, nanosDelta = %8lld, rate = %lld", |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 491 | (long long) framesDelta, (long long) nanosDelta, (long long) rate); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 492 | } |
| 493 | oldPosition = framePosition; |
| 494 | oldTime = nanoTime; |
| 495 | } |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 496 | |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 497 | aaudio_result_t AudioStreamInternal::onTimestampService(AAudioServiceMessage *message) { |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 498 | #if LOG_TIMESTAMPS |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 499 | logTimestamp(*message); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 500 | #endif |
Phil Burk | b31b66f | 2019-09-30 09:33:41 -0700 | [diff] [blame] | 501 | processTimestamp(message->timestamp.position, |
| 502 | message->timestamp.timestamp + mTimeOffsetNanos); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 503 | return AAUDIO_OK; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 504 | } |
| 505 | |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 506 | aaudio_result_t AudioStreamInternal::onTimestampHardware(AAudioServiceMessage *message) { |
| 507 | Timestamp timestamp(message->timestamp.position, message->timestamp.timestamp); |
Phil Burk | a53ffa6 | 2018-10-10 16:21:37 -0700 | [diff] [blame] | 508 | mAtomicInternalTimestamp.write(timestamp); |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 509 | return AAUDIO_OK; |
| 510 | } |
| 511 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 512 | aaudio_result_t AudioStreamInternal::onEventFromServer(AAudioServiceMessage *message) { |
| 513 | aaudio_result_t result = AAUDIO_OK; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 514 | switch (message->event.event) { |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 515 | case AAUDIO_SERVICE_EVENT_STARTED: |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 516 | ALOGD("%s - got AAUDIO_SERVICE_EVENT_STARTED", __func__); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 517 | if (getState() == AAUDIO_STREAM_STATE_STARTING) { |
| 518 | setState(AAUDIO_STREAM_STATE_STARTED); |
| 519 | } |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 520 | break; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 521 | case AAUDIO_SERVICE_EVENT_PAUSED: |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 522 | ALOGD("%s - got AAUDIO_SERVICE_EVENT_PAUSED", __func__); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 523 | if (getState() == AAUDIO_STREAM_STATE_PAUSING) { |
| 524 | setState(AAUDIO_STREAM_STATE_PAUSED); |
| 525 | } |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 526 | break; |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 527 | case AAUDIO_SERVICE_EVENT_STOPPED: |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 528 | ALOGD("%s - got AAUDIO_SERVICE_EVENT_STOPPED", __func__); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 529 | if (getState() == AAUDIO_STREAM_STATE_STOPPING) { |
| 530 | setState(AAUDIO_STREAM_STATE_STOPPED); |
| 531 | } |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 532 | break; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 533 | case AAUDIO_SERVICE_EVENT_FLUSHED: |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 534 | ALOGD("%s - got AAUDIO_SERVICE_EVENT_FLUSHED", __func__); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 535 | if (getState() == AAUDIO_STREAM_STATE_FLUSHING) { |
| 536 | setState(AAUDIO_STREAM_STATE_FLUSHED); |
| 537 | onFlushFromServer(); |
| 538 | } |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 539 | break; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 540 | case AAUDIO_SERVICE_EVENT_DISCONNECTED: |
Phil Burk | ea04d97 | 2017-08-07 12:30:44 -0700 | [diff] [blame] | 541 | // Prevent hardware from looping on old data and making buzzing sounds. |
| 542 | if (getDirection() == AAUDIO_DIRECTION_OUTPUT) { |
| 543 | mAudioEndpoint.eraseDataMemory(); |
| 544 | } |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 545 | result = AAUDIO_ERROR_DISCONNECTED; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 546 | setState(AAUDIO_STREAM_STATE_DISCONNECTED); |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 547 | ALOGW("%s - AAUDIO_SERVICE_EVENT_DISCONNECTED - FIFO cleared", __func__); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 548 | break; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 549 | case AAUDIO_SERVICE_EVENT_VOLUME: |
Phil Burk | 55e5eab | 2018-04-10 15:16:38 -0700 | [diff] [blame] | 550 | ALOGD("%s - AAUDIO_SERVICE_EVENT_VOLUME %lf", __func__, message->event.dataDouble); |
Eric Laurent | a2f296e | 2017-06-21 18:51:47 -0700 | [diff] [blame] | 551 | mStreamVolume = (float)message->event.dataDouble; |
| 552 | doSetVolume(); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 553 | break; |
Phil Burk | 2329638 | 2017-11-20 15:45:11 -0800 | [diff] [blame] | 554 | case AAUDIO_SERVICE_EVENT_XRUN: |
| 555 | mXRunCount = static_cast<int32_t>(message->event.dataLong); |
| 556 | break; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 557 | default: |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 558 | ALOGE("%s - Unrecognized event = %d", __func__, (int) message->event.event); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 559 | break; |
| 560 | } |
| 561 | return result; |
| 562 | } |
| 563 | |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 564 | aaudio_result_t AudioStreamInternal::drainTimestampsFromService() { |
| 565 | aaudio_result_t result = AAUDIO_OK; |
| 566 | |
| 567 | while (result == AAUDIO_OK) { |
| 568 | AAudioServiceMessage message; |
| 569 | if (mAudioEndpoint.readUpCommand(&message) != 1) { |
| 570 | break; // no command this time, no problem |
| 571 | } |
| 572 | switch (message.what) { |
| 573 | // ignore most messages |
| 574 | case AAudioServiceMessage::code::TIMESTAMP_SERVICE: |
| 575 | case AAudioServiceMessage::code::TIMESTAMP_HARDWARE: |
| 576 | break; |
| 577 | |
| 578 | case AAudioServiceMessage::code::EVENT: |
| 579 | result = onEventFromServer(&message); |
| 580 | break; |
| 581 | |
| 582 | default: |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 583 | ALOGE("%s - unrecognized message.what = %d", __func__, (int) message.what); |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 584 | result = AAUDIO_ERROR_INTERNAL; |
| 585 | break; |
| 586 | } |
| 587 | } |
| 588 | return result; |
| 589 | } |
| 590 | |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 591 | // Process all the commands coming from the server. |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 592 | aaudio_result_t AudioStreamInternal::processCommands() { |
| 593 | aaudio_result_t result = AAUDIO_OK; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 594 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 595 | while (result == AAUDIO_OK) { |
| 596 | AAudioServiceMessage message; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 597 | if (mAudioEndpoint.readUpCommand(&message) != 1) { |
| 598 | break; // no command this time, no problem |
| 599 | } |
| 600 | switch (message.what) { |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 601 | case AAudioServiceMessage::code::TIMESTAMP_SERVICE: |
| 602 | result = onTimestampService(&message); |
| 603 | break; |
| 604 | |
| 605 | case AAudioServiceMessage::code::TIMESTAMP_HARDWARE: |
| 606 | result = onTimestampHardware(&message); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 607 | break; |
| 608 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 609 | case AAudioServiceMessage::code::EVENT: |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 610 | result = onEventFromServer(&message); |
| 611 | break; |
| 612 | |
| 613 | default: |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 614 | ALOGE("%s - unrecognized message.what = %d", __func__, (int) message.what); |
Phil Burk | 17fff38 | 2017-05-16 14:06:45 -0700 | [diff] [blame] | 615 | result = AAUDIO_ERROR_INTERNAL; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 616 | break; |
| 617 | } |
| 618 | } |
| 619 | return result; |
| 620 | } |
| 621 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 622 | // Read or write the data, block if needed and timeoutMillis > 0 |
| 623 | aaudio_result_t AudioStreamInternal::processData(void *buffer, int32_t numFrames, |
| 624 | int64_t timeoutNanoseconds) |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 625 | { |
Phil Burk | fd34a93 | 2017-07-19 07:03:52 -0700 | [diff] [blame] | 626 | const char * traceName = "aaProc"; |
| 627 | const char * fifoName = "aaRdy"; |
Phil Burk | 4485d41 | 2017-05-09 15:55:02 -0700 | [diff] [blame] | 628 | ATRACE_BEGIN(traceName); |
Phil Burk | 4485d41 | 2017-05-09 15:55:02 -0700 | [diff] [blame] | 629 | if (ATRACE_ENABLED()) { |
Phil Burk | fd34a93 | 2017-07-19 07:03:52 -0700 | [diff] [blame] | 630 | int32_t fullFrames = mAudioEndpoint.getFullFramesAvailable(); |
| 631 | ATRACE_INT(fifoName, fullFrames); |
Phil Burk | 4485d41 | 2017-05-09 15:55:02 -0700 | [diff] [blame] | 632 | } |
| 633 | |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 634 | aaudio_result_t result = AAUDIO_OK; |
| 635 | int32_t loopCount = 0; |
| 636 | uint8_t* audioData = (uint8_t*)buffer; |
| 637 | int64_t currentTimeNanos = AudioClock::getNanoseconds(); |
| 638 | const int64_t entryTimeNanos = currentTimeNanos; |
| 639 | const int64_t deadlineNanos = currentTimeNanos + timeoutNanoseconds; |
| 640 | int32_t framesLeft = numFrames; |
| 641 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 642 | // Loop until all the data has been processed or until a timeout occurs. |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 643 | while (framesLeft > 0) { |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 644 | // The call to processDataNow() will not block. It will just process as much as it can. |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 645 | int64_t wakeTimeNanos = 0; |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 646 | aaudio_result_t framesProcessed = processDataNow(audioData, framesLeft, |
| 647 | currentTimeNanos, &wakeTimeNanos); |
| 648 | if (framesProcessed < 0) { |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 649 | result = framesProcessed; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 650 | break; |
| 651 | } |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 652 | framesLeft -= (int32_t) framesProcessed; |
| 653 | audioData += framesProcessed * getBytesPerFrame(); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 654 | |
| 655 | // Should we block? |
| 656 | if (timeoutNanoseconds == 0) { |
| 657 | break; // don't block |
Phil Burk | 8d4f006 | 2019-10-03 15:55:41 -0700 | [diff] [blame] | 658 | } else if (wakeTimeNanos != 0) { |
Phil Burk | fd34a93 | 2017-07-19 07:03:52 -0700 | [diff] [blame] | 659 | if (!mAudioEndpoint.isFreeRunning()) { |
| 660 | // If there is software on the other end of the FIFO then it may get delayed. |
| 661 | // So wake up just a little after we expect it to be ready. |
| 662 | wakeTimeNanos += mWakeupDelayNanos; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 663 | } |
Phil Burk | fd34a93 | 2017-07-19 07:03:52 -0700 | [diff] [blame] | 664 | |
Phil Burk | 2bc7c18 | 2017-08-28 11:45:01 -0700 | [diff] [blame] | 665 | currentTimeNanos = AudioClock::getNanoseconds(); |
| 666 | int64_t earliestWakeTime = currentTimeNanos + mMinimumSleepNanos; |
| 667 | // Guarantee a minimum sleep time. |
| 668 | if (wakeTimeNanos < earliestWakeTime) { |
| 669 | wakeTimeNanos = earliestWakeTime; |
| 670 | } |
| 671 | |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 672 | if (wakeTimeNanos > deadlineNanos) { |
| 673 | // If we time out, just return the framesWritten so far. |
Phil Burk | cf5f6d2 | 2017-05-26 12:35:07 -0700 | [diff] [blame] | 674 | // TODO remove after we fix the deadline bug |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 675 | ALOGW("processData(): entered at %lld nanos, currently %lld", |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 676 | (long long) entryTimeNanos, (long long) currentTimeNanos); |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 677 | ALOGW("processData(): TIMEOUT after %lld nanos", |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 678 | (long long) timeoutNanoseconds); |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 679 | ALOGW("processData(): wakeTime = %lld, deadline = %lld nanos", |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 680 | (long long) wakeTimeNanos, (long long) deadlineNanos); |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 681 | ALOGW("processData(): past deadline by %d micros", |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 682 | (int)((wakeTimeNanos - deadlineNanos) / AAUDIO_NANOS_PER_MICROSECOND)); |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 683 | mClockModel.dump(); |
| 684 | mAudioEndpoint.dump(); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 685 | break; |
| 686 | } |
| 687 | |
Phil Burk | fd34a93 | 2017-07-19 07:03:52 -0700 | [diff] [blame] | 688 | if (ATRACE_ENABLED()) { |
| 689 | int32_t fullFrames = mAudioEndpoint.getFullFramesAvailable(); |
| 690 | ATRACE_INT(fifoName, fullFrames); |
| 691 | int64_t sleepForNanos = wakeTimeNanos - currentTimeNanos; |
| 692 | ATRACE_INT("aaSlpNs", (int32_t)sleepForNanos); |
| 693 | } |
| 694 | |
| 695 | AudioClock::sleepUntilNanoTime(wakeTimeNanos); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 696 | currentTimeNanos = AudioClock::getNanoseconds(); |
| 697 | } |
| 698 | } |
| 699 | |
Phil Burk | fd34a93 | 2017-07-19 07:03:52 -0700 | [diff] [blame] | 700 | if (ATRACE_ENABLED()) { |
| 701 | int32_t fullFrames = mAudioEndpoint.getFullFramesAvailable(); |
| 702 | ATRACE_INT(fifoName, fullFrames); |
| 703 | } |
| 704 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 705 | // return error or framesProcessed |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 706 | (void) loopCount; |
Phil Burk | 4485d41 | 2017-05-09 15:55:02 -0700 | [diff] [blame] | 707 | ATRACE_END(); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 708 | return (result < 0) ? result : numFrames - framesLeft; |
| 709 | } |
| 710 | |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 711 | void AudioStreamInternal::processTimestamp(uint64_t position, int64_t time) { |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 712 | mClockModel.processTimestamp(position, time); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 713 | } |
| 714 | |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 715 | aaudio_result_t AudioStreamInternal::setBufferSize(int32_t requestedFrames) { |
Phil Burk | 6479d50 | 2017-11-20 09:32:52 -0800 | [diff] [blame] | 716 | int32_t adjustedFrames = requestedFrames; |
Phil Burk | 8d4f006 | 2019-10-03 15:55:41 -0700 | [diff] [blame] | 717 | const int32_t maximumSize = getBufferCapacity() - mFramesPerBurst; |
| 718 | // The buffer size can be set to zero. |
| 719 | // This means that the callback may be called when the internal buffer becomes empty. |
| 720 | // This will be fine on some devices in ideal circumstances and will result in the |
| 721 | // lowest possible latency. |
| 722 | // If there are glitches then they should be detected as XRuns and the size can be increased. |
| 723 | static const int32_t minimumSize = 0; |
Phil Burk | 6479d50 | 2017-11-20 09:32:52 -0800 | [diff] [blame] | 724 | |
| 725 | // Clip to minimum size so that rounding up will work better. |
Phil Burk | 8d4f006 | 2019-10-03 15:55:41 -0700 | [diff] [blame] | 726 | adjustedFrames = std::max(minimumSize, adjustedFrames); |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 727 | |
Phil Burk | 8d4f006 | 2019-10-03 15:55:41 -0700 | [diff] [blame] | 728 | // Prevent arithmetic overflow by clipping before we round. |
| 729 | if (adjustedFrames >= maximumSize) { |
Phil Burk | 6479d50 | 2017-11-20 09:32:52 -0800 | [diff] [blame] | 730 | adjustedFrames = maximumSize; |
| 731 | } else { |
| 732 | // Round to the next highest burst size. |
| 733 | int32_t numBursts = (adjustedFrames + mFramesPerBurst - 1) / mFramesPerBurst; |
| 734 | adjustedFrames = numBursts * mFramesPerBurst; |
Phil Burk | 6479d50 | 2017-11-20 09:32:52 -0800 | [diff] [blame] | 735 | } |
| 736 | |
Phil Burk | 8d4f006 | 2019-10-03 15:55:41 -0700 | [diff] [blame] | 737 | // Clip against the actual size from the endpoint. |
| 738 | int32_t actualFrames = 0; |
| 739 | mAudioEndpoint.setBufferSizeInFrames(maximumSize, &actualFrames); |
| 740 | // actualFrames should be <= maximumSize |
| 741 | adjustedFrames = std::min(actualFrames, adjustedFrames); |
| 742 | |
| 743 | mBufferSizeInFrames = adjustedFrames; |
Phil Burk | 6c63ae3 | 2019-10-28 10:28:21 -0700 | [diff] [blame] | 744 | ALOGV("%s(%d) returns %d", __func__, requestedFrames, adjustedFrames); |
Phil Burk | 8d4f006 | 2019-10-03 15:55:41 -0700 | [diff] [blame] | 745 | return (aaudio_result_t) adjustedFrames; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 746 | } |
| 747 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 748 | int32_t AudioStreamInternal::getBufferSize() const { |
Phil Burk | 8d4f006 | 2019-10-03 15:55:41 -0700 | [diff] [blame] | 749 | return mBufferSizeInFrames; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 750 | } |
| 751 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 752 | int32_t AudioStreamInternal::getBufferCapacity() const { |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 753 | return mAudioEndpoint.getBufferCapacityInFrames(); |
| 754 | } |
| 755 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 756 | int32_t AudioStreamInternal::getFramesPerBurst() const { |
Phil Burk | 6479d50 | 2017-11-20 09:32:52 -0800 | [diff] [blame] | 757 | return mFramesPerBurst; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 758 | } |
| 759 | |
Phil Burk | 13d3d83 | 2019-06-10 14:36:48 -0700 | [diff] [blame] | 760 | // This must be called under mStreamLock. |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 761 | aaudio_result_t AudioStreamInternal::joinThread(void** returnArg) { |
| 762 | return AudioStream::joinThread(returnArg, calculateReasonableTimeout(getFramesPerBurst())); |
Phil Burk | 4c5129b | 2017-04-28 15:17:32 -0700 | [diff] [blame] | 763 | } |
Phil Burk | 377c1c2 | 2018-12-12 16:06:54 -0800 | [diff] [blame] | 764 | |
| 765 | bool AudioStreamInternal::isClockModelInControl() const { |
| 766 | return isActive() && mAudioEndpoint.isFreeRunning() && mClockModel.isRunning(); |
| 767 | } |