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 | |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 44 | using android::String16; |
Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 45 | using android::Mutex; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 46 | using android::WrappingBuffer; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 47 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 48 | using namespace aaudio; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 49 | |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 50 | #define MIN_TIMEOUT_NANOS (1000 * AAUDIO_NANOS_PER_MILLISECOND) |
| 51 | |
| 52 | // Wait at least this many times longer than the operation should take. |
| 53 | #define MIN_TIMEOUT_OPERATIONS 4 |
| 54 | |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 55 | //static int64_t s_logCounter = 0; |
| 56 | //#define MYLOG_CONDITION (mInService == true && s_logCounter++ < 500) |
| 57 | //#define MYLOG_CONDITION (s_logCounter++ < 500000) |
| 58 | #define MYLOG_CONDITION (1) |
Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 59 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 60 | #define LOG_TIMESTAMPS 0 |
| 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 | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 143 | mFramesPerBurst = mEndpointDescriptor.dataQueueDescriptor.framesPerBurst; |
| 144 | int32_t capacity = mEndpointDescriptor.dataQueueDescriptor.capacityInFrames; |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 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(); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 196 | if (isActive()) { |
Phil Burk | 4485d41 | 2017-05-09 15:55:02 -0700 | [diff] [blame] | 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 | static void *aaudio_callback_thread_proc(void *context) |
| 221 | { |
| 222 | AudioStreamInternal *stream = (AudioStreamInternal *)context; |
Phil Burk | 677d791 | 2017-04-07 12:17:12 -0700 | [diff] [blame] | 223 | //LOGD("AudioStreamInternal(): oboe_callback_thread, stream = %p", stream); |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 224 | if (stream != NULL) { |
| 225 | return stream->callbackLoop(); |
| 226 | } else { |
| 227 | return NULL; |
| 228 | } |
| 229 | } |
| 230 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 231 | aaudio_result_t AudioStreamInternal::requestStart() |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 232 | { |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 233 | int64_t startTime; |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 234 | ALOGD_IF(MYLOG_CONDITION, "AudioStreamInternal(): start()"); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 235 | if (mServiceStreamHandle == AAUDIO_HANDLE_INVALID) { |
| 236 | return AAUDIO_ERROR_INVALID_STATE; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 237 | } |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 238 | |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 239 | startTime = AudioClock::getNanoseconds(); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 240 | mClockModel.start(startTime); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 241 | setState(AAUDIO_STREAM_STATE_STARTING); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 242 | aaudio_result_t result = mServiceInterface.startStream(mServiceStreamHandle);; |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 243 | |
| 244 | if (result == AAUDIO_OK && getDataCallbackProc() != nullptr) { |
| 245 | // Launch the callback loop thread. |
| 246 | int64_t periodNanos = mCallbackFrames |
| 247 | * AAUDIO_NANOS_PER_SECOND |
| 248 | / getSampleRate(); |
| 249 | mCallbackEnabled.store(true); |
| 250 | result = createThread(periodNanos, aaudio_callback_thread_proc, this); |
| 251 | } |
| 252 | return result; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 253 | } |
| 254 | |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 255 | int64_t AudioStreamInternal::calculateReasonableTimeout(int32_t framesPerOperation) { |
| 256 | |
| 257 | // 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] | 258 | int64_t timeoutNanoseconds = (MIN_TIMEOUT_OPERATIONS |
| 259 | * framesPerOperation |
| 260 | * AAUDIO_NANOS_PER_SECOND) |
| 261 | / getSampleRate(); |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 262 | if (timeoutNanoseconds < MIN_TIMEOUT_NANOS) { // arbitrary number of seconds |
| 263 | timeoutNanoseconds = MIN_TIMEOUT_NANOS; |
| 264 | } |
| 265 | return timeoutNanoseconds; |
| 266 | } |
| 267 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 268 | int64_t AudioStreamInternal::calculateReasonableTimeout() { |
| 269 | return calculateReasonableTimeout(getFramesPerBurst()); |
| 270 | } |
| 271 | |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 272 | aaudio_result_t AudioStreamInternal::stopCallback() |
| 273 | { |
| 274 | if (isDataCallbackActive()) { |
| 275 | mCallbackEnabled.store(false); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 276 | return joinThread(NULL); |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 277 | } else { |
| 278 | return AAUDIO_OK; |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | aaudio_result_t AudioStreamInternal::requestPauseInternal() |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 283 | { |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 284 | if (mServiceStreamHandle == AAUDIO_HANDLE_INVALID) { |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 285 | ALOGE("AudioStreamInternal(): requestPauseInternal() mServiceStreamHandle invalid = 0x%08X", |
| 286 | mServiceStreamHandle); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 287 | return AAUDIO_ERROR_INVALID_STATE; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 288 | } |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 289 | |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 290 | mClockModel.stop(AudioClock::getNanoseconds()); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 291 | setState(AAUDIO_STREAM_STATE_PAUSING); |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 292 | return mServiceInterface.pauseStream(mServiceStreamHandle); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 293 | } |
| 294 | |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 295 | aaudio_result_t AudioStreamInternal::requestPause() |
| 296 | { |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 297 | ALOGD_IF(MYLOG_CONDITION, "AudioStreamInternal(): %s requestPause()", getLocationName()); |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 298 | aaudio_result_t result = stopCallback(); |
| 299 | if (result != AAUDIO_OK) { |
| 300 | return result; |
| 301 | } |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 302 | result = requestPauseInternal(); |
| 303 | ALOGD("AudioStreamInternal(): requestPause() returns %d", result); |
| 304 | return result; |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 305 | } |
| 306 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 307 | aaudio_result_t AudioStreamInternal::requestFlush() { |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 308 | ALOGD_IF(MYLOG_CONDITION, "AudioStreamInternal(): requestFlush()"); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 309 | if (mServiceStreamHandle == AAUDIO_HANDLE_INVALID) { |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 310 | ALOGE("AudioStreamInternal(): requestFlush() mServiceStreamHandle invalid = 0x%08X", |
| 311 | mServiceStreamHandle); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 312 | return AAUDIO_ERROR_INVALID_STATE; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 313 | } |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 314 | |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 315 | setState(AAUDIO_STREAM_STATE_FLUSHING); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 316 | return mServiceInterface.flushStream(mServiceStreamHandle); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 317 | } |
| 318 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 319 | // TODO for Play only |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 320 | void AudioStreamInternal::onFlushFromServer() { |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 321 | ALOGD_IF(MYLOG_CONDITION, "AudioStreamInternal(): onFlushFromServer()"); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 322 | int64_t readCounter = mAudioEndpoint.getDataReadCounter(); |
| 323 | int64_t writeCounter = mAudioEndpoint.getDataWriteCounter(); |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 324 | |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 325 | // Bump offset so caller does not see the retrograde motion in getFramesRead(). |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 326 | int64_t framesFlushed = writeCounter - readCounter; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 327 | mFramesOffsetFromService += framesFlushed; |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 328 | |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 329 | // Flush written frames by forcing writeCounter to readCounter. |
| 330 | // This is because we cannot move the read counter in the hardware. |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 331 | mAudioEndpoint.setDataWriteCounter(readCounter); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 332 | } |
| 333 | |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 334 | aaudio_result_t AudioStreamInternal::requestStopInternal() |
| 335 | { |
| 336 | if (mServiceStreamHandle == AAUDIO_HANDLE_INVALID) { |
| 337 | ALOGE("AudioStreamInternal(): requestStopInternal() mServiceStreamHandle invalid = 0x%08X", |
| 338 | mServiceStreamHandle); |
| 339 | return AAUDIO_ERROR_INVALID_STATE; |
| 340 | } |
| 341 | |
| 342 | mClockModel.stop(AudioClock::getNanoseconds()); |
| 343 | setState(AAUDIO_STREAM_STATE_STOPPING); |
| 344 | return mServiceInterface.stopStream(mServiceStreamHandle); |
| 345 | } |
| 346 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 347 | aaudio_result_t AudioStreamInternal::requestStop() |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 348 | { |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 349 | ALOGD_IF(MYLOG_CONDITION, "AudioStreamInternal(): %s requestStop()", getLocationName()); |
| 350 | aaudio_result_t result = stopCallback(); |
| 351 | if (result != AAUDIO_OK) { |
| 352 | return result; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 353 | } |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 354 | result = requestStopInternal(); |
| 355 | ALOGD("AudioStreamInternal(): requestStop() returns %d", result); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 356 | return result; |
| 357 | } |
| 358 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 359 | aaudio_result_t AudioStreamInternal::registerThread() { |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 360 | if (mServiceStreamHandle == AAUDIO_HANDLE_INVALID) { |
| 361 | return AAUDIO_ERROR_INVALID_STATE; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 362 | } |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 363 | return mServiceInterface.registerAudioThread(mServiceStreamHandle, |
| 364 | getpid(), |
| 365 | gettid(), |
| 366 | getPeriodNanoseconds()); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 367 | } |
| 368 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 369 | aaudio_result_t AudioStreamInternal::unregisterThread() { |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 370 | if (mServiceStreamHandle == AAUDIO_HANDLE_INVALID) { |
| 371 | return AAUDIO_ERROR_INVALID_STATE; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 372 | } |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 373 | return mServiceInterface.unregisterAudioThread(mServiceStreamHandle, getpid(), gettid()); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 374 | } |
| 375 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 376 | aaudio_result_t AudioStreamInternal::getTimestamp(clockid_t clockId, |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 377 | int64_t *framePosition, |
| 378 | int64_t *timeNanoseconds) { |
Phil Burk | 5204d31 | 2017-05-04 17:16:13 -0700 | [diff] [blame] | 379 | // TODO Generate in server and pass to client. Return latest. |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 380 | int64_t time = AudioClock::getNanoseconds(); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 381 | *framePosition = mClockModel.convertTimeToPosition(time); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 382 | // TODO Get a more accurate timestamp from the service. This code just adds a fudge factor. |
| 383 | *timeNanoseconds = time + (6 * AAUDIO_NANOS_PER_MILLISECOND); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 384 | return AAUDIO_OK; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 385 | } |
| 386 | |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 387 | aaudio_result_t AudioStreamInternal::updateStateWhileWaiting() { |
| 388 | if (isDataCallbackActive()) { |
| 389 | return AAUDIO_OK; // state is getting updated by the callback thread read/write call |
| 390 | } |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 391 | return processCommands(); |
| 392 | } |
| 393 | |
| 394 | #if LOG_TIMESTAMPS |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 395 | static void AudioStreamInternal_logTimestamp(AAudioServiceMessage &command) { |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 396 | static int64_t oldPosition = 0; |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 397 | static int64_t oldTime = 0; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 398 | int64_t framePosition = command.timestamp.position; |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 399 | int64_t nanoTime = command.timestamp.timestamp; |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 400 | ALOGD_IF(MYLOG_CONDITION, "AudioStreamInternal() timestamp says framePosition = %08lld at nanoTime %llu", |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 401 | (long long) framePosition, |
| 402 | (long long) nanoTime); |
| 403 | int64_t nanosDelta = nanoTime - oldTime; |
| 404 | if (nanosDelta > 0 && oldTime > 0) { |
| 405 | int64_t framesDelta = framePosition - oldPosition; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 406 | int64_t rate = (framesDelta * AAUDIO_NANOS_PER_SECOND) / nanosDelta; |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 407 | ALOGD_IF(MYLOG_CONDITION, "AudioStreamInternal() - framesDelta = %08lld", (long long) framesDelta); |
| 408 | ALOGD_IF(MYLOG_CONDITION, "AudioStreamInternal() - nanosDelta = %08lld", (long long) nanosDelta); |
| 409 | ALOGD_IF(MYLOG_CONDITION, "AudioStreamInternal() - measured rate = %llu", (unsigned long long) rate); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 410 | } |
| 411 | oldPosition = framePosition; |
| 412 | oldTime = nanoTime; |
| 413 | } |
| 414 | #endif |
| 415 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 416 | aaudio_result_t AudioStreamInternal::onTimestampFromServer(AAudioServiceMessage *message) { |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 417 | #if LOG_TIMESTAMPS |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 418 | AudioStreamInternal_logTimestamp(*message); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 419 | #endif |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 420 | processTimestamp(message->timestamp.position, message->timestamp.timestamp); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 421 | return AAUDIO_OK; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 422 | } |
| 423 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 424 | aaudio_result_t AudioStreamInternal::onEventFromServer(AAudioServiceMessage *message) { |
| 425 | aaudio_result_t result = AAUDIO_OK; |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 426 | ALOGD_IF(MYLOG_CONDITION, "processCommands() got event %d", message->event.event); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 427 | switch (message->event.event) { |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 428 | case AAUDIO_SERVICE_EVENT_STARTED: |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 429 | ALOGD_IF(MYLOG_CONDITION, "processCommands() got AAUDIO_SERVICE_EVENT_STARTED"); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 430 | if (getState() == AAUDIO_STREAM_STATE_STARTING) { |
| 431 | setState(AAUDIO_STREAM_STATE_STARTED); |
| 432 | } |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 433 | break; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 434 | case AAUDIO_SERVICE_EVENT_PAUSED: |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 435 | ALOGD_IF(MYLOG_CONDITION, "processCommands() got AAUDIO_SERVICE_EVENT_PAUSED"); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 436 | if (getState() == AAUDIO_STREAM_STATE_PAUSING) { |
| 437 | setState(AAUDIO_STREAM_STATE_PAUSED); |
| 438 | } |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 439 | break; |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 440 | case AAUDIO_SERVICE_EVENT_STOPPED: |
| 441 | ALOGD_IF(MYLOG_CONDITION, "processCommands() got AAUDIO_SERVICE_EVENT_STOPPED"); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 442 | if (getState() == AAUDIO_STREAM_STATE_STOPPING) { |
| 443 | setState(AAUDIO_STREAM_STATE_STOPPED); |
| 444 | } |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 445 | break; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 446 | case AAUDIO_SERVICE_EVENT_FLUSHED: |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 447 | ALOGD_IF(MYLOG_CONDITION, "processCommands() got AAUDIO_SERVICE_EVENT_FLUSHED"); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 448 | if (getState() == AAUDIO_STREAM_STATE_FLUSHING) { |
| 449 | setState(AAUDIO_STREAM_STATE_FLUSHED); |
| 450 | onFlushFromServer(); |
| 451 | } |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 452 | break; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 453 | case AAUDIO_SERVICE_EVENT_CLOSED: |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 454 | ALOGD_IF(MYLOG_CONDITION, "processCommands() got AAUDIO_SERVICE_EVENT_CLOSED"); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 455 | setState(AAUDIO_STREAM_STATE_CLOSED); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 456 | break; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 457 | case AAUDIO_SERVICE_EVENT_DISCONNECTED: |
| 458 | result = AAUDIO_ERROR_DISCONNECTED; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 459 | setState(AAUDIO_STREAM_STATE_DISCONNECTED); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 460 | ALOGW("WARNING - processCommands() AAUDIO_SERVICE_EVENT_DISCONNECTED"); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 461 | break; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 462 | case AAUDIO_SERVICE_EVENT_VOLUME: |
Phil Burk | e572f46 | 2017-04-20 13:03:19 -0700 | [diff] [blame] | 463 | mVolumeRamp.setTarget((float) message->event.dataDouble); |
| 464 | ALOGD_IF(MYLOG_CONDITION, "processCommands() AAUDIO_SERVICE_EVENT_VOLUME %f", |
| 465 | message->event.dataDouble); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 466 | break; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 467 | default: |
| 468 | ALOGW("WARNING - processCommands() Unrecognized event = %d", |
| 469 | (int) message->event.event); |
| 470 | break; |
| 471 | } |
| 472 | return result; |
| 473 | } |
| 474 | |
| 475 | // Process all the commands coming from the server. |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 476 | aaudio_result_t AudioStreamInternal::processCommands() { |
| 477 | aaudio_result_t result = AAUDIO_OK; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 478 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 479 | while (result == AAUDIO_OK) { |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 480 | //ALOGD_IF(MYLOG_CONDITION, "AudioStreamInternal::processCommands() - looping, %d", result); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 481 | AAudioServiceMessage message; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 482 | if (mAudioEndpoint.readUpCommand(&message) != 1) { |
| 483 | break; // no command this time, no problem |
| 484 | } |
| 485 | switch (message.what) { |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 486 | case AAudioServiceMessage::code::TIMESTAMP: |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 487 | result = onTimestampFromServer(&message); |
| 488 | break; |
| 489 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 490 | case AAudioServiceMessage::code::EVENT: |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 491 | result = onEventFromServer(&message); |
| 492 | break; |
| 493 | |
| 494 | default: |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 495 | ALOGE("WARNING - AudioStreamInternal::processCommands() Unrecognized what = %d", |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 496 | (int) message.what); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 497 | result = AAUDIO_ERROR_UNEXPECTED_VALUE; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 498 | break; |
| 499 | } |
| 500 | } |
| 501 | return result; |
| 502 | } |
| 503 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 504 | // Read or write the data, block if needed and timeoutMillis > 0 |
| 505 | aaudio_result_t AudioStreamInternal::processData(void *buffer, int32_t numFrames, |
| 506 | int64_t timeoutNanoseconds) |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 507 | { |
Phil Burk | 4485d41 | 2017-05-09 15:55:02 -0700 | [diff] [blame] | 508 | const char * traceName = (mInService) ? "aaWrtS" : "aaWrtC"; |
| 509 | ATRACE_BEGIN(traceName); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 510 | aaudio_result_t result = AAUDIO_OK; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 511 | int32_t loopCount = 0; |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 512 | uint8_t* audioData = (uint8_t*)buffer; |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 513 | int64_t currentTimeNanos = AudioClock::getNanoseconds(); |
| 514 | int64_t deadlineNanos = currentTimeNanos + timeoutNanoseconds; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 515 | int32_t framesLeft = numFrames; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 516 | |
Phil Burk | 4485d41 | 2017-05-09 15:55:02 -0700 | [diff] [blame] | 517 | int32_t fullFrames = mAudioEndpoint.getFullFramesAvailable(); |
| 518 | if (ATRACE_ENABLED()) { |
| 519 | const char * traceName = (mInService) ? "aaFullS" : "aaFullC"; |
| 520 | ATRACE_INT(traceName, fullFrames); |
| 521 | } |
| 522 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 523 | // Loop until all the data has been processed or until a timeout occurs. |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 524 | while (framesLeft > 0) { |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 525 | // The call to processDataNow() will not block. It will just read as much as it can. |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 526 | int64_t wakeTimeNanos = 0; |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 527 | aaudio_result_t framesProcessed = processDataNow(audioData, framesLeft, |
| 528 | currentTimeNanos, &wakeTimeNanos); |
| 529 | if (framesProcessed < 0) { |
| 530 | ALOGE("AudioStreamInternal::processData() loop: framesProcessed = %d", framesProcessed); |
| 531 | result = framesProcessed; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 532 | break; |
| 533 | } |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 534 | framesLeft -= (int32_t) framesProcessed; |
| 535 | audioData += framesProcessed * getBytesPerFrame(); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 536 | |
| 537 | // Should we block? |
| 538 | if (timeoutNanoseconds == 0) { |
| 539 | break; // don't block |
| 540 | } else if (framesLeft > 0) { |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 541 | // clip the wake time to something reasonable |
| 542 | if (wakeTimeNanos < currentTimeNanos) { |
| 543 | wakeTimeNanos = currentTimeNanos; |
| 544 | } |
| 545 | if (wakeTimeNanos > deadlineNanos) { |
| 546 | // If we time out, just return the framesWritten so far. |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 547 | ALOGE("AudioStreamInternal::processData(): timed out after %lld nanos", |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 548 | (long long) timeoutNanoseconds); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 549 | ALOGE("AudioStreamInternal::processData(): wakeTime = %lld, deadline = %lld nanos", |
| 550 | (long long) wakeTimeNanos, (long long) deadlineNanos); |
| 551 | ALOGE("AudioStreamInternal::processData(): past deadline by %d micros", |
| 552 | (int)((wakeTimeNanos - deadlineNanos) / AAUDIO_NANOS_PER_MICROSECOND)); |
| 553 | |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 554 | break; |
| 555 | } |
| 556 | |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 557 | int64_t sleepForNanos = wakeTimeNanos - currentTimeNanos; |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 558 | //ALOGE("AudioStreamInternal::processData(): sleep for %d micros", |
| 559 | // (int)(sleepForNanos / AAUDIO_NANOS_PER_MICROSECOND)); |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 560 | AudioClock::sleepForNanos(sleepForNanos); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 561 | currentTimeNanos = AudioClock::getNanoseconds(); |
| 562 | } |
| 563 | } |
| 564 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 565 | // return error or framesProcessed |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 566 | (void) loopCount; |
Phil Burk | 4485d41 | 2017-05-09 15:55:02 -0700 | [diff] [blame] | 567 | ATRACE_END(); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 568 | return (result < 0) ? result : numFrames - framesLeft; |
| 569 | } |
| 570 | |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 571 | void AudioStreamInternal::processTimestamp(uint64_t position, int64_t time) { |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 572 | mClockModel.processTimestamp(position, time); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 573 | } |
| 574 | |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 575 | aaudio_result_t AudioStreamInternal::setBufferSize(int32_t requestedFrames) { |
| 576 | int32_t actualFrames = 0; |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 577 | // Round to the next highest burst size. |
| 578 | if (getFramesPerBurst() > 0) { |
| 579 | int32_t numBursts = (requestedFrames + getFramesPerBurst() - 1) / getFramesPerBurst(); |
| 580 | requestedFrames = numBursts * getFramesPerBurst(); |
| 581 | } |
| 582 | |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 583 | aaudio_result_t result = mAudioEndpoint.setBufferSizeInFrames(requestedFrames, &actualFrames); |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 584 | ALOGD_IF(MYLOG_CONDITION, "AudioStreamInternal::setBufferSize() %s req = %d => %d", |
| 585 | getLocationName(), requestedFrames, actualFrames); |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 586 | if (result < 0) { |
| 587 | return result; |
| 588 | } else { |
| 589 | return (aaudio_result_t) actualFrames; |
| 590 | } |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 591 | } |
| 592 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 593 | int32_t AudioStreamInternal::getBufferSize() const { |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 594 | return mAudioEndpoint.getBufferSizeInFrames(); |
| 595 | } |
| 596 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 597 | int32_t AudioStreamInternal::getBufferCapacity() const { |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 598 | return mAudioEndpoint.getBufferCapacityInFrames(); |
| 599 | } |
| 600 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 601 | int32_t AudioStreamInternal::getFramesPerBurst() const { |
| 602 | return mEndpointDescriptor.dataQueueDescriptor.framesPerBurst; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 603 | } |
| 604 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 605 | aaudio_result_t AudioStreamInternal::joinThread(void** returnArg) { |
| 606 | return AudioStream::joinThread(returnArg, calculateReasonableTimeout(getFramesPerBurst())); |
Phil Burk | 4c5129b | 2017-04-28 15:17:32 -0700 | [diff] [blame] | 607 | } |