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