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