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