| 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 |  | 
 | 27 | #include <binder/IServiceManager.h> | 
 | 28 |  | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 29 | #include <aaudio/AAudio.h> | 
| Phil Burk | fd34a93 | 2017-07-19 07:03:52 -0700 | [diff] [blame] | 30 | #include <cutils/properties.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 "AudioEndpointParcelable.h" | 
 | 35 | #include "binding/AAudioStreamRequest.h" | 
 | 36 | #include "binding/AAudioStreamConfiguration.h" | 
 | 37 | #include "binding/IAAudioService.h" | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 38 | #include "binding/AAudioServiceMessage.h" | 
| Phil Burk | 3df348f | 2017-02-08 11:41:55 -0800 | [diff] [blame] | 39 | #include "core/AudioStreamBuilder.h" | 
| Phil Burk | e572f46 | 2017-04-20 13:03:19 -0700 | [diff] [blame] | 40 | #include "fifo/FifoBuffer.h" | 
| Phil Burk | fd34a93 | 2017-07-19 07:03:52 -0700 | [diff] [blame] | 41 | #include "utility/AudioClock.h" | 
| Phil Burk | e572f46 | 2017-04-20 13:03:19 -0700 | [diff] [blame] | 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 | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 57 | #define LOG_TIMESTAMPS            0 | 
| Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 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 | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 64 |         , mInService(inService) | 
| Phil Burk | fd34a93 | 2017-07-19 07:03:52 -0700 | [diff] [blame] | 65 |         , mServiceInterface(serviceInterface) | 
| Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 66 |         , mAtomicTimestamp() | 
| Phil Burk | fd34a93 | 2017-07-19 07:03:52 -0700 | [diff] [blame] | 67 |         , mWakeupDelayNanos(AAudioProperty_getWakeupDelayMicros() * AAUDIO_NANOS_PER_MICROSECOND) | 
 | 68 |         , mMinimumSleepNanos(AAudioProperty_getMinimumSleepMicros() * AAUDIO_NANOS_PER_MICROSECOND) | 
 | 69 |         { | 
| Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 70 |     ALOGD("%s - mWakeupDelayNanos = %d, mMinimumSleepNanos = %d", | 
 | 71 |           __func__, mWakeupDelayNanos, mMinimumSleepNanos); | 
| Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 72 | } | 
 | 73 |  | 
 | 74 | AudioStreamInternal::~AudioStreamInternal() { | 
 | 75 | } | 
 | 76 |  | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 77 | aaudio_result_t AudioStreamInternal::open(const AudioStreamBuilder &builder) { | 
| Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 78 |  | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 79 |     aaudio_result_t result = AAUDIO_OK; | 
| Phil Burk | 99306c8 | 2017-08-14 12:38:58 -0700 | [diff] [blame] | 80 |     int32_t capacity; | 
| Phil Burk | 6479d50 | 2017-11-20 09:32:52 -0800 | [diff] [blame] | 81 |     int32_t framesPerBurst; | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 82 |     AAudioStreamRequest request; | 
| Phil Burk | 99306c8 | 2017-08-14 12:38:58 -0700 | [diff] [blame] | 83 |     AAudioStreamConfiguration configurationOutput; | 
| Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 84 |  | 
| Phil Burk | 99306c8 | 2017-08-14 12:38:58 -0700 | [diff] [blame] | 85 |     if (getState() != AAUDIO_STREAM_STATE_UNINITIALIZED) { | 
| Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 86 |         ALOGE("%s - already open! state = %d", __func__, getState()); | 
| Phil Burk | 99306c8 | 2017-08-14 12:38:58 -0700 | [diff] [blame] | 87 |         return AAUDIO_ERROR_INVALID_STATE; | 
 | 88 |     } | 
 | 89 |  | 
 | 90 |     // Copy requested parameters to the stream. | 
| Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 91 |     result = AudioStream::open(builder); | 
 | 92 |     if (result < 0) { | 
 | 93 |         return result; | 
 | 94 |     } | 
 | 95 |  | 
| Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 96 |     // We have to do volume scaling. So we prefer FLOAT format. | 
| Glenn Kasten | 37a466a | 2017-05-30 15:53:14 -0700 | [diff] [blame] | 97 |     if (getFormat() == AAUDIO_FORMAT_UNSPECIFIED) { | 
| Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 98 |         setFormat(AAUDIO_FORMAT_PCM_FLOAT); | 
 | 99 |     } | 
| Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 100 |     // Request FLOAT for the shared mixer. | 
| jiabin | 901f65d | 2017-07-12 17:56:35 -0700 | [diff] [blame] | 101 |     request.getConfiguration().setFormat(AAUDIO_FORMAT_PCM_FLOAT); | 
| Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 102 |  | 
| Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 103 |     // Build the request to send to the server. | 
| Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 104 |     request.setUserId(getuid()); | 
 | 105 |     request.setProcessId(getpid()); | 
| Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 106 |     request.setSharingModeMatchRequired(isSharingModeMatchRequired()); | 
| Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 107 |     request.setInService(mInService); | 
| Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 108 |  | 
| Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 109 |     request.getConfiguration().setDeviceId(getDeviceId()); | 
 | 110 |     request.getConfiguration().setSampleRate(getSampleRate()); | 
 | 111 |     request.getConfiguration().setSamplesPerFrame(getSamplesPerFrame()); | 
| Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 112 |     request.getConfiguration().setDirection(getDirection()); | 
| Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 113 |     request.getConfiguration().setSharingMode(getSharingMode()); | 
 | 114 |  | 
| Phil Burk | 3df348f | 2017-02-08 11:41:55 -0800 | [diff] [blame] | 115 |     request.getConfiguration().setBufferCapacity(builder.getBufferCapacity()); | 
| Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 116 |  | 
| Phil Burk | 99306c8 | 2017-08-14 12:38:58 -0700 | [diff] [blame] | 117 |     mServiceStreamHandle = mServiceInterface.openStream(request, configurationOutput); | 
| Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 118 |     if (mServiceStreamHandle < 0) { | 
 | 119 |         result = mServiceStreamHandle; | 
| Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 120 |         ALOGE("%s - openStream() returned %d", __func__, result); | 
| Phil Burk | 99306c8 | 2017-08-14 12:38:58 -0700 | [diff] [blame] | 121 |         return result; | 
| Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 122 |     } | 
| Phil Burk | 99306c8 | 2017-08-14 12:38:58 -0700 | [diff] [blame] | 123 |  | 
 | 124 |     result = configurationOutput.validate(); | 
 | 125 |     if (result != AAUDIO_OK) { | 
 | 126 |         goto error; | 
 | 127 |     } | 
 | 128 |     // Save results of the open. | 
 | 129 |     setSampleRate(configurationOutput.getSampleRate()); | 
 | 130 |     setSamplesPerFrame(configurationOutput.getSamplesPerFrame()); | 
 | 131 |     setDeviceId(configurationOutput.getDeviceId()); | 
 | 132 |     setSharingMode(configurationOutput.getSharingMode()); | 
 | 133 |  | 
 | 134 |     // Save device format so we can do format conversion and volume scaling together. | 
 | 135 |     mDeviceFormat = configurationOutput.getFormat(); | 
 | 136 |  | 
 | 137 |     result = mServiceInterface.getStreamDescription(mServiceStreamHandle, mEndPointParcelable); | 
 | 138 |     if (result != AAUDIO_OK) { | 
 | 139 |         goto error; | 
 | 140 |     } | 
 | 141 |  | 
 | 142 |     // Resolve parcelable into a descriptor. | 
 | 143 |     result = mEndPointParcelable.resolve(&mEndpointDescriptor); | 
 | 144 |     if (result != AAUDIO_OK) { | 
 | 145 |         goto error; | 
 | 146 |     } | 
 | 147 |  | 
 | 148 |     // Configure endpoint based on descriptor. | 
 | 149 |     result = mAudioEndpoint.configure(&mEndpointDescriptor, getDirection()); | 
 | 150 |     if (result != AAUDIO_OK) { | 
 | 151 |         goto error; | 
 | 152 |     } | 
 | 153 |  | 
| Phil Burk | 99306c8 | 2017-08-14 12:38:58 -0700 | [diff] [blame] | 154 |  | 
 | 155 |     // Validate result from server. | 
| Phil Burk | 6479d50 | 2017-11-20 09:32:52 -0800 | [diff] [blame] | 156 |     framesPerBurst = mEndpointDescriptor.dataQueueDescriptor.framesPerBurst; | 
 | 157 |     if (framesPerBurst < MIN_FRAMES_PER_BURST || framesPerBurst > MAX_FRAMES_PER_BURST) { | 
 | 158 |         ALOGE("%s - framesPerBurst out of range = %d", __func__, framesPerBurst); | 
| Phil Burk | 99306c8 | 2017-08-14 12:38:58 -0700 | [diff] [blame] | 159 |         result = AAUDIO_ERROR_OUT_OF_RANGE; | 
 | 160 |         goto error; | 
 | 161 |     } | 
| Phil Burk | 6479d50 | 2017-11-20 09:32:52 -0800 | [diff] [blame] | 162 |     mFramesPerBurst = framesPerBurst; // only save good value | 
 | 163 |  | 
 | 164 |     capacity = mEndpointDescriptor.dataQueueDescriptor.capacityInFrames; | 
 | 165 |     if (capacity < mFramesPerBurst || capacity > MAX_BUFFER_CAPACITY_IN_FRAMES) { | 
| Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 166 |         ALOGE("%s - bufferCapacity out of range = %d", __func__, capacity); | 
| Phil Burk | 99306c8 | 2017-08-14 12:38:58 -0700 | [diff] [blame] | 167 |         result = AAUDIO_ERROR_OUT_OF_RANGE; | 
 | 168 |         goto error; | 
 | 169 |     } | 
 | 170 |  | 
 | 171 |     mClockModel.setSampleRate(getSampleRate()); | 
 | 172 |     mClockModel.setFramesPerBurst(mFramesPerBurst); | 
 | 173 |  | 
 | 174 |     if (getDataCallbackProc()) { | 
 | 175 |         mCallbackFrames = builder.getFramesPerDataCallback(); | 
 | 176 |         if (mCallbackFrames > getBufferCapacity() / 2) { | 
| Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 177 |             ALOGE("%s - framesPerCallback too big = %d, capacity = %d", | 
 | 178 |                   __func__, mCallbackFrames, getBufferCapacity()); | 
| Phil Burk | 99306c8 | 2017-08-14 12:38:58 -0700 | [diff] [blame] | 179 |             result = AAUDIO_ERROR_OUT_OF_RANGE; | 
 | 180 |             goto error; | 
 | 181 |  | 
 | 182 |         } else if (mCallbackFrames < 0) { | 
| Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 183 |             ALOGE("%s - framesPerCallback negative", __func__); | 
| Phil Burk | 99306c8 | 2017-08-14 12:38:58 -0700 | [diff] [blame] | 184 |             result = AAUDIO_ERROR_OUT_OF_RANGE; | 
 | 185 |             goto error; | 
 | 186 |  | 
 | 187 |         } | 
 | 188 |         if (mCallbackFrames == AAUDIO_UNSPECIFIED) { | 
 | 189 |             mCallbackFrames = mFramesPerBurst; | 
 | 190 |         } | 
 | 191 |  | 
 | 192 |         int32_t bytesPerFrame = getSamplesPerFrame() | 
 | 193 |                                 * AAudioConvert_formatToSizeInBytes(getFormat()); | 
 | 194 |         int32_t callbackBufferSize = mCallbackFrames * bytesPerFrame; | 
 | 195 |         mCallbackBuffer = new uint8_t[callbackBufferSize]; | 
 | 196 |     } | 
 | 197 |  | 
 | 198 |     setState(AAUDIO_STREAM_STATE_OPEN); | 
| Phil Burk | 99306c8 | 2017-08-14 12:38:58 -0700 | [diff] [blame] | 199 |  | 
 | 200 |     return result; | 
 | 201 |  | 
 | 202 | error: | 
 | 203 |     close(); | 
| Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 204 |     return result; | 
 | 205 | } | 
 | 206 |  | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 207 | aaudio_result_t AudioStreamInternal::close() { | 
| Phil Burk | 965650e | 2017-09-07 21:00:09 -0700 | [diff] [blame] | 208 |     aaudio_result_t result = AAUDIO_OK; | 
 | 209 |     ALOGD("close(): mServiceStreamHandle = 0x%08X", | 
| Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 210 |              mServiceStreamHandle); | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 211 |     if (mServiceStreamHandle != AAUDIO_HANDLE_INVALID) { | 
| Phil Burk | 4485d41 | 2017-05-09 15:55:02 -0700 | [diff] [blame] | 212 |         // Don't close a stream while it is running. | 
 | 213 |         aaudio_stream_state_t currentState = getState(); | 
| Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 214 |         if (isActive()) { | 
| Phil Burk | 4485d41 | 2017-05-09 15:55:02 -0700 | [diff] [blame] | 215 |             requestStop(); | 
 | 216 |             aaudio_stream_state_t nextState; | 
 | 217 |             int64_t timeoutNanoseconds = MIN_TIMEOUT_NANOS; | 
| Phil Burk | 965650e | 2017-09-07 21:00:09 -0700 | [diff] [blame] | 218 |             result = waitForStateChange(currentState, &nextState, | 
| Phil Burk | 4485d41 | 2017-05-09 15:55:02 -0700 | [diff] [blame] | 219 |                                                        timeoutNanoseconds); | 
 | 220 |             if (result != AAUDIO_OK) { | 
| Phil Burk | 965650e | 2017-09-07 21:00:09 -0700 | [diff] [blame] | 221 |                 ALOGE("close() waitForStateChange() returned %d %s", | 
| Phil Burk | 4485d41 | 2017-05-09 15:55:02 -0700 | [diff] [blame] | 222 |                 result, AAudio_convertResultToText(result)); | 
 | 223 |             } | 
 | 224 |         } | 
| Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 225 |         setState(AAUDIO_STREAM_STATE_CLOSING); | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 226 |         aaudio_handle_t serviceStreamHandle = mServiceStreamHandle; | 
 | 227 |         mServiceStreamHandle = AAUDIO_HANDLE_INVALID; | 
| Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 228 |  | 
 | 229 |         mServiceInterface.closeStream(serviceStreamHandle); | 
| Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 230 |         delete[] mCallbackBuffer; | 
| Phil Burk | 4485d41 | 2017-05-09 15:55:02 -0700 | [diff] [blame] | 231 |         mCallbackBuffer = nullptr; | 
| Phil Burk | 965650e | 2017-09-07 21:00:09 -0700 | [diff] [blame] | 232 |  | 
| Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 233 |         setState(AAUDIO_STREAM_STATE_CLOSED); | 
| Phil Burk | 965650e | 2017-09-07 21:00:09 -0700 | [diff] [blame] | 234 |         result = mEndPointParcelable.close(); | 
 | 235 |         aaudio_result_t result2 = AudioStream::close(); | 
 | 236 |         return (result != AAUDIO_OK) ? result : result2; | 
| Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 237 |     } else { | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 238 |         return AAUDIO_ERROR_INVALID_HANDLE; | 
| Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 239 |     } | 
 | 240 | } | 
 | 241 |  | 
| Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 242 | static void *aaudio_callback_thread_proc(void *context) | 
 | 243 | { | 
 | 244 |     AudioStreamInternal *stream = (AudioStreamInternal *)context; | 
| Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 245 |     //LOGD("oboe_callback_thread, stream = %p", stream); | 
| Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 246 |     if (stream != NULL) { | 
 | 247 |         return stream->callbackLoop(); | 
 | 248 |     } else { | 
 | 249 |         return NULL; | 
 | 250 |     } | 
 | 251 | } | 
 | 252 |  | 
| Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 253 | /* | 
 | 254 |  * It normally takes about 20-30 msec to start a stream on the server. | 
 | 255 |  * But the first time can take as much as 200-300 msec. The HW | 
 | 256 |  * starts right away so by the time the client gets a chance to write into | 
 | 257 |  * the buffer, it is already in a deep underflow state. That can cause the | 
 | 258 |  * XRunCount to be non-zero, which could lead an app to tune its latency higher. | 
 | 259 |  * To avoid this problem, we set a request for the processing code to start the | 
 | 260 |  * client stream at the same position as the server stream. | 
 | 261 |  * The processing code will then save the current offset | 
 | 262 |  * between client and server and apply that to any position given to the app. | 
 | 263 |  */ | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 264 | aaudio_result_t AudioStreamInternal::requestStart() | 
| Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 265 | { | 
| Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 266 |     int64_t startTime; | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 267 |     if (mServiceStreamHandle == AAUDIO_HANDLE_INVALID) { | 
| Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 268 |         ALOGE("requestStart() mServiceStreamHandle invalid"); | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 269 |         return AAUDIO_ERROR_INVALID_STATE; | 
| Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 270 |     } | 
| Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 271 |     if (isActive()) { | 
| Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 272 |         ALOGE("requestStart() already active"); | 
| Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 273 |         return AAUDIO_ERROR_INVALID_STATE; | 
 | 274 |     } | 
| Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 275 |  | 
| Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 276 |     aaudio_stream_state_t originalState = getState(); | 
 | 277 |     if (originalState == AAUDIO_STREAM_STATE_DISCONNECTED) { | 
 | 278 |         ALOGE("requestStart() but DISCONNECTED"); | 
 | 279 |         return AAUDIO_ERROR_DISCONNECTED; | 
 | 280 |     } | 
| Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 281 |     setState(AAUDIO_STREAM_STATE_STARTING); | 
| Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 282 |  | 
 | 283 |     // Clear any stale timestamps from the previous run. | 
 | 284 |     drainTimestampsFromService(); | 
 | 285 |  | 
| Phil Burk | 965650e | 2017-09-07 21:00:09 -0700 | [diff] [blame] | 286 |     aaudio_result_t result = mServiceInterface.startStream(mServiceStreamHandle); | 
| Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 287 |  | 
| Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 288 |     startTime = AudioClock::getNanoseconds(); | 
| Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 289 |     mClockModel.start(startTime); | 
| Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 290 |     mNeedCatchUp.request();  // Ask data processing code to catch up when first timestamp received. | 
| Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 291 |  | 
| Phil Burk | 965650e | 2017-09-07 21:00:09 -0700 | [diff] [blame] | 292 |     // Start data callback thread. | 
| Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 293 |     if (result == AAUDIO_OK && getDataCallbackProc() != nullptr) { | 
 | 294 |         // Launch the callback loop thread. | 
 | 295 |         int64_t periodNanos = mCallbackFrames | 
 | 296 |                               * AAUDIO_NANOS_PER_SECOND | 
 | 297 |                               / getSampleRate(); | 
 | 298 |         mCallbackEnabled.store(true); | 
 | 299 |         result = createThread(periodNanos, aaudio_callback_thread_proc, this); | 
 | 300 |     } | 
| Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 301 |     if (result != AAUDIO_OK) { | 
 | 302 |         setState(originalState); | 
 | 303 |     } | 
| Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 304 |     return result; | 
| Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 305 | } | 
 | 306 |  | 
| Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 307 | int64_t AudioStreamInternal::calculateReasonableTimeout(int32_t framesPerOperation) { | 
 | 308 |  | 
 | 309 |     // 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] | 310 |     int64_t timeoutNanoseconds = (MIN_TIMEOUT_OPERATIONS | 
 | 311 |                                   * framesPerOperation | 
 | 312 |                                   * AAUDIO_NANOS_PER_SECOND) | 
 | 313 |                                   / getSampleRate(); | 
| Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 314 |     if (timeoutNanoseconds < MIN_TIMEOUT_NANOS) { // arbitrary number of seconds | 
 | 315 |         timeoutNanoseconds = MIN_TIMEOUT_NANOS; | 
 | 316 |     } | 
 | 317 |     return timeoutNanoseconds; | 
 | 318 | } | 
 | 319 |  | 
| Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 320 | int64_t AudioStreamInternal::calculateReasonableTimeout() { | 
 | 321 |     return calculateReasonableTimeout(getFramesPerBurst()); | 
 | 322 | } | 
 | 323 |  | 
| Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 324 | aaudio_result_t AudioStreamInternal::stopCallback() | 
 | 325 | { | 
 | 326 |     if (isDataCallbackActive()) { | 
 | 327 |         mCallbackEnabled.store(false); | 
| Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 328 |         return joinThread(NULL); | 
| Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 329 |     } else { | 
 | 330 |         return AAUDIO_OK; | 
 | 331 |     } | 
 | 332 | } | 
 | 333 |  | 
| Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 334 | aaudio_result_t AudioStreamInternal::requestStopInternal() | 
 | 335 | { | 
 | 336 |     if (mServiceStreamHandle == AAUDIO_HANDLE_INVALID) { | 
| Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 337 |         ALOGE("requestStopInternal() mServiceStreamHandle invalid = 0x%08X", | 
| Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 338 |               mServiceStreamHandle); | 
 | 339 |         return AAUDIO_ERROR_INVALID_STATE; | 
 | 340 |     } | 
 | 341 |  | 
 | 342 |     mClockModel.stop(AudioClock::getNanoseconds()); | 
 | 343 |     setState(AAUDIO_STREAM_STATE_STOPPING); | 
| Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 344 |     mAtomicTimestamp.clear(); | 
| Phil Burk | 965650e | 2017-09-07 21:00:09 -0700 | [diff] [blame] | 345 |  | 
 | 346 |     return mServiceInterface.stopStream(mServiceStreamHandle); | 
| Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 347 | } | 
 | 348 |  | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 349 | aaudio_result_t AudioStreamInternal::requestStop() | 
| Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 350 | { | 
| Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 351 |     aaudio_result_t result = stopCallback(); | 
 | 352 |     if (result != AAUDIO_OK) { | 
 | 353 |         return result; | 
| Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 354 |     } | 
| Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 355 |     result = requestStopInternal(); | 
| Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 356 |     return result; | 
 | 357 | } | 
 | 358 |  | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 359 | aaudio_result_t AudioStreamInternal::registerThread() { | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 360 |     if (mServiceStreamHandle == AAUDIO_HANDLE_INVALID) { | 
| Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 361 |         ALOGE("registerThread() mServiceStreamHandle invalid"); | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 362 |         return AAUDIO_ERROR_INVALID_STATE; | 
| Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 363 |     } | 
| Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 364 |     return mServiceInterface.registerAudioThread(mServiceStreamHandle, | 
| Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 365 |                                               gettid(), | 
 | 366 |                                               getPeriodNanoseconds()); | 
| Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 367 | } | 
 | 368 |  | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 369 | aaudio_result_t AudioStreamInternal::unregisterThread() { | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 370 |     if (mServiceStreamHandle == AAUDIO_HANDLE_INVALID) { | 
| Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 371 |         ALOGE("unregisterThread() mServiceStreamHandle invalid"); | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 372 |         return AAUDIO_ERROR_INVALID_STATE; | 
| Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 373 |     } | 
| Phil Burk | 2ac035f | 2017-06-23 14:51:14 -0700 | [diff] [blame] | 374 |     return mServiceInterface.unregisterAudioThread(mServiceStreamHandle, gettid()); | 
| Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 375 | } | 
 | 376 |  | 
| Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 377 | aaudio_result_t AudioStreamInternal::startClient(const android::AudioClient& client, | 
 | 378 |                                                  audio_port_handle_t *clientHandle) { | 
 | 379 |     if (mServiceStreamHandle == AAUDIO_HANDLE_INVALID) { | 
 | 380 |         return AAUDIO_ERROR_INVALID_STATE; | 
 | 381 |     } | 
| Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 382 |  | 
| Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 383 |     return mServiceInterface.startClient(mServiceStreamHandle, client, clientHandle); | 
 | 384 | } | 
 | 385 |  | 
 | 386 | aaudio_result_t AudioStreamInternal::stopClient(audio_port_handle_t clientHandle) { | 
 | 387 |     if (mServiceStreamHandle == AAUDIO_HANDLE_INVALID) { | 
 | 388 |         return AAUDIO_ERROR_INVALID_STATE; | 
 | 389 |     } | 
 | 390 |     return mServiceInterface.stopClient(mServiceStreamHandle, clientHandle); | 
 | 391 | } | 
 | 392 |  | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 393 | aaudio_result_t AudioStreamInternal::getTimestamp(clockid_t clockId, | 
| Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 394 |                            int64_t *framePosition, | 
 | 395 |                            int64_t *timeNanoseconds) { | 
| Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 396 |     // Generated in server and passed to client. Return latest. | 
 | 397 |     if (mAtomicTimestamp.isValid()) { | 
 | 398 |         Timestamp timestamp = mAtomicTimestamp.read(); | 
| Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 399 |         int64_t position = timestamp.getPosition() + mFramesOffsetFromService; | 
 | 400 |         if (position >= 0) { | 
 | 401 |             *framePosition = position; | 
 | 402 |             *timeNanoseconds = timestamp.getNanoseconds(); | 
 | 403 |             return AAUDIO_OK; | 
 | 404 |         } | 
| Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 405 |     } | 
| Phil Burk | c75d97f | 2017-09-08 15:48:36 -0700 | [diff] [blame] | 406 |     return AAUDIO_ERROR_INVALID_STATE; | 
| Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 407 | } | 
 | 408 |  | 
| Phil Burk | 0befec6 | 2017-07-28 15:12:13 -0700 | [diff] [blame] | 409 | aaudio_result_t AudioStreamInternal::updateStateMachine() { | 
| Phil Burk | e4d7bb4 | 2017-03-28 11:32:39 -0700 | [diff] [blame] | 410 |     if (isDataCallbackActive()) { | 
 | 411 |         return AAUDIO_OK; // state is getting updated by the callback thread read/write call | 
 | 412 |     } | 
| Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 413 |     return processCommands(); | 
 | 414 | } | 
 | 415 |  | 
| Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 416 | void AudioStreamInternal::logTimestamp(AAudioServiceMessage &command) { | 
| Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 417 |     static int64_t oldPosition = 0; | 
| Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 418 |     static int64_t oldTime = 0; | 
| Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 419 |     int64_t framePosition = command.timestamp.position; | 
| Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 420 |     int64_t nanoTime = command.timestamp.timestamp; | 
| Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 421 |     ALOGD("logTimestamp: timestamp says framePosition = %8lld at nanoTime %lld", | 
| Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 422 |          (long long) framePosition, | 
 | 423 |          (long long) nanoTime); | 
 | 424 |     int64_t nanosDelta = nanoTime - oldTime; | 
 | 425 |     if (nanosDelta > 0 && oldTime > 0) { | 
 | 426 |         int64_t framesDelta = framePosition - oldPosition; | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 427 |         int64_t rate = (framesDelta * AAUDIO_NANOS_PER_SECOND) / nanosDelta; | 
| Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 428 |         ALOGD("logTimestamp:     framesDelta = %8lld, nanosDelta = %8lld, rate = %lld", | 
| Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 429 |               (long long) framesDelta, (long long) nanosDelta, (long long) rate); | 
| Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 430 |     } | 
 | 431 |     oldPosition = framePosition; | 
 | 432 |     oldTime = nanoTime; | 
 | 433 | } | 
| Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 434 |  | 
| Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 435 | aaudio_result_t AudioStreamInternal::onTimestampService(AAudioServiceMessage *message) { | 
| Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 436 | #if LOG_TIMESTAMPS | 
| Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 437 |     logTimestamp(*message); | 
| Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 438 | #endif | 
| Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 439 |     processTimestamp(message->timestamp.position, message->timestamp.timestamp); | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 440 |     return AAUDIO_OK; | 
| Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 441 | } | 
 | 442 |  | 
| Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 443 | aaudio_result_t AudioStreamInternal::onTimestampHardware(AAudioServiceMessage *message) { | 
 | 444 |     Timestamp timestamp(message->timestamp.position, message->timestamp.timestamp); | 
 | 445 |     mAtomicTimestamp.write(timestamp); | 
 | 446 |     return AAUDIO_OK; | 
 | 447 | } | 
 | 448 |  | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 449 | aaudio_result_t AudioStreamInternal::onEventFromServer(AAudioServiceMessage *message) { | 
 | 450 |     aaudio_result_t result = AAUDIO_OK; | 
| Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 451 |     switch (message->event.event) { | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 452 |         case AAUDIO_SERVICE_EVENT_STARTED: | 
| Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 453 |             ALOGD("%s - got AAUDIO_SERVICE_EVENT_STARTED", __func__); | 
| Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 454 |             if (getState() == AAUDIO_STREAM_STATE_STARTING) { | 
 | 455 |                 setState(AAUDIO_STREAM_STATE_STARTED); | 
 | 456 |             } | 
| Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 457 |             break; | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 458 |         case AAUDIO_SERVICE_EVENT_PAUSED: | 
| Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 459 |             ALOGD("%s - got AAUDIO_SERVICE_EVENT_PAUSED", __func__); | 
| Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 460 |             if (getState() == AAUDIO_STREAM_STATE_PAUSING) { | 
 | 461 |                 setState(AAUDIO_STREAM_STATE_PAUSED); | 
 | 462 |             } | 
| Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 463 |             break; | 
| Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 464 |         case AAUDIO_SERVICE_EVENT_STOPPED: | 
| Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 465 |             ALOGD("%s - got AAUDIO_SERVICE_EVENT_STOPPED", __func__); | 
| Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 466 |             if (getState() == AAUDIO_STREAM_STATE_STOPPING) { | 
 | 467 |                 setState(AAUDIO_STREAM_STATE_STOPPED); | 
 | 468 |             } | 
| Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 469 |             break; | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 470 |         case AAUDIO_SERVICE_EVENT_FLUSHED: | 
| Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 471 |             ALOGD("%s - got AAUDIO_SERVICE_EVENT_FLUSHED", __func__); | 
| Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 472 |             if (getState() == AAUDIO_STREAM_STATE_FLUSHING) { | 
 | 473 |                 setState(AAUDIO_STREAM_STATE_FLUSHED); | 
 | 474 |                 onFlushFromServer(); | 
 | 475 |             } | 
| Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 476 |             break; | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 477 |         case AAUDIO_SERVICE_EVENT_CLOSED: | 
| Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 478 |             ALOGD("%s - got AAUDIO_SERVICE_EVENT_CLOSED", __func__); | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 479 |             setState(AAUDIO_STREAM_STATE_CLOSED); | 
| Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 480 |             break; | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 481 |         case AAUDIO_SERVICE_EVENT_DISCONNECTED: | 
| Phil Burk | ea04d97 | 2017-08-07 12:30:44 -0700 | [diff] [blame] | 482 |             // Prevent hardware from looping on old data and making buzzing sounds. | 
 | 483 |             if (getDirection() == AAUDIO_DIRECTION_OUTPUT) { | 
 | 484 |                 mAudioEndpoint.eraseDataMemory(); | 
 | 485 |             } | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 486 |             result = AAUDIO_ERROR_DISCONNECTED; | 
| Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 487 |             setState(AAUDIO_STREAM_STATE_DISCONNECTED); | 
| Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 488 |             ALOGW("%s - AAUDIO_SERVICE_EVENT_DISCONNECTED - FIFO cleared", __func__); | 
| Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 489 |             break; | 
| Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 490 |         case AAUDIO_SERVICE_EVENT_VOLUME: | 
| Eric Laurent | a2f296e | 2017-06-21 18:51:47 -0700 | [diff] [blame] | 491 |             mStreamVolume = (float)message->event.dataDouble; | 
 | 492 |             doSetVolume(); | 
| Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 493 |             ALOGD("%s - AAUDIO_SERVICE_EVENT_VOLUME %lf", __func__, message->event.dataDouble); | 
| Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 494 |             break; | 
| Phil Burk | 2329638 | 2017-11-20 15:45:11 -0800 | [diff] [blame] | 495 |         case AAUDIO_SERVICE_EVENT_XRUN: | 
 | 496 |             mXRunCount = static_cast<int32_t>(message->event.dataLong); | 
 | 497 |             break; | 
| Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 498 |         default: | 
| Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 499 |             ALOGE("%s - Unrecognized event = %d", __func__, (int) message->event.event); | 
| Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 500 |             break; | 
 | 501 |     } | 
 | 502 |     return result; | 
 | 503 | } | 
 | 504 |  | 
| Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 505 | aaudio_result_t AudioStreamInternal::drainTimestampsFromService() { | 
 | 506 |     aaudio_result_t result = AAUDIO_OK; | 
 | 507 |  | 
 | 508 |     while (result == AAUDIO_OK) { | 
 | 509 |         AAudioServiceMessage message; | 
 | 510 |         if (mAudioEndpoint.readUpCommand(&message) != 1) { | 
 | 511 |             break; // no command this time, no problem | 
 | 512 |         } | 
 | 513 |         switch (message.what) { | 
 | 514 |             // ignore most messages | 
 | 515 |             case AAudioServiceMessage::code::TIMESTAMP_SERVICE: | 
 | 516 |             case AAudioServiceMessage::code::TIMESTAMP_HARDWARE: | 
 | 517 |                 break; | 
 | 518 |  | 
 | 519 |             case AAudioServiceMessage::code::EVENT: | 
 | 520 |                 result = onEventFromServer(&message); | 
 | 521 |                 break; | 
 | 522 |  | 
 | 523 |             default: | 
| Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 524 |                 ALOGE("%s - unrecognized message.what = %d", __func__, (int) message.what); | 
| Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 525 |                 result = AAUDIO_ERROR_INTERNAL; | 
 | 526 |                 break; | 
 | 527 |         } | 
 | 528 |     } | 
 | 529 |     return result; | 
 | 530 | } | 
 | 531 |  | 
| Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 532 | // Process all the commands coming from the server. | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 533 | aaudio_result_t AudioStreamInternal::processCommands() { | 
 | 534 |     aaudio_result_t result = AAUDIO_OK; | 
| Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 535 |  | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 536 |     while (result == AAUDIO_OK) { | 
 | 537 |         AAudioServiceMessage message; | 
| Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 538 |         if (mAudioEndpoint.readUpCommand(&message) != 1) { | 
 | 539 |             break; // no command this time, no problem | 
 | 540 |         } | 
 | 541 |         switch (message.what) { | 
| Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 542 |         case AAudioServiceMessage::code::TIMESTAMP_SERVICE: | 
 | 543 |             result = onTimestampService(&message); | 
 | 544 |             break; | 
 | 545 |  | 
 | 546 |         case AAudioServiceMessage::code::TIMESTAMP_HARDWARE: | 
 | 547 |             result = onTimestampHardware(&message); | 
| Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 548 |             break; | 
 | 549 |  | 
| Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 550 |         case AAudioServiceMessage::code::EVENT: | 
| Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 551 |             result = onEventFromServer(&message); | 
 | 552 |             break; | 
 | 553 |  | 
 | 554 |         default: | 
| Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 555 |             ALOGE("%s - unrecognized message.what = %d", __func__, (int) message.what); | 
| Phil Burk | 17fff38 | 2017-05-16 14:06:45 -0700 | [diff] [blame] | 556 |             result = AAUDIO_ERROR_INTERNAL; | 
| Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 557 |             break; | 
 | 558 |         } | 
 | 559 |     } | 
 | 560 |     return result; | 
 | 561 | } | 
 | 562 |  | 
| Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 563 | // Read or write the data, block if needed and timeoutMillis > 0 | 
 | 564 | aaudio_result_t AudioStreamInternal::processData(void *buffer, int32_t numFrames, | 
 | 565 |                                                  int64_t timeoutNanoseconds) | 
| Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 566 | { | 
| Phil Burk | fd34a93 | 2017-07-19 07:03:52 -0700 | [diff] [blame] | 567 |     const char * traceName = "aaProc"; | 
 | 568 |     const char * fifoName = "aaRdy"; | 
| Phil Burk | 4485d41 | 2017-05-09 15:55:02 -0700 | [diff] [blame] | 569 |     ATRACE_BEGIN(traceName); | 
| Phil Burk | 4485d41 | 2017-05-09 15:55:02 -0700 | [diff] [blame] | 570 |     if (ATRACE_ENABLED()) { | 
| Phil Burk | fd34a93 | 2017-07-19 07:03:52 -0700 | [diff] [blame] | 571 |         int32_t fullFrames = mAudioEndpoint.getFullFramesAvailable(); | 
 | 572 |         ATRACE_INT(fifoName, fullFrames); | 
| Phil Burk | 4485d41 | 2017-05-09 15:55:02 -0700 | [diff] [blame] | 573 |     } | 
 | 574 |  | 
| Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 575 |     aaudio_result_t result = AAUDIO_OK; | 
 | 576 |     int32_t loopCount = 0; | 
 | 577 |     uint8_t* audioData = (uint8_t*)buffer; | 
 | 578 |     int64_t currentTimeNanos = AudioClock::getNanoseconds(); | 
 | 579 |     const int64_t entryTimeNanos = currentTimeNanos; | 
 | 580 |     const int64_t deadlineNanos = currentTimeNanos + timeoutNanoseconds; | 
 | 581 |     int32_t framesLeft = numFrames; | 
 | 582 |  | 
| Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 583 |     // 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] | 584 |     while (framesLeft > 0) { | 
| Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 585 |         // 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] | 586 |         int64_t wakeTimeNanos = 0; | 
| Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 587 |         aaudio_result_t framesProcessed = processDataNow(audioData, framesLeft, | 
 | 588 |                                                   currentTimeNanos, &wakeTimeNanos); | 
 | 589 |         if (framesProcessed < 0) { | 
| Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 590 |             result = framesProcessed; | 
| Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 591 |             break; | 
 | 592 |         } | 
| Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 593 |         framesLeft -= (int32_t) framesProcessed; | 
 | 594 |         audioData += framesProcessed * getBytesPerFrame(); | 
| Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 595 |  | 
 | 596 |         // Should we block? | 
 | 597 |         if (timeoutNanoseconds == 0) { | 
 | 598 |             break; // don't block | 
 | 599 |         } else if (framesLeft > 0) { | 
| Phil Burk | fd34a93 | 2017-07-19 07:03:52 -0700 | [diff] [blame] | 600 |             if (!mAudioEndpoint.isFreeRunning()) { | 
 | 601 |                 // If there is software on the other end of the FIFO then it may get delayed. | 
 | 602 |                 // So wake up just a little after we expect it to be ready. | 
 | 603 |                 wakeTimeNanos += mWakeupDelayNanos; | 
| Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 604 |             } | 
| Phil Burk | fd34a93 | 2017-07-19 07:03:52 -0700 | [diff] [blame] | 605 |  | 
| Phil Burk | 2bc7c18 | 2017-08-28 11:45:01 -0700 | [diff] [blame] | 606 |             currentTimeNanos = AudioClock::getNanoseconds(); | 
 | 607 |             int64_t earliestWakeTime = currentTimeNanos + mMinimumSleepNanos; | 
 | 608 |             // Guarantee a minimum sleep time. | 
 | 609 |             if (wakeTimeNanos < earliestWakeTime) { | 
 | 610 |                 wakeTimeNanos = earliestWakeTime; | 
 | 611 |             } | 
 | 612 |  | 
| Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 613 |             if (wakeTimeNanos > deadlineNanos) { | 
 | 614 |                 // If we time out, just return the framesWritten so far. | 
| Phil Burk | cf5f6d2 | 2017-05-26 12:35:07 -0700 | [diff] [blame] | 615 |                 // TODO remove after we fix the deadline bug | 
| Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 616 |                 ALOGW("processData(): entered at %lld nanos, currently %lld", | 
| Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 617 |                       (long long) entryTimeNanos, (long long) currentTimeNanos); | 
| Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 618 |                 ALOGW("processData(): TIMEOUT after %lld nanos", | 
| Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 619 |                       (long long) timeoutNanoseconds); | 
| Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 620 |                 ALOGW("processData(): wakeTime = %lld, deadline = %lld nanos", | 
| Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 621 |                       (long long) wakeTimeNanos, (long long) deadlineNanos); | 
| Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 622 |                 ALOGW("processData(): past deadline by %d micros", | 
| Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 623 |                       (int)((wakeTimeNanos - deadlineNanos) / AAUDIO_NANOS_PER_MICROSECOND)); | 
| Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 624 |                 mClockModel.dump(); | 
 | 625 |                 mAudioEndpoint.dump(); | 
| Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 626 |                 break; | 
 | 627 |             } | 
 | 628 |  | 
| Phil Burk | fd34a93 | 2017-07-19 07:03:52 -0700 | [diff] [blame] | 629 |             if (ATRACE_ENABLED()) { | 
 | 630 |                 int32_t fullFrames = mAudioEndpoint.getFullFramesAvailable(); | 
 | 631 |                 ATRACE_INT(fifoName, fullFrames); | 
 | 632 |                 int64_t sleepForNanos = wakeTimeNanos - currentTimeNanos; | 
 | 633 |                 ATRACE_INT("aaSlpNs", (int32_t)sleepForNanos); | 
 | 634 |             } | 
 | 635 |  | 
 | 636 |             AudioClock::sleepUntilNanoTime(wakeTimeNanos); | 
| Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 637 |             currentTimeNanos = AudioClock::getNanoseconds(); | 
 | 638 |         } | 
 | 639 |     } | 
 | 640 |  | 
| Phil Burk | fd34a93 | 2017-07-19 07:03:52 -0700 | [diff] [blame] | 641 |     if (ATRACE_ENABLED()) { | 
 | 642 |         int32_t fullFrames = mAudioEndpoint.getFullFramesAvailable(); | 
 | 643 |         ATRACE_INT(fifoName, fullFrames); | 
 | 644 |     } | 
 | 645 |  | 
| Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 646 |     // return error or framesProcessed | 
| Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 647 |     (void) loopCount; | 
| Phil Burk | 4485d41 | 2017-05-09 15:55:02 -0700 | [diff] [blame] | 648 |     ATRACE_END(); | 
| Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 649 |     return (result < 0) ? result : numFrames - framesLeft; | 
 | 650 | } | 
 | 651 |  | 
| Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 652 | void AudioStreamInternal::processTimestamp(uint64_t position, int64_t time) { | 
| Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 653 |     mClockModel.processTimestamp(position, time); | 
| Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 654 | } | 
 | 655 |  | 
| Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 656 | aaudio_result_t AudioStreamInternal::setBufferSize(int32_t requestedFrames) { | 
| Phil Burk | 6479d50 | 2017-11-20 09:32:52 -0800 | [diff] [blame] | 657 |     int32_t adjustedFrames = requestedFrames; | 
| Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 658 |     int32_t actualFrames = 0; | 
| Phil Burk | 6479d50 | 2017-11-20 09:32:52 -0800 | [diff] [blame] | 659 |     int32_t maximumSize = getBufferCapacity(); | 
 | 660 |  | 
 | 661 |     // Clip to minimum size so that rounding up will work better. | 
 | 662 |     if (adjustedFrames < 1) { | 
 | 663 |         adjustedFrames = 1; | 
| Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 664 |     } | 
 | 665 |  | 
| Phil Burk | 6479d50 | 2017-11-20 09:32:52 -0800 | [diff] [blame] | 666 |     if (adjustedFrames > maximumSize) { | 
 | 667 |         // Clip to maximum size. | 
 | 668 |         adjustedFrames = maximumSize; | 
 | 669 |     } else { | 
 | 670 |         // Round to the next highest burst size. | 
 | 671 |         int32_t numBursts = (adjustedFrames + mFramesPerBurst - 1) / mFramesPerBurst; | 
 | 672 |         adjustedFrames = numBursts * mFramesPerBurst; | 
 | 673 |         // Rounding may have gone above maximum. | 
 | 674 |         if (adjustedFrames > maximumSize) { | 
 | 675 |             adjustedFrames = maximumSize; | 
 | 676 |         } | 
 | 677 |     } | 
 | 678 |  | 
 | 679 |     aaudio_result_t result = mAudioEndpoint.setBufferSizeInFrames(adjustedFrames, &actualFrames); | 
| Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 680 |     ALOGD("setBufferSize() req = %d => %d", requestedFrames, actualFrames); | 
| Phil Burk | 3316d5e | 2017-02-15 11:23:01 -0800 | [diff] [blame] | 681 |     if (result < 0) { | 
 | 682 |         return result; | 
 | 683 |     } else { | 
 | 684 |         return (aaudio_result_t) actualFrames; | 
 | 685 |     } | 
| Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 686 | } | 
 | 687 |  | 
| Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 688 | int32_t AudioStreamInternal::getBufferSize() const { | 
| Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 689 |     return mAudioEndpoint.getBufferSizeInFrames(); | 
 | 690 | } | 
 | 691 |  | 
| Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 692 | int32_t AudioStreamInternal::getBufferCapacity() const { | 
| Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 693 |     return mAudioEndpoint.getBufferCapacityInFrames(); | 
 | 694 | } | 
 | 695 |  | 
| Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 696 | int32_t AudioStreamInternal::getFramesPerBurst() const { | 
| Phil Burk | 6479d50 | 2017-11-20 09:32:52 -0800 | [diff] [blame] | 697 |     return mFramesPerBurst; | 
| Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 698 | } | 
 | 699 |  | 
| Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 700 | aaudio_result_t AudioStreamInternal::joinThread(void** returnArg) { | 
 | 701 |     return AudioStream::joinThread(returnArg, calculateReasonableTimeout(getFramesPerBurst())); | 
| Phil Burk | 4c5129b | 2017-04-28 15:17:32 -0700 | [diff] [blame] | 702 | } |