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