2 * Copyright (C) 2017 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #define LOG_TAG "AAudioServiceStreamShared"
18 //#define LOG_NDEBUG 0
19 #include <utils/Log.h>
25 #include <aaudio/AAudio.h>
27 #include "binding/IAAudioService.h"
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"
36 using namespace android;
37 using namespace aaudio;
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)
44 AAudioServiceStreamShared::AAudioServiceStreamShared(AAudioService &audioService)
45 : AAudioServiceStreamBase(audioService)
46 , mTimestampPositionOffset(0)
50 std::string AAudioServiceStreamShared::dumpHeader() {
51 std::stringstream result;
52 result << AAudioServiceStreamBase::dumpHeader();
53 result << " Write# Read# Avail XRuns";
57 std::string AAudioServiceStreamShared::dump() const {
58 std::stringstream result;
60 result << AAudioServiceStreamBase::dump();
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();
73 int32_t AAudioServiceStreamShared::calculateBufferCapacity(int32_t requestedCapacityFrames,
74 int32_t framesPerBurst) {
76 if (requestedCapacityFrames > MAX_FRAMES_PER_BUFFER) {
77 ALOGE("AAudioServiceStreamShared::calculateBufferCapacity() requested capacity %d > max %d",
78 requestedCapacityFrames, MAX_FRAMES_PER_BUFFER);
79 return AAUDIO_ERROR_OUT_OF_RANGE;
82 // Determine how many bursts will fit in the buffer.
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;
89 numBursts = DEFAULT_BURSTS_PER_BUFFER;
92 // round up to nearest burst boundary
93 numBursts = (requestedCapacityFrames + framesPerBurst - 1) / framesPerBurst;
96 // Clip to bare minimum.
97 if (numBursts < MIN_BURSTS_PER_BUFFER) {
98 numBursts = MIN_BURSTS_PER_BUFFER;
100 // Check for numeric overflow.
101 if (numBursts > 0x8000 || framesPerBurst > 0x8000) {
102 ALOGE("AAudioServiceStreamShared::calculateBufferCapacity() overflow, capacity = %d * %d",
103 numBursts, framesPerBurst);
104 return AAUDIO_ERROR_OUT_OF_RANGE;
106 int32_t capacityInFrames = numBursts * framesPerBurst;
108 // Final sanity check.
109 if (capacityInFrames > MAX_FRAMES_PER_BUFFER) {
110 ALOGE("AAudioServiceStreamShared::calculateBufferCapacity() calc capacity %d > max %d",
111 capacityInFrames, MAX_FRAMES_PER_BUFFER);
112 return AAUDIO_ERROR_OUT_OF_RANGE;
114 ALOGD("AAudioServiceStreamShared::calculateBufferCapacity() requested %d frames, actual = %d",
115 requestedCapacityFrames, capacityInFrames);
116 return capacityInFrames;
119 aaudio_result_t AAudioServiceStreamShared::open(const aaudio::AAudioStreamRequest &request) {
121 sp<AAudioServiceStreamShared> keep(this);
123 aaudio_result_t result = AAudioServiceStreamBase::open(request, AAUDIO_SHARING_MODE_SHARED);
124 if (result != AAUDIO_OK) {
125 ALOGE("AAudioServiceStreamBase open() returned %d", result);
129 const AAudioStreamConfiguration &configurationInput = request.getConstantConfiguration();
132 // Is the request compatible with the shared endpoint?
133 setFormat(configurationInput.getFormat());
134 if (getFormat() == AAUDIO_FORMAT_UNSPECIFIED) {
135 setFormat(AAUDIO_FORMAT_PCM_FLOAT);
136 } else if (getFormat() != AAUDIO_FORMAT_PCM_FLOAT) {
137 ALOGE("AAudioServiceStreamShared::open() mAudioFormat = %d, need FLOAT", getFormat());
138 result = AAUDIO_ERROR_INVALID_FORMAT;
142 setSampleRate(configurationInput.getSampleRate());
143 if (getSampleRate() == AAUDIO_UNSPECIFIED) {
144 setSampleRate(mServiceEndpoint->getSampleRate());
145 } else if (getSampleRate() != mServiceEndpoint->getSampleRate()) {
146 ALOGE("AAudioServiceStreamShared::open() mSampleRate = %d, need %d",
147 getSampleRate(), mServiceEndpoint->getSampleRate());
148 result = AAUDIO_ERROR_INVALID_RATE;
152 setSamplesPerFrame(configurationInput.getSamplesPerFrame());
153 if (getSamplesPerFrame() == AAUDIO_UNSPECIFIED) {
154 setSamplesPerFrame(mServiceEndpoint->getSamplesPerFrame());
155 } else if (getSamplesPerFrame() != mServiceEndpoint->getSamplesPerFrame()) {
156 ALOGE("AAudioServiceStreamShared::open() mSamplesPerFrame = %d, need %d",
157 getSamplesPerFrame(), mServiceEndpoint->getSamplesPerFrame());
158 result = AAUDIO_ERROR_OUT_OF_RANGE;
162 setBufferCapacity(calculateBufferCapacity(configurationInput.getBufferCapacity(),
164 if (getBufferCapacity() < 0) {
165 result = getBufferCapacity(); // negative error code
166 setBufferCapacity(0);
171 std::lock_guard<std::mutex> lock(mAudioDataQueueLock);
172 // Create audio data shared memory buffer for client.
173 mAudioDataQueue = new SharedRingBuffer();
174 result = mAudioDataQueue->allocate(calculateBytesPerFrame(), getBufferCapacity());
175 if (result != AAUDIO_OK) {
176 ALOGE("AAudioServiceStreamShared::open() could not allocate FIFO with %d frames",
177 getBufferCapacity());
178 result = AAUDIO_ERROR_NO_MEMORY;
183 ALOGD("AAudioServiceStreamShared::open() actual rate = %d, channels = %d, deviceId = %d",
184 getSampleRate(), getSamplesPerFrame(), mServiceEndpoint->getDeviceId());
186 result = mServiceEndpoint->registerStream(keep);
187 if (result != AAUDIO_OK) {
191 setState(AAUDIO_STREAM_STATE_OPEN);
200 aaudio_result_t AAudioServiceStreamShared::close() {
201 aaudio_result_t result = AAudioServiceStreamBase::close();
204 std::lock_guard<std::mutex> lock(mAudioDataQueueLock);
205 delete mAudioDataQueue;
206 mAudioDataQueue = nullptr;
213 * Get an immutable description of the data queue created by this service.
215 aaudio_result_t AAudioServiceStreamShared::getAudioDataDescription(
216 AudioEndpointParcelable &parcelable)
218 std::lock_guard<std::mutex> lock(mAudioDataQueueLock);
219 if (mAudioDataQueue == nullptr) {
220 ALOGE("getAudioDataDescription(): mUpMessageQueue null! - stream not open");
221 return AAUDIO_ERROR_NULL;
223 // Gather information on the data queue.
224 mAudioDataQueue->fillParcelable(parcelable,
225 parcelable.mDownDataQueueParcelable);
226 parcelable.mDownDataQueueParcelable.setFramesPerBurst(getFramesPerBurst());
230 void AAudioServiceStreamShared::markTransferTime(Timestamp ×tamp) {
231 mAtomicTimestamp.write(timestamp);
234 // Get timestamp that was written by mixer or distributor.
235 aaudio_result_t AAudioServiceStreamShared::getFreeRunningPosition(int64_t *positionFrames,
236 int64_t *timeNanos) {
237 // TODO Get presentation timestamp from the HAL
238 if (mAtomicTimestamp.isValid()) {
239 Timestamp timestamp = mAtomicTimestamp.read();
240 *positionFrames = timestamp.getPosition();
241 *timeNanos = timestamp.getNanoseconds();
244 return AAUDIO_ERROR_UNAVAILABLE;
248 // Get timestamp from lower level service.
249 aaudio_result_t AAudioServiceStreamShared::getHardwareTimestamp(int64_t *positionFrames,
250 int64_t *timeNanos) {
252 int64_t position = 0;
253 aaudio_result_t result = mServiceEndpoint->getTimestamp(&position, timeNanos);
254 if (result == AAUDIO_OK) {
255 int64_t offset = mTimestampPositionOffset.load();
256 // TODO, do not go below starting value
257 position -= offset; // Offset from shared MMAP stream
258 ALOGV("getHardwareTimestamp() %8lld = %8lld - %8lld",
259 (long long) position, (long long) (position + offset), (long long) offset);
261 *positionFrames = position;