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