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