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