Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 17 | #define LOG_TAG "AAudioServiceStreamShared" |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 18 | //#define LOG_NDEBUG 0 |
| 19 | #include <utils/Log.h> |
| 20 | |
| 21 | #include <mutex> |
| 22 | |
| 23 | #include <aaudio/AAudio.h> |
| 24 | |
| 25 | #include "binding/IAAudioService.h" |
| 26 | |
| 27 | #include "binding/AAudioServiceMessage.h" |
| 28 | #include "AAudioServiceStreamBase.h" |
| 29 | #include "AAudioServiceStreamShared.h" |
| 30 | #include "AAudioEndpointManager.h" |
| 31 | #include "AAudioService.h" |
| 32 | #include "AAudioServiceEndpoint.h" |
| 33 | |
| 34 | using namespace android; |
| 35 | using namespace aaudio; |
| 36 | |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 37 | #define MIN_BURSTS_PER_BUFFER 2 |
| 38 | #define DEFAULT_BURSTS_PER_BUFFER 16 |
| 39 | // This is an arbitrary range. TODO review. |
| 40 | #define MAX_FRAMES_PER_BUFFER (32 * 1024) |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 41 | |
| 42 | AAudioServiceStreamShared::AAudioServiceStreamShared(AAudioService &audioService) |
| 43 | : mAudioService(audioService) |
| 44 | { |
| 45 | } |
| 46 | |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 47 | int32_t AAudioServiceStreamShared::calculateBufferCapacity(int32_t requestedCapacityFrames, |
| 48 | int32_t framesPerBurst) { |
| 49 | |
| 50 | if (requestedCapacityFrames > MAX_FRAMES_PER_BUFFER) { |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 51 | ALOGE("AAudioServiceStreamShared::calculateBufferCapacity() requested capacity %d > max %d", |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 52 | requestedCapacityFrames, MAX_FRAMES_PER_BUFFER); |
| 53 | return AAUDIO_ERROR_OUT_OF_RANGE; |
| 54 | } |
| 55 | |
| 56 | // Determine how many bursts will fit in the buffer. |
| 57 | int32_t numBursts; |
| 58 | if (requestedCapacityFrames == AAUDIO_UNSPECIFIED) { |
| 59 | // Use fewer bursts if default is too many. |
| 60 | if ((DEFAULT_BURSTS_PER_BUFFER * framesPerBurst) > MAX_FRAMES_PER_BUFFER) { |
| 61 | numBursts = MAX_FRAMES_PER_BUFFER / framesPerBurst; |
| 62 | } else { |
| 63 | numBursts = DEFAULT_BURSTS_PER_BUFFER; |
| 64 | } |
| 65 | } else { |
| 66 | // round up to nearest burst boundary |
| 67 | numBursts = (requestedCapacityFrames + framesPerBurst - 1) / framesPerBurst; |
| 68 | } |
| 69 | |
| 70 | // Clip to bare minimum. |
| 71 | if (numBursts < MIN_BURSTS_PER_BUFFER) { |
| 72 | numBursts = MIN_BURSTS_PER_BUFFER; |
| 73 | } |
| 74 | // Check for numeric overflow. |
| 75 | if (numBursts > 0x8000 || framesPerBurst > 0x8000) { |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 76 | ALOGE("AAudioServiceStreamShared::calculateBufferCapacity() overflow, capacity = %d * %d", |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 77 | numBursts, framesPerBurst); |
| 78 | return AAUDIO_ERROR_OUT_OF_RANGE; |
| 79 | } |
| 80 | int32_t capacityInFrames = numBursts * framesPerBurst; |
| 81 | |
| 82 | // Final sanity check. |
| 83 | if (capacityInFrames > MAX_FRAMES_PER_BUFFER) { |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 84 | ALOGE("AAudioServiceStreamShared::calculateBufferCapacity() calc capacity %d > max %d", |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 85 | capacityInFrames, MAX_FRAMES_PER_BUFFER); |
| 86 | return AAUDIO_ERROR_OUT_OF_RANGE; |
| 87 | } |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 88 | ALOGD("AAudioServiceStreamShared::calculateBufferCapacity() requested %d frames, actual = %d", |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 89 | requestedCapacityFrames, capacityInFrames); |
| 90 | return capacityInFrames; |
| 91 | } |
| 92 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 93 | aaudio_result_t AAudioServiceStreamShared::open(const aaudio::AAudioStreamRequest &request, |
| 94 | aaudio::AAudioStreamConfiguration &configurationOutput) { |
| 95 | |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 96 | sp<AAudioServiceStreamShared> keep(this); |
| 97 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 98 | aaudio_result_t result = AAudioServiceStreamBase::open(request, configurationOutput); |
| 99 | if (result != AAUDIO_OK) { |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 100 | ALOGE("AAudioServiceStreamBase open() returned %d", result); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 101 | return result; |
| 102 | } |
| 103 | |
| 104 | const AAudioStreamConfiguration &configurationInput = request.getConstantConfiguration(); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 105 | aaudio_direction_t direction = request.getDirection(); |
| 106 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 107 | AAudioEndpointManager &mEndpointManager = AAudioEndpointManager::getInstance(); |
Eric Laurent | a17ae74 | 2017-06-29 15:43:55 -0700 | [diff] [blame] | 108 | mServiceEndpoint = mEndpointManager.openEndpoint(mAudioService, configurationOutput, direction); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 109 | if (mServiceEndpoint == nullptr) { |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 110 | ALOGE("AAudioServiceStreamShared::open() mServiceEndPoint = %p", mServiceEndpoint); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 111 | return AAUDIO_ERROR_UNAVAILABLE; |
| 112 | } |
| 113 | |
| 114 | // Is the request compatible with the shared endpoint? |
jiabin | 901f65d | 2017-07-12 17:56:35 -0700 | [diff] [blame] | 115 | mAudioFormat = configurationInput.getFormat(); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 116 | if (mAudioFormat == AAUDIO_FORMAT_UNSPECIFIED) { |
| 117 | mAudioFormat = AAUDIO_FORMAT_PCM_FLOAT; |
| 118 | } else if (mAudioFormat != AAUDIO_FORMAT_PCM_FLOAT) { |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 119 | ALOGE("AAudioServiceStreamShared::open() mAudioFormat = %d, need FLOAT", mAudioFormat); |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 120 | result = AAUDIO_ERROR_INVALID_FORMAT; |
| 121 | goto error; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | mSampleRate = configurationInput.getSampleRate(); |
Glenn Kasten | 37a466a | 2017-05-30 15:53:14 -0700 | [diff] [blame] | 125 | if (mSampleRate == AAUDIO_UNSPECIFIED) { |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 126 | mSampleRate = mServiceEndpoint->getSampleRate(); |
| 127 | } else if (mSampleRate != mServiceEndpoint->getSampleRate()) { |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 128 | ALOGE("AAudioServiceStreamShared::open() mSampleRate = %d, need %d", |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 129 | mSampleRate, mServiceEndpoint->getSampleRate()); |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 130 | result = AAUDIO_ERROR_INVALID_RATE; |
| 131 | goto error; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | mSamplesPerFrame = configurationInput.getSamplesPerFrame(); |
Glenn Kasten | 37a466a | 2017-05-30 15:53:14 -0700 | [diff] [blame] | 135 | if (mSamplesPerFrame == AAUDIO_UNSPECIFIED) { |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 136 | mSamplesPerFrame = mServiceEndpoint->getSamplesPerFrame(); |
| 137 | } else if (mSamplesPerFrame != mServiceEndpoint->getSamplesPerFrame()) { |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 138 | ALOGE("AAudioServiceStreamShared::open() mSamplesPerFrame = %d, need %d", |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 139 | mSamplesPerFrame, mServiceEndpoint->getSamplesPerFrame()); |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 140 | result = AAUDIO_ERROR_OUT_OF_RANGE; |
| 141 | goto error; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 142 | } |
| 143 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 144 | mFramesPerBurst = mServiceEndpoint->getFramesPerBurst(); |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 145 | ALOGD("AAudioServiceStreamShared::open() mSampleRate = %d, mFramesPerBurst = %d", |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 146 | mSampleRate, mFramesPerBurst); |
| 147 | |
| 148 | mCapacityInFrames = calculateBufferCapacity(configurationInput.getBufferCapacity(), |
| 149 | mFramesPerBurst); |
| 150 | if (mCapacityInFrames < 0) { |
| 151 | result = mCapacityInFrames; // negative error code |
| 152 | mCapacityInFrames = 0; |
| 153 | goto error; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 154 | } |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 155 | |
| 156 | // Create audio data shared memory buffer for client. |
| 157 | mAudioDataQueue = new SharedRingBuffer(); |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 158 | result = mAudioDataQueue->allocate(calculateBytesPerFrame(), mCapacityInFrames); |
| 159 | if (result != AAUDIO_OK) { |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 160 | ALOGE("AAudioServiceStreamShared::open() could not allocate FIFO with %d frames", |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 161 | mCapacityInFrames); |
| 162 | result = AAUDIO_ERROR_NO_MEMORY; |
| 163 | goto error; |
| 164 | } |
| 165 | |
| 166 | ALOGD("AAudioServiceStreamShared::open() actual rate = %d, channels = %d, deviceId = %d", |
| 167 | mSampleRate, mSamplesPerFrame, mServiceEndpoint->getDeviceId()); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 168 | |
| 169 | // Fill in configuration for client. |
| 170 | configurationOutput.setSampleRate(mSampleRate); |
| 171 | configurationOutput.setSamplesPerFrame(mSamplesPerFrame); |
jiabin | 901f65d | 2017-07-12 17:56:35 -0700 | [diff] [blame] | 172 | configurationOutput.setFormat(mAudioFormat); |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 173 | configurationOutput.setDeviceId(mServiceEndpoint->getDeviceId()); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 174 | |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 175 | result = mServiceEndpoint->registerStream(keep); |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 176 | if (result != AAUDIO_OK) { |
| 177 | goto error; |
| 178 | } |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 179 | |
Phil Burk | 5a26e66 | 2017-07-07 12:44:48 -0700 | [diff] [blame] | 180 | setState(AAUDIO_STREAM_STATE_OPEN); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 181 | return AAUDIO_OK; |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 182 | |
| 183 | error: |
| 184 | close(); |
| 185 | return result; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Start the flow of audio data. |
| 190 | * |
| 191 | * An AAUDIO_SERVICE_EVENT_STARTED will be sent to the client when complete. |
| 192 | */ |
| 193 | aaudio_result_t AAudioServiceStreamShared::start() { |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 194 | if (isRunning()) { |
| 195 | return AAUDIO_OK; |
| 196 | } |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 197 | AAudioServiceEndpoint *endpoint = mServiceEndpoint; |
| 198 | if (endpoint == nullptr) { |
| 199 | return AAUDIO_ERROR_INVALID_STATE; |
| 200 | } |
Phil Burk | 87c9f64 | 2017-05-17 07:22:39 -0700 | [diff] [blame] | 201 | // For output streams, this will add the stream to the mixer. |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 202 | aaudio_result_t result = endpoint->startStream(this); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 203 | if (result != AAUDIO_OK) { |
| 204 | ALOGE("AAudioServiceStreamShared::start() mServiceEndpoint returned %d", result); |
Phil Burk | 5ef003b | 2017-06-30 11:43:37 -0700 | [diff] [blame] | 205 | disconnect(); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 206 | } else { |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 207 | result = endpoint->getStreamInternal()->startClient(mMmapClient, &mClientHandle); |
| 208 | if (result == AAUDIO_OK) { |
| 209 | result = AAudioServiceStreamBase::start(); |
| 210 | } |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 211 | } |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 212 | return result; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | /** |
| 216 | * Stop the flow of data so that start() can resume without loss of data. |
| 217 | * |
| 218 | * An AAUDIO_SERVICE_EVENT_PAUSED will be sent to the client when complete. |
| 219 | */ |
| 220 | aaudio_result_t AAudioServiceStreamShared::pause() { |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 221 | if (!isRunning()) { |
| 222 | return AAUDIO_OK; |
| 223 | } |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 224 | AAudioServiceEndpoint *endpoint = mServiceEndpoint; |
| 225 | if (endpoint == nullptr) { |
| 226 | return AAUDIO_ERROR_INVALID_STATE; |
| 227 | } |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 228 | endpoint->getStreamInternal()->stopClient(mClientHandle); |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 229 | aaudio_result_t result = endpoint->stopStream(this); |
| 230 | if (result != AAUDIO_OK) { |
| 231 | ALOGE("AAudioServiceStreamShared::pause() mServiceEndpoint returned %d", result); |
Phil Burk | 5ef003b | 2017-06-30 11:43:37 -0700 | [diff] [blame] | 232 | disconnect(); // TODO should we return or pause Base first? |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 233 | } |
| 234 | return AAudioServiceStreamBase::pause(); |
| 235 | } |
| 236 | |
| 237 | aaudio_result_t AAudioServiceStreamShared::stop() { |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 238 | if (!isRunning()) { |
| 239 | return AAUDIO_OK; |
| 240 | } |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 241 | AAudioServiceEndpoint *endpoint = mServiceEndpoint; |
| 242 | if (endpoint == nullptr) { |
| 243 | return AAUDIO_ERROR_INVALID_STATE; |
| 244 | } |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 245 | endpoint->getStreamInternal()->stopClient(mClientHandle); |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 246 | aaudio_result_t result = endpoint->stopStream(this); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 247 | if (result != AAUDIO_OK) { |
| 248 | ALOGE("AAudioServiceStreamShared::stop() mServiceEndpoint returned %d", result); |
Phil Burk | 5ef003b | 2017-06-30 11:43:37 -0700 | [diff] [blame] | 249 | disconnect(); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 250 | } |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 251 | return AAudioServiceStreamBase::stop(); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | /** |
| 255 | * Discard any data held by the underlying HAL or Service. |
| 256 | * |
| 257 | * An AAUDIO_SERVICE_EVENT_FLUSHED will be sent to the client when complete. |
| 258 | */ |
| 259 | aaudio_result_t AAudioServiceStreamShared::flush() { |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 260 | AAudioServiceEndpoint *endpoint = mServiceEndpoint; |
| 261 | if (endpoint == nullptr) { |
| 262 | return AAUDIO_ERROR_INVALID_STATE; |
| 263 | } |
| 264 | if (mState != AAUDIO_STREAM_STATE_PAUSED) { |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 265 | ALOGE("AAudioServiceStreamShared::flush() stream not paused, state = %s", |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 266 | AAudio_convertStreamStateToText(mState)); |
| 267 | return AAUDIO_ERROR_INVALID_STATE; |
| 268 | } |
| 269 | // Data will get flushed when the client receives the FLUSHED event. |
| 270 | return AAudioServiceStreamBase::flush(); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | aaudio_result_t AAudioServiceStreamShared::close() { |
Phil Burk | 98d6d92 | 2017-07-06 11:52:45 -0700 | [diff] [blame] | 274 | if (mState == AAUDIO_STREAM_STATE_CLOSED) { |
| 275 | return AAUDIO_OK; |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 276 | } |
Phil Burk | 98d6d92 | 2017-07-06 11:52:45 -0700 | [diff] [blame] | 277 | |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 278 | stop(); |
| 279 | |
Phil Burk | 98d6d92 | 2017-07-06 11:52:45 -0700 | [diff] [blame] | 280 | AAudioServiceEndpoint *endpoint = mServiceEndpoint; |
| 281 | if (endpoint == nullptr) { |
| 282 | return AAUDIO_ERROR_INVALID_STATE; |
| 283 | } |
Phil Burk | 98d6d92 | 2017-07-06 11:52:45 -0700 | [diff] [blame] | 284 | |
| 285 | endpoint->unregisterStream(this); |
| 286 | |
| 287 | AAudioEndpointManager &mEndpointManager = AAudioEndpointManager::getInstance(); |
| 288 | mEndpointManager.closeEndpoint(endpoint); |
| 289 | mServiceEndpoint = nullptr; |
| 290 | |
Phil Burk | 942bdc0 | 2017-05-03 11:36:50 -0700 | [diff] [blame] | 291 | if (mAudioDataQueue != nullptr) { |
| 292 | delete mAudioDataQueue; |
| 293 | mAudioDataQueue = nullptr; |
| 294 | } |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 295 | return AAudioServiceStreamBase::close(); |
| 296 | } |
| 297 | |
| 298 | /** |
| 299 | * Get an immutable description of the data queue created by this service. |
| 300 | */ |
| 301 | aaudio_result_t AAudioServiceStreamShared::getDownDataDescription(AudioEndpointParcelable &parcelable) |
| 302 | { |
| 303 | // Gather information on the data queue. |
| 304 | mAudioDataQueue->fillParcelable(parcelable, |
| 305 | parcelable.mDownDataQueueParcelable); |
| 306 | parcelable.mDownDataQueueParcelable.setFramesPerBurst(getFramesPerBurst()); |
| 307 | return AAUDIO_OK; |
| 308 | } |
| 309 | |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 310 | void AAudioServiceStreamShared::markTransferTime(int64_t nanoseconds) { |
| 311 | mMarkedPosition = mAudioDataQueue->getFifoBuffer()->getReadCounter(); |
| 312 | mMarkedTime = nanoseconds; |
| 313 | } |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 314 | |
| 315 | aaudio_result_t AAudioServiceStreamShared::getFreeRunningPosition(int64_t *positionFrames, |
| 316 | int64_t *timeNanos) { |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 317 | // TODO get these two numbers as an atomic pair |
| 318 | *positionFrames = mMarkedPosition; |
| 319 | *timeNanos = mMarkedTime; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 320 | return AAUDIO_OK; |
| 321 | } |