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 | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 17 | #define LOG_TAG "AAudio" |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 18 | //#define LOG_NDEBUG 0 |
| 19 | #include <utils/Log.h> |
| 20 | |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame^] | 21 | #include <stdint.h> |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 22 | #include <assert.h> |
| 23 | |
| 24 | #include <binder/IServiceManager.h> |
| 25 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 26 | #include <aaudio/AAudio.h> |
Phil Burk | c8f372c | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 27 | #include <utils/String16.h> |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 28 | |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame^] | 29 | #include "AudioClock.h" |
| 30 | #include "AudioEndpointParcelable.h" |
| 31 | #include "binding/AAudioStreamRequest.h" |
| 32 | #include "binding/AAudioStreamConfiguration.h" |
| 33 | #include "binding/IAAudioService.h" |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 34 | #include "binding/AAudioServiceMessage.h" |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame^] | 35 | #include "fifo/FifoBuffer.h" |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 36 | |
Phil Burk | 3df348f | 2017-02-08 11:41:55 -0800 | [diff] [blame] | 37 | #include "core/AudioStreamBuilder.h" |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame^] | 38 | #include "AudioStreamInternal.h" |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 39 | |
| 40 | #define LOG_TIMESTAMPS 0 |
| 41 | |
| 42 | using android::String16; |
| 43 | using android::IServiceManager; |
| 44 | using android::defaultServiceManager; |
| 45 | using android::interface_cast; |
Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 46 | using android::Mutex; |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame^] | 47 | using android::WrappingBuffer; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 48 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 49 | using namespace aaudio; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 50 | |
Phil Burk | c8f372c | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 51 | #define MIN_TIMEOUT_NANOS (1000 * AAUDIO_NANOS_PER_MILLISECOND) |
| 52 | |
| 53 | // Wait at least this many times longer than the operation should take. |
| 54 | #define MIN_TIMEOUT_OPERATIONS 4 |
| 55 | |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame^] | 56 | #define ALOG_CONDITION (mInService == false) |
Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 57 | |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame^] | 58 | AudioStreamInternal::AudioStreamInternal(AAudioServiceInterface &serviceInterface, bool inService) |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 59 | : AudioStream() |
| 60 | , mClockModel() |
| 61 | , mAudioEndpoint() |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 62 | , mServiceStreamHandle(AAUDIO_HANDLE_INVALID) |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 63 | , mFramesPerBurst(16) |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame^] | 64 | , mServiceInterface(serviceInterface) |
| 65 | , mInService(inService) |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 66 | { |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | AudioStreamInternal::~AudioStreamInternal() { |
| 70 | } |
| 71 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 72 | aaudio_result_t AudioStreamInternal::open(const AudioStreamBuilder &builder) { |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 73 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 74 | aaudio_result_t result = AAUDIO_OK; |
| 75 | AAudioStreamRequest request; |
| 76 | AAudioStreamConfiguration configuration; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 77 | |
| 78 | result = AudioStream::open(builder); |
| 79 | if (result < 0) { |
| 80 | return result; |
| 81 | } |
| 82 | |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame^] | 83 | // We have to do volume scaling. So we prefer FLOAT format. |
| 84 | if (getFormat() == AAUDIO_UNSPECIFIED) { |
| 85 | setFormat(AAUDIO_FORMAT_PCM_FLOAT); |
| 86 | } |
| 87 | |
Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 88 | // Build the request to send to the server. |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 89 | request.setUserId(getuid()); |
| 90 | request.setProcessId(getpid()); |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame^] | 91 | request.setDirection(getDirection()); |
| 92 | |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 93 | request.getConfiguration().setDeviceId(getDeviceId()); |
| 94 | request.getConfiguration().setSampleRate(getSampleRate()); |
| 95 | request.getConfiguration().setSamplesPerFrame(getSamplesPerFrame()); |
| 96 | request.getConfiguration().setAudioFormat(getFormat()); |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame^] | 97 | aaudio_sharing_mode_t sharingMode = getSharingMode(); |
| 98 | ALOGE("AudioStreamInternal.open(): sharingMode %d", sharingMode); |
| 99 | request.getConfiguration().setSharingMode(sharingMode); |
Phil Burk | 3df348f | 2017-02-08 11:41:55 -0800 | [diff] [blame] | 100 | request.getConfiguration().setBufferCapacity(builder.getBufferCapacity()); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 101 | |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame^] | 102 | mServiceStreamHandle = mServiceInterface.openStream(request, configuration); |
| 103 | ALOGD_IF(ALOG_CONDITION, "AudioStreamInternal.open(): openStream returned mServiceStreamHandle = 0x%08X", |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 104 | (unsigned int)mServiceStreamHandle); |
| 105 | if (mServiceStreamHandle < 0) { |
| 106 | result = mServiceStreamHandle; |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame^] | 107 | ALOGE("AudioStreamInternal.open(): openStream() returned %d", result); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 108 | } else { |
| 109 | result = configuration.validate(); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 110 | if (result != AAUDIO_OK) { |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 111 | close(); |
| 112 | return result; |
| 113 | } |
| 114 | // Save results of the open. |
| 115 | setSampleRate(configuration.getSampleRate()); |
| 116 | setSamplesPerFrame(configuration.getSamplesPerFrame()); |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame^] | 117 | setDeviceId(configuration.getDeviceId()); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 118 | |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame^] | 119 | // Save device format so we can do format conversion and volume scaling together. |
| 120 | mDeviceFormat = configuration.getAudioFormat(); |
| 121 | |
| 122 | result = mServiceInterface.getStreamDescription(mServiceStreamHandle, mEndPointParcelable); |
| 123 | ALOGD_IF(ALOG_CONDITION, "AudioStreamInternal.open(): getStreamDescriptor(0x%08X) returns %d", |
| 124 | mServiceStreamHandle, result); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 125 | if (result != AAUDIO_OK) { |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 126 | ALOGE("AudioStreamInternal.open(): getStreamDescriptor returns %d", result); |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame^] | 127 | mServiceInterface.closeStream(mServiceStreamHandle); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 128 | return result; |
| 129 | } |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame^] | 130 | |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 131 | // resolve parcelable into a descriptor |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame^] | 132 | result = mEndPointParcelable.resolve(&mEndpointDescriptor); |
| 133 | if (result != AAUDIO_OK) { |
| 134 | ALOGE("AudioStreamInternal.open(): resolve() returns %d", result); |
| 135 | mServiceInterface.closeStream(mServiceStreamHandle); |
| 136 | return result; |
| 137 | } |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 138 | |
| 139 | // Configure endpoint based on descriptor. |
| 140 | mAudioEndpoint.configure(&mEndpointDescriptor); |
| 141 | |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 142 | mFramesPerBurst = mEndpointDescriptor.downDataQueueDescriptor.framesPerBurst; |
| 143 | assert(mFramesPerBurst >= 16); |
| 144 | assert(mEndpointDescriptor.downDataQueueDescriptor.capacityInFrames < 10 * 1024); |
| 145 | |
| 146 | mClockModel.setSampleRate(getSampleRate()); |
| 147 | mClockModel.setFramesPerBurst(mFramesPerBurst); |
| 148 | |
Phil Burk | c8f372c | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 149 | if (getDataCallbackProc()) { |
| 150 | mCallbackFrames = builder.getFramesPerDataCallback(); |
| 151 | if (mCallbackFrames > getBufferCapacity() / 2) { |
| 152 | ALOGE("AudioStreamInternal.open(): framesPerCallback too large"); |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame^] | 153 | mServiceInterface.closeStream(mServiceStreamHandle); |
Phil Burk | c8f372c | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 154 | return AAUDIO_ERROR_OUT_OF_RANGE; |
| 155 | |
| 156 | } else if (mCallbackFrames < 0) { |
| 157 | ALOGE("AudioStreamInternal.open(): framesPerCallback negative"); |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame^] | 158 | mServiceInterface.closeStream(mServiceStreamHandle); |
Phil Burk | c8f372c | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 159 | return AAUDIO_ERROR_OUT_OF_RANGE; |
| 160 | |
| 161 | } |
| 162 | if (mCallbackFrames == AAUDIO_UNSPECIFIED) { |
| 163 | mCallbackFrames = mFramesPerBurst; |
| 164 | } |
| 165 | |
| 166 | int32_t bytesPerFrame = getSamplesPerFrame() |
| 167 | * AAudioConvert_formatToSizeInBytes(getFormat()); |
| 168 | int32_t callbackBufferSize = mCallbackFrames * bytesPerFrame; |
| 169 | mCallbackBuffer = new uint8_t[callbackBufferSize]; |
| 170 | } |
| 171 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 172 | setState(AAUDIO_STREAM_STATE_OPEN); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 173 | } |
| 174 | return result; |
| 175 | } |
| 176 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 177 | aaudio_result_t AudioStreamInternal::close() { |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame^] | 178 | ALOGD_IF(ALOG_CONDITION, "AudioStreamInternal.close(): mServiceStreamHandle = 0x%08X", mServiceStreamHandle); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 179 | if (mServiceStreamHandle != AAUDIO_HANDLE_INVALID) { |
| 180 | aaudio_handle_t serviceStreamHandle = mServiceStreamHandle; |
| 181 | mServiceStreamHandle = AAUDIO_HANDLE_INVALID; |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame^] | 182 | |
| 183 | mServiceInterface.closeStream(serviceStreamHandle); |
Phil Burk | c8f372c | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 184 | delete[] mCallbackBuffer; |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame^] | 185 | return mEndPointParcelable.close(); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 186 | } else { |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 187 | return AAUDIO_ERROR_INVALID_HANDLE; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 188 | } |
| 189 | } |
| 190 | |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame^] | 191 | |
Phil Burk | c8f372c | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 192 | // Render audio in the application callback and then write the data to the stream. |
| 193 | void *AudioStreamInternal::callbackLoop() { |
| 194 | aaudio_result_t result = AAUDIO_OK; |
| 195 | aaudio_data_callback_result_t callbackResult = AAUDIO_CALLBACK_RESULT_CONTINUE; |
| 196 | int32_t framesWritten = 0; |
| 197 | AAudioStream_dataCallback appCallback = getDataCallbackProc(); |
| 198 | if (appCallback == nullptr) return NULL; |
| 199 | |
| 200 | while (mCallbackEnabled.load() && isPlaying() && (result >= 0)) { // result might be a frame count |
| 201 | // Call application using the AAudio callback interface. |
| 202 | callbackResult = (*appCallback)( |
| 203 | (AAudioStream *) this, |
| 204 | getDataCallbackUserData(), |
| 205 | mCallbackBuffer, |
| 206 | mCallbackFrames); |
| 207 | |
| 208 | if (callbackResult == AAUDIO_CALLBACK_RESULT_CONTINUE) { |
| 209 | // Write audio data to stream |
| 210 | int64_t timeoutNanos = calculateReasonableTimeout(mCallbackFrames); |
| 211 | result = write(mCallbackBuffer, mCallbackFrames, timeoutNanos); |
| 212 | if (result == AAUDIO_ERROR_DISCONNECTED) { |
| 213 | if (getErrorCallbackProc() != nullptr) { |
| 214 | ALOGD("AudioStreamAAudio(): callbackLoop() stream disconnected"); |
| 215 | (*getErrorCallbackProc())( |
| 216 | (AAudioStream *) this, |
| 217 | getErrorCallbackUserData(), |
| 218 | AAUDIO_OK); |
| 219 | } |
| 220 | break; |
| 221 | } else if (result != mCallbackFrames) { |
| 222 | ALOGE("AudioStreamAAudio(): callbackLoop() wrote %d / %d", |
| 223 | framesWritten, mCallbackFrames); |
| 224 | break; |
| 225 | } |
| 226 | } else if (callbackResult == AAUDIO_CALLBACK_RESULT_STOP) { |
| 227 | ALOGD("AudioStreamAAudio(): callback returned AAUDIO_CALLBACK_RESULT_STOP"); |
| 228 | break; |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | ALOGD("AudioStreamAAudio(): callbackLoop() exiting, result = %d, isPlaying() = %d", |
| 233 | result, (int) isPlaying()); |
| 234 | return NULL; // TODO review |
| 235 | } |
| 236 | |
| 237 | static void *aaudio_callback_thread_proc(void *context) |
| 238 | { |
| 239 | AudioStreamInternal *stream = (AudioStreamInternal *)context; |
| 240 | //LOGD("AudioStreamAAudio(): oboe_callback_thread, stream = %p", stream); |
| 241 | if (stream != NULL) { |
| 242 | return stream->callbackLoop(); |
| 243 | } else { |
| 244 | return NULL; |
| 245 | } |
| 246 | } |
| 247 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 248 | aaudio_result_t AudioStreamInternal::requestStart() |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 249 | { |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 250 | int64_t startTime; |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame^] | 251 | ALOGD_IF(ALOG_CONDITION, "AudioStreamInternal(): start()"); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 252 | if (mServiceStreamHandle == AAUDIO_HANDLE_INVALID) { |
| 253 | return AAUDIO_ERROR_INVALID_STATE; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 254 | } |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame^] | 255 | |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 256 | startTime = AudioClock::getNanoseconds(); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 257 | mClockModel.start(startTime); |
| 258 | processTimestamp(0, startTime); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 259 | setState(AAUDIO_STREAM_STATE_STARTING); |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame^] | 260 | aaudio_result_t result = mServiceInterface.startStream(mServiceStreamHandle);; |
Phil Burk | c8f372c | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 261 | |
| 262 | if (result == AAUDIO_OK && getDataCallbackProc() != nullptr) { |
| 263 | // Launch the callback loop thread. |
| 264 | int64_t periodNanos = mCallbackFrames |
| 265 | * AAUDIO_NANOS_PER_SECOND |
| 266 | / getSampleRate(); |
| 267 | mCallbackEnabled.store(true); |
| 268 | result = createThread(periodNanos, aaudio_callback_thread_proc, this); |
| 269 | } |
| 270 | return result; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 271 | } |
| 272 | |
Phil Burk | c8f372c | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 273 | int64_t AudioStreamInternal::calculateReasonableTimeout(int32_t framesPerOperation) { |
| 274 | |
| 275 | // Wait for at least a second or some number of callbacks to join the thread. |
| 276 | int64_t timeoutNanoseconds = (MIN_TIMEOUT_OPERATIONS * framesPerOperation * AAUDIO_NANOS_PER_SECOND) |
| 277 | / getSampleRate(); |
| 278 | if (timeoutNanoseconds < MIN_TIMEOUT_NANOS) { // arbitrary number of seconds |
| 279 | timeoutNanoseconds = MIN_TIMEOUT_NANOS; |
| 280 | } |
| 281 | return timeoutNanoseconds; |
| 282 | } |
| 283 | |
| 284 | aaudio_result_t AudioStreamInternal::stopCallback() |
| 285 | { |
| 286 | if (isDataCallbackActive()) { |
| 287 | mCallbackEnabled.store(false); |
| 288 | return joinThread(NULL, calculateReasonableTimeout(mCallbackFrames)); |
| 289 | } else { |
| 290 | return AAUDIO_OK; |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | aaudio_result_t AudioStreamInternal::requestPauseInternal() |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 295 | { |
| 296 | ALOGD("AudioStreamInternal(): pause()"); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 297 | if (mServiceStreamHandle == AAUDIO_HANDLE_INVALID) { |
| 298 | return AAUDIO_ERROR_INVALID_STATE; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 299 | } |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame^] | 300 | |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 301 | mClockModel.stop(AudioClock::getNanoseconds()); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 302 | setState(AAUDIO_STREAM_STATE_PAUSING); |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame^] | 303 | return mServiceInterface.startStream(mServiceStreamHandle); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 304 | } |
| 305 | |
Phil Burk | c8f372c | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 306 | aaudio_result_t AudioStreamInternal::requestPause() |
| 307 | { |
| 308 | aaudio_result_t result = stopCallback(); |
| 309 | if (result != AAUDIO_OK) { |
| 310 | return result; |
| 311 | } |
| 312 | return requestPauseInternal(); |
| 313 | } |
| 314 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 315 | aaudio_result_t AudioStreamInternal::requestFlush() { |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame^] | 316 | ALOGD_IF(ALOG_CONDITION, "AudioStreamInternal(): flush()"); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 317 | if (mServiceStreamHandle == AAUDIO_HANDLE_INVALID) { |
| 318 | return AAUDIO_ERROR_INVALID_STATE; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 319 | } |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame^] | 320 | |
Phil Burk | c8f372c | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 321 | setState(AAUDIO_STREAM_STATE_FLUSHING); |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame^] | 322 | return mServiceInterface.flushStream(mServiceStreamHandle); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | void AudioStreamInternal::onFlushFromServer() { |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame^] | 326 | ALOGD_IF(ALOG_CONDITION, "AudioStreamInternal(): onFlushFromServer()"); |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 327 | int64_t readCounter = mAudioEndpoint.getDownDataReadCounter(); |
| 328 | int64_t writeCounter = mAudioEndpoint.getDownDataWriteCounter(); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 329 | // Bump offset so caller does not see the retrograde motion in getFramesRead(). |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 330 | int64_t framesFlushed = writeCounter - readCounter; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 331 | mFramesOffsetFromService += framesFlushed; |
| 332 | // Flush written frames by forcing writeCounter to readCounter. |
| 333 | // This is because we cannot move the read counter in the hardware. |
| 334 | mAudioEndpoint.setDownDataWriteCounter(readCounter); |
| 335 | } |
| 336 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 337 | aaudio_result_t AudioStreamInternal::requestStop() |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 338 | { |
| 339 | // TODO better implementation of requestStop() |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 340 | aaudio_result_t result = requestPause(); |
| 341 | if (result == AAUDIO_OK) { |
| 342 | aaudio_stream_state_t state; |
| 343 | result = waitForStateChange(AAUDIO_STREAM_STATE_PAUSING, |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 344 | &state, |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 345 | 500 * AAUDIO_NANOS_PER_MILLISECOND);// TODO temporary code |
| 346 | if (result == AAUDIO_OK) { |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 347 | result = requestFlush(); |
| 348 | } |
| 349 | } |
| 350 | return result; |
| 351 | } |
| 352 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 353 | aaudio_result_t AudioStreamInternal::registerThread() { |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame^] | 354 | ALOGD_IF(ALOG_CONDITION, "AudioStreamInternal(): registerThread()"); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 355 | if (mServiceStreamHandle == AAUDIO_HANDLE_INVALID) { |
| 356 | return AAUDIO_ERROR_INVALID_STATE; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 357 | } |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame^] | 358 | return mServiceInterface.registerAudioThread(mServiceStreamHandle, |
| 359 | getpid(), |
| 360 | gettid(), |
| 361 | getPeriodNanoseconds()); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 362 | } |
| 363 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 364 | aaudio_result_t AudioStreamInternal::unregisterThread() { |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame^] | 365 | ALOGD_IF(ALOG_CONDITION, "AudioStreamInternal(): unregisterThread()"); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 366 | if (mServiceStreamHandle == AAUDIO_HANDLE_INVALID) { |
| 367 | return AAUDIO_ERROR_INVALID_STATE; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 368 | } |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame^] | 369 | return mServiceInterface.unregisterAudioThread(mServiceStreamHandle, getpid(), gettid()); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 370 | } |
| 371 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 372 | aaudio_result_t AudioStreamInternal::getTimestamp(clockid_t clockId, |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 373 | int64_t *framePosition, |
| 374 | int64_t *timeNanoseconds) { |
Phil Burk | c8f372c | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 375 | // TODO implement using real HAL |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 376 | int64_t time = AudioClock::getNanoseconds(); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 377 | *framePosition = mClockModel.convertTimeToPosition(time); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 378 | *timeNanoseconds = time + (10 * AAUDIO_NANOS_PER_MILLISECOND); // Fake hardware delay |
| 379 | return AAUDIO_OK; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 380 | } |
| 381 | |
Phil Burk | c8f372c | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 382 | aaudio_result_t AudioStreamInternal::updateStateWhileWaiting() { |
| 383 | if (isDataCallbackActive()) { |
| 384 | return AAUDIO_OK; // state is getting updated by the callback thread read/write call |
| 385 | } |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 386 | return processCommands(); |
| 387 | } |
| 388 | |
| 389 | #if LOG_TIMESTAMPS |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 390 | static void AudioStreamInternal_LogTimestamp(AAudioServiceMessage &command) { |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 391 | static int64_t oldPosition = 0; |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 392 | static int64_t oldTime = 0; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 393 | int64_t framePosition = command.timestamp.position; |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 394 | int64_t nanoTime = command.timestamp.timestamp; |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame^] | 395 | ALOGD_IF(ALOG_CONDITION, "AudioStreamInternal() timestamp says framePosition = %08lld at nanoTime %llu", |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 396 | (long long) framePosition, |
| 397 | (long long) nanoTime); |
| 398 | int64_t nanosDelta = nanoTime - oldTime; |
| 399 | if (nanosDelta > 0 && oldTime > 0) { |
| 400 | int64_t framesDelta = framePosition - oldPosition; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 401 | int64_t rate = (framesDelta * AAUDIO_NANOS_PER_SECOND) / nanosDelta; |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame^] | 402 | ALOGD_IF(ALOG_CONDITION, "AudioStreamInternal() - framesDelta = %08lld", (long long) framesDelta); |
| 403 | ALOGD_IF(ALOG_CONDITION, "AudioStreamInternal() - nanosDelta = %08lld", (long long) nanosDelta); |
| 404 | ALOGD_IF(ALOG_CONDITION, "AudioStreamInternal() - measured rate = %llu", (unsigned long long) rate); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 405 | } |
| 406 | oldPosition = framePosition; |
| 407 | oldTime = nanoTime; |
| 408 | } |
| 409 | #endif |
| 410 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 411 | aaudio_result_t AudioStreamInternal::onTimestampFromServer(AAudioServiceMessage *message) { |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 412 | int64_t framePosition = 0; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 413 | #if LOG_TIMESTAMPS |
| 414 | AudioStreamInternal_LogTimestamp(command); |
| 415 | #endif |
| 416 | framePosition = message->timestamp.position; |
| 417 | processTimestamp(framePosition, message->timestamp.timestamp); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 418 | return AAUDIO_OK; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 419 | } |
| 420 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 421 | aaudio_result_t AudioStreamInternal::onEventFromServer(AAudioServiceMessage *message) { |
| 422 | aaudio_result_t result = AAUDIO_OK; |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame^] | 423 | ALOGD_IF(ALOG_CONDITION, "processCommands() got event %d", message->event.event); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 424 | switch (message->event.event) { |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 425 | case AAUDIO_SERVICE_EVENT_STARTED: |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame^] | 426 | ALOGD_IF(ALOG_CONDITION, "processCommands() got AAUDIO_SERVICE_EVENT_STARTED"); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 427 | setState(AAUDIO_STREAM_STATE_STARTED); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 428 | break; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 429 | case AAUDIO_SERVICE_EVENT_PAUSED: |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame^] | 430 | ALOGD_IF(ALOG_CONDITION, "processCommands() got AAUDIO_SERVICE_EVENT_PAUSED"); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 431 | setState(AAUDIO_STREAM_STATE_PAUSED); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 432 | break; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 433 | case AAUDIO_SERVICE_EVENT_FLUSHED: |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame^] | 434 | ALOGD_IF(ALOG_CONDITION, "processCommands() got AAUDIO_SERVICE_EVENT_FLUSHED"); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 435 | setState(AAUDIO_STREAM_STATE_FLUSHED); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 436 | onFlushFromServer(); |
| 437 | break; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 438 | case AAUDIO_SERVICE_EVENT_CLOSED: |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame^] | 439 | ALOGD_IF(ALOG_CONDITION, "processCommands() got AAUDIO_SERVICE_EVENT_CLOSED"); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 440 | setState(AAUDIO_STREAM_STATE_CLOSED); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 441 | break; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 442 | case AAUDIO_SERVICE_EVENT_DISCONNECTED: |
| 443 | result = AAUDIO_ERROR_DISCONNECTED; |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame^] | 444 | setState(AAUDIO_STREAM_STATE_DISCONNECTED); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 445 | ALOGW("WARNING - processCommands() AAUDIO_SERVICE_EVENT_DISCONNECTED"); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 446 | break; |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame^] | 447 | case AAUDIO_SERVICE_EVENT_VOLUME: |
| 448 | mVolume = message->event.dataDouble; |
| 449 | ALOGD_IF(ALOG_CONDITION, "processCommands() AAUDIO_SERVICE_EVENT_VOLUME %f", mVolume); |
| 450 | break; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 451 | default: |
| 452 | ALOGW("WARNING - processCommands() Unrecognized event = %d", |
| 453 | (int) message->event.event); |
| 454 | break; |
| 455 | } |
| 456 | return result; |
| 457 | } |
| 458 | |
| 459 | // Process all the commands coming from the server. |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 460 | aaudio_result_t AudioStreamInternal::processCommands() { |
| 461 | aaudio_result_t result = AAUDIO_OK; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 462 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 463 | while (result == AAUDIO_OK) { |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame^] | 464 | //ALOGD_IF(ALOG_CONDITION, "AudioStreamInternal::processCommands() - looping, %d", result); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 465 | AAudioServiceMessage message; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 466 | if (mAudioEndpoint.readUpCommand(&message) != 1) { |
| 467 | break; // no command this time, no problem |
| 468 | } |
| 469 | switch (message.what) { |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 470 | case AAudioServiceMessage::code::TIMESTAMP: |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 471 | result = onTimestampFromServer(&message); |
| 472 | break; |
| 473 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 474 | case AAudioServiceMessage::code::EVENT: |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 475 | result = onEventFromServer(&message); |
| 476 | break; |
| 477 | |
| 478 | default: |
| 479 | ALOGW("WARNING - AudioStreamInternal::processCommands() Unrecognized what = %d", |
| 480 | (int) message.what); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 481 | result = AAUDIO_ERROR_UNEXPECTED_VALUE; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 482 | break; |
| 483 | } |
| 484 | } |
| 485 | return result; |
| 486 | } |
| 487 | |
| 488 | // Write the data, block if needed and timeoutMillis > 0 |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 489 | aaudio_result_t AudioStreamInternal::write(const void *buffer, int32_t numFrames, |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 490 | int64_t timeoutNanoseconds) |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 491 | { |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 492 | aaudio_result_t result = AAUDIO_OK; |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame^] | 493 | int32_t loopCount = 0; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 494 | uint8_t* source = (uint8_t*)buffer; |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 495 | int64_t currentTimeNanos = AudioClock::getNanoseconds(); |
| 496 | int64_t deadlineNanos = currentTimeNanos + timeoutNanoseconds; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 497 | int32_t framesLeft = numFrames; |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame^] | 498 | //ALOGD_IF(ALOG_CONDITION, "AudioStreamInternal::write(%p, %d) at time %08llu , mState = %s", |
| 499 | // buffer, numFrames, (unsigned long long) currentTimeNanos, |
| 500 | // AAudio_convertStreamStateToText(getState())); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 501 | |
| 502 | // Write until all the data has been written or until a timeout occurs. |
| 503 | while (framesLeft > 0) { |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame^] | 504 | //ALOGD_IF(ALOG_CONDITION, "AudioStreamInternal::write() loop: framesLeft = %d, loopCount = %d =====", |
| 505 | // framesLeft, loopCount++); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 506 | // The call to writeNow() will not block. It will just write as much as it can. |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 507 | int64_t wakeTimeNanos = 0; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 508 | aaudio_result_t framesWritten = writeNow(source, framesLeft, |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 509 | currentTimeNanos, &wakeTimeNanos); |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame^] | 510 | //ALOGD_IF(ALOG_CONDITION, "AudioStreamInternal::write() loop: framesWritten = %d", framesWritten); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 511 | if (framesWritten < 0) { |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame^] | 512 | ALOGE("AudioStreamInternal::write() loop: writeNow returned %d", framesWritten); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 513 | result = framesWritten; |
| 514 | break; |
| 515 | } |
| 516 | framesLeft -= (int32_t) framesWritten; |
| 517 | source += framesWritten * getBytesPerFrame(); |
| 518 | |
| 519 | // Should we block? |
| 520 | if (timeoutNanoseconds == 0) { |
| 521 | break; // don't block |
| 522 | } else if (framesLeft > 0) { |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame^] | 523 | //ALOGD_IF(ALOG_CONDITION, "AudioStreamInternal:: original wakeTimeNanos %lld", (long long) wakeTimeNanos); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 524 | // clip the wake time to something reasonable |
| 525 | if (wakeTimeNanos < currentTimeNanos) { |
| 526 | wakeTimeNanos = currentTimeNanos; |
| 527 | } |
| 528 | if (wakeTimeNanos > deadlineNanos) { |
| 529 | // If we time out, just return the framesWritten so far. |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame^] | 530 | ALOGE("AudioStreamInternal::write(): timed out after %lld nanos", |
| 531 | (long long) timeoutNanoseconds); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 532 | break; |
| 533 | } |
| 534 | |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame^] | 535 | //ALOGD_IF(ALOG_CONDITION, "AudioStreamInternal:: sleep until %lld, dur = %lld", (long long) wakeTimeNanos, |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 536 | // (long long) (wakeTimeNanos - currentTimeNanos)); |
| 537 | AudioClock::sleepForNanos(wakeTimeNanos - currentTimeNanos); |
| 538 | currentTimeNanos = AudioClock::getNanoseconds(); |
| 539 | } |
| 540 | } |
| 541 | |
| 542 | // return error or framesWritten |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame^] | 543 | //ALOGD_IF(ALOG_CONDITION, "AudioStreamInternal::write() result = %d, framesLeft = %d, #%d", |
| 544 | // result, framesLeft, loopCount); |
| 545 | (void) loopCount; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 546 | return (result < 0) ? result : numFrames - framesLeft; |
| 547 | } |
| 548 | |
| 549 | // Write as much data as we can without blocking. |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 550 | aaudio_result_t AudioStreamInternal::writeNow(const void *buffer, int32_t numFrames, |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 551 | int64_t currentNanoTime, int64_t *wakeTimePtr) { |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame^] | 552 | |
| 553 | //ALOGD_IF(ALOG_CONDITION, "AudioStreamInternal::writeNow(%p) - enter", buffer); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 554 | { |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 555 | aaudio_result_t result = processCommands(); |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame^] | 556 | //ALOGD_IF(ALOG_CONDITION, "AudioStreamInternal::writeNow() - processCommands() returned %d", result); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 557 | if (result != AAUDIO_OK) { |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 558 | return result; |
| 559 | } |
| 560 | } |
| 561 | |
| 562 | if (mAudioEndpoint.isOutputFreeRunning()) { |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame^] | 563 | ALOGD_IF(ALOG_CONDITION, "AudioStreamInternal::writeNow() - update read counter"); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 564 | // Update data queue based on the timing model. |
| 565 | int64_t estimatedReadCounter = mClockModel.convertTimeToPosition(currentNanoTime); |
| 566 | mAudioEndpoint.setDownDataReadCounter(estimatedReadCounter); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 567 | } |
| 568 | // TODO else query from endpoint cuz set by actual reader, maybe |
| 569 | |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame^] | 570 | // If the read index passed the write index then consider it an underrun. |
| 571 | if (mAudioEndpoint.getFullFramesAvailable() < 0) { |
| 572 | mXRunCount++; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 573 | } |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame^] | 574 | |
| 575 | // Write some data to the buffer. |
| 576 | //ALOGD_IF(ALOG_CONDITION, "AudioStreamInternal::writeNow() - writeNowWithConversion(%d)", numFrames); |
| 577 | int32_t framesWritten = writeNowWithConversion(buffer, numFrames); |
| 578 | //ALOGD_IF(ALOG_CONDITION, "AudioStreamInternal::writeNow() - tried to write %d frames, wrote %d", |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 579 | // numFrames, framesWritten); |
| 580 | |
| 581 | // Calculate an ideal time to wake up. |
| 582 | if (wakeTimePtr != nullptr && framesWritten >= 0) { |
| 583 | // By default wake up a few milliseconds from now. // TODO review |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame^] | 584 | int64_t wakeTime = currentNanoTime + (1 * AAUDIO_NANOS_PER_MILLISECOND); |
| 585 | aaudio_stream_state_t state = getState(); |
| 586 | //ALOGD_IF(ALOG_CONDITION, "AudioStreamInternal::writeNow() - wakeTime based on %s", |
| 587 | // AAudio_convertStreamStateToText(state)); |
| 588 | switch (state) { |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 589 | case AAUDIO_STREAM_STATE_OPEN: |
| 590 | case AAUDIO_STREAM_STATE_STARTING: |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 591 | if (framesWritten != 0) { |
| 592 | // Don't wait to write more data. Just prime the buffer. |
| 593 | wakeTime = currentNanoTime; |
| 594 | } |
| 595 | break; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 596 | case AAUDIO_STREAM_STATE_STARTED: // When do we expect the next read burst to occur? |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 597 | { |
| 598 | uint32_t burstSize = mFramesPerBurst; |
| 599 | if (burstSize < 32) { |
| 600 | burstSize = 32; // TODO review |
| 601 | } |
| 602 | |
| 603 | uint64_t nextReadPosition = mAudioEndpoint.getDownDataReadCounter() + burstSize; |
| 604 | wakeTime = mClockModel.convertPositionToTime(nextReadPosition); |
| 605 | } |
| 606 | break; |
| 607 | default: |
| 608 | break; |
| 609 | } |
| 610 | *wakeTimePtr = wakeTime; |
| 611 | |
| 612 | } |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame^] | 613 | // ALOGD_IF(ALOG_CONDITION, "AudioStreamInternal::writeNow finished: now = %llu, read# = %llu, wrote# = %llu", |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 614 | // (unsigned long long)currentNanoTime, |
| 615 | // (unsigned long long)mAudioEndpoint.getDownDataReadCounter(), |
| 616 | // (unsigned long long)mAudioEndpoint.getDownDataWriteCounter()); |
| 617 | return framesWritten; |
| 618 | } |
| 619 | |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame^] | 620 | |
| 621 | // TODO this function needs a major cleanup. |
| 622 | aaudio_result_t AudioStreamInternal::writeNowWithConversion(const void *buffer, |
| 623 | int32_t numFrames) { |
| 624 | // ALOGD_IF(ALOG_CONDITION, "AudioStreamInternal::writeNowWithConversion(%p, %d)", buffer, numFrames); |
| 625 | WrappingBuffer wrappingBuffer; |
| 626 | mAudioEndpoint.getEmptyRoomAvailable(&wrappingBuffer); |
| 627 | uint8_t *source = (uint8_t *) buffer; |
| 628 | int32_t framesLeft = numFrames; |
| 629 | |
| 630 | mAudioEndpoint.getEmptyRoomAvailable(&wrappingBuffer); |
| 631 | |
| 632 | // Read data in one or two parts. |
| 633 | int partIndex = 0; |
| 634 | while (framesLeft > 0 && partIndex < WrappingBuffer::SIZE) { |
| 635 | int32_t framesToWrite = framesLeft; |
| 636 | int32_t framesAvailable = wrappingBuffer.numFrames[partIndex]; |
| 637 | if (framesAvailable > 0) { |
| 638 | if (framesToWrite > framesAvailable) { |
| 639 | framesToWrite = framesAvailable; |
| 640 | } |
| 641 | int32_t numBytes = getBytesPerFrame(); |
| 642 | // TODO handle volume scaling |
| 643 | if (getFormat() == mDeviceFormat) { |
| 644 | // Copy straight through. |
| 645 | memcpy(wrappingBuffer.data[partIndex], source, numBytes); |
| 646 | } else if (getFormat() == AAUDIO_FORMAT_PCM_FLOAT |
| 647 | && mDeviceFormat == AAUDIO_FORMAT_PCM_I16) { |
| 648 | // Data conversion. |
| 649 | AAudioConvert_floatToPcm16( |
| 650 | (const float *) source, |
| 651 | framesToWrite * getSamplesPerFrame(), |
| 652 | (int16_t *) wrappingBuffer.data[partIndex]); |
| 653 | } else { |
| 654 | // TODO handle more conversions |
| 655 | ALOGE("AudioStreamInternal::writeNowWithConversion() unsupported formats: %d, %d", |
| 656 | getFormat(), mDeviceFormat); |
| 657 | return AAUDIO_ERROR_UNEXPECTED_VALUE; |
| 658 | } |
| 659 | |
| 660 | source += numBytes; |
| 661 | framesLeft -= framesToWrite; |
| 662 | } |
| 663 | partIndex++; |
| 664 | } |
| 665 | int32_t framesWritten = numFrames - framesLeft; |
| 666 | mAudioEndpoint.advanceWriteIndex(framesWritten); |
| 667 | |
| 668 | if (framesWritten > 0) { |
| 669 | incrementFramesWritten(framesWritten); |
| 670 | } |
| 671 | // ALOGD_IF(ALOG_CONDITION, "AudioStreamInternal::writeNowWithConversion() returns %d", framesWritten); |
| 672 | return framesWritten; |
| 673 | } |
| 674 | |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 675 | void AudioStreamInternal::processTimestamp(uint64_t position, int64_t time) { |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 676 | mClockModel.processTimestamp( position, time); |
| 677 | } |
| 678 | |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 679 | aaudio_result_t AudioStreamInternal::setBufferSize(int32_t requestedFrames) { |
| 680 | int32_t actualFrames = 0; |
| 681 | aaudio_result_t result = mAudioEndpoint.setBufferSizeInFrames(requestedFrames, &actualFrames); |
| 682 | if (result < 0) { |
| 683 | return result; |
| 684 | } else { |
| 685 | return (aaudio_result_t) actualFrames; |
| 686 | } |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 687 | } |
| 688 | |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 689 | int32_t AudioStreamInternal::getBufferSize() const |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 690 | { |
| 691 | return mAudioEndpoint.getBufferSizeInFrames(); |
| 692 | } |
| 693 | |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 694 | int32_t AudioStreamInternal::getBufferCapacity() const |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 695 | { |
| 696 | return mAudioEndpoint.getBufferCapacityInFrames(); |
| 697 | } |
| 698 | |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 699 | int32_t AudioStreamInternal::getFramesPerBurst() const |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 700 | { |
| 701 | return mEndpointDescriptor.downDataQueueDescriptor.framesPerBurst; |
| 702 | } |
| 703 | |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 704 | int64_t AudioStreamInternal::getFramesRead() |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 705 | { |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 706 | int64_t framesRead = |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 707 | mClockModel.convertTimeToPosition(AudioClock::getNanoseconds()) |
| 708 | + mFramesOffsetFromService; |
| 709 | // Prevent retrograde motion. |
| 710 | if (framesRead < mLastFramesRead) { |
| 711 | framesRead = mLastFramesRead; |
| 712 | } else { |
| 713 | mLastFramesRead = framesRead; |
| 714 | } |
Phil Burk | 7f6b40d | 2017-02-09 13:18:38 -0800 | [diff] [blame^] | 715 | ALOGD_IF(ALOG_CONDITION, "AudioStreamInternal::getFramesRead() returns %lld", (long long)framesRead); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 716 | return framesRead; |
| 717 | } |
| 718 | |
| 719 | // TODO implement getTimestamp |