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