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 | #include "utility/LinearRamp.h" |
| 43 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 44 | #include "AudioStreamInternal.h" |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 45 | |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 46 | using android::String16; |
Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 47 | using android::Mutex; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 48 | using android::WrappingBuffer; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 49 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 50 | using namespace aaudio; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 51 | |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 52 | #define MIN_TIMEOUT_NANOS (1000 * AAUDIO_NANOS_PER_MILLISECOND) |
| 53 | |
| 54 | // Wait at least this many times longer than the operation should take. |
| 55 | #define MIN_TIMEOUT_OPERATIONS 4 |
| 56 | |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 57 | #define LOG_TIMESTAMPS 0 |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 58 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 59 | AudioStreamInternal::AudioStreamInternal(AAudioServiceInterface &serviceInterface, bool inService) |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 60 | : AudioStream() |
| 61 | , mClockModel() |
| 62 | , mAudioEndpoint() |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 63 | , mServiceStreamHandle(AAUDIO_HANDLE_INVALID) |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 64 | , mInService(inService) |
Phil Burk | fd34a93 | 2017-07-19 07:03:52 -0700 | [diff] [blame] | 65 | , mServiceInterface(serviceInterface) |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 66 | , mAtomicTimestamp() |
Phil Burk | fd34a93 | 2017-07-19 07:03:52 -0700 | [diff] [blame] | 67 | , mWakeupDelayNanos(AAudioProperty_getWakeupDelayMicros() * AAUDIO_NANOS_PER_MICROSECOND) |
| 68 | , mMinimumSleepNanos(AAudioProperty_getMinimumSleepMicros() * AAUDIO_NANOS_PER_MICROSECOND) |
| 69 | { |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 70 | ALOGD("%s - mWakeupDelayNanos = %d, mMinimumSleepNanos = %d", |
| 71 | __func__, mWakeupDelayNanos, mMinimumSleepNanos); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | AudioStreamInternal::~AudioStreamInternal() { |
| 75 | } |
| 76 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 77 | aaudio_result_t AudioStreamInternal::open(const AudioStreamBuilder &builder) { |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 78 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 79 | aaudio_result_t result = AAUDIO_OK; |
Phil Burk | 99306c8 | 2017-08-14 12:38:58 -0700 | [diff] [blame] | 80 | int32_t capacity; |
Phil Burk | 6479d50 | 2017-11-20 09:32:52 -0800 | [diff] [blame] | 81 | int32_t framesPerBurst; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 82 | AAudioStreamRequest request; |
Phil Burk | 99306c8 | 2017-08-14 12:38:58 -0700 | [diff] [blame] | 83 | AAudioStreamConfiguration configurationOutput; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 84 | |
Phil Burk | 99306c8 | 2017-08-14 12:38:58 -0700 | [diff] [blame] | 85 | if (getState() != AAUDIO_STREAM_STATE_UNINITIALIZED) { |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 86 | ALOGE("%s - already open! state = %d", __func__, getState()); |
Phil Burk | 99306c8 | 2017-08-14 12:38:58 -0700 | [diff] [blame] | 87 | return AAUDIO_ERROR_INVALID_STATE; |
| 88 | } |
| 89 | |
| 90 | // Copy requested parameters to the stream. |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 91 | result = AudioStream::open(builder); |
| 92 | if (result < 0) { |
| 93 | return result; |
| 94 | } |
| 95 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 96 | // We have to do volume scaling. So we prefer FLOAT format. |
Glenn Kasten | 37a466a | 2017-05-30 15:53:14 -0700 | [diff] [blame] | 97 | if (getFormat() == AAUDIO_FORMAT_UNSPECIFIED) { |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 98 | setFormat(AAUDIO_FORMAT_PCM_FLOAT); |
| 99 | } |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 100 | // Request FLOAT for the shared mixer. |
jiabin | 901f65d | 2017-07-12 17:56:35 -0700 | [diff] [blame] | 101 | request.getConfiguration().setFormat(AAUDIO_FORMAT_PCM_FLOAT); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 102 | |
Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 103 | // Build the request to send to the server. |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 104 | request.setUserId(getuid()); |
| 105 | request.setProcessId(getpid()); |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 106 | request.setSharingModeMatchRequired(isSharingModeMatchRequired()); |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 107 | request.setInService(mInService); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 108 | |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 109 | request.getConfiguration().setDeviceId(getDeviceId()); |
| 110 | request.getConfiguration().setSampleRate(getSampleRate()); |
| 111 | request.getConfiguration().setSamplesPerFrame(getSamplesPerFrame()); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 112 | request.getConfiguration().setDirection(getDirection()); |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 113 | request.getConfiguration().setSharingMode(getSharingMode()); |
| 114 | |
Phil Burk | a62fb95 | 2018-01-16 12:44:06 -0800 | [diff] [blame] | 115 | request.getConfiguration().setUsage(getUsage()); |
| 116 | request.getConfiguration().setContentType(getContentType()); |
| 117 | request.getConfiguration().setInputPreset(getInputPreset()); |
| 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 | 99306c8 | 2017-08-14 12:38:58 -0700 | [diff] [blame] | 121 | mServiceStreamHandle = mServiceInterface.openStream(request, configurationOutput); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 122 | if (mServiceStreamHandle < 0) { |
| 123 | result = mServiceStreamHandle; |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 124 | ALOGE("%s - openStream() returned %d", __func__, result); |
Phil Burk | 99306c8 | 2017-08-14 12:38:58 -0700 | [diff] [blame] | 125 | return result; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 126 | } |
Phil Burk | 99306c8 | 2017-08-14 12:38:58 -0700 | [diff] [blame] | 127 | |
| 128 | result = configurationOutput.validate(); |
| 129 | if (result != AAUDIO_OK) { |
| 130 | goto error; |
| 131 | } |
| 132 | // Save results of the open. |
| 133 | setSampleRate(configurationOutput.getSampleRate()); |
| 134 | setSamplesPerFrame(configurationOutput.getSamplesPerFrame()); |
| 135 | setDeviceId(configurationOutput.getDeviceId()); |
Phil Burk | 4e1af9f | 2018-01-03 15:54:35 -0800 | [diff] [blame] | 136 | setSessionId(configurationOutput.getSessionId()); |
Phil Burk | 99306c8 | 2017-08-14 12:38:58 -0700 | [diff] [blame] | 137 | setSharingMode(configurationOutput.getSharingMode()); |
| 138 | |
Phil Burk | a62fb95 | 2018-01-16 12:44:06 -0800 | [diff] [blame] | 139 | setUsage(configurationOutput.getUsage()); |
| 140 | setContentType(configurationOutput.getContentType()); |
| 141 | setInputPreset(configurationOutput.getInputPreset()); |
| 142 | |
Phil Burk | 99306c8 | 2017-08-14 12:38:58 -0700 | [diff] [blame] | 143 | // Save device format so we can do format conversion and volume scaling together. |
| 144 | mDeviceFormat = configurationOutput.getFormat(); |
| 145 | |
| 146 | result = mServiceInterface.getStreamDescription(mServiceStreamHandle, mEndPointParcelable); |
| 147 | if (result != AAUDIO_OK) { |
| 148 | goto error; |
| 149 | } |
| 150 | |
| 151 | // Resolve parcelable into a descriptor. |
| 152 | result = mEndPointParcelable.resolve(&mEndpointDescriptor); |
| 153 | if (result != AAUDIO_OK) { |
| 154 | goto error; |
| 155 | } |
| 156 | |
| 157 | // Configure endpoint based on descriptor. |
| 158 | result = mAudioEndpoint.configure(&mEndpointDescriptor, getDirection()); |
| 159 | if (result != AAUDIO_OK) { |
| 160 | goto error; |
| 161 | } |
| 162 | |
Phil Burk | 99306c8 | 2017-08-14 12:38:58 -0700 | [diff] [blame] | 163 | |
| 164 | // Validate result from server. |
Phil Burk | 6479d50 | 2017-11-20 09:32:52 -0800 | [diff] [blame] | 165 | framesPerBurst = mEndpointDescriptor.dataQueueDescriptor.framesPerBurst; |
| 166 | if (framesPerBurst < MIN_FRAMES_PER_BURST || framesPerBurst > MAX_FRAMES_PER_BURST) { |
| 167 | ALOGE("%s - framesPerBurst out of range = %d", __func__, framesPerBurst); |
Phil Burk | 99306c8 | 2017-08-14 12:38:58 -0700 | [diff] [blame] | 168 | result = AAUDIO_ERROR_OUT_OF_RANGE; |
| 169 | goto error; |
| 170 | } |
Phil Burk | 6479d50 | 2017-11-20 09:32:52 -0800 | [diff] [blame] | 171 | mFramesPerBurst = framesPerBurst; // only save good value |
| 172 | |
| 173 | capacity = mEndpointDescriptor.dataQueueDescriptor.capacityInFrames; |
| 174 | if (capacity < mFramesPerBurst || capacity > MAX_BUFFER_CAPACITY_IN_FRAMES) { |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 175 | ALOGE("%s - bufferCapacity out of range = %d", __func__, capacity); |
Phil Burk | 99306c8 | 2017-08-14 12:38:58 -0700 | [diff] [blame] | 176 | result = AAUDIO_ERROR_OUT_OF_RANGE; |
| 177 | goto error; |
| 178 | } |
| 179 | |
| 180 | mClockModel.setSampleRate(getSampleRate()); |
| 181 | mClockModel.setFramesPerBurst(mFramesPerBurst); |
| 182 | |
Phil Burk | 134f197 | 2017-12-08 13:06:11 -0800 | [diff] [blame] | 183 | if (isDataCallbackSet()) { |
Phil Burk | 99306c8 | 2017-08-14 12:38:58 -0700 | [diff] [blame] | 184 | mCallbackFrames = builder.getFramesPerDataCallback(); |
| 185 | if (mCallbackFrames > getBufferCapacity() / 2) { |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 186 | ALOGE("%s - framesPerCallback too big = %d, capacity = %d", |
| 187 | __func__, mCallbackFrames, getBufferCapacity()); |
Phil Burk | 99306c8 | 2017-08-14 12:38:58 -0700 | [diff] [blame] | 188 | result = AAUDIO_ERROR_OUT_OF_RANGE; |
| 189 | goto error; |
| 190 | |
| 191 | } else if (mCallbackFrames < 0) { |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 192 | ALOGE("%s - framesPerCallback negative", __func__); |
Phil Burk | 99306c8 | 2017-08-14 12:38:58 -0700 | [diff] [blame] | 193 | result = AAUDIO_ERROR_OUT_OF_RANGE; |
| 194 | goto error; |
| 195 | |
| 196 | } |
| 197 | if (mCallbackFrames == AAUDIO_UNSPECIFIED) { |
| 198 | mCallbackFrames = mFramesPerBurst; |
| 199 | } |
| 200 | |
| 201 | int32_t bytesPerFrame = getSamplesPerFrame() |
| 202 | * AAudioConvert_formatToSizeInBytes(getFormat()); |
| 203 | int32_t callbackBufferSize = mCallbackFrames * bytesPerFrame; |
| 204 | mCallbackBuffer = new uint8_t[callbackBufferSize]; |
| 205 | } |
| 206 | |
| 207 | setState(AAUDIO_STREAM_STATE_OPEN); |
Phil Burk | 99306c8 | 2017-08-14 12:38:58 -0700 | [diff] [blame] | 208 | |
| 209 | return result; |
| 210 | |
| 211 | error: |
| 212 | close(); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 213 | return result; |
| 214 | } |
| 215 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 216 | aaudio_result_t AudioStreamInternal::close() { |
Phil Burk | 965650e | 2017-09-07 21:00:09 -0700 | [diff] [blame] | 217 | aaudio_result_t result = AAUDIO_OK; |
| 218 | ALOGD("close(): mServiceStreamHandle = 0x%08X", |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 219 | mServiceStreamHandle); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 220 | if (mServiceStreamHandle != AAUDIO_HANDLE_INVALID) { |
Phil Burk | 4485d41 | 2017-05-09 15:55:02 -0700 | [diff] [blame] | 221 | // Don't close a stream while it is running. |
| 222 | aaudio_stream_state_t currentState = getState(); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 223 | if (isActive()) { |
Phil Burk | 4485d41 | 2017-05-09 15:55:02 -0700 | [diff] [blame] | 224 | requestStop(); |
| 225 | aaudio_stream_state_t nextState; |
| 226 | int64_t timeoutNanoseconds = MIN_TIMEOUT_NANOS; |
Phil Burk | 965650e | 2017-09-07 21:00:09 -0700 | [diff] [blame] | 227 | result = waitForStateChange(currentState, &nextState, |
Phil Burk | 4485d41 | 2017-05-09 15:55:02 -0700 | [diff] [blame] | 228 | timeoutNanoseconds); |
| 229 | if (result != AAUDIO_OK) { |
Phil Burk | 965650e | 2017-09-07 21:00:09 -0700 | [diff] [blame] | 230 | ALOGE("close() waitForStateChange() returned %d %s", |
Phil Burk | 4485d41 | 2017-05-09 15:55:02 -0700 | [diff] [blame] | 231 | result, AAudio_convertResultToText(result)); |
| 232 | } |
| 233 | } |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 234 | setState(AAUDIO_STREAM_STATE_CLOSING); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 235 | aaudio_handle_t serviceStreamHandle = mServiceStreamHandle; |
| 236 | mServiceStreamHandle = AAUDIO_HANDLE_INVALID; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 237 | |
| 238 | mServiceInterface.closeStream(serviceStreamHandle); |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 239 | delete[] mCallbackBuffer; |
Phil Burk | 4485d41 | 2017-05-09 15:55:02 -0700 | [diff] [blame] | 240 | mCallbackBuffer = nullptr; |
Phil Burk | 965650e | 2017-09-07 21:00:09 -0700 | [diff] [blame] | 241 | |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 242 | setState(AAUDIO_STREAM_STATE_CLOSED); |
Phil Burk | 965650e | 2017-09-07 21:00:09 -0700 | [diff] [blame] | 243 | result = mEndPointParcelable.close(); |
| 244 | aaudio_result_t result2 = AudioStream::close(); |
| 245 | return (result != AAUDIO_OK) ? result : result2; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 246 | } else { |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 247 | return AAUDIO_ERROR_INVALID_HANDLE; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 248 | } |
| 249 | } |
| 250 | |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 251 | static void *aaudio_callback_thread_proc(void *context) |
| 252 | { |
| 253 | AudioStreamInternal *stream = (AudioStreamInternal *)context; |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 254 | //LOGD("oboe_callback_thread, stream = %p", stream); |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 255 | if (stream != NULL) { |
| 256 | return stream->callbackLoop(); |
| 257 | } else { |
| 258 | return NULL; |
| 259 | } |
| 260 | } |
| 261 | |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 262 | /* |
| 263 | * It normally takes about 20-30 msec to start a stream on the server. |
| 264 | * But the first time can take as much as 200-300 msec. The HW |
| 265 | * starts right away so by the time the client gets a chance to write into |
| 266 | * the buffer, it is already in a deep underflow state. That can cause the |
| 267 | * XRunCount to be non-zero, which could lead an app to tune its latency higher. |
| 268 | * To avoid this problem, we set a request for the processing code to start the |
| 269 | * client stream at the same position as the server stream. |
| 270 | * The processing code will then save the current offset |
| 271 | * between client and server and apply that to any position given to the app. |
| 272 | */ |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 273 | aaudio_result_t AudioStreamInternal::requestStart() |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 274 | { |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 275 | int64_t startTime; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 276 | if (mServiceStreamHandle == AAUDIO_HANDLE_INVALID) { |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 277 | ALOGE("requestStart() mServiceStreamHandle invalid"); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 278 | return AAUDIO_ERROR_INVALID_STATE; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 279 | } |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 280 | if (isActive()) { |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 281 | ALOGE("requestStart() already active"); |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 282 | return AAUDIO_ERROR_INVALID_STATE; |
| 283 | } |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 284 | |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 285 | aaudio_stream_state_t originalState = getState(); |
| 286 | if (originalState == AAUDIO_STREAM_STATE_DISCONNECTED) { |
| 287 | ALOGE("requestStart() but DISCONNECTED"); |
| 288 | return AAUDIO_ERROR_DISCONNECTED; |
| 289 | } |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 290 | setState(AAUDIO_STREAM_STATE_STARTING); |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 291 | |
| 292 | // Clear any stale timestamps from the previous run. |
| 293 | drainTimestampsFromService(); |
| 294 | |
Phil Burk | 965650e | 2017-09-07 21:00:09 -0700 | [diff] [blame] | 295 | aaudio_result_t result = mServiceInterface.startStream(mServiceStreamHandle); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 296 | |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 297 | startTime = AudioClock::getNanoseconds(); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 298 | mClockModel.start(startTime); |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 299 | 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] | 300 | |
Phil Burk | 965650e | 2017-09-07 21:00:09 -0700 | [diff] [blame] | 301 | // Start data callback thread. |
Phil Burk | 134f197 | 2017-12-08 13:06:11 -0800 | [diff] [blame] | 302 | if (result == AAUDIO_OK && isDataCallbackSet()) { |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 303 | // Launch the callback loop thread. |
| 304 | int64_t periodNanos = mCallbackFrames |
| 305 | * AAUDIO_NANOS_PER_SECOND |
| 306 | / getSampleRate(); |
| 307 | mCallbackEnabled.store(true); |
| 308 | result = createThread(periodNanos, aaudio_callback_thread_proc, this); |
| 309 | } |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 310 | if (result != AAUDIO_OK) { |
| 311 | setState(originalState); |
| 312 | } |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 313 | return result; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 314 | } |
| 315 | |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 316 | int64_t AudioStreamInternal::calculateReasonableTimeout(int32_t framesPerOperation) { |
| 317 | |
| 318 | // 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] | 319 | int64_t timeoutNanoseconds = (MIN_TIMEOUT_OPERATIONS |
| 320 | * framesPerOperation |
| 321 | * AAUDIO_NANOS_PER_SECOND) |
| 322 | / getSampleRate(); |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 323 | if (timeoutNanoseconds < MIN_TIMEOUT_NANOS) { // arbitrary number of seconds |
| 324 | timeoutNanoseconds = MIN_TIMEOUT_NANOS; |
| 325 | } |
| 326 | return timeoutNanoseconds; |
| 327 | } |
| 328 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 329 | int64_t AudioStreamInternal::calculateReasonableTimeout() { |
| 330 | return calculateReasonableTimeout(getFramesPerBurst()); |
| 331 | } |
| 332 | |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 333 | aaudio_result_t AudioStreamInternal::stopCallback() |
| 334 | { |
| 335 | if (isDataCallbackActive()) { |
| 336 | mCallbackEnabled.store(false); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 337 | return joinThread(NULL); |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 338 | } else { |
| 339 | return AAUDIO_OK; |
| 340 | } |
| 341 | } |
| 342 | |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 343 | aaudio_result_t AudioStreamInternal::requestStopInternal() |
| 344 | { |
| 345 | if (mServiceStreamHandle == AAUDIO_HANDLE_INVALID) { |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 346 | ALOGE("requestStopInternal() mServiceStreamHandle invalid = 0x%08X", |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 347 | mServiceStreamHandle); |
| 348 | return AAUDIO_ERROR_INVALID_STATE; |
| 349 | } |
| 350 | |
| 351 | mClockModel.stop(AudioClock::getNanoseconds()); |
| 352 | setState(AAUDIO_STREAM_STATE_STOPPING); |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 353 | mAtomicTimestamp.clear(); |
Phil Burk | 965650e | 2017-09-07 21:00:09 -0700 | [diff] [blame] | 354 | |
| 355 | return mServiceInterface.stopStream(mServiceStreamHandle); |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 356 | } |
| 357 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 358 | aaudio_result_t AudioStreamInternal::requestStop() |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 359 | { |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 360 | aaudio_result_t result = stopCallback(); |
| 361 | if (result != AAUDIO_OK) { |
| 362 | return result; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 363 | } |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 364 | result = requestStopInternal(); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 365 | return result; |
| 366 | } |
| 367 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 368 | aaudio_result_t AudioStreamInternal::registerThread() { |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 369 | if (mServiceStreamHandle == AAUDIO_HANDLE_INVALID) { |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 370 | ALOGE("registerThread() mServiceStreamHandle invalid"); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 371 | return AAUDIO_ERROR_INVALID_STATE; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 372 | } |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 373 | return mServiceInterface.registerAudioThread(mServiceStreamHandle, |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 374 | gettid(), |
| 375 | getPeriodNanoseconds()); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 376 | } |
| 377 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 378 | aaudio_result_t AudioStreamInternal::unregisterThread() { |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 379 | if (mServiceStreamHandle == AAUDIO_HANDLE_INVALID) { |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 380 | ALOGE("unregisterThread() mServiceStreamHandle invalid"); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 381 | return AAUDIO_ERROR_INVALID_STATE; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 382 | } |
Phil Burk | 2ac035f | 2017-06-23 14:51:14 -0700 | [diff] [blame] | 383 | return mServiceInterface.unregisterAudioThread(mServiceStreamHandle, gettid()); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 384 | } |
| 385 | |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 386 | aaudio_result_t AudioStreamInternal::startClient(const android::AudioClient& client, |
| 387 | audio_port_handle_t *clientHandle) { |
| 388 | if (mServiceStreamHandle == AAUDIO_HANDLE_INVALID) { |
| 389 | return AAUDIO_ERROR_INVALID_STATE; |
| 390 | } |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 391 | |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 392 | return mServiceInterface.startClient(mServiceStreamHandle, client, clientHandle); |
| 393 | } |
| 394 | |
| 395 | aaudio_result_t AudioStreamInternal::stopClient(audio_port_handle_t clientHandle) { |
| 396 | if (mServiceStreamHandle == AAUDIO_HANDLE_INVALID) { |
| 397 | return AAUDIO_ERROR_INVALID_STATE; |
| 398 | } |
| 399 | return mServiceInterface.stopClient(mServiceStreamHandle, clientHandle); |
| 400 | } |
| 401 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 402 | aaudio_result_t AudioStreamInternal::getTimestamp(clockid_t clockId, |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 403 | int64_t *framePosition, |
| 404 | int64_t *timeNanoseconds) { |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 405 | // Generated in server and passed to client. Return latest. |
| 406 | if (mAtomicTimestamp.isValid()) { |
| 407 | Timestamp timestamp = mAtomicTimestamp.read(); |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 408 | int64_t position = timestamp.getPosition() + mFramesOffsetFromService; |
| 409 | if (position >= 0) { |
| 410 | *framePosition = position; |
| 411 | *timeNanoseconds = timestamp.getNanoseconds(); |
| 412 | return AAUDIO_OK; |
| 413 | } |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 414 | } |
Phil Burk | c75d97f | 2017-09-08 15:48:36 -0700 | [diff] [blame] | 415 | return AAUDIO_ERROR_INVALID_STATE; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 416 | } |
| 417 | |
Phil Burk | 0befec6 | 2017-07-28 15:12:13 -0700 | [diff] [blame] | 418 | aaudio_result_t AudioStreamInternal::updateStateMachine() { |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 419 | if (isDataCallbackActive()) { |
| 420 | return AAUDIO_OK; // state is getting updated by the callback thread read/write call |
| 421 | } |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 422 | return processCommands(); |
| 423 | } |
| 424 | |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 425 | void AudioStreamInternal::logTimestamp(AAudioServiceMessage &command) { |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 426 | static int64_t oldPosition = 0; |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 427 | static int64_t oldTime = 0; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 428 | int64_t framePosition = command.timestamp.position; |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 429 | int64_t nanoTime = command.timestamp.timestamp; |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 430 | ALOGD("logTimestamp: timestamp says framePosition = %8lld at nanoTime %lld", |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 431 | (long long) framePosition, |
| 432 | (long long) nanoTime); |
| 433 | int64_t nanosDelta = nanoTime - oldTime; |
| 434 | if (nanosDelta > 0 && oldTime > 0) { |
| 435 | int64_t framesDelta = framePosition - oldPosition; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 436 | int64_t rate = (framesDelta * AAUDIO_NANOS_PER_SECOND) / nanosDelta; |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 437 | ALOGD("logTimestamp: framesDelta = %8lld, nanosDelta = %8lld, rate = %lld", |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 438 | (long long) framesDelta, (long long) nanosDelta, (long long) rate); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 439 | } |
| 440 | oldPosition = framePosition; |
| 441 | oldTime = nanoTime; |
| 442 | } |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 443 | |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 444 | aaudio_result_t AudioStreamInternal::onTimestampService(AAudioServiceMessage *message) { |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 445 | #if LOG_TIMESTAMPS |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 446 | logTimestamp(*message); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 447 | #endif |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 448 | processTimestamp(message->timestamp.position, message->timestamp.timestamp); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 449 | return AAUDIO_OK; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 450 | } |
| 451 | |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 452 | aaudio_result_t AudioStreamInternal::onTimestampHardware(AAudioServiceMessage *message) { |
| 453 | Timestamp timestamp(message->timestamp.position, message->timestamp.timestamp); |
| 454 | mAtomicTimestamp.write(timestamp); |
| 455 | return AAUDIO_OK; |
| 456 | } |
| 457 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 458 | aaudio_result_t AudioStreamInternal::onEventFromServer(AAudioServiceMessage *message) { |
| 459 | aaudio_result_t result = AAUDIO_OK; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 460 | switch (message->event.event) { |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 461 | case AAUDIO_SERVICE_EVENT_STARTED: |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 462 | ALOGD("%s - got AAUDIO_SERVICE_EVENT_STARTED", __func__); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 463 | if (getState() == AAUDIO_STREAM_STATE_STARTING) { |
| 464 | setState(AAUDIO_STREAM_STATE_STARTED); |
| 465 | } |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 466 | break; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 467 | case AAUDIO_SERVICE_EVENT_PAUSED: |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 468 | ALOGD("%s - got AAUDIO_SERVICE_EVENT_PAUSED", __func__); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 469 | if (getState() == AAUDIO_STREAM_STATE_PAUSING) { |
| 470 | setState(AAUDIO_STREAM_STATE_PAUSED); |
| 471 | } |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 472 | break; |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 473 | case AAUDIO_SERVICE_EVENT_STOPPED: |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 474 | ALOGD("%s - got AAUDIO_SERVICE_EVENT_STOPPED", __func__); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 475 | if (getState() == AAUDIO_STREAM_STATE_STOPPING) { |
| 476 | setState(AAUDIO_STREAM_STATE_STOPPED); |
| 477 | } |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 478 | break; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 479 | case AAUDIO_SERVICE_EVENT_FLUSHED: |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 480 | ALOGD("%s - got AAUDIO_SERVICE_EVENT_FLUSHED", __func__); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 481 | if (getState() == AAUDIO_STREAM_STATE_FLUSHING) { |
| 482 | setState(AAUDIO_STREAM_STATE_FLUSHED); |
| 483 | onFlushFromServer(); |
| 484 | } |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 485 | break; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 486 | case AAUDIO_SERVICE_EVENT_CLOSED: |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 487 | ALOGD("%s - got AAUDIO_SERVICE_EVENT_CLOSED", __func__); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 488 | setState(AAUDIO_STREAM_STATE_CLOSED); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 489 | break; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 490 | case AAUDIO_SERVICE_EVENT_DISCONNECTED: |
Phil Burk | ea04d97 | 2017-08-07 12:30:44 -0700 | [diff] [blame] | 491 | // Prevent hardware from looping on old data and making buzzing sounds. |
| 492 | if (getDirection() == AAUDIO_DIRECTION_OUTPUT) { |
| 493 | mAudioEndpoint.eraseDataMemory(); |
| 494 | } |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 495 | result = AAUDIO_ERROR_DISCONNECTED; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 496 | setState(AAUDIO_STREAM_STATE_DISCONNECTED); |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 497 | ALOGW("%s - AAUDIO_SERVICE_EVENT_DISCONNECTED - FIFO cleared", __func__); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 498 | break; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 499 | case AAUDIO_SERVICE_EVENT_VOLUME: |
Eric Laurent | a2f296e | 2017-06-21 18:51:47 -0700 | [diff] [blame] | 500 | mStreamVolume = (float)message->event.dataDouble; |
| 501 | doSetVolume(); |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 502 | ALOGD("%s - AAUDIO_SERVICE_EVENT_VOLUME %lf", __func__, message->event.dataDouble); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 503 | break; |
Phil Burk | 2329638 | 2017-11-20 15:45:11 -0800 | [diff] [blame] | 504 | case AAUDIO_SERVICE_EVENT_XRUN: |
| 505 | mXRunCount = static_cast<int32_t>(message->event.dataLong); |
| 506 | break; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 507 | default: |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 508 | ALOGE("%s - Unrecognized event = %d", __func__, (int) message->event.event); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 509 | break; |
| 510 | } |
| 511 | return result; |
| 512 | } |
| 513 | |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 514 | aaudio_result_t AudioStreamInternal::drainTimestampsFromService() { |
| 515 | aaudio_result_t result = AAUDIO_OK; |
| 516 | |
| 517 | while (result == AAUDIO_OK) { |
| 518 | AAudioServiceMessage message; |
| 519 | if (mAudioEndpoint.readUpCommand(&message) != 1) { |
| 520 | break; // no command this time, no problem |
| 521 | } |
| 522 | switch (message.what) { |
| 523 | // ignore most messages |
| 524 | case AAudioServiceMessage::code::TIMESTAMP_SERVICE: |
| 525 | case AAudioServiceMessage::code::TIMESTAMP_HARDWARE: |
| 526 | break; |
| 527 | |
| 528 | case AAudioServiceMessage::code::EVENT: |
| 529 | result = onEventFromServer(&message); |
| 530 | break; |
| 531 | |
| 532 | default: |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 533 | ALOGE("%s - unrecognized message.what = %d", __func__, (int) message.what); |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 534 | result = AAUDIO_ERROR_INTERNAL; |
| 535 | break; |
| 536 | } |
| 537 | } |
| 538 | return result; |
| 539 | } |
| 540 | |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 541 | // Process all the commands coming from the server. |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 542 | aaudio_result_t AudioStreamInternal::processCommands() { |
| 543 | aaudio_result_t result = AAUDIO_OK; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 544 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 545 | while (result == AAUDIO_OK) { |
| 546 | AAudioServiceMessage message; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 547 | if (mAudioEndpoint.readUpCommand(&message) != 1) { |
| 548 | break; // no command this time, no problem |
| 549 | } |
| 550 | switch (message.what) { |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 551 | case AAudioServiceMessage::code::TIMESTAMP_SERVICE: |
| 552 | result = onTimestampService(&message); |
| 553 | break; |
| 554 | |
| 555 | case AAudioServiceMessage::code::TIMESTAMP_HARDWARE: |
| 556 | result = onTimestampHardware(&message); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 557 | break; |
| 558 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 559 | case AAudioServiceMessage::code::EVENT: |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 560 | result = onEventFromServer(&message); |
| 561 | break; |
| 562 | |
| 563 | default: |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 564 | ALOGE("%s - unrecognized message.what = %d", __func__, (int) message.what); |
Phil Burk | 17fff38 | 2017-05-16 14:06:45 -0700 | [diff] [blame] | 565 | result = AAUDIO_ERROR_INTERNAL; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 566 | break; |
| 567 | } |
| 568 | } |
| 569 | return result; |
| 570 | } |
| 571 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 572 | // Read or write the data, block if needed and timeoutMillis > 0 |
| 573 | aaudio_result_t AudioStreamInternal::processData(void *buffer, int32_t numFrames, |
| 574 | int64_t timeoutNanoseconds) |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 575 | { |
Phil Burk | fd34a93 | 2017-07-19 07:03:52 -0700 | [diff] [blame] | 576 | const char * traceName = "aaProc"; |
| 577 | const char * fifoName = "aaRdy"; |
Phil Burk | 4485d41 | 2017-05-09 15:55:02 -0700 | [diff] [blame] | 578 | ATRACE_BEGIN(traceName); |
Phil Burk | 4485d41 | 2017-05-09 15:55:02 -0700 | [diff] [blame] | 579 | if (ATRACE_ENABLED()) { |
Phil Burk | fd34a93 | 2017-07-19 07:03:52 -0700 | [diff] [blame] | 580 | int32_t fullFrames = mAudioEndpoint.getFullFramesAvailable(); |
| 581 | ATRACE_INT(fifoName, fullFrames); |
Phil Burk | 4485d41 | 2017-05-09 15:55:02 -0700 | [diff] [blame] | 582 | } |
| 583 | |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 584 | aaudio_result_t result = AAUDIO_OK; |
| 585 | int32_t loopCount = 0; |
| 586 | uint8_t* audioData = (uint8_t*)buffer; |
| 587 | int64_t currentTimeNanos = AudioClock::getNanoseconds(); |
| 588 | const int64_t entryTimeNanos = currentTimeNanos; |
| 589 | const int64_t deadlineNanos = currentTimeNanos + timeoutNanoseconds; |
| 590 | int32_t framesLeft = numFrames; |
| 591 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 592 | // 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] | 593 | while (framesLeft > 0) { |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 594 | // 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] | 595 | int64_t wakeTimeNanos = 0; |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 596 | aaudio_result_t framesProcessed = processDataNow(audioData, framesLeft, |
| 597 | currentTimeNanos, &wakeTimeNanos); |
| 598 | if (framesProcessed < 0) { |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 599 | result = framesProcessed; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 600 | break; |
| 601 | } |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 602 | framesLeft -= (int32_t) framesProcessed; |
| 603 | audioData += framesProcessed * getBytesPerFrame(); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 604 | |
| 605 | // Should we block? |
| 606 | if (timeoutNanoseconds == 0) { |
| 607 | break; // don't block |
| 608 | } else if (framesLeft > 0) { |
Phil Burk | fd34a93 | 2017-07-19 07:03:52 -0700 | [diff] [blame] | 609 | if (!mAudioEndpoint.isFreeRunning()) { |
| 610 | // If there is software on the other end of the FIFO then it may get delayed. |
| 611 | // So wake up just a little after we expect it to be ready. |
| 612 | wakeTimeNanos += mWakeupDelayNanos; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 613 | } |
Phil Burk | fd34a93 | 2017-07-19 07:03:52 -0700 | [diff] [blame] | 614 | |
Phil Burk | 2bc7c18 | 2017-08-28 11:45:01 -0700 | [diff] [blame] | 615 | currentTimeNanos = AudioClock::getNanoseconds(); |
| 616 | int64_t earliestWakeTime = currentTimeNanos + mMinimumSleepNanos; |
| 617 | // Guarantee a minimum sleep time. |
| 618 | if (wakeTimeNanos < earliestWakeTime) { |
| 619 | wakeTimeNanos = earliestWakeTime; |
| 620 | } |
| 621 | |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 622 | if (wakeTimeNanos > deadlineNanos) { |
| 623 | // If we time out, just return the framesWritten so far. |
Phil Burk | cf5f6d2 | 2017-05-26 12:35:07 -0700 | [diff] [blame] | 624 | // TODO remove after we fix the deadline bug |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 625 | ALOGW("processData(): entered at %lld nanos, currently %lld", |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 626 | (long long) entryTimeNanos, (long long) currentTimeNanos); |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 627 | ALOGW("processData(): TIMEOUT after %lld nanos", |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 628 | (long long) timeoutNanoseconds); |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 629 | ALOGW("processData(): wakeTime = %lld, deadline = %lld nanos", |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 630 | (long long) wakeTimeNanos, (long long) deadlineNanos); |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 631 | ALOGW("processData(): past deadline by %d micros", |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 632 | (int)((wakeTimeNanos - deadlineNanos) / AAUDIO_NANOS_PER_MICROSECOND)); |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 633 | mClockModel.dump(); |
| 634 | mAudioEndpoint.dump(); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 635 | break; |
| 636 | } |
| 637 | |
Phil Burk | fd34a93 | 2017-07-19 07:03:52 -0700 | [diff] [blame] | 638 | if (ATRACE_ENABLED()) { |
| 639 | int32_t fullFrames = mAudioEndpoint.getFullFramesAvailable(); |
| 640 | ATRACE_INT(fifoName, fullFrames); |
| 641 | int64_t sleepForNanos = wakeTimeNanos - currentTimeNanos; |
| 642 | ATRACE_INT("aaSlpNs", (int32_t)sleepForNanos); |
| 643 | } |
| 644 | |
| 645 | AudioClock::sleepUntilNanoTime(wakeTimeNanos); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 646 | currentTimeNanos = AudioClock::getNanoseconds(); |
| 647 | } |
| 648 | } |
| 649 | |
Phil Burk | fd34a93 | 2017-07-19 07:03:52 -0700 | [diff] [blame] | 650 | if (ATRACE_ENABLED()) { |
| 651 | int32_t fullFrames = mAudioEndpoint.getFullFramesAvailable(); |
| 652 | ATRACE_INT(fifoName, fullFrames); |
| 653 | } |
| 654 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 655 | // return error or framesProcessed |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 656 | (void) loopCount; |
Phil Burk | 4485d41 | 2017-05-09 15:55:02 -0700 | [diff] [blame] | 657 | ATRACE_END(); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 658 | return (result < 0) ? result : numFrames - framesLeft; |
| 659 | } |
| 660 | |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 661 | void AudioStreamInternal::processTimestamp(uint64_t position, int64_t time) { |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 662 | mClockModel.processTimestamp(position, time); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 663 | } |
| 664 | |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 665 | aaudio_result_t AudioStreamInternal::setBufferSize(int32_t requestedFrames) { |
Phil Burk | 6479d50 | 2017-11-20 09:32:52 -0800 | [diff] [blame] | 666 | int32_t adjustedFrames = requestedFrames; |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 667 | int32_t actualFrames = 0; |
Phil Burk | 6479d50 | 2017-11-20 09:32:52 -0800 | [diff] [blame] | 668 | int32_t maximumSize = getBufferCapacity(); |
| 669 | |
| 670 | // Clip to minimum size so that rounding up will work better. |
| 671 | if (adjustedFrames < 1) { |
| 672 | adjustedFrames = 1; |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 673 | } |
| 674 | |
Phil Burk | 6479d50 | 2017-11-20 09:32:52 -0800 | [diff] [blame] | 675 | if (adjustedFrames > maximumSize) { |
| 676 | // Clip to maximum size. |
| 677 | adjustedFrames = maximumSize; |
| 678 | } else { |
| 679 | // Round to the next highest burst size. |
| 680 | int32_t numBursts = (adjustedFrames + mFramesPerBurst - 1) / mFramesPerBurst; |
| 681 | adjustedFrames = numBursts * mFramesPerBurst; |
| 682 | // Rounding may have gone above maximum. |
| 683 | if (adjustedFrames > maximumSize) { |
| 684 | adjustedFrames = maximumSize; |
| 685 | } |
| 686 | } |
| 687 | |
| 688 | aaudio_result_t result = mAudioEndpoint.setBufferSizeInFrames(adjustedFrames, &actualFrames); |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 689 | ALOGD("setBufferSize() req = %d => %d", requestedFrames, actualFrames); |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 690 | if (result < 0) { |
| 691 | return result; |
| 692 | } else { |
| 693 | return (aaudio_result_t) actualFrames; |
| 694 | } |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 695 | } |
| 696 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 697 | int32_t AudioStreamInternal::getBufferSize() const { |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 698 | return mAudioEndpoint.getBufferSizeInFrames(); |
| 699 | } |
| 700 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 701 | int32_t AudioStreamInternal::getBufferCapacity() const { |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 702 | return mAudioEndpoint.getBufferCapacityInFrames(); |
| 703 | } |
| 704 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 705 | int32_t AudioStreamInternal::getFramesPerBurst() const { |
Phil Burk | 6479d50 | 2017-11-20 09:32:52 -0800 | [diff] [blame] | 706 | return mFramesPerBurst; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 707 | } |
| 708 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 709 | aaudio_result_t AudioStreamInternal::joinThread(void** returnArg) { |
| 710 | return AudioStream::joinThread(returnArg, calculateReasonableTimeout(getFramesPerBurst())); |
Phil Burk | 4c5129b | 2017-04-28 15:17:32 -0700 | [diff] [blame] | 711 | } |