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