Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 17 | #define LOG_TAG "AAudio" |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 18 | //#define LOG_NDEBUG 0 |
| 19 | #include <utils/Log.h> |
| 20 | |
| 21 | #include <stdint.h> |
| 22 | #include <assert.h> |
| 23 | |
| 24 | #include <binder/IServiceManager.h> |
Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 25 | #include <utils/Mutex.h> |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 26 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 27 | #include <aaudio/AAudio.h> |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 28 | |
| 29 | #include "AudioClock.h" |
| 30 | #include "AudioEndpointParcelable.h" |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 31 | #include "binding/AAudioStreamRequest.h" |
| 32 | #include "binding/AAudioStreamConfiguration.h" |
| 33 | #include "binding/IAAudioService.h" |
| 34 | #include "binding/AAudioServiceMessage.h" |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 35 | |
| 36 | #include "AudioStreamInternal.h" |
| 37 | |
| 38 | #define LOG_TIMESTAMPS 0 |
| 39 | |
| 40 | using android::String16; |
| 41 | using android::IServiceManager; |
| 42 | using android::defaultServiceManager; |
| 43 | using android::interface_cast; |
Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 44 | using android::Mutex; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 45 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 46 | using namespace aaudio; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 47 | |
Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 48 | static android::Mutex gServiceLock; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 49 | static sp<IAAudioService> gAAudioService; |
Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 50 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 51 | #define AAUDIO_SERVICE_NAME "AAudioService" |
Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 52 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 53 | // Helper function to get access to the "AAudioService" service. |
Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 54 | // This code was modeled after frameworks/av/media/libaudioclient/AudioSystem.cpp |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 55 | static const sp<IAAudioService> getAAudioService() { |
Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 56 | sp<IBinder> binder; |
| 57 | Mutex::Autolock _l(gServiceLock); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 58 | if (gAAudioService == 0) { |
Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 59 | sp<IServiceManager> sm = defaultServiceManager(); |
| 60 | // Try several times to get the service. |
| 61 | int retries = 4; |
| 62 | do { |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 63 | binder = sm->getService(String16(AAUDIO_SERVICE_NAME)); // This will wait a while. |
Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 64 | if (binder != 0) { |
| 65 | break; |
| 66 | } |
| 67 | } while (retries-- > 0); |
| 68 | |
| 69 | if (binder != 0) { |
| 70 | // TODO Add linkToDeath() like in frameworks/av/media/libaudioclient/AudioSystem.cpp |
| 71 | // TODO Create a DeathRecipient that disconnects all active streams. |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 72 | gAAudioService = interface_cast<IAAudioService>(binder); |
Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 73 | } else { |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 74 | ALOGE("AudioStreamInternal could not get %s", AAUDIO_SERVICE_NAME); |
Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 75 | } |
| 76 | } |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 77 | return gAAudioService; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | AudioStreamInternal::AudioStreamInternal() |
| 81 | : AudioStream() |
| 82 | , mClockModel() |
| 83 | , mAudioEndpoint() |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 84 | , mServiceStreamHandle(AAUDIO_HANDLE_INVALID) |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 85 | , mFramesPerBurst(16) |
| 86 | { |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | AudioStreamInternal::~AudioStreamInternal() { |
| 90 | } |
| 91 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 92 | aaudio_result_t AudioStreamInternal::open(const AudioStreamBuilder &builder) { |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 93 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 94 | const sp<IAAudioService>& service = getAAudioService(); |
| 95 | if (service == 0) return AAUDIO_ERROR_NO_SERVICE; |
Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 96 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 97 | aaudio_result_t result = AAUDIO_OK; |
| 98 | AAudioStreamRequest request; |
| 99 | AAudioStreamConfiguration configuration; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 100 | |
| 101 | result = AudioStream::open(builder); |
| 102 | if (result < 0) { |
| 103 | return result; |
| 104 | } |
| 105 | |
Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 106 | // Build the request to send to the server. |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 107 | request.setUserId(getuid()); |
| 108 | request.setProcessId(getpid()); |
| 109 | request.getConfiguration().setDeviceId(getDeviceId()); |
| 110 | request.getConfiguration().setSampleRate(getSampleRate()); |
| 111 | request.getConfiguration().setSamplesPerFrame(getSamplesPerFrame()); |
| 112 | request.getConfiguration().setAudioFormat(getFormat()); |
| 113 | request.dump(); |
| 114 | |
Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 115 | mServiceStreamHandle = service->openStream(request, configuration); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 116 | ALOGD("AudioStreamInternal.open(): openStream returned mServiceStreamHandle = 0x%08X", |
| 117 | (unsigned int)mServiceStreamHandle); |
| 118 | if (mServiceStreamHandle < 0) { |
| 119 | result = mServiceStreamHandle; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 120 | ALOGE("AudioStreamInternal.open(): acquireRealtimeStream aaudio_result_t = 0x%08X", result); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 121 | } else { |
| 122 | result = configuration.validate(); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 123 | if (result != AAUDIO_OK) { |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 124 | close(); |
| 125 | return result; |
| 126 | } |
| 127 | // Save results of the open. |
| 128 | setSampleRate(configuration.getSampleRate()); |
| 129 | setSamplesPerFrame(configuration.getSamplesPerFrame()); |
| 130 | setFormat(configuration.getAudioFormat()); |
| 131 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 132 | aaudio::AudioEndpointParcelable parcelable; |
Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 133 | result = service->getStreamDescription(mServiceStreamHandle, parcelable); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 134 | if (result != AAUDIO_OK) { |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 135 | ALOGE("AudioStreamInternal.open(): getStreamDescriptor returns %d", result); |
Phil Burk | dec33ab | 2017-01-17 14:48:16 -0800 | [diff] [blame] | 136 | service->closeStream(mServiceStreamHandle); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 137 | return result; |
| 138 | } |
| 139 | // resolve parcelable into a descriptor |
| 140 | parcelable.resolve(&mEndpointDescriptor); |
| 141 | |
| 142 | // Configure endpoint based on descriptor. |
| 143 | mAudioEndpoint.configure(&mEndpointDescriptor); |
| 144 | |
| 145 | |
| 146 | mFramesPerBurst = mEndpointDescriptor.downDataQueueDescriptor.framesPerBurst; |
| 147 | assert(mFramesPerBurst >= 16); |
| 148 | assert(mEndpointDescriptor.downDataQueueDescriptor.capacityInFrames < 10 * 1024); |
| 149 | |
| 150 | mClockModel.setSampleRate(getSampleRate()); |
| 151 | mClockModel.setFramesPerBurst(mFramesPerBurst); |
| 152 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 153 | setState(AAUDIO_STREAM_STATE_OPEN); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 154 | } |
| 155 | return result; |
| 156 | } |
| 157 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 158 | aaudio_result_t AudioStreamInternal::close() { |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 159 | ALOGD("AudioStreamInternal.close(): mServiceStreamHandle = 0x%08X", mServiceStreamHandle); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 160 | if (mServiceStreamHandle != AAUDIO_HANDLE_INVALID) { |
| 161 | aaudio_handle_t serviceStreamHandle = mServiceStreamHandle; |
| 162 | mServiceStreamHandle = AAUDIO_HANDLE_INVALID; |
| 163 | const sp<IAAudioService>& aaudioService = getAAudioService(); |
| 164 | if (aaudioService == 0) return AAUDIO_ERROR_NO_SERVICE; |
| 165 | aaudioService->closeStream(serviceStreamHandle); |
| 166 | return AAUDIO_OK; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 167 | } else { |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 168 | return AAUDIO_ERROR_INVALID_HANDLE; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 169 | } |
| 170 | } |
| 171 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 172 | aaudio_result_t AudioStreamInternal::requestStart() |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 173 | { |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 174 | aaudio_nanoseconds_t startTime; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 175 | ALOGD("AudioStreamInternal(): start()"); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 176 | if (mServiceStreamHandle == AAUDIO_HANDLE_INVALID) { |
| 177 | return AAUDIO_ERROR_INVALID_STATE; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 178 | } |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 179 | const sp<IAAudioService>& aaudioService = getAAudioService(); |
| 180 | if (aaudioService == 0) return AAUDIO_ERROR_NO_SERVICE; |
| 181 | startTime = AAudio_getNanoseconds(AAUDIO_CLOCK_MONOTONIC); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 182 | mClockModel.start(startTime); |
| 183 | processTimestamp(0, startTime); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 184 | setState(AAUDIO_STREAM_STATE_STARTING); |
| 185 | return aaudioService->startStream(mServiceStreamHandle); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 186 | } |
| 187 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 188 | aaudio_result_t AudioStreamInternal::requestPause() |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 189 | { |
| 190 | ALOGD("AudioStreamInternal(): pause()"); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 191 | if (mServiceStreamHandle == AAUDIO_HANDLE_INVALID) { |
| 192 | return AAUDIO_ERROR_INVALID_STATE; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 193 | } |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 194 | const sp<IAAudioService>& aaudioService = getAAudioService(); |
| 195 | if (aaudioService == 0) return AAUDIO_ERROR_NO_SERVICE; |
| 196 | mClockModel.stop(AAudio_getNanoseconds(AAUDIO_CLOCK_MONOTONIC)); |
| 197 | setState(AAUDIO_STREAM_STATE_PAUSING); |
| 198 | return aaudioService->pauseStream(mServiceStreamHandle); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 199 | } |
| 200 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 201 | aaudio_result_t AudioStreamInternal::requestFlush() { |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 202 | ALOGD("AudioStreamInternal(): flush()"); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 203 | if (mServiceStreamHandle == AAUDIO_HANDLE_INVALID) { |
| 204 | return AAUDIO_ERROR_INVALID_STATE; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 205 | } |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 206 | const sp<IAAudioService>& aaudioService = getAAudioService(); |
| 207 | if (aaudioService == 0) return AAUDIO_ERROR_NO_SERVICE; |
| 208 | setState(AAUDIO_STREAM_STATE_FLUSHING); |
| 209 | return aaudioService->flushStream(mServiceStreamHandle); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | void AudioStreamInternal::onFlushFromServer() { |
| 213 | ALOGD("AudioStreamInternal(): onFlushFromServer()"); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 214 | aaudio_position_frames_t readCounter = mAudioEndpoint.getDownDataReadCounter(); |
| 215 | aaudio_position_frames_t writeCounter = mAudioEndpoint.getDownDataWriteCounter(); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 216 | // Bump offset so caller does not see the retrograde motion in getFramesRead(). |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 217 | aaudio_position_frames_t framesFlushed = writeCounter - readCounter; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 218 | mFramesOffsetFromService += framesFlushed; |
| 219 | // Flush written frames by forcing writeCounter to readCounter. |
| 220 | // This is because we cannot move the read counter in the hardware. |
| 221 | mAudioEndpoint.setDownDataWriteCounter(readCounter); |
| 222 | } |
| 223 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 224 | aaudio_result_t AudioStreamInternal::requestStop() |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 225 | { |
| 226 | // TODO better implementation of requestStop() |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 227 | aaudio_result_t result = requestPause(); |
| 228 | if (result == AAUDIO_OK) { |
| 229 | aaudio_stream_state_t state; |
| 230 | result = waitForStateChange(AAUDIO_STREAM_STATE_PAUSING, |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 231 | &state, |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 232 | 500 * AAUDIO_NANOS_PER_MILLISECOND);// TODO temporary code |
| 233 | if (result == AAUDIO_OK) { |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 234 | result = requestFlush(); |
| 235 | } |
| 236 | } |
| 237 | return result; |
| 238 | } |
| 239 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 240 | aaudio_result_t AudioStreamInternal::registerThread() { |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 241 | ALOGD("AudioStreamInternal(): registerThread()"); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 242 | if (mServiceStreamHandle == AAUDIO_HANDLE_INVALID) { |
| 243 | return AAUDIO_ERROR_INVALID_STATE; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 244 | } |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 245 | const sp<IAAudioService>& aaudioService = getAAudioService(); |
| 246 | if (aaudioService == 0) return AAUDIO_ERROR_NO_SERVICE; |
| 247 | return aaudioService->registerAudioThread(mServiceStreamHandle, |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 248 | gettid(), |
| 249 | getPeriodNanoseconds()); |
| 250 | } |
| 251 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 252 | aaudio_result_t AudioStreamInternal::unregisterThread() { |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 253 | ALOGD("AudioStreamInternal(): unregisterThread()"); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 254 | if (mServiceStreamHandle == AAUDIO_HANDLE_INVALID) { |
| 255 | return AAUDIO_ERROR_INVALID_STATE; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 256 | } |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 257 | const sp<IAAudioService>& aaudioService = getAAudioService(); |
| 258 | if (aaudioService == 0) return AAUDIO_ERROR_NO_SERVICE; |
| 259 | return aaudioService->unregisterAudioThread(mServiceStreamHandle, gettid()); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 260 | } |
| 261 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 262 | // TODO use aaudio_clockid_t all the way down to AudioClock |
| 263 | aaudio_result_t AudioStreamInternal::getTimestamp(clockid_t clockId, |
| 264 | aaudio_position_frames_t *framePosition, |
| 265 | aaudio_nanoseconds_t *timeNanoseconds) { |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 266 | // TODO implement using real HAL |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 267 | aaudio_nanoseconds_t time = AudioClock::getNanoseconds(); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 268 | *framePosition = mClockModel.convertTimeToPosition(time); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 269 | *timeNanoseconds = time + (10 * AAUDIO_NANOS_PER_MILLISECOND); // Fake hardware delay |
| 270 | return AAUDIO_OK; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 271 | } |
| 272 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 273 | aaudio_result_t AudioStreamInternal::updateState() { |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 274 | return processCommands(); |
| 275 | } |
| 276 | |
| 277 | #if LOG_TIMESTAMPS |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 278 | static void AudioStreamInternal_LogTimestamp(AAudioServiceMessage &command) { |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 279 | static int64_t oldPosition = 0; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 280 | static aaudio_nanoseconds_t oldTime = 0; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 281 | int64_t framePosition = command.timestamp.position; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 282 | aaudio_nanoseconds_t nanoTime = command.timestamp.timestamp; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 283 | ALOGD("AudioStreamInternal() timestamp says framePosition = %08lld at nanoTime %llu", |
| 284 | (long long) framePosition, |
| 285 | (long long) nanoTime); |
| 286 | int64_t nanosDelta = nanoTime - oldTime; |
| 287 | if (nanosDelta > 0 && oldTime > 0) { |
| 288 | int64_t framesDelta = framePosition - oldPosition; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 289 | int64_t rate = (framesDelta * AAUDIO_NANOS_PER_SECOND) / nanosDelta; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 290 | ALOGD("AudioStreamInternal() - framesDelta = %08lld", (long long) framesDelta); |
| 291 | ALOGD("AudioStreamInternal() - nanosDelta = %08lld", (long long) nanosDelta); |
| 292 | ALOGD("AudioStreamInternal() - measured rate = %llu", (unsigned long long) rate); |
| 293 | } |
| 294 | oldPosition = framePosition; |
| 295 | oldTime = nanoTime; |
| 296 | } |
| 297 | #endif |
| 298 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 299 | aaudio_result_t AudioStreamInternal::onTimestampFromServer(AAudioServiceMessage *message) { |
| 300 | aaudio_position_frames_t framePosition = 0; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 301 | #if LOG_TIMESTAMPS |
| 302 | AudioStreamInternal_LogTimestamp(command); |
| 303 | #endif |
| 304 | framePosition = message->timestamp.position; |
| 305 | processTimestamp(framePosition, message->timestamp.timestamp); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 306 | return AAUDIO_OK; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 307 | } |
| 308 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 309 | aaudio_result_t AudioStreamInternal::onEventFromServer(AAudioServiceMessage *message) { |
| 310 | aaudio_result_t result = AAUDIO_OK; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 311 | ALOGD("processCommands() got event %d", message->event.event); |
| 312 | switch (message->event.event) { |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 313 | case AAUDIO_SERVICE_EVENT_STARTED: |
| 314 | ALOGD("processCommands() got AAUDIO_SERVICE_EVENT_STARTED"); |
| 315 | setState(AAUDIO_STREAM_STATE_STARTED); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 316 | break; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 317 | case AAUDIO_SERVICE_EVENT_PAUSED: |
| 318 | ALOGD("processCommands() got AAUDIO_SERVICE_EVENT_PAUSED"); |
| 319 | setState(AAUDIO_STREAM_STATE_PAUSED); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 320 | break; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 321 | case AAUDIO_SERVICE_EVENT_FLUSHED: |
| 322 | ALOGD("processCommands() got AAUDIO_SERVICE_EVENT_FLUSHED"); |
| 323 | setState(AAUDIO_STREAM_STATE_FLUSHED); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 324 | onFlushFromServer(); |
| 325 | break; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 326 | case AAUDIO_SERVICE_EVENT_CLOSED: |
| 327 | ALOGD("processCommands() got AAUDIO_SERVICE_EVENT_CLOSED"); |
| 328 | setState(AAUDIO_STREAM_STATE_CLOSED); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 329 | break; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 330 | case AAUDIO_SERVICE_EVENT_DISCONNECTED: |
| 331 | result = AAUDIO_ERROR_DISCONNECTED; |
| 332 | ALOGW("WARNING - processCommands() AAUDIO_SERVICE_EVENT_DISCONNECTED"); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 333 | break; |
| 334 | default: |
| 335 | ALOGW("WARNING - processCommands() Unrecognized event = %d", |
| 336 | (int) message->event.event); |
| 337 | break; |
| 338 | } |
| 339 | return result; |
| 340 | } |
| 341 | |
| 342 | // Process all the commands coming from the server. |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 343 | aaudio_result_t AudioStreamInternal::processCommands() { |
| 344 | aaudio_result_t result = AAUDIO_OK; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 345 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 346 | while (result == AAUDIO_OK) { |
| 347 | AAudioServiceMessage message; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 348 | if (mAudioEndpoint.readUpCommand(&message) != 1) { |
| 349 | break; // no command this time, no problem |
| 350 | } |
| 351 | switch (message.what) { |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 352 | case AAudioServiceMessage::code::TIMESTAMP: |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 353 | result = onTimestampFromServer(&message); |
| 354 | break; |
| 355 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 356 | case AAudioServiceMessage::code::EVENT: |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 357 | result = onEventFromServer(&message); |
| 358 | break; |
| 359 | |
| 360 | default: |
| 361 | ALOGW("WARNING - AudioStreamInternal::processCommands() Unrecognized what = %d", |
| 362 | (int) message.what); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 363 | result = AAUDIO_ERROR_UNEXPECTED_VALUE; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 364 | break; |
| 365 | } |
| 366 | } |
| 367 | return result; |
| 368 | } |
| 369 | |
| 370 | // Write the data, block if needed and timeoutMillis > 0 |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 371 | aaudio_result_t AudioStreamInternal::write(const void *buffer, int32_t numFrames, |
| 372 | aaudio_nanoseconds_t timeoutNanoseconds) |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 373 | { |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 374 | aaudio_result_t result = AAUDIO_OK; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 375 | uint8_t* source = (uint8_t*)buffer; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 376 | aaudio_nanoseconds_t currentTimeNanos = AudioClock::getNanoseconds(); |
| 377 | aaudio_nanoseconds_t deadlineNanos = currentTimeNanos + timeoutNanoseconds; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 378 | int32_t framesLeft = numFrames; |
| 379 | // ALOGD("AudioStreamInternal::write(%p, %d) at time %08llu , mState = %d ------------------", |
| 380 | // buffer, numFrames, (unsigned long long) currentTimeNanos, mState); |
| 381 | |
| 382 | // Write until all the data has been written or until a timeout occurs. |
| 383 | while (framesLeft > 0) { |
| 384 | // The call to writeNow() will not block. It will just write as much as it can. |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 385 | aaudio_nanoseconds_t wakeTimeNanos = 0; |
| 386 | aaudio_result_t framesWritten = writeNow(source, framesLeft, |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 387 | currentTimeNanos, &wakeTimeNanos); |
| 388 | // ALOGD("AudioStreamInternal::write() writeNow() framesLeft = %d --> framesWritten = %d", framesLeft, framesWritten); |
| 389 | if (framesWritten < 0) { |
| 390 | result = framesWritten; |
| 391 | break; |
| 392 | } |
| 393 | framesLeft -= (int32_t) framesWritten; |
| 394 | source += framesWritten * getBytesPerFrame(); |
| 395 | |
| 396 | // Should we block? |
| 397 | if (timeoutNanoseconds == 0) { |
| 398 | break; // don't block |
| 399 | } else if (framesLeft > 0) { |
| 400 | //ALOGD("AudioStreamInternal:: original wakeTimeNanos %lld", (long long) wakeTimeNanos); |
| 401 | // clip the wake time to something reasonable |
| 402 | if (wakeTimeNanos < currentTimeNanos) { |
| 403 | wakeTimeNanos = currentTimeNanos; |
| 404 | } |
| 405 | if (wakeTimeNanos > deadlineNanos) { |
| 406 | // If we time out, just return the framesWritten so far. |
| 407 | ALOGE("AudioStreamInternal::write(): timed out after %lld nanos", (long long) timeoutNanoseconds); |
| 408 | break; |
| 409 | } |
| 410 | |
| 411 | //ALOGD("AudioStreamInternal:: sleep until %lld, dur = %lld", (long long) wakeTimeNanos, |
| 412 | // (long long) (wakeTimeNanos - currentTimeNanos)); |
| 413 | AudioClock::sleepForNanos(wakeTimeNanos - currentTimeNanos); |
| 414 | currentTimeNanos = AudioClock::getNanoseconds(); |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | // return error or framesWritten |
| 419 | return (result < 0) ? result : numFrames - framesLeft; |
| 420 | } |
| 421 | |
| 422 | // Write as much data as we can without blocking. |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 423 | aaudio_result_t AudioStreamInternal::writeNow(const void *buffer, int32_t numFrames, |
| 424 | aaudio_nanoseconds_t currentNanoTime, aaudio_nanoseconds_t *wakeTimePtr) { |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 425 | { |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 426 | aaudio_result_t result = processCommands(); |
| 427 | if (result != AAUDIO_OK) { |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 428 | return result; |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | if (mAudioEndpoint.isOutputFreeRunning()) { |
| 433 | // Update data queue based on the timing model. |
| 434 | int64_t estimatedReadCounter = mClockModel.convertTimeToPosition(currentNanoTime); |
| 435 | mAudioEndpoint.setDownDataReadCounter(estimatedReadCounter); |
| 436 | // If the read index passed the write index then consider it an underrun. |
| 437 | if (mAudioEndpoint.getFullFramesAvailable() < 0) { |
| 438 | mXRunCount++; |
| 439 | } |
| 440 | } |
| 441 | // TODO else query from endpoint cuz set by actual reader, maybe |
| 442 | |
| 443 | // Write some data to the buffer. |
| 444 | int32_t framesWritten = mAudioEndpoint.writeDataNow(buffer, numFrames); |
| 445 | if (framesWritten > 0) { |
| 446 | incrementFramesWritten(framesWritten); |
| 447 | } |
| 448 | //ALOGD("AudioStreamInternal::writeNow() - tried to write %d frames, wrote %d", |
| 449 | // numFrames, framesWritten); |
| 450 | |
| 451 | // Calculate an ideal time to wake up. |
| 452 | if (wakeTimePtr != nullptr && framesWritten >= 0) { |
| 453 | // By default wake up a few milliseconds from now. // TODO review |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 454 | aaudio_nanoseconds_t wakeTime = currentNanoTime + (2 * AAUDIO_NANOS_PER_MILLISECOND); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 455 | switch (getState()) { |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 456 | case AAUDIO_STREAM_STATE_OPEN: |
| 457 | case AAUDIO_STREAM_STATE_STARTING: |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 458 | if (framesWritten != 0) { |
| 459 | // Don't wait to write more data. Just prime the buffer. |
| 460 | wakeTime = currentNanoTime; |
| 461 | } |
| 462 | break; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 463 | case AAUDIO_STREAM_STATE_STARTED: // When do we expect the next read burst to occur? |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 464 | { |
| 465 | uint32_t burstSize = mFramesPerBurst; |
| 466 | if (burstSize < 32) { |
| 467 | burstSize = 32; // TODO review |
| 468 | } |
| 469 | |
| 470 | uint64_t nextReadPosition = mAudioEndpoint.getDownDataReadCounter() + burstSize; |
| 471 | wakeTime = mClockModel.convertPositionToTime(nextReadPosition); |
| 472 | } |
| 473 | break; |
| 474 | default: |
| 475 | break; |
| 476 | } |
| 477 | *wakeTimePtr = wakeTime; |
| 478 | |
| 479 | } |
| 480 | // ALOGD("AudioStreamInternal::writeNow finished: now = %llu, read# = %llu, wrote# = %llu", |
| 481 | // (unsigned long long)currentNanoTime, |
| 482 | // (unsigned long long)mAudioEndpoint.getDownDataReadCounter(), |
| 483 | // (unsigned long long)mAudioEndpoint.getDownDataWriteCounter()); |
| 484 | return framesWritten; |
| 485 | } |
| 486 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 487 | aaudio_result_t AudioStreamInternal::waitForStateChange(aaudio_stream_state_t currentState, |
| 488 | aaudio_stream_state_t *nextState, |
| 489 | aaudio_nanoseconds_t timeoutNanoseconds) |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 490 | |
| 491 | { |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 492 | aaudio_result_t result = processCommands(); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 493 | // ALOGD("AudioStreamInternal::waitForStateChange() - processCommands() returned %d", result); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 494 | if (result != AAUDIO_OK) { |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 495 | return result; |
| 496 | } |
| 497 | // TODO replace this polling with a timed sleep on a futex on the message queue |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 498 | int32_t durationNanos = 5 * AAUDIO_NANOS_PER_MILLISECOND; |
| 499 | aaudio_stream_state_t state = getState(); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 500 | // ALOGD("AudioStreamInternal::waitForStateChange() - state = %d", state); |
| 501 | while (state == currentState && timeoutNanoseconds > 0) { |
| 502 | // TODO use futex from service message queue |
| 503 | if (durationNanos > timeoutNanoseconds) { |
| 504 | durationNanos = timeoutNanoseconds; |
| 505 | } |
| 506 | AudioClock::sleepForNanos(durationNanos); |
| 507 | timeoutNanoseconds -= durationNanos; |
| 508 | |
| 509 | result = processCommands(); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 510 | if (result != AAUDIO_OK) { |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 511 | return result; |
| 512 | } |
| 513 | |
| 514 | state = getState(); |
| 515 | // ALOGD("AudioStreamInternal::waitForStateChange() - state = %d", state); |
| 516 | } |
| 517 | if (nextState != nullptr) { |
| 518 | *nextState = state; |
| 519 | } |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 520 | return (state == currentState) ? AAUDIO_ERROR_TIMEOUT : AAUDIO_OK; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 524 | void AudioStreamInternal::processTimestamp(uint64_t position, aaudio_nanoseconds_t time) { |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 525 | mClockModel.processTimestamp( position, time); |
| 526 | } |
| 527 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 528 | aaudio_result_t AudioStreamInternal::setBufferSize(aaudio_size_frames_t requestedFrames, |
| 529 | aaudio_size_frames_t *actualFrames) { |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 530 | return mAudioEndpoint.setBufferSizeInFrames(requestedFrames, actualFrames); |
| 531 | } |
| 532 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 533 | aaudio_size_frames_t AudioStreamInternal::getBufferSize() const |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 534 | { |
| 535 | return mAudioEndpoint.getBufferSizeInFrames(); |
| 536 | } |
| 537 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 538 | aaudio_size_frames_t AudioStreamInternal::getBufferCapacity() const |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 539 | { |
| 540 | return mAudioEndpoint.getBufferCapacityInFrames(); |
| 541 | } |
| 542 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 543 | aaudio_size_frames_t AudioStreamInternal::getFramesPerBurst() const |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 544 | { |
| 545 | return mEndpointDescriptor.downDataQueueDescriptor.framesPerBurst; |
| 546 | } |
| 547 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 548 | aaudio_position_frames_t AudioStreamInternal::getFramesRead() |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 549 | { |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame^] | 550 | aaudio_position_frames_t framesRead = |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 551 | mClockModel.convertTimeToPosition(AudioClock::getNanoseconds()) |
| 552 | + mFramesOffsetFromService; |
| 553 | // Prevent retrograde motion. |
| 554 | if (framesRead < mLastFramesRead) { |
| 555 | framesRead = mLastFramesRead; |
| 556 | } else { |
| 557 | mLastFramesRead = framesRead; |
| 558 | } |
| 559 | ALOGD("AudioStreamInternal::getFramesRead() returns %lld", (long long)framesRead); |
| 560 | return framesRead; |
| 561 | } |
| 562 | |
| 563 | // TODO implement getTimestamp |