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 | e4d7bb4 | 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 | e4d7bb4 | 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 | e4d7bb4 | 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 | e4d7bb4 | 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 | e4d7bb4 | 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 | e4d7bb4 | 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; |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 202 | AAudioStream_dataCallback appCallback = getDataCallbackProc(); |
| 203 | if (appCallback == nullptr) return NULL; |
| 204 | |
Phil Burk | 677d791 | 2017-04-07 12:17:12 -0700 | [diff] [blame^] | 205 | // result might be a frame count |
| 206 | while (mCallbackEnabled.load() && isPlaying() && (result >= 0)) { |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 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) { |
Phil Burk | 677d791 | 2017-04-07 12:17:12 -0700 | [diff] [blame^] | 215 | // Write audio data to stream. |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 216 | int64_t timeoutNanos = calculateReasonableTimeout(mCallbackFrames); |
Phil Burk | 677d791 | 2017-04-07 12:17:12 -0700 | [diff] [blame^] | 217 | |
| 218 | // This is a BLOCKING WRITE! |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 219 | result = write(mCallbackBuffer, mCallbackFrames, timeoutNanos); |
Phil Burk | 677d791 | 2017-04-07 12:17:12 -0700 | [diff] [blame^] | 220 | if ((result != mCallbackFrames)) { |
| 221 | ALOGE("AudioStreamInternal(): callbackLoop: write() returned %d", result); |
| 222 | if (result >= 0) { |
| 223 | // Only wrote some of the frames requested. Must have timed out. |
| 224 | result = AAUDIO_ERROR_TIMEOUT; |
| 225 | } |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 226 | if (getErrorCallbackProc() != nullptr) { |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 227 | (*getErrorCallbackProc())( |
| 228 | (AAudioStream *) this, |
| 229 | getErrorCallbackUserData(), |
Phil Burk | 677d791 | 2017-04-07 12:17:12 -0700 | [diff] [blame^] | 230 | result); |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 231 | } |
| 232 | break; |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 233 | } |
| 234 | } else if (callbackResult == AAUDIO_CALLBACK_RESULT_STOP) { |
Phil Burk | 677d791 | 2017-04-07 12:17:12 -0700 | [diff] [blame^] | 235 | ALOGD("AudioStreamInternal(): callback returned AAUDIO_CALLBACK_RESULT_STOP"); |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 236 | break; |
| 237 | } |
| 238 | } |
| 239 | |
Phil Burk | 677d791 | 2017-04-07 12:17:12 -0700 | [diff] [blame^] | 240 | ALOGD("AudioStreamInternal(): callbackLoop() exiting, result = %d, isPlaying() = %d", |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 241 | result, (int) isPlaying()); |
| 242 | return NULL; // TODO review |
| 243 | } |
| 244 | |
| 245 | static void *aaudio_callback_thread_proc(void *context) |
| 246 | { |
| 247 | AudioStreamInternal *stream = (AudioStreamInternal *)context; |
Phil Burk | 677d791 | 2017-04-07 12:17:12 -0700 | [diff] [blame^] | 248 | //LOGD("AudioStreamInternal(): oboe_callback_thread, stream = %p", stream); |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 249 | if (stream != NULL) { |
| 250 | return stream->callbackLoop(); |
| 251 | } else { |
| 252 | return NULL; |
| 253 | } |
| 254 | } |
| 255 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 256 | aaudio_result_t AudioStreamInternal::requestStart() |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 257 | { |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 258 | int64_t startTime; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 259 | ALOGD("AudioStreamInternal(): start()"); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 260 | if (mServiceStreamHandle == AAUDIO_HANDLE_INVALID) { |
| 261 | return AAUDIO_ERROR_INVALID_STATE; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 262 | } |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 263 | const sp<IAAudioService>& aaudioService = getAAudioService(); |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 264 | if (aaudioService == 0) { |
| 265 | return AAUDIO_ERROR_NO_SERVICE; |
| 266 | } |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 267 | startTime = AudioClock::getNanoseconds(); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 268 | mClockModel.start(startTime); |
| 269 | processTimestamp(0, startTime); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 270 | setState(AAUDIO_STREAM_STATE_STARTING); |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 271 | aaudio_result_t result = aaudioService->startStream(mServiceStreamHandle); |
| 272 | |
| 273 | if (result == AAUDIO_OK && getDataCallbackProc() != nullptr) { |
| 274 | // Launch the callback loop thread. |
| 275 | int64_t periodNanos = mCallbackFrames |
| 276 | * AAUDIO_NANOS_PER_SECOND |
| 277 | / getSampleRate(); |
| 278 | mCallbackEnabled.store(true); |
| 279 | result = createThread(periodNanos, aaudio_callback_thread_proc, this); |
| 280 | } |
| 281 | return result; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 282 | } |
| 283 | |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 284 | int64_t AudioStreamInternal::calculateReasonableTimeout(int32_t framesPerOperation) { |
| 285 | |
| 286 | // Wait for at least a second or some number of callbacks to join the thread. |
| 287 | int64_t timeoutNanoseconds = (MIN_TIMEOUT_OPERATIONS * framesPerOperation * AAUDIO_NANOS_PER_SECOND) |
| 288 | / getSampleRate(); |
| 289 | if (timeoutNanoseconds < MIN_TIMEOUT_NANOS) { // arbitrary number of seconds |
| 290 | timeoutNanoseconds = MIN_TIMEOUT_NANOS; |
| 291 | } |
| 292 | return timeoutNanoseconds; |
| 293 | } |
| 294 | |
| 295 | aaudio_result_t AudioStreamInternal::stopCallback() |
| 296 | { |
| 297 | if (isDataCallbackActive()) { |
| 298 | mCallbackEnabled.store(false); |
| 299 | return joinThread(NULL, calculateReasonableTimeout(mCallbackFrames)); |
| 300 | } else { |
| 301 | return AAUDIO_OK; |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | aaudio_result_t AudioStreamInternal::requestPauseInternal() |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 306 | { |
| 307 | ALOGD("AudioStreamInternal(): pause()"); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 308 | if (mServiceStreamHandle == AAUDIO_HANDLE_INVALID) { |
| 309 | return AAUDIO_ERROR_INVALID_STATE; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 310 | } |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 311 | const sp<IAAudioService>& aaudioService = getAAudioService(); |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 312 | if (aaudioService == 0) { |
| 313 | return AAUDIO_ERROR_NO_SERVICE; |
| 314 | } |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 315 | mClockModel.stop(AudioClock::getNanoseconds()); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 316 | setState(AAUDIO_STREAM_STATE_PAUSING); |
| 317 | return aaudioService->pauseStream(mServiceStreamHandle); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 318 | } |
| 319 | |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 320 | aaudio_result_t AudioStreamInternal::requestPause() |
| 321 | { |
| 322 | aaudio_result_t result = stopCallback(); |
| 323 | if (result != AAUDIO_OK) { |
| 324 | return result; |
| 325 | } |
| 326 | return requestPauseInternal(); |
| 327 | } |
| 328 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 329 | aaudio_result_t AudioStreamInternal::requestFlush() { |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 330 | ALOGD("AudioStreamInternal(): flush()"); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 331 | if (mServiceStreamHandle == AAUDIO_HANDLE_INVALID) { |
| 332 | return AAUDIO_ERROR_INVALID_STATE; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 333 | } |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 334 | const sp<IAAudioService>& aaudioService = getAAudioService(); |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 335 | if (aaudioService == 0) { |
| 336 | return AAUDIO_ERROR_NO_SERVICE; |
| 337 | } |
| 338 | setState(AAUDIO_STREAM_STATE_FLUSHING); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 339 | return aaudioService->flushStream(mServiceStreamHandle); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | void AudioStreamInternal::onFlushFromServer() { |
| 343 | ALOGD("AudioStreamInternal(): onFlushFromServer()"); |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 344 | int64_t readCounter = mAudioEndpoint.getDownDataReadCounter(); |
| 345 | int64_t writeCounter = mAudioEndpoint.getDownDataWriteCounter(); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 346 | // Bump offset so caller does not see the retrograde motion in getFramesRead(). |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 347 | int64_t framesFlushed = writeCounter - readCounter; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 348 | mFramesOffsetFromService += framesFlushed; |
| 349 | // Flush written frames by forcing writeCounter to readCounter. |
| 350 | // This is because we cannot move the read counter in the hardware. |
| 351 | mAudioEndpoint.setDownDataWriteCounter(readCounter); |
| 352 | } |
| 353 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 354 | aaudio_result_t AudioStreamInternal::requestStop() |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 355 | { |
| 356 | // TODO better implementation of requestStop() |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 357 | aaudio_result_t result = requestPause(); |
| 358 | if (result == AAUDIO_OK) { |
| 359 | aaudio_stream_state_t state; |
| 360 | result = waitForStateChange(AAUDIO_STREAM_STATE_PAUSING, |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 361 | &state, |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 362 | 500 * AAUDIO_NANOS_PER_MILLISECOND);// TODO temporary code |
| 363 | if (result == AAUDIO_OK) { |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 364 | result = requestFlush(); |
| 365 | } |
| 366 | } |
| 367 | return result; |
| 368 | } |
| 369 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 370 | aaudio_result_t AudioStreamInternal::registerThread() { |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 371 | ALOGD("AudioStreamInternal(): registerThread()"); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 372 | if (mServiceStreamHandle == AAUDIO_HANDLE_INVALID) { |
| 373 | return AAUDIO_ERROR_INVALID_STATE; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 374 | } |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 375 | const sp<IAAudioService>& aaudioService = getAAudioService(); |
| 376 | if (aaudioService == 0) return AAUDIO_ERROR_NO_SERVICE; |
| 377 | return aaudioService->registerAudioThread(mServiceStreamHandle, |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 378 | gettid(), |
| 379 | getPeriodNanoseconds()); |
| 380 | } |
| 381 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 382 | aaudio_result_t AudioStreamInternal::unregisterThread() { |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 383 | ALOGD("AudioStreamInternal(): unregisterThread()"); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 384 | if (mServiceStreamHandle == AAUDIO_HANDLE_INVALID) { |
| 385 | return AAUDIO_ERROR_INVALID_STATE; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 386 | } |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 387 | const sp<IAAudioService>& aaudioService = getAAudioService(); |
| 388 | if (aaudioService == 0) return AAUDIO_ERROR_NO_SERVICE; |
| 389 | return aaudioService->unregisterAudioThread(mServiceStreamHandle, gettid()); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 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 | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 395 | // TODO implement using real HAL |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 396 | int64_t time = AudioClock::getNanoseconds(); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 397 | *framePosition = mClockModel.convertTimeToPosition(time); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 398 | *timeNanoseconds = time + (10 * AAUDIO_NANOS_PER_MILLISECOND); // Fake hardware delay |
| 399 | return AAUDIO_OK; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 400 | } |
| 401 | |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 402 | aaudio_result_t AudioStreamInternal::updateStateWhileWaiting() { |
| 403 | if (isDataCallbackActive()) { |
| 404 | return AAUDIO_OK; // state is getting updated by the callback thread read/write call |
| 405 | } |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 406 | return processCommands(); |
| 407 | } |
| 408 | |
| 409 | #if LOG_TIMESTAMPS |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 410 | static void AudioStreamInternal_LogTimestamp(AAudioServiceMessage &command) { |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 411 | static int64_t oldPosition = 0; |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 412 | static int64_t oldTime = 0; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 413 | int64_t framePosition = command.timestamp.position; |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 414 | int64_t nanoTime = command.timestamp.timestamp; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 415 | ALOGD("AudioStreamInternal() timestamp says framePosition = %08lld at nanoTime %llu", |
| 416 | (long long) framePosition, |
| 417 | (long long) nanoTime); |
| 418 | int64_t nanosDelta = nanoTime - oldTime; |
| 419 | if (nanosDelta > 0 && oldTime > 0) { |
| 420 | int64_t framesDelta = framePosition - oldPosition; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 421 | int64_t rate = (framesDelta * AAUDIO_NANOS_PER_SECOND) / nanosDelta; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 422 | ALOGD("AudioStreamInternal() - framesDelta = %08lld", (long long) framesDelta); |
| 423 | ALOGD("AudioStreamInternal() - nanosDelta = %08lld", (long long) nanosDelta); |
| 424 | ALOGD("AudioStreamInternal() - measured rate = %llu", (unsigned long long) rate); |
| 425 | } |
| 426 | oldPosition = framePosition; |
| 427 | oldTime = nanoTime; |
| 428 | } |
| 429 | #endif |
| 430 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 431 | aaudio_result_t AudioStreamInternal::onTimestampFromServer(AAudioServiceMessage *message) { |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 432 | int64_t framePosition = 0; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 433 | #if LOG_TIMESTAMPS |
| 434 | AudioStreamInternal_LogTimestamp(command); |
| 435 | #endif |
| 436 | framePosition = message->timestamp.position; |
| 437 | processTimestamp(framePosition, message->timestamp.timestamp); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 438 | return AAUDIO_OK; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 439 | } |
| 440 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 441 | aaudio_result_t AudioStreamInternal::onEventFromServer(AAudioServiceMessage *message) { |
| 442 | aaudio_result_t result = AAUDIO_OK; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 443 | ALOGD("processCommands() got event %d", message->event.event); |
| 444 | switch (message->event.event) { |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 445 | case AAUDIO_SERVICE_EVENT_STARTED: |
| 446 | ALOGD("processCommands() got AAUDIO_SERVICE_EVENT_STARTED"); |
| 447 | setState(AAUDIO_STREAM_STATE_STARTED); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 448 | break; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 449 | case AAUDIO_SERVICE_EVENT_PAUSED: |
| 450 | ALOGD("processCommands() got AAUDIO_SERVICE_EVENT_PAUSED"); |
| 451 | setState(AAUDIO_STREAM_STATE_PAUSED); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 452 | break; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 453 | case AAUDIO_SERVICE_EVENT_FLUSHED: |
| 454 | ALOGD("processCommands() got AAUDIO_SERVICE_EVENT_FLUSHED"); |
| 455 | setState(AAUDIO_STREAM_STATE_FLUSHED); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 456 | onFlushFromServer(); |
| 457 | break; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 458 | case AAUDIO_SERVICE_EVENT_CLOSED: |
| 459 | ALOGD("processCommands() got AAUDIO_SERVICE_EVENT_CLOSED"); |
| 460 | setState(AAUDIO_STREAM_STATE_CLOSED); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 461 | break; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 462 | case AAUDIO_SERVICE_EVENT_DISCONNECTED: |
| 463 | result = AAUDIO_ERROR_DISCONNECTED; |
| 464 | ALOGW("WARNING - processCommands() AAUDIO_SERVICE_EVENT_DISCONNECTED"); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 465 | break; |
| 466 | default: |
| 467 | ALOGW("WARNING - processCommands() Unrecognized event = %d", |
| 468 | (int) message->event.event); |
| 469 | break; |
| 470 | } |
| 471 | return result; |
| 472 | } |
| 473 | |
| 474 | // Process all the commands coming from the server. |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 475 | aaudio_result_t AudioStreamInternal::processCommands() { |
| 476 | aaudio_result_t result = AAUDIO_OK; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 477 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 478 | while (result == AAUDIO_OK) { |
| 479 | AAudioServiceMessage message; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 480 | if (mAudioEndpoint.readUpCommand(&message) != 1) { |
| 481 | break; // no command this time, no problem |
| 482 | } |
| 483 | switch (message.what) { |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 484 | case AAudioServiceMessage::code::TIMESTAMP: |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 485 | result = onTimestampFromServer(&message); |
| 486 | break; |
| 487 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 488 | case AAudioServiceMessage::code::EVENT: |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 489 | result = onEventFromServer(&message); |
| 490 | break; |
| 491 | |
| 492 | default: |
| 493 | ALOGW("WARNING - AudioStreamInternal::processCommands() Unrecognized what = %d", |
| 494 | (int) message.what); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 495 | result = AAUDIO_ERROR_UNEXPECTED_VALUE; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 496 | break; |
| 497 | } |
| 498 | } |
| 499 | return result; |
| 500 | } |
| 501 | |
| 502 | // Write the data, block if needed and timeoutMillis > 0 |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 503 | aaudio_result_t AudioStreamInternal::write(const void *buffer, int32_t numFrames, |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 504 | int64_t timeoutNanoseconds) |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 505 | { |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 506 | aaudio_result_t result = AAUDIO_OK; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 507 | uint8_t* source = (uint8_t*)buffer; |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 508 | int64_t currentTimeNanos = AudioClock::getNanoseconds(); |
| 509 | int64_t deadlineNanos = currentTimeNanos + timeoutNanoseconds; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 510 | int32_t framesLeft = numFrames; |
| 511 | // ALOGD("AudioStreamInternal::write(%p, %d) at time %08llu , mState = %d ------------------", |
| 512 | // buffer, numFrames, (unsigned long long) currentTimeNanos, mState); |
| 513 | |
| 514 | // Write until all the data has been written or until a timeout occurs. |
| 515 | while (framesLeft > 0) { |
| 516 | // 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] | 517 | int64_t wakeTimeNanos = 0; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 518 | aaudio_result_t framesWritten = writeNow(source, framesLeft, |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 519 | currentTimeNanos, &wakeTimeNanos); |
| 520 | // ALOGD("AudioStreamInternal::write() writeNow() framesLeft = %d --> framesWritten = %d", framesLeft, framesWritten); |
| 521 | if (framesWritten < 0) { |
| 522 | result = framesWritten; |
| 523 | break; |
| 524 | } |
| 525 | framesLeft -= (int32_t) framesWritten; |
| 526 | source += framesWritten * getBytesPerFrame(); |
| 527 | |
| 528 | // Should we block? |
| 529 | if (timeoutNanoseconds == 0) { |
| 530 | break; // don't block |
| 531 | } else if (framesLeft > 0) { |
| 532 | //ALOGD("AudioStreamInternal:: original wakeTimeNanos %lld", (long long) wakeTimeNanos); |
| 533 | // clip the wake time to something reasonable |
| 534 | if (wakeTimeNanos < currentTimeNanos) { |
| 535 | wakeTimeNanos = currentTimeNanos; |
| 536 | } |
| 537 | if (wakeTimeNanos > deadlineNanos) { |
| 538 | // If we time out, just return the framesWritten so far. |
| 539 | ALOGE("AudioStreamInternal::write(): timed out after %lld nanos", (long long) timeoutNanoseconds); |
| 540 | break; |
| 541 | } |
| 542 | |
| 543 | //ALOGD("AudioStreamInternal:: sleep until %lld, dur = %lld", (long long) wakeTimeNanos, |
| 544 | // (long long) (wakeTimeNanos - currentTimeNanos)); |
| 545 | AudioClock::sleepForNanos(wakeTimeNanos - currentTimeNanos); |
| 546 | currentTimeNanos = AudioClock::getNanoseconds(); |
| 547 | } |
| 548 | } |
| 549 | |
| 550 | // return error or framesWritten |
| 551 | return (result < 0) ? result : numFrames - framesLeft; |
| 552 | } |
| 553 | |
| 554 | // Write as much data as we can without blocking. |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 555 | aaudio_result_t AudioStreamInternal::writeNow(const void *buffer, int32_t numFrames, |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 556 | int64_t currentNanoTime, int64_t *wakeTimePtr) { |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 557 | { |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 558 | aaudio_result_t result = processCommands(); |
| 559 | if (result != AAUDIO_OK) { |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 560 | return result; |
| 561 | } |
| 562 | } |
| 563 | |
| 564 | if (mAudioEndpoint.isOutputFreeRunning()) { |
| 565 | // Update data queue based on the timing model. |
| 566 | int64_t estimatedReadCounter = mClockModel.convertTimeToPosition(currentNanoTime); |
| 567 | mAudioEndpoint.setDownDataReadCounter(estimatedReadCounter); |
| 568 | // If the read index passed the write index then consider it an underrun. |
| 569 | if (mAudioEndpoint.getFullFramesAvailable() < 0) { |
| 570 | mXRunCount++; |
| 571 | } |
| 572 | } |
| 573 | // TODO else query from endpoint cuz set by actual reader, maybe |
| 574 | |
| 575 | // Write some data to the buffer. |
| 576 | int32_t framesWritten = mAudioEndpoint.writeDataNow(buffer, numFrames); |
| 577 | if (framesWritten > 0) { |
| 578 | incrementFramesWritten(framesWritten); |
| 579 | } |
| 580 | //ALOGD("AudioStreamInternal::writeNow() - tried to write %d frames, wrote %d", |
| 581 | // numFrames, framesWritten); |
| 582 | |
| 583 | // Calculate an ideal time to wake up. |
| 584 | if (wakeTimePtr != nullptr && framesWritten >= 0) { |
| 585 | // By default wake up a few milliseconds from now. // TODO review |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 586 | int64_t wakeTime = currentNanoTime + (2 * AAUDIO_NANOS_PER_MILLISECOND); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 587 | switch (getState()) { |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 588 | case AAUDIO_STREAM_STATE_OPEN: |
| 589 | case AAUDIO_STREAM_STATE_STARTING: |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 590 | if (framesWritten != 0) { |
| 591 | // Don't wait to write more data. Just prime the buffer. |
| 592 | wakeTime = currentNanoTime; |
| 593 | } |
| 594 | break; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 595 | 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] | 596 | { |
| 597 | uint32_t burstSize = mFramesPerBurst; |
| 598 | if (burstSize < 32) { |
| 599 | burstSize = 32; // TODO review |
| 600 | } |
| 601 | |
| 602 | uint64_t nextReadPosition = mAudioEndpoint.getDownDataReadCounter() + burstSize; |
| 603 | wakeTime = mClockModel.convertPositionToTime(nextReadPosition); |
| 604 | } |
| 605 | break; |
| 606 | default: |
| 607 | break; |
| 608 | } |
| 609 | *wakeTimePtr = wakeTime; |
| 610 | |
| 611 | } |
| 612 | // ALOGD("AudioStreamInternal::writeNow finished: now = %llu, read# = %llu, wrote# = %llu", |
| 613 | // (unsigned long long)currentNanoTime, |
| 614 | // (unsigned long long)mAudioEndpoint.getDownDataReadCounter(), |
| 615 | // (unsigned long long)mAudioEndpoint.getDownDataWriteCounter()); |
| 616 | return framesWritten; |
| 617 | } |
| 618 | |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 619 | void AudioStreamInternal::processTimestamp(uint64_t position, int64_t time) { |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 620 | mClockModel.processTimestamp( position, time); |
| 621 | } |
| 622 | |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 623 | aaudio_result_t AudioStreamInternal::setBufferSize(int32_t requestedFrames) { |
| 624 | int32_t actualFrames = 0; |
| 625 | aaudio_result_t result = mAudioEndpoint.setBufferSizeInFrames(requestedFrames, &actualFrames); |
| 626 | if (result < 0) { |
| 627 | return result; |
| 628 | } else { |
| 629 | return (aaudio_result_t) actualFrames; |
| 630 | } |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 631 | } |
| 632 | |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 633 | int32_t AudioStreamInternal::getBufferSize() const |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 634 | { |
| 635 | return mAudioEndpoint.getBufferSizeInFrames(); |
| 636 | } |
| 637 | |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 638 | int32_t AudioStreamInternal::getBufferCapacity() const |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 639 | { |
| 640 | return mAudioEndpoint.getBufferCapacityInFrames(); |
| 641 | } |
| 642 | |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 643 | int32_t AudioStreamInternal::getFramesPerBurst() const |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 644 | { |
| 645 | return mEndpointDescriptor.downDataQueueDescriptor.framesPerBurst; |
| 646 | } |
| 647 | |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 648 | int64_t AudioStreamInternal::getFramesRead() |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 649 | { |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 650 | int64_t framesRead = |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 651 | mClockModel.convertTimeToPosition(AudioClock::getNanoseconds()) |
| 652 | + mFramesOffsetFromService; |
| 653 | // Prevent retrograde motion. |
| 654 | if (framesRead < mLastFramesRead) { |
| 655 | framesRead = mLastFramesRead; |
| 656 | } else { |
| 657 | mLastFramesRead = framesRead; |
| 658 | } |
| 659 | ALOGD("AudioStreamInternal::getFramesRead() returns %lld", (long long)framesRead); |
| 660 | return framesRead; |
| 661 | } |
| 662 | |
| 663 | // TODO implement getTimestamp |