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