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 | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 58 | #define LOG_TIMESTAMPS 0 |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 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) |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 69 | , mAtomicTimestamp() |
Phil Burk | fd34a93 | 2017-07-19 07:03:52 -0700 | [diff] [blame] | 70 | , mWakeupDelayNanos(AAudioProperty_getWakeupDelayMicros() * AAUDIO_NANOS_PER_MICROSECOND) |
| 71 | , mMinimumSleepNanos(AAudioProperty_getMinimumSleepMicros() * AAUDIO_NANOS_PER_MICROSECOND) |
| 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 | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 253 | /* |
| 254 | * It normally takes about 20-30 msec to start a stream on the server. |
| 255 | * But the first time can take as much as 200-300 msec. The HW |
| 256 | * starts right away so by the time the client gets a chance to write into |
| 257 | * the buffer, it is already in a deep underflow state. That can cause the |
| 258 | * XRunCount to be non-zero, which could lead an app to tune its latency higher. |
| 259 | * To avoid this problem, we set a request for the processing code to start the |
| 260 | * client stream at the same position as the server stream. |
| 261 | * The processing code will then save the current offset |
| 262 | * between client and server and apply that to any position given to the app. |
| 263 | */ |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 264 | aaudio_result_t AudioStreamInternal::requestStart() |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 265 | { |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 266 | int64_t startTime; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 267 | if (mServiceStreamHandle == AAUDIO_HANDLE_INVALID) { |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 268 | ALOGE("requestStart() mServiceStreamHandle invalid"); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 269 | return AAUDIO_ERROR_INVALID_STATE; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 270 | } |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 271 | if (isActive()) { |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 272 | ALOGE("requestStart() already active"); |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 273 | return AAUDIO_ERROR_INVALID_STATE; |
| 274 | } |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 275 | |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 276 | aaudio_stream_state_t originalState = getState(); |
| 277 | if (originalState == AAUDIO_STREAM_STATE_DISCONNECTED) { |
| 278 | ALOGE("requestStart() but DISCONNECTED"); |
| 279 | return AAUDIO_ERROR_DISCONNECTED; |
| 280 | } |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 281 | setState(AAUDIO_STREAM_STATE_STARTING); |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 282 | |
| 283 | // Clear any stale timestamps from the previous run. |
| 284 | drainTimestampsFromService(); |
| 285 | |
| 286 | status_t status = startWithStatus(); // Call PlayerBase, which will start the device stream. |
| 287 | aaudio_result_t result = AAudioConvert_androidToAAudioResult(status); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 288 | |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 289 | startTime = AudioClock::getNanoseconds(); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 290 | mClockModel.start(startTime); |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 291 | 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] | 292 | |
| 293 | if (result == AAUDIO_OK && getDataCallbackProc() != nullptr) { |
| 294 | // Launch the callback loop thread. |
| 295 | int64_t periodNanos = mCallbackFrames |
| 296 | * AAUDIO_NANOS_PER_SECOND |
| 297 | / getSampleRate(); |
| 298 | mCallbackEnabled.store(true); |
| 299 | result = createThread(periodNanos, aaudio_callback_thread_proc, this); |
| 300 | } |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 301 | if (result != AAUDIO_OK) { |
| 302 | setState(originalState); |
| 303 | } |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 304 | return result; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 305 | } |
| 306 | |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 307 | int64_t AudioStreamInternal::calculateReasonableTimeout(int32_t framesPerOperation) { |
| 308 | |
| 309 | // 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] | 310 | int64_t timeoutNanoseconds = (MIN_TIMEOUT_OPERATIONS |
| 311 | * framesPerOperation |
| 312 | * AAUDIO_NANOS_PER_SECOND) |
| 313 | / getSampleRate(); |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 314 | if (timeoutNanoseconds < MIN_TIMEOUT_NANOS) { // arbitrary number of seconds |
| 315 | timeoutNanoseconds = MIN_TIMEOUT_NANOS; |
| 316 | } |
| 317 | return timeoutNanoseconds; |
| 318 | } |
| 319 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 320 | int64_t AudioStreamInternal::calculateReasonableTimeout() { |
| 321 | return calculateReasonableTimeout(getFramesPerBurst()); |
| 322 | } |
| 323 | |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 324 | aaudio_result_t AudioStreamInternal::stopCallback() |
| 325 | { |
| 326 | if (isDataCallbackActive()) { |
| 327 | mCallbackEnabled.store(false); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 328 | return joinThread(NULL); |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 329 | } else { |
| 330 | return AAUDIO_OK; |
| 331 | } |
| 332 | } |
| 333 | |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 334 | aaudio_result_t AudioStreamInternal::requestStopInternal() |
| 335 | { |
| 336 | if (mServiceStreamHandle == AAUDIO_HANDLE_INVALID) { |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 337 | ALOGE("requestStopInternal() mServiceStreamHandle invalid = 0x%08X", |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 338 | mServiceStreamHandle); |
| 339 | return AAUDIO_ERROR_INVALID_STATE; |
| 340 | } |
| 341 | |
| 342 | mClockModel.stop(AudioClock::getNanoseconds()); |
| 343 | setState(AAUDIO_STREAM_STATE_STOPPING); |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 344 | mAtomicTimestamp.clear(); |
Eric Laurent | a2f296e | 2017-06-21 18:51:47 -0700 | [diff] [blame] | 345 | return AAudioConvert_androidToAAudioResult(stopWithStatus()); |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 346 | } |
| 347 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 348 | aaudio_result_t AudioStreamInternal::requestStop() |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 349 | { |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 350 | aaudio_result_t result = stopCallback(); |
| 351 | if (result != AAUDIO_OK) { |
| 352 | return result; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 353 | } |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 354 | result = requestStopInternal(); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 355 | return result; |
| 356 | } |
| 357 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 358 | aaudio_result_t AudioStreamInternal::registerThread() { |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 359 | if (mServiceStreamHandle == AAUDIO_HANDLE_INVALID) { |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 360 | ALOGE("registerThread() mServiceStreamHandle invalid"); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 361 | return AAUDIO_ERROR_INVALID_STATE; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 362 | } |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 363 | return mServiceInterface.registerAudioThread(mServiceStreamHandle, |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 364 | gettid(), |
| 365 | getPeriodNanoseconds()); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 366 | } |
| 367 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 368 | aaudio_result_t AudioStreamInternal::unregisterThread() { |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 369 | if (mServiceStreamHandle == AAUDIO_HANDLE_INVALID) { |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 370 | ALOGE("unregisterThread() mServiceStreamHandle invalid"); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 371 | return AAUDIO_ERROR_INVALID_STATE; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 372 | } |
Phil Burk | 2ac035f | 2017-06-23 14:51:14 -0700 | [diff] [blame] | 373 | return mServiceInterface.unregisterAudioThread(mServiceStreamHandle, gettid()); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 374 | } |
| 375 | |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 376 | aaudio_result_t AudioStreamInternal::startClient(const android::AudioClient& client, |
| 377 | audio_port_handle_t *clientHandle) { |
| 378 | if (mServiceStreamHandle == AAUDIO_HANDLE_INVALID) { |
| 379 | return AAUDIO_ERROR_INVALID_STATE; |
| 380 | } |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 381 | |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 382 | return mServiceInterface.startClient(mServiceStreamHandle, client, clientHandle); |
| 383 | } |
| 384 | |
| 385 | aaudio_result_t AudioStreamInternal::stopClient(audio_port_handle_t clientHandle) { |
| 386 | if (mServiceStreamHandle == AAUDIO_HANDLE_INVALID) { |
| 387 | return AAUDIO_ERROR_INVALID_STATE; |
| 388 | } |
| 389 | return mServiceInterface.stopClient(mServiceStreamHandle, clientHandle); |
| 390 | } |
| 391 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 392 | aaudio_result_t AudioStreamInternal::getTimestamp(clockid_t clockId, |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 393 | int64_t *framePosition, |
| 394 | int64_t *timeNanoseconds) { |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 395 | // Generated in server and passed to client. Return latest. |
| 396 | if (mAtomicTimestamp.isValid()) { |
| 397 | Timestamp timestamp = mAtomicTimestamp.read(); |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 398 | int64_t position = timestamp.getPosition() + mFramesOffsetFromService; |
| 399 | if (position >= 0) { |
| 400 | *framePosition = position; |
| 401 | *timeNanoseconds = timestamp.getNanoseconds(); |
| 402 | return AAUDIO_OK; |
| 403 | } |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 404 | } |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 405 | return AAUDIO_ERROR_UNAVAILABLE; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 406 | } |
| 407 | |
Phil Burk | 0befec6 | 2017-07-28 15:12:13 -0700 | [diff] [blame] | 408 | aaudio_result_t AudioStreamInternal::updateStateMachine() { |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 409 | if (isDataCallbackActive()) { |
| 410 | return AAUDIO_OK; // state is getting updated by the callback thread read/write call |
| 411 | } |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 412 | return processCommands(); |
| 413 | } |
| 414 | |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 415 | void AudioStreamInternal::logTimestamp(AAudioServiceMessage &command) { |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 416 | static int64_t oldPosition = 0; |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 417 | static int64_t oldTime = 0; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 418 | int64_t framePosition = command.timestamp.position; |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 419 | int64_t nanoTime = command.timestamp.timestamp; |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 420 | ALOGD("logTimestamp: timestamp says framePosition = %8lld at nanoTime %lld", |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 421 | (long long) framePosition, |
| 422 | (long long) nanoTime); |
| 423 | int64_t nanosDelta = nanoTime - oldTime; |
| 424 | if (nanosDelta > 0 && oldTime > 0) { |
| 425 | int64_t framesDelta = framePosition - oldPosition; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 426 | int64_t rate = (framesDelta * AAUDIO_NANOS_PER_SECOND) / nanosDelta; |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 427 | ALOGD("logTimestamp: framesDelta = %8lld, nanosDelta = %8lld, rate = %lld", |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 428 | (long long) framesDelta, (long long) nanosDelta, (long long) rate); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 429 | } |
| 430 | oldPosition = framePosition; |
| 431 | oldTime = nanoTime; |
| 432 | } |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 433 | |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 434 | aaudio_result_t AudioStreamInternal::onTimestampService(AAudioServiceMessage *message) { |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 435 | #if LOG_TIMESTAMPS |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 436 | logTimestamp(*message); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 437 | #endif |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 438 | processTimestamp(message->timestamp.position, message->timestamp.timestamp); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 439 | return AAUDIO_OK; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 440 | } |
| 441 | |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 442 | aaudio_result_t AudioStreamInternal::onTimestampHardware(AAudioServiceMessage *message) { |
| 443 | Timestamp timestamp(message->timestamp.position, message->timestamp.timestamp); |
| 444 | mAtomicTimestamp.write(timestamp); |
| 445 | return AAUDIO_OK; |
| 446 | } |
| 447 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 448 | aaudio_result_t AudioStreamInternal::onEventFromServer(AAudioServiceMessage *message) { |
| 449 | aaudio_result_t result = AAUDIO_OK; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 450 | switch (message->event.event) { |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 451 | case AAUDIO_SERVICE_EVENT_STARTED: |
Phil Burk | a5222e2 | 2017-07-28 13:31:14 -0700 | [diff] [blame] | 452 | ALOGD("AudioStreamInternal::onEventFromServer() got AAUDIO_SERVICE_EVENT_STARTED"); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 453 | if (getState() == AAUDIO_STREAM_STATE_STARTING) { |
| 454 | setState(AAUDIO_STREAM_STATE_STARTED); |
| 455 | } |
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_PAUSED: |
Phil Burk | a5222e2 | 2017-07-28 13:31:14 -0700 | [diff] [blame] | 458 | ALOGD("AudioStreamInternal::onEventFromServer() got AAUDIO_SERVICE_EVENT_PAUSED"); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 459 | if (getState() == AAUDIO_STREAM_STATE_PAUSING) { |
| 460 | setState(AAUDIO_STREAM_STATE_PAUSED); |
| 461 | } |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 462 | break; |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 463 | case AAUDIO_SERVICE_EVENT_STOPPED: |
Phil Burk | a5222e2 | 2017-07-28 13:31:14 -0700 | [diff] [blame] | 464 | ALOGD("AudioStreamInternal::onEventFromServer() got AAUDIO_SERVICE_EVENT_STOPPED"); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 465 | if (getState() == AAUDIO_STREAM_STATE_STOPPING) { |
| 466 | setState(AAUDIO_STREAM_STATE_STOPPED); |
| 467 | } |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 468 | break; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 469 | case AAUDIO_SERVICE_EVENT_FLUSHED: |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 470 | ALOGD("AudioStreamInternal::onEventFromServer() got AAUDIO_SERVICE_EVENT_FLUSHED"); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 471 | if (getState() == AAUDIO_STREAM_STATE_FLUSHING) { |
| 472 | setState(AAUDIO_STREAM_STATE_FLUSHED); |
| 473 | onFlushFromServer(); |
| 474 | } |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 475 | break; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 476 | case AAUDIO_SERVICE_EVENT_CLOSED: |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 477 | ALOGD("AudioStreamInternal::onEventFromServer() got AAUDIO_SERVICE_EVENT_CLOSED"); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 478 | setState(AAUDIO_STREAM_STATE_CLOSED); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 479 | break; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 480 | case AAUDIO_SERVICE_EVENT_DISCONNECTED: |
Phil Burk | ea04d97 | 2017-08-07 12:30:44 -0700 | [diff] [blame] | 481 | // Prevent hardware from looping on old data and making buzzing sounds. |
| 482 | if (getDirection() == AAUDIO_DIRECTION_OUTPUT) { |
| 483 | mAudioEndpoint.eraseDataMemory(); |
| 484 | } |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 485 | result = AAUDIO_ERROR_DISCONNECTED; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 486 | setState(AAUDIO_STREAM_STATE_DISCONNECTED); |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 487 | ALOGW("WARNING - AudioStreamInternal::onEventFromServer()" |
Phil Burk | ea04d97 | 2017-08-07 12:30:44 -0700 | [diff] [blame] | 488 | " AAUDIO_SERVICE_EVENT_DISCONNECTED - FIFO cleared"); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 489 | break; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 490 | case AAUDIO_SERVICE_EVENT_VOLUME: |
Eric Laurent | a2f296e | 2017-06-21 18:51:47 -0700 | [diff] [blame] | 491 | mStreamVolume = (float)message->event.dataDouble; |
| 492 | doSetVolume(); |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 493 | ALOGD("AudioStreamInternal::onEventFromServer() AAUDIO_SERVICE_EVENT_VOLUME %lf", |
Phil Burk | e572f46 | 2017-04-20 13:03:19 -0700 | [diff] [blame] | 494 | message->event.dataDouble); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 495 | break; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 496 | default: |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 497 | ALOGW("WARNING - AudioStreamInternal::onEventFromServer() Unrecognized event = %d", |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 498 | (int) message->event.event); |
| 499 | break; |
| 500 | } |
| 501 | return result; |
| 502 | } |
| 503 | |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 504 | aaudio_result_t AudioStreamInternal::drainTimestampsFromService() { |
| 505 | aaudio_result_t result = AAUDIO_OK; |
| 506 | |
| 507 | while (result == AAUDIO_OK) { |
| 508 | AAudioServiceMessage message; |
| 509 | if (mAudioEndpoint.readUpCommand(&message) != 1) { |
| 510 | break; // no command this time, no problem |
| 511 | } |
| 512 | switch (message.what) { |
| 513 | // ignore most messages |
| 514 | case AAudioServiceMessage::code::TIMESTAMP_SERVICE: |
| 515 | case AAudioServiceMessage::code::TIMESTAMP_HARDWARE: |
| 516 | break; |
| 517 | |
| 518 | case AAudioServiceMessage::code::EVENT: |
| 519 | result = onEventFromServer(&message); |
| 520 | break; |
| 521 | |
| 522 | default: |
| 523 | ALOGE("WARNING - drainTimestampsFromService() Unrecognized what = %d", |
| 524 | (int) message.what); |
| 525 | result = AAUDIO_ERROR_INTERNAL; |
| 526 | break; |
| 527 | } |
| 528 | } |
| 529 | return result; |
| 530 | } |
| 531 | |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 532 | // Process all the commands coming from the server. |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 533 | aaudio_result_t AudioStreamInternal::processCommands() { |
| 534 | aaudio_result_t result = AAUDIO_OK; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 535 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 536 | while (result == AAUDIO_OK) { |
Phil Burk | cf5f6d2 | 2017-05-26 12:35:07 -0700 | [diff] [blame] | 537 | //ALOGD("AudioStreamInternal::processCommands() - looping, %d", result); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 538 | AAudioServiceMessage message; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 539 | if (mAudioEndpoint.readUpCommand(&message) != 1) { |
| 540 | break; // no command this time, no problem |
| 541 | } |
| 542 | switch (message.what) { |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 543 | case AAudioServiceMessage::code::TIMESTAMP_SERVICE: |
| 544 | result = onTimestampService(&message); |
| 545 | break; |
| 546 | |
| 547 | case AAudioServiceMessage::code::TIMESTAMP_HARDWARE: |
| 548 | result = onTimestampHardware(&message); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 549 | break; |
| 550 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 551 | case AAudioServiceMessage::code::EVENT: |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 552 | result = onEventFromServer(&message); |
| 553 | break; |
| 554 | |
| 555 | default: |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 556 | ALOGE("WARNING - processCommands() Unrecognized what = %d", |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 557 | (int) message.what); |
Phil Burk | 17fff38 | 2017-05-16 14:06:45 -0700 | [diff] [blame] | 558 | result = AAUDIO_ERROR_INTERNAL; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 559 | break; |
| 560 | } |
| 561 | } |
| 562 | return result; |
| 563 | } |
| 564 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 565 | // Read or write the data, block if needed and timeoutMillis > 0 |
| 566 | aaudio_result_t AudioStreamInternal::processData(void *buffer, int32_t numFrames, |
| 567 | int64_t timeoutNanoseconds) |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 568 | { |
Phil Burk | fd34a93 | 2017-07-19 07:03:52 -0700 | [diff] [blame] | 569 | const char * traceName = "aaProc"; |
| 570 | const char * fifoName = "aaRdy"; |
Phil Burk | 4485d41 | 2017-05-09 15:55:02 -0700 | [diff] [blame] | 571 | ATRACE_BEGIN(traceName); |
Phil Burk | 4485d41 | 2017-05-09 15:55:02 -0700 | [diff] [blame] | 572 | if (ATRACE_ENABLED()) { |
Phil Burk | fd34a93 | 2017-07-19 07:03:52 -0700 | [diff] [blame] | 573 | int32_t fullFrames = mAudioEndpoint.getFullFramesAvailable(); |
| 574 | ATRACE_INT(fifoName, fullFrames); |
Phil Burk | 4485d41 | 2017-05-09 15:55:02 -0700 | [diff] [blame] | 575 | } |
| 576 | |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 577 | aaudio_result_t result = AAUDIO_OK; |
| 578 | int32_t loopCount = 0; |
| 579 | uint8_t* audioData = (uint8_t*)buffer; |
| 580 | int64_t currentTimeNanos = AudioClock::getNanoseconds(); |
| 581 | const int64_t entryTimeNanos = currentTimeNanos; |
| 582 | const int64_t deadlineNanos = currentTimeNanos + timeoutNanoseconds; |
| 583 | int32_t framesLeft = numFrames; |
| 584 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 585 | // 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] | 586 | while (framesLeft > 0) { |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 587 | // 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] | 588 | int64_t wakeTimeNanos = 0; |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 589 | aaudio_result_t framesProcessed = processDataNow(audioData, framesLeft, |
| 590 | currentTimeNanos, &wakeTimeNanos); |
| 591 | if (framesProcessed < 0) { |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 592 | result = framesProcessed; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 593 | break; |
| 594 | } |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 595 | framesLeft -= (int32_t) framesProcessed; |
| 596 | audioData += framesProcessed * getBytesPerFrame(); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 597 | |
| 598 | // Should we block? |
| 599 | if (timeoutNanoseconds == 0) { |
| 600 | break; // don't block |
| 601 | } else if (framesLeft > 0) { |
Phil Burk | fd34a93 | 2017-07-19 07:03:52 -0700 | [diff] [blame] | 602 | if (!mAudioEndpoint.isFreeRunning()) { |
| 603 | // If there is software on the other end of the FIFO then it may get delayed. |
| 604 | // So wake up just a little after we expect it to be ready. |
| 605 | wakeTimeNanos += mWakeupDelayNanos; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 606 | } |
Phil Burk | fd34a93 | 2017-07-19 07:03:52 -0700 | [diff] [blame] | 607 | |
Phil Burk | 2bc7c18 | 2017-08-28 11:45:01 -0700 | [diff] [blame] | 608 | currentTimeNanos = AudioClock::getNanoseconds(); |
| 609 | int64_t earliestWakeTime = currentTimeNanos + mMinimumSleepNanos; |
| 610 | // Guarantee a minimum sleep time. |
| 611 | if (wakeTimeNanos < earliestWakeTime) { |
| 612 | wakeTimeNanos = earliestWakeTime; |
| 613 | } |
| 614 | |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 615 | if (wakeTimeNanos > deadlineNanos) { |
| 616 | // If we time out, just return the framesWritten so far. |
Phil Burk | cf5f6d2 | 2017-05-26 12:35:07 -0700 | [diff] [blame] | 617 | // TODO remove after we fix the deadline bug |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 618 | ALOGW("AudioStreamInternal::processData(): entered at %lld nanos, currently %lld", |
| 619 | (long long) entryTimeNanos, (long long) currentTimeNanos); |
Phil Burk | 2bc7c18 | 2017-08-28 11:45:01 -0700 | [diff] [blame] | 620 | ALOGW("AudioStreamInternal::processData(): TIMEOUT after %lld nanos", |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 621 | (long long) timeoutNanoseconds); |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 622 | ALOGW("AudioStreamInternal::processData(): wakeTime = %lld, deadline = %lld nanos", |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 623 | (long long) wakeTimeNanos, (long long) deadlineNanos); |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 624 | ALOGW("AudioStreamInternal::processData(): past deadline by %d micros", |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 625 | (int)((wakeTimeNanos - deadlineNanos) / AAUDIO_NANOS_PER_MICROSECOND)); |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 626 | mClockModel.dump(); |
| 627 | mAudioEndpoint.dump(); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 628 | break; |
| 629 | } |
| 630 | |
Phil Burk | fd34a93 | 2017-07-19 07:03:52 -0700 | [diff] [blame] | 631 | if (ATRACE_ENABLED()) { |
| 632 | int32_t fullFrames = mAudioEndpoint.getFullFramesAvailable(); |
| 633 | ATRACE_INT(fifoName, fullFrames); |
| 634 | int64_t sleepForNanos = wakeTimeNanos - currentTimeNanos; |
| 635 | ATRACE_INT("aaSlpNs", (int32_t)sleepForNanos); |
| 636 | } |
| 637 | |
| 638 | AudioClock::sleepUntilNanoTime(wakeTimeNanos); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 639 | currentTimeNanos = AudioClock::getNanoseconds(); |
| 640 | } |
| 641 | } |
| 642 | |
Phil Burk | fd34a93 | 2017-07-19 07:03:52 -0700 | [diff] [blame] | 643 | if (ATRACE_ENABLED()) { |
| 644 | int32_t fullFrames = mAudioEndpoint.getFullFramesAvailable(); |
| 645 | ATRACE_INT(fifoName, fullFrames); |
| 646 | } |
| 647 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 648 | // return error or framesProcessed |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 649 | (void) loopCount; |
Phil Burk | 4485d41 | 2017-05-09 15:55:02 -0700 | [diff] [blame] | 650 | ATRACE_END(); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 651 | return (result < 0) ? result : numFrames - framesLeft; |
| 652 | } |
| 653 | |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 654 | void AudioStreamInternal::processTimestamp(uint64_t position, int64_t time) { |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 655 | mClockModel.processTimestamp(position, time); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 656 | } |
| 657 | |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 658 | aaudio_result_t AudioStreamInternal::setBufferSize(int32_t requestedFrames) { |
| 659 | int32_t actualFrames = 0; |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 660 | // Round to the next highest burst size. |
| 661 | if (getFramesPerBurst() > 0) { |
| 662 | int32_t numBursts = (requestedFrames + getFramesPerBurst() - 1) / getFramesPerBurst(); |
| 663 | requestedFrames = numBursts * getFramesPerBurst(); |
| 664 | } |
| 665 | |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 666 | aaudio_result_t result = mAudioEndpoint.setBufferSizeInFrames(requestedFrames, &actualFrames); |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 667 | ALOGD("setBufferSize() req = %d => %d", requestedFrames, actualFrames); |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 668 | if (result < 0) { |
| 669 | return result; |
| 670 | } else { |
| 671 | return (aaudio_result_t) actualFrames; |
| 672 | } |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 673 | } |
| 674 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 675 | int32_t AudioStreamInternal::getBufferSize() const { |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 676 | return mAudioEndpoint.getBufferSizeInFrames(); |
| 677 | } |
| 678 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 679 | int32_t AudioStreamInternal::getBufferCapacity() const { |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 680 | return mAudioEndpoint.getBufferCapacityInFrames(); |
| 681 | } |
| 682 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 683 | int32_t AudioStreamInternal::getFramesPerBurst() const { |
| 684 | return mEndpointDescriptor.dataQueueDescriptor.framesPerBurst; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 685 | } |
| 686 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 687 | aaudio_result_t AudioStreamInternal::joinThread(void** returnArg) { |
| 688 | return AudioStream::joinThread(returnArg, calculateReasonableTimeout(getFramesPerBurst())); |
Phil Burk | 4c5129b | 2017-04-28 15:17:32 -0700 | [diff] [blame] | 689 | } |
Eric Laurent | a2f296e | 2017-06-21 18:51:47 -0700 | [diff] [blame] | 690 | |
| 691 | void AudioStreamInternal::doSetVolume() { |
| 692 | // No pan and only left volume is taken into account from IPLayer interface |
| 693 | mVolumeRamp.setTarget(mStreamVolume * mVolumeMultiplierL /* * mPanMultiplierL */); |
| 694 | } |
| 695 | |
| 696 | |
| 697 | //------------------------------------------------------------------------------ |
| 698 | // Implementation of PlayerBase |
| 699 | status_t AudioStreamInternal::playerStart() { |
| 700 | return AAudioConvert_aaudioToAndroidStatus(mServiceInterface.startStream(mServiceStreamHandle)); |
| 701 | } |
| 702 | |
| 703 | status_t AudioStreamInternal::playerPause() { |
| 704 | return AAudioConvert_aaudioToAndroidStatus(mServiceInterface.pauseStream(mServiceStreamHandle)); |
| 705 | } |
| 706 | |
| 707 | status_t AudioStreamInternal::playerStop() { |
| 708 | return AAudioConvert_aaudioToAndroidStatus(mServiceInterface.stopStream(mServiceStreamHandle)); |
| 709 | } |
| 710 | |
| 711 | status_t AudioStreamInternal::playerSetVolume() { |
| 712 | doSetVolume(); |
| 713 | return NO_ERROR; |
| 714 | } |
| 715 | |
| 716 | void AudioStreamInternal::destroy() { |
| 717 | baseDestroy(); |
| 718 | } |