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