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 | |
Phil Burk | a5222e2 | 2017-07-28 13:31:14 -0700 | [diff] [blame] | 21 | #include <iomanip> |
| 22 | #include <iostream> |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 23 | #include <mutex> |
| 24 | |
| 25 | #include <aaudio/AAudio.h> |
| 26 | |
| 27 | #include "binding/IAAudioService.h" |
| 28 | |
| 29 | #include "binding/AAudioServiceMessage.h" |
| 30 | #include "AAudioServiceStreamBase.h" |
| 31 | #include "AAudioServiceStreamShared.h" |
| 32 | #include "AAudioEndpointManager.h" |
| 33 | #include "AAudioService.h" |
| 34 | #include "AAudioServiceEndpoint.h" |
| 35 | |
| 36 | using namespace android; |
| 37 | using namespace aaudio; |
| 38 | |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 39 | #define MIN_BURSTS_PER_BUFFER 2 |
| 40 | #define DEFAULT_BURSTS_PER_BUFFER 16 |
| 41 | // This is an arbitrary range. TODO review. |
| 42 | #define MAX_FRAMES_PER_BUFFER (32 * 1024) |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 43 | |
| 44 | AAudioServiceStreamShared::AAudioServiceStreamShared(AAudioService &audioService) |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 45 | : AAudioServiceStreamBase(audioService) |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 46 | , mTimestampPositionOffset(0) |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 47 | , mXRunCount(0) { |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 48 | } |
| 49 | |
Phil Burk | a5222e2 | 2017-07-28 13:31:14 -0700 | [diff] [blame] | 50 | std::string AAudioServiceStreamShared::dumpHeader() { |
| 51 | std::stringstream result; |
| 52 | result << AAudioServiceStreamBase::dumpHeader(); |
| 53 | result << " Write# Read# Avail XRuns"; |
| 54 | return result.str(); |
| 55 | } |
| 56 | |
| 57 | std::string AAudioServiceStreamShared::dump() const { |
| 58 | std::stringstream result; |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 59 | |
Phil Burk | a5222e2 | 2017-07-28 13:31:14 -0700 | [diff] [blame] | 60 | result << AAudioServiceStreamBase::dump(); |
| 61 | |
| 62 | auto fifo = mAudioDataQueue->getFifoBuffer(); |
| 63 | int32_t readCounter = fifo->getReadCounter(); |
| 64 | int32_t writeCounter = fifo->getWriteCounter(); |
| 65 | result << std::setw(10) << writeCounter; |
| 66 | result << std::setw(10) << readCounter; |
| 67 | result << std::setw(8) << (writeCounter - readCounter); |
| 68 | result << std::setw(8) << getXRunCount(); |
| 69 | |
| 70 | return result.str(); |
| 71 | } |
| 72 | |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 73 | int32_t AAudioServiceStreamShared::calculateBufferCapacity(int32_t requestedCapacityFrames, |
| 74 | int32_t framesPerBurst) { |
| 75 | |
| 76 | if (requestedCapacityFrames > MAX_FRAMES_PER_BUFFER) { |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 77 | ALOGE("calculateBufferCapacity() requested capacity %d > max %d", |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 78 | requestedCapacityFrames, MAX_FRAMES_PER_BUFFER); |
| 79 | return AAUDIO_ERROR_OUT_OF_RANGE; |
| 80 | } |
| 81 | |
| 82 | // Determine how many bursts will fit in the buffer. |
| 83 | int32_t numBursts; |
| 84 | if (requestedCapacityFrames == AAUDIO_UNSPECIFIED) { |
| 85 | // Use fewer bursts if default is too many. |
| 86 | if ((DEFAULT_BURSTS_PER_BUFFER * framesPerBurst) > MAX_FRAMES_PER_BUFFER) { |
| 87 | numBursts = MAX_FRAMES_PER_BUFFER / framesPerBurst; |
| 88 | } else { |
| 89 | numBursts = DEFAULT_BURSTS_PER_BUFFER; |
| 90 | } |
| 91 | } else { |
| 92 | // round up to nearest burst boundary |
| 93 | numBursts = (requestedCapacityFrames + framesPerBurst - 1) / framesPerBurst; |
| 94 | } |
| 95 | |
| 96 | // Clip to bare minimum. |
| 97 | if (numBursts < MIN_BURSTS_PER_BUFFER) { |
| 98 | numBursts = MIN_BURSTS_PER_BUFFER; |
| 99 | } |
| 100 | // Check for numeric overflow. |
| 101 | if (numBursts > 0x8000 || framesPerBurst > 0x8000) { |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 102 | ALOGE("calculateBufferCapacity() overflow, capacity = %d * %d", |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 103 | numBursts, framesPerBurst); |
| 104 | return AAUDIO_ERROR_OUT_OF_RANGE; |
| 105 | } |
| 106 | int32_t capacityInFrames = numBursts * framesPerBurst; |
| 107 | |
| 108 | // Final sanity check. |
| 109 | if (capacityInFrames > MAX_FRAMES_PER_BUFFER) { |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 110 | ALOGE("calculateBufferCapacity() calc capacity %d > max %d", |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 111 | capacityInFrames, MAX_FRAMES_PER_BUFFER); |
| 112 | return AAUDIO_ERROR_OUT_OF_RANGE; |
| 113 | } |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 114 | ALOGD("calculateBufferCapacity() requested %d frames, actual = %d", |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 115 | requestedCapacityFrames, capacityInFrames); |
| 116 | return capacityInFrames; |
| 117 | } |
| 118 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 119 | aaudio_result_t AAudioServiceStreamShared::open(const aaudio::AAudioStreamRequest &request) { |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 120 | |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 121 | sp<AAudioServiceStreamShared> keep(this); |
| 122 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 123 | aaudio_result_t result = AAudioServiceStreamBase::open(request, AAUDIO_SHARING_MODE_SHARED); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 124 | if (result != AAUDIO_OK) { |
Phil Burk | 55e5eab | 2018-04-10 15:16:38 -0700 | [diff] [blame] | 125 | ALOGE("%s() returned %d", __func__, result); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 126 | return result; |
| 127 | } |
| 128 | |
| 129 | const AAudioStreamConfiguration &configurationInput = request.getConstantConfiguration(); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 130 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 131 | // Is the request compatible with the shared endpoint? |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 132 | setFormat(configurationInput.getFormat()); |
| 133 | if (getFormat() == AAUDIO_FORMAT_UNSPECIFIED) { |
| 134 | setFormat(AAUDIO_FORMAT_PCM_FLOAT); |
| 135 | } else if (getFormat() != AAUDIO_FORMAT_PCM_FLOAT) { |
Phil Burk | 55e5eab | 2018-04-10 15:16:38 -0700 | [diff] [blame] | 136 | ALOGD("%s() mAudioFormat = %d, need FLOAT", __func__, getFormat()); |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 137 | result = AAUDIO_ERROR_INVALID_FORMAT; |
| 138 | goto error; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 139 | } |
| 140 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 141 | setSampleRate(configurationInput.getSampleRate()); |
| 142 | if (getSampleRate() == AAUDIO_UNSPECIFIED) { |
| 143 | setSampleRate(mServiceEndpoint->getSampleRate()); |
| 144 | } else if (getSampleRate() != mServiceEndpoint->getSampleRate()) { |
Phil Burk | 55e5eab | 2018-04-10 15:16:38 -0700 | [diff] [blame] | 145 | ALOGD("%s() mSampleRate = %d, need %d", |
| 146 | __func__, getSampleRate(), mServiceEndpoint->getSampleRate()); |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 147 | result = AAUDIO_ERROR_INVALID_RATE; |
| 148 | goto error; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 149 | } |
| 150 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 151 | setSamplesPerFrame(configurationInput.getSamplesPerFrame()); |
| 152 | if (getSamplesPerFrame() == AAUDIO_UNSPECIFIED) { |
| 153 | setSamplesPerFrame(mServiceEndpoint->getSamplesPerFrame()); |
| 154 | } else if (getSamplesPerFrame() != mServiceEndpoint->getSamplesPerFrame()) { |
Phil Burk | 55e5eab | 2018-04-10 15:16:38 -0700 | [diff] [blame] | 155 | ALOGD("%s() mSamplesPerFrame = %d, need %d", |
| 156 | __func__, getSamplesPerFrame(), mServiceEndpoint->getSamplesPerFrame()); |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 157 | result = AAUDIO_ERROR_OUT_OF_RANGE; |
| 158 | goto error; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 159 | } |
| 160 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 161 | setBufferCapacity(calculateBufferCapacity(configurationInput.getBufferCapacity(), |
| 162 | mFramesPerBurst)); |
| 163 | if (getBufferCapacity() < 0) { |
| 164 | result = getBufferCapacity(); // negative error code |
| 165 | setBufferCapacity(0); |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 166 | goto error; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 167 | } |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 168 | |
Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 169 | { |
| 170 | std::lock_guard<std::mutex> lock(mAudioDataQueueLock); |
| 171 | // Create audio data shared memory buffer for client. |
| 172 | mAudioDataQueue = new SharedRingBuffer(); |
| 173 | result = mAudioDataQueue->allocate(calculateBytesPerFrame(), getBufferCapacity()); |
| 174 | if (result != AAUDIO_OK) { |
Phil Burk | 55e5eab | 2018-04-10 15:16:38 -0700 | [diff] [blame] | 175 | ALOGE("%s() could not allocate FIFO with %d frames", |
| 176 | __func__, getBufferCapacity()); |
Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 177 | result = AAUDIO_ERROR_NO_MEMORY; |
| 178 | goto error; |
| 179 | } |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 180 | } |
| 181 | |
Phil Burk | 11e8d33 | 2017-05-24 09:59:02 -0700 | [diff] [blame] | 182 | result = mServiceEndpoint->registerStream(keep); |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 183 | if (result != AAUDIO_OK) { |
| 184 | goto error; |
| 185 | } |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 186 | |
Phil Burk | 5a26e66 | 2017-07-07 12:44:48 -0700 | [diff] [blame] | 187 | setState(AAUDIO_STREAM_STATE_OPEN); |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 188 | return AAUDIO_OK; |
Phil Burk | ec89b2e | 2017-06-20 15:05:06 -0700 | [diff] [blame] | 189 | |
| 190 | error: |
| 191 | close(); |
| 192 | return result; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 193 | } |
| 194 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 195 | |
| 196 | aaudio_result_t AAudioServiceStreamShared::close() { |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 197 | aaudio_result_t result = AAudioServiceStreamBase::close(); |
Phil Burk | 98d6d92 | 2017-07-06 11:52:45 -0700 | [diff] [blame] | 198 | |
Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 199 | { |
| 200 | std::lock_guard<std::mutex> lock(mAudioDataQueueLock); |
| 201 | delete mAudioDataQueue; |
| 202 | mAudioDataQueue = nullptr; |
| 203 | } |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 204 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 205 | return result; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | /** |
| 209 | * Get an immutable description of the data queue created by this service. |
| 210 | */ |
Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 211 | aaudio_result_t AAudioServiceStreamShared::getAudioDataDescription( |
| 212 | AudioEndpointParcelable &parcelable) |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 213 | { |
Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 214 | std::lock_guard<std::mutex> lock(mAudioDataQueueLock); |
| 215 | if (mAudioDataQueue == nullptr) { |
Phil Burk | 55e5eab | 2018-04-10 15:16:38 -0700 | [diff] [blame] | 216 | ALOGE("%s(): mUpMessageQueue null! - stream not open", __func__); |
Phil Burk | 523b304 | 2017-09-13 13:03:08 -0700 | [diff] [blame] | 217 | return AAUDIO_ERROR_NULL; |
| 218 | } |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 219 | // Gather information on the data queue. |
| 220 | mAudioDataQueue->fillParcelable(parcelable, |
| 221 | parcelable.mDownDataQueueParcelable); |
| 222 | parcelable.mDownDataQueueParcelable.setFramesPerBurst(getFramesPerBurst()); |
| 223 | return AAUDIO_OK; |
| 224 | } |
| 225 | |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 226 | void AAudioServiceStreamShared::markTransferTime(Timestamp ×tamp) { |
| 227 | mAtomicTimestamp.write(timestamp); |
Phil Burk | 71f35bb | 2017-04-13 16:05:07 -0700 | [diff] [blame] | 228 | } |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 229 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 230 | // Get timestamp that was written by mixer or distributor. |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 231 | aaudio_result_t AAudioServiceStreamShared::getFreeRunningPosition(int64_t *positionFrames, |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 232 | int64_t *timeNanos) { |
| 233 | // TODO Get presentation timestamp from the HAL |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 234 | if (mAtomicTimestamp.isValid()) { |
| 235 | Timestamp timestamp = mAtomicTimestamp.read(); |
| 236 | *positionFrames = timestamp.getPosition(); |
| 237 | *timeNanos = timestamp.getNanoseconds(); |
| 238 | return AAUDIO_OK; |
| 239 | } else { |
| 240 | return AAUDIO_ERROR_UNAVAILABLE; |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | // Get timestamp from lower level service. |
| 245 | aaudio_result_t AAudioServiceStreamShared::getHardwareTimestamp(int64_t *positionFrames, |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 246 | int64_t *timeNanos) { |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 247 | |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 248 | int64_t position = 0; |
| 249 | aaudio_result_t result = mServiceEndpoint->getTimestamp(&position, timeNanos); |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 250 | if (result == AAUDIO_OK) { |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 251 | int64_t offset = mTimestampPositionOffset.load(); |
| 252 | // TODO, do not go below starting value |
| 253 | position -= offset; // Offset from shared MMAP stream |
Phil Burk | 55e5eab | 2018-04-10 15:16:38 -0700 | [diff] [blame] | 254 | ALOGV("%s() %8lld = %8lld - %8lld", |
| 255 | __func__, (long long) position, (long long) (position + offset), (long long) offset); |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 256 | } |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 257 | *positionFrames = position; |
Phil Burk | 97350f9 | 2017-07-21 15:59:44 -0700 | [diff] [blame] | 258 | return result; |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 259 | } |