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 | cf5f6d2 | 2017-05-26 12:35:07 -0700 | [diff] [blame] | 17 | // This file is used in both client and server processes. |
| 18 | // This is needed to make sense of the logs more easily. |
| 19 | #define LOG_TAG (mInService ? "AAudioService" : "AAudio") |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 20 | //#define LOG_NDEBUG 0 |
| 21 | #include <utils/Log.h> |
| 22 | |
Phil Burk | 4485d41 | 2017-05-09 15:55:02 -0700 | [diff] [blame] | 23 | #define ATRACE_TAG ATRACE_TAG_AUDIO |
| 24 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 25 | #include <stdint.h> |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 26 | #include <assert.h> |
| 27 | |
| 28 | #include <binder/IServiceManager.h> |
| 29 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 30 | #include <aaudio/AAudio.h> |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 31 | #include <utils/String16.h> |
Phil Burk | 4485d41 | 2017-05-09 15:55:02 -0700 | [diff] [blame] | 32 | #include <utils/Trace.h> |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 33 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 34 | #include "AudioClock.h" |
| 35 | #include "AudioEndpointParcelable.h" |
| 36 | #include "binding/AAudioStreamRequest.h" |
| 37 | #include "binding/AAudioStreamConfiguration.h" |
| 38 | #include "binding/IAAudioService.h" |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 39 | #include "binding/AAudioServiceMessage.h" |
Phil Burk | 3df348f | 2017-02-08 11:41:55 -0800 | [diff] [blame] | 40 | #include "core/AudioStreamBuilder.h" |
Phil Burk | e572f46 | 2017-04-20 13:03:19 -0700 | [diff] [blame] | 41 | #include "fifo/FifoBuffer.h" |
| 42 | #include "utility/LinearRamp.h" |
| 43 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 44 | #include "AudioStreamInternal.h" |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 45 | |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 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 | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 57 | #define LOG_TIMESTAMPS 0 |
| 58 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 59 | AudioStreamInternal::AudioStreamInternal(AAudioServiceInterface &serviceInterface, bool inService) |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 60 | : AudioStream() |
| 61 | , mClockModel() |
| 62 | , mAudioEndpoint() |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 63 | , mServiceStreamHandle(AAUDIO_HANDLE_INVALID) |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 64 | , mFramesPerBurst(16) |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 65 | , mServiceInterface(serviceInterface) |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 66 | , mInService(inService) { |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | AudioStreamInternal::~AudioStreamInternal() { |
| 70 | } |
| 71 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 72 | aaudio_result_t AudioStreamInternal::open(const AudioStreamBuilder &builder) { |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 73 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 74 | aaudio_result_t result = AAUDIO_OK; |
| 75 | AAudioStreamRequest request; |
| 76 | AAudioStreamConfiguration configuration; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 77 | |
| 78 | result = AudioStream::open(builder); |
| 79 | if (result < 0) { |
| 80 | return result; |
| 81 | } |
| 82 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 83 | // We have to do volume scaling. So we prefer FLOAT format. |
Glenn Kasten | 37a466a | 2017-05-30 15:53:14 -0700 | [diff] [blame] | 84 | if (getFormat() == AAUDIO_FORMAT_UNSPECIFIED) { |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 85 | setFormat(AAUDIO_FORMAT_PCM_FLOAT); |
| 86 | } |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 87 | // Request FLOAT for the shared mixer. |
| 88 | request.getConfiguration().setAudioFormat(AAUDIO_FORMAT_PCM_FLOAT); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 89 | |
Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 90 | // Build the request to send to the server. |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 91 | request.setUserId(getuid()); |
| 92 | request.setProcessId(getpid()); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 93 | request.setDirection(getDirection()); |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 94 | request.setSharingModeMatchRequired(isSharingModeMatchRequired()); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 95 | |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 96 | request.getConfiguration().setDeviceId(getDeviceId()); |
| 97 | request.getConfiguration().setSampleRate(getSampleRate()); |
| 98 | request.getConfiguration().setSamplesPerFrame(getSamplesPerFrame()); |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 99 | request.getConfiguration().setSharingMode(getSharingMode()); |
| 100 | |
Phil Burk | 3df348f | 2017-02-08 11:41:55 -0800 | [diff] [blame] | 101 | request.getConfiguration().setBufferCapacity(builder.getBufferCapacity()); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 102 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 103 | mServiceStreamHandle = mServiceInterface.openStream(request, configuration); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 104 | if (mServiceStreamHandle < 0) { |
| 105 | result = mServiceStreamHandle; |
Phil Burk | cf5f6d2 | 2017-05-26 12:35:07 -0700 | [diff] [blame] | 106 | ALOGE("AudioStreamInternal.open(): openStream() returned %d", result); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 107 | } else { |
| 108 | result = configuration.validate(); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 109 | if (result != AAUDIO_OK) { |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 110 | close(); |
| 111 | return result; |
| 112 | } |
| 113 | // Save results of the open. |
| 114 | setSampleRate(configuration.getSampleRate()); |
| 115 | setSamplesPerFrame(configuration.getSamplesPerFrame()); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 116 | setDeviceId(configuration.getDeviceId()); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 117 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 118 | // Save device format so we can do format conversion and volume scaling together. |
| 119 | mDeviceFormat = configuration.getAudioFormat(); |
| 120 | |
| 121 | result = mServiceInterface.getStreamDescription(mServiceStreamHandle, mEndPointParcelable); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 122 | if (result != AAUDIO_OK) { |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 123 | mServiceInterface.closeStream(mServiceStreamHandle); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 124 | return result; |
| 125 | } |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 126 | |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 127 | // resolve parcelable into a descriptor |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 128 | result = mEndPointParcelable.resolve(&mEndpointDescriptor); |
| 129 | if (result != AAUDIO_OK) { |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 130 | mServiceInterface.closeStream(mServiceStreamHandle); |
| 131 | return result; |
| 132 | } |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 133 | |
| 134 | // Configure endpoint based on descriptor. |
| 135 | mAudioEndpoint.configure(&mEndpointDescriptor); |
| 136 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 137 | mFramesPerBurst = mEndpointDescriptor.dataQueueDescriptor.framesPerBurst; |
| 138 | int32_t capacity = mEndpointDescriptor.dataQueueDescriptor.capacityInFrames; |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 139 | |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 140 | // Validate result from server. |
| 141 | if (mFramesPerBurst < 16 || mFramesPerBurst > 16 * 1024) { |
| 142 | ALOGE("AudioStream::open(): framesPerBurst out of range = %d", mFramesPerBurst); |
| 143 | return AAUDIO_ERROR_OUT_OF_RANGE; |
| 144 | } |
| 145 | if (capacity < mFramesPerBurst || capacity > 32 * 1024) { |
| 146 | ALOGE("AudioStream::open(): bufferCapacity out of range = %d", capacity); |
| 147 | return AAUDIO_ERROR_OUT_OF_RANGE; |
| 148 | } |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 149 | |
| 150 | mClockModel.setSampleRate(getSampleRate()); |
| 151 | mClockModel.setFramesPerBurst(mFramesPerBurst); |
| 152 | |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 153 | if (getDataCallbackProc()) { |
| 154 | mCallbackFrames = builder.getFramesPerDataCallback(); |
| 155 | if (mCallbackFrames > getBufferCapacity() / 2) { |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 156 | ALOGE("AudioStreamInternal.open(): framesPerCallback too large = %d, capacity = %d", |
| 157 | mCallbackFrames, getBufferCapacity()); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 158 | mServiceInterface.closeStream(mServiceStreamHandle); |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 159 | return AAUDIO_ERROR_OUT_OF_RANGE; |
| 160 | |
| 161 | } else if (mCallbackFrames < 0) { |
| 162 | ALOGE("AudioStreamInternal.open(): framesPerCallback negative"); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 163 | mServiceInterface.closeStream(mServiceStreamHandle); |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 164 | return AAUDIO_ERROR_OUT_OF_RANGE; |
| 165 | |
| 166 | } |
| 167 | if (mCallbackFrames == AAUDIO_UNSPECIFIED) { |
| 168 | mCallbackFrames = mFramesPerBurst; |
| 169 | } |
| 170 | |
| 171 | int32_t bytesPerFrame = getSamplesPerFrame() |
| 172 | * AAudioConvert_formatToSizeInBytes(getFormat()); |
| 173 | int32_t callbackBufferSize = mCallbackFrames * bytesPerFrame; |
| 174 | mCallbackBuffer = new uint8_t[callbackBufferSize]; |
| 175 | } |
| 176 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 177 | setState(AAUDIO_STREAM_STATE_OPEN); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 178 | } |
| 179 | return result; |
| 180 | } |
| 181 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 182 | aaudio_result_t AudioStreamInternal::close() { |
Phil Burk | cf5f6d2 | 2017-05-26 12:35:07 -0700 | [diff] [blame] | 183 | ALOGD("AudioStreamInternal.close(): mServiceStreamHandle = 0x%08X", |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 184 | mServiceStreamHandle); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 185 | if (mServiceStreamHandle != AAUDIO_HANDLE_INVALID) { |
Phil Burk | 4485d41 | 2017-05-09 15:55:02 -0700 | [diff] [blame] | 186 | // Don't close a stream while it is running. |
| 187 | aaudio_stream_state_t currentState = getState(); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 188 | if (isActive()) { |
Phil Burk | 4485d41 | 2017-05-09 15:55:02 -0700 | [diff] [blame] | 189 | requestStop(); |
| 190 | aaudio_stream_state_t nextState; |
| 191 | int64_t timeoutNanoseconds = MIN_TIMEOUT_NANOS; |
| 192 | aaudio_result_t result = waitForStateChange(currentState, &nextState, |
| 193 | timeoutNanoseconds); |
| 194 | if (result != AAUDIO_OK) { |
| 195 | ALOGE("AudioStreamInternal::close() waitForStateChange() returned %d %s", |
| 196 | result, AAudio_convertResultToText(result)); |
| 197 | } |
| 198 | } |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 199 | aaudio_handle_t serviceStreamHandle = mServiceStreamHandle; |
| 200 | mServiceStreamHandle = AAUDIO_HANDLE_INVALID; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 201 | |
| 202 | mServiceInterface.closeStream(serviceStreamHandle); |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 203 | delete[] mCallbackBuffer; |
Phil Burk | 4485d41 | 2017-05-09 15:55:02 -0700 | [diff] [blame] | 204 | mCallbackBuffer = nullptr; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 205 | return mEndPointParcelable.close(); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 206 | } else { |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 207 | return AAUDIO_ERROR_INVALID_HANDLE; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 208 | } |
| 209 | } |
| 210 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 211 | |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 212 | static void *aaudio_callback_thread_proc(void *context) |
| 213 | { |
| 214 | AudioStreamInternal *stream = (AudioStreamInternal *)context; |
Phil Burk | 677d791 | 2017-04-07 12:17:12 -0700 | [diff] [blame] | 215 | //LOGD("AudioStreamInternal(): oboe_callback_thread, stream = %p", stream); |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 216 | if (stream != NULL) { |
| 217 | return stream->callbackLoop(); |
| 218 | } else { |
| 219 | return NULL; |
| 220 | } |
| 221 | } |
| 222 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 223 | aaudio_result_t AudioStreamInternal::requestStart() |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 224 | { |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 225 | int64_t startTime; |
Phil Burk | cf5f6d2 | 2017-05-26 12:35:07 -0700 | [diff] [blame] | 226 | ALOGD("AudioStreamInternal(): start()"); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 227 | if (mServiceStreamHandle == AAUDIO_HANDLE_INVALID) { |
| 228 | return AAUDIO_ERROR_INVALID_STATE; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 229 | } |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 230 | |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 231 | startTime = AudioClock::getNanoseconds(); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 232 | mClockModel.start(startTime); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 233 | setState(AAUDIO_STREAM_STATE_STARTING); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 234 | aaudio_result_t result = mServiceInterface.startStream(mServiceStreamHandle);; |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 235 | |
| 236 | if (result == AAUDIO_OK && getDataCallbackProc() != nullptr) { |
| 237 | // Launch the callback loop thread. |
| 238 | int64_t periodNanos = mCallbackFrames |
| 239 | * AAUDIO_NANOS_PER_SECOND |
| 240 | / getSampleRate(); |
| 241 | mCallbackEnabled.store(true); |
| 242 | result = createThread(periodNanos, aaudio_callback_thread_proc, this); |
| 243 | } |
| 244 | return result; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 245 | } |
| 246 | |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 247 | int64_t AudioStreamInternal::calculateReasonableTimeout(int32_t framesPerOperation) { |
| 248 | |
| 249 | // 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] | 250 | int64_t timeoutNanoseconds = (MIN_TIMEOUT_OPERATIONS |
| 251 | * framesPerOperation |
| 252 | * AAUDIO_NANOS_PER_SECOND) |
| 253 | / getSampleRate(); |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 254 | if (timeoutNanoseconds < MIN_TIMEOUT_NANOS) { // arbitrary number of seconds |
| 255 | timeoutNanoseconds = MIN_TIMEOUT_NANOS; |
| 256 | } |
| 257 | return timeoutNanoseconds; |
| 258 | } |
| 259 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 260 | int64_t AudioStreamInternal::calculateReasonableTimeout() { |
| 261 | return calculateReasonableTimeout(getFramesPerBurst()); |
| 262 | } |
| 263 | |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 264 | aaudio_result_t AudioStreamInternal::stopCallback() |
| 265 | { |
| 266 | if (isDataCallbackActive()) { |
| 267 | mCallbackEnabled.store(false); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 268 | return joinThread(NULL); |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 269 | } else { |
| 270 | return AAUDIO_OK; |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | aaudio_result_t AudioStreamInternal::requestPauseInternal() |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 275 | { |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 276 | if (mServiceStreamHandle == AAUDIO_HANDLE_INVALID) { |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 277 | ALOGE("AudioStreamInternal(): requestPauseInternal() mServiceStreamHandle invalid = 0x%08X", |
| 278 | mServiceStreamHandle); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 279 | return AAUDIO_ERROR_INVALID_STATE; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 280 | } |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 281 | |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 282 | mClockModel.stop(AudioClock::getNanoseconds()); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 283 | setState(AAUDIO_STREAM_STATE_PAUSING); |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 284 | return mServiceInterface.pauseStream(mServiceStreamHandle); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 285 | } |
| 286 | |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 287 | aaudio_result_t AudioStreamInternal::requestPause() |
| 288 | { |
| 289 | aaudio_result_t result = stopCallback(); |
| 290 | if (result != AAUDIO_OK) { |
| 291 | return result; |
| 292 | } |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 293 | result = requestPauseInternal(); |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 294 | return result; |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 295 | } |
| 296 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 297 | aaudio_result_t AudioStreamInternal::requestFlush() { |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 298 | if (mServiceStreamHandle == AAUDIO_HANDLE_INVALID) { |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 299 | ALOGE("AudioStreamInternal(): requestFlush() mServiceStreamHandle invalid = 0x%08X", |
| 300 | mServiceStreamHandle); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 301 | return AAUDIO_ERROR_INVALID_STATE; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 302 | } |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 303 | |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 304 | setState(AAUDIO_STREAM_STATE_FLUSHING); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 305 | return mServiceInterface.flushStream(mServiceStreamHandle); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 306 | } |
| 307 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 308 | // TODO for Play only |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 309 | void AudioStreamInternal::onFlushFromServer() { |
Phil Burk | cf5f6d2 | 2017-05-26 12:35:07 -0700 | [diff] [blame] | 310 | ALOGD("AudioStreamInternal(): onFlushFromServer()"); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 311 | int64_t readCounter = mAudioEndpoint.getDataReadCounter(); |
| 312 | int64_t writeCounter = mAudioEndpoint.getDataWriteCounter(); |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 313 | |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 314 | // Bump offset so caller does not see the retrograde motion in getFramesRead(). |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 315 | int64_t framesFlushed = writeCounter - readCounter; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 316 | mFramesOffsetFromService += framesFlushed; |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 317 | |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 318 | // Flush written frames by forcing writeCounter to readCounter. |
| 319 | // This is because we cannot move the read counter in the hardware. |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 320 | mAudioEndpoint.setDataWriteCounter(readCounter); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 321 | } |
| 322 | |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 323 | aaudio_result_t AudioStreamInternal::requestStopInternal() |
| 324 | { |
| 325 | if (mServiceStreamHandle == AAUDIO_HANDLE_INVALID) { |
| 326 | ALOGE("AudioStreamInternal(): requestStopInternal() mServiceStreamHandle invalid = 0x%08X", |
| 327 | mServiceStreamHandle); |
| 328 | return AAUDIO_ERROR_INVALID_STATE; |
| 329 | } |
| 330 | |
| 331 | mClockModel.stop(AudioClock::getNanoseconds()); |
| 332 | setState(AAUDIO_STREAM_STATE_STOPPING); |
| 333 | return mServiceInterface.stopStream(mServiceStreamHandle); |
| 334 | } |
| 335 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 336 | aaudio_result_t AudioStreamInternal::requestStop() |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 337 | { |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 338 | aaudio_result_t result = stopCallback(); |
| 339 | if (result != AAUDIO_OK) { |
| 340 | return result; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 341 | } |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 342 | result = requestStopInternal(); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 343 | return result; |
| 344 | } |
| 345 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 346 | aaudio_result_t AudioStreamInternal::registerThread() { |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 347 | if (mServiceStreamHandle == AAUDIO_HANDLE_INVALID) { |
| 348 | return AAUDIO_ERROR_INVALID_STATE; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 349 | } |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 350 | return mServiceInterface.registerAudioThread(mServiceStreamHandle, |
| 351 | getpid(), |
| 352 | gettid(), |
| 353 | getPeriodNanoseconds()); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 354 | } |
| 355 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 356 | aaudio_result_t AudioStreamInternal::unregisterThread() { |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 357 | if (mServiceStreamHandle == AAUDIO_HANDLE_INVALID) { |
| 358 | return AAUDIO_ERROR_INVALID_STATE; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 359 | } |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 360 | return mServiceInterface.unregisterAudioThread(mServiceStreamHandle, getpid(), gettid()); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 361 | } |
| 362 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 363 | aaudio_result_t AudioStreamInternal::getTimestamp(clockid_t clockId, |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 364 | int64_t *framePosition, |
| 365 | int64_t *timeNanoseconds) { |
Phil Burk | 5204d31 | 2017-05-04 17:16:13 -0700 | [diff] [blame] | 366 | // TODO Generate in server and pass to client. Return latest. |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 367 | int64_t time = AudioClock::getNanoseconds(); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 368 | *framePosition = mClockModel.convertTimeToPosition(time); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 369 | // TODO Get a more accurate timestamp from the service. This code just adds a fudge factor. |
| 370 | *timeNanoseconds = time + (6 * AAUDIO_NANOS_PER_MILLISECOND); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 371 | return AAUDIO_OK; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 372 | } |
| 373 | |
Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 374 | aaudio_result_t AudioStreamInternal::updateStateWhileWaiting() { |
| 375 | if (isDataCallbackActive()) { |
| 376 | return AAUDIO_OK; // state is getting updated by the callback thread read/write call |
| 377 | } |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 378 | return processCommands(); |
| 379 | } |
| 380 | |
| 381 | #if LOG_TIMESTAMPS |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 382 | static void AudioStreamInternal_logTimestamp(AAudioServiceMessage &command) { |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 383 | static int64_t oldPosition = 0; |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 384 | static int64_t oldTime = 0; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 385 | int64_t framePosition = command.timestamp.position; |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 386 | int64_t nanoTime = command.timestamp.timestamp; |
Phil Burk | cf5f6d2 | 2017-05-26 12:35:07 -0700 | [diff] [blame] | 387 | ALOGD("AudioStreamInternal() timestamp says framePosition = %08lld at nanoTime %lld", |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 388 | (long long) framePosition, |
| 389 | (long long) nanoTime); |
| 390 | int64_t nanosDelta = nanoTime - oldTime; |
| 391 | if (nanosDelta > 0 && oldTime > 0) { |
| 392 | int64_t framesDelta = framePosition - oldPosition; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 393 | int64_t rate = (framesDelta * AAUDIO_NANOS_PER_SECOND) / nanosDelta; |
Phil Burk | cf5f6d2 | 2017-05-26 12:35:07 -0700 | [diff] [blame] | 394 | ALOGD("AudioStreamInternal() - framesDelta = %08lld", (long long) framesDelta); |
| 395 | ALOGD("AudioStreamInternal() - nanosDelta = %08lld", (long long) nanosDelta); |
| 396 | ALOGD("AudioStreamInternal() - measured rate = %lld", (long long) rate); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 397 | } |
| 398 | oldPosition = framePosition; |
| 399 | oldTime = nanoTime; |
| 400 | } |
| 401 | #endif |
| 402 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 403 | aaudio_result_t AudioStreamInternal::onTimestampFromServer(AAudioServiceMessage *message) { |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 404 | #if LOG_TIMESTAMPS |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 405 | AudioStreamInternal_logTimestamp(*message); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 406 | #endif |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 407 | processTimestamp(message->timestamp.position, message->timestamp.timestamp); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 408 | return AAUDIO_OK; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 409 | } |
| 410 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 411 | aaudio_result_t AudioStreamInternal::onEventFromServer(AAudioServiceMessage *message) { |
| 412 | aaudio_result_t result = AAUDIO_OK; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 413 | switch (message->event.event) { |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 414 | case AAUDIO_SERVICE_EVENT_STARTED: |
Phil Burk | cf5f6d2 | 2017-05-26 12:35:07 -0700 | [diff] [blame] | 415 | ALOGD("processCommands() got AAUDIO_SERVICE_EVENT_STARTED"); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 416 | if (getState() == AAUDIO_STREAM_STATE_STARTING) { |
| 417 | setState(AAUDIO_STREAM_STATE_STARTED); |
| 418 | } |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 419 | break; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 420 | case AAUDIO_SERVICE_EVENT_PAUSED: |
Phil Burk | cf5f6d2 | 2017-05-26 12:35:07 -0700 | [diff] [blame] | 421 | ALOGD("processCommands() got AAUDIO_SERVICE_EVENT_PAUSED"); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 422 | if (getState() == AAUDIO_STREAM_STATE_PAUSING) { |
| 423 | setState(AAUDIO_STREAM_STATE_PAUSED); |
| 424 | } |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 425 | break; |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 426 | case AAUDIO_SERVICE_EVENT_STOPPED: |
Phil Burk | cf5f6d2 | 2017-05-26 12:35:07 -0700 | [diff] [blame] | 427 | ALOGD("processCommands() got AAUDIO_SERVICE_EVENT_STOPPED"); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 428 | if (getState() == AAUDIO_STREAM_STATE_STOPPING) { |
| 429 | setState(AAUDIO_STREAM_STATE_STOPPED); |
| 430 | } |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 431 | break; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 432 | case AAUDIO_SERVICE_EVENT_FLUSHED: |
Phil Burk | cf5f6d2 | 2017-05-26 12:35:07 -0700 | [diff] [blame] | 433 | ALOGD("processCommands() got AAUDIO_SERVICE_EVENT_FLUSHED"); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 434 | if (getState() == AAUDIO_STREAM_STATE_FLUSHING) { |
| 435 | setState(AAUDIO_STREAM_STATE_FLUSHED); |
| 436 | onFlushFromServer(); |
| 437 | } |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 438 | break; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 439 | case AAUDIO_SERVICE_EVENT_CLOSED: |
Phil Burk | cf5f6d2 | 2017-05-26 12:35:07 -0700 | [diff] [blame] | 440 | ALOGD("processCommands() got AAUDIO_SERVICE_EVENT_CLOSED"); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 441 | setState(AAUDIO_STREAM_STATE_CLOSED); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 442 | break; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 443 | case AAUDIO_SERVICE_EVENT_DISCONNECTED: |
| 444 | result = AAUDIO_ERROR_DISCONNECTED; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 445 | setState(AAUDIO_STREAM_STATE_DISCONNECTED); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 446 | ALOGW("WARNING - processCommands() AAUDIO_SERVICE_EVENT_DISCONNECTED"); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 447 | break; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 448 | case AAUDIO_SERVICE_EVENT_VOLUME: |
Phil Burk | e572f46 | 2017-04-20 13:03:19 -0700 | [diff] [blame] | 449 | mVolumeRamp.setTarget((float) message->event.dataDouble); |
Phil Burk | cf5f6d2 | 2017-05-26 12:35:07 -0700 | [diff] [blame] | 450 | ALOGD("processCommands() AAUDIO_SERVICE_EVENT_VOLUME %lf", |
Phil Burk | e572f46 | 2017-04-20 13:03:19 -0700 | [diff] [blame] | 451 | message->event.dataDouble); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 452 | break; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 453 | default: |
| 454 | ALOGW("WARNING - processCommands() Unrecognized event = %d", |
| 455 | (int) message->event.event); |
| 456 | break; |
| 457 | } |
| 458 | return result; |
| 459 | } |
| 460 | |
| 461 | // Process all the commands coming from the server. |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 462 | aaudio_result_t AudioStreamInternal::processCommands() { |
| 463 | aaudio_result_t result = AAUDIO_OK; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 464 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 465 | while (result == AAUDIO_OK) { |
Phil Burk | cf5f6d2 | 2017-05-26 12:35:07 -0700 | [diff] [blame] | 466 | //ALOGD("AudioStreamInternal::processCommands() - looping, %d", result); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 467 | AAudioServiceMessage message; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 468 | if (mAudioEndpoint.readUpCommand(&message) != 1) { |
| 469 | break; // no command this time, no problem |
| 470 | } |
| 471 | switch (message.what) { |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 472 | case AAudioServiceMessage::code::TIMESTAMP: |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 473 | result = onTimestampFromServer(&message); |
| 474 | break; |
| 475 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 476 | case AAudioServiceMessage::code::EVENT: |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 477 | result = onEventFromServer(&message); |
| 478 | break; |
| 479 | |
| 480 | default: |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 481 | ALOGE("WARNING - AudioStreamInternal::processCommands() Unrecognized what = %d", |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 482 | (int) message.what); |
Phil Burk | 17fff38 | 2017-05-16 14:06:45 -0700 | [diff] [blame] | 483 | result = AAUDIO_ERROR_INTERNAL; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 484 | break; |
| 485 | } |
| 486 | } |
| 487 | return result; |
| 488 | } |
| 489 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 490 | // Read or write the data, block if needed and timeoutMillis > 0 |
| 491 | aaudio_result_t AudioStreamInternal::processData(void *buffer, int32_t numFrames, |
| 492 | int64_t timeoutNanoseconds) |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 493 | { |
Phil Burk | 4485d41 | 2017-05-09 15:55:02 -0700 | [diff] [blame] | 494 | const char * traceName = (mInService) ? "aaWrtS" : "aaWrtC"; |
| 495 | ATRACE_BEGIN(traceName); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 496 | aaudio_result_t result = AAUDIO_OK; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 497 | int32_t loopCount = 0; |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 498 | uint8_t* audioData = (uint8_t*)buffer; |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 499 | int64_t currentTimeNanos = AudioClock::getNanoseconds(); |
| 500 | int64_t deadlineNanos = currentTimeNanos + timeoutNanoseconds; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 501 | int32_t framesLeft = numFrames; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 502 | |
Phil Burk | 4485d41 | 2017-05-09 15:55:02 -0700 | [diff] [blame] | 503 | int32_t fullFrames = mAudioEndpoint.getFullFramesAvailable(); |
| 504 | if (ATRACE_ENABLED()) { |
| 505 | const char * traceName = (mInService) ? "aaFullS" : "aaFullC"; |
| 506 | ATRACE_INT(traceName, fullFrames); |
| 507 | } |
| 508 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 509 | // 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] | 510 | while (framesLeft > 0) { |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 511 | // 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] | 512 | int64_t wakeTimeNanos = 0; |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 513 | aaudio_result_t framesProcessed = processDataNow(audioData, framesLeft, |
| 514 | currentTimeNanos, &wakeTimeNanos); |
| 515 | if (framesProcessed < 0) { |
| 516 | ALOGE("AudioStreamInternal::processData() loop: framesProcessed = %d", framesProcessed); |
| 517 | result = framesProcessed; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 518 | break; |
| 519 | } |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 520 | framesLeft -= (int32_t) framesProcessed; |
| 521 | audioData += framesProcessed * getBytesPerFrame(); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 522 | |
| 523 | // Should we block? |
| 524 | if (timeoutNanoseconds == 0) { |
| 525 | break; // don't block |
| 526 | } else if (framesLeft > 0) { |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 527 | // clip the wake time to something reasonable |
| 528 | if (wakeTimeNanos < currentTimeNanos) { |
| 529 | wakeTimeNanos = currentTimeNanos; |
| 530 | } |
| 531 | if (wakeTimeNanos > deadlineNanos) { |
| 532 | // If we time out, just return the framesWritten so far. |
Phil Burk | cf5f6d2 | 2017-05-26 12:35:07 -0700 | [diff] [blame] | 533 | // TODO remove after we fix the deadline bug |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 534 | ALOGE("AudioStreamInternal::processData(): timed out after %lld nanos", |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 535 | (long long) timeoutNanoseconds); |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 536 | ALOGE("AudioStreamInternal::processData(): wakeTime = %lld, deadline = %lld nanos", |
| 537 | (long long) wakeTimeNanos, (long long) deadlineNanos); |
| 538 | ALOGE("AudioStreamInternal::processData(): past deadline by %d micros", |
| 539 | (int)((wakeTimeNanos - deadlineNanos) / AAUDIO_NANOS_PER_MICROSECOND)); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 540 | break; |
| 541 | } |
| 542 | |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 543 | int64_t sleepForNanos = wakeTimeNanos - currentTimeNanos; |
| 544 | AudioClock::sleepForNanos(sleepForNanos); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 545 | currentTimeNanos = AudioClock::getNanoseconds(); |
| 546 | } |
| 547 | } |
| 548 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 549 | // return error or framesProcessed |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 550 | (void) loopCount; |
Phil Burk | 4485d41 | 2017-05-09 15:55:02 -0700 | [diff] [blame] | 551 | ATRACE_END(); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 552 | return (result < 0) ? result : numFrames - framesLeft; |
| 553 | } |
| 554 | |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 555 | void AudioStreamInternal::processTimestamp(uint64_t position, int64_t time) { |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 556 | mClockModel.processTimestamp(position, time); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 557 | } |
| 558 | |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 559 | aaudio_result_t AudioStreamInternal::setBufferSize(int32_t requestedFrames) { |
| 560 | int32_t actualFrames = 0; |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 561 | // Round to the next highest burst size. |
| 562 | if (getFramesPerBurst() > 0) { |
| 563 | int32_t numBursts = (requestedFrames + getFramesPerBurst() - 1) / getFramesPerBurst(); |
| 564 | requestedFrames = numBursts * getFramesPerBurst(); |
| 565 | } |
| 566 | |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 567 | aaudio_result_t result = mAudioEndpoint.setBufferSizeInFrames(requestedFrames, &actualFrames); |
Phil Burk | cf5f6d2 | 2017-05-26 12:35:07 -0700 | [diff] [blame] | 568 | ALOGD("AudioStreamInternal::setBufferSize() req = %d => %d", requestedFrames, actualFrames); |
Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 569 | if (result < 0) { |
| 570 | return result; |
| 571 | } else { |
| 572 | return (aaudio_result_t) actualFrames; |
| 573 | } |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 574 | } |
| 575 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 576 | int32_t AudioStreamInternal::getBufferSize() const { |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 577 | return mAudioEndpoint.getBufferSizeInFrames(); |
| 578 | } |
| 579 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 580 | int32_t AudioStreamInternal::getBufferCapacity() const { |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 581 | return mAudioEndpoint.getBufferCapacityInFrames(); |
| 582 | } |
| 583 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 584 | int32_t AudioStreamInternal::getFramesPerBurst() const { |
| 585 | return mEndpointDescriptor.dataQueueDescriptor.framesPerBurst; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 586 | } |
| 587 | |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 588 | aaudio_result_t AudioStreamInternal::joinThread(void** returnArg) { |
| 589 | return AudioStream::joinThread(returnArg, calculateReasonableTimeout(getFramesPerBurst())); |
Phil Burk | 4c5129b | 2017-04-28 15:17:32 -0700 | [diff] [blame] | 590 | } |