blob: e88a81e2b34a0ee5371b1061215ca45bdc7c9822 [file] [log] [blame]
Phil Burkc0c70e32017-02-09 13:18:38 -08001/*
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 Laurentcb4dae22017-07-01 19:39:32 -070017#define LOG_TAG "AAudioServiceStreamShared"
Phil Burkc0c70e32017-02-09 13:18:38 -080018//#define LOG_NDEBUG 0
19#include <utils/Log.h>
20
Phil Burka5222e22017-07-28 13:31:14 -070021#include <iomanip>
22#include <iostream>
Phil Burkc0c70e32017-02-09 13:18:38 -080023#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
36using namespace android;
37using namespace aaudio;
38
Phil Burkec89b2e2017-06-20 15:05:06 -070039#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 Burkc0c70e32017-02-09 13:18:38 -080043
44AAudioServiceStreamShared::AAudioServiceStreamShared(AAudioService &audioService)
Phil Burk39f02dd2017-08-04 09:13:31 -070045 : AAudioServiceStreamBase(audioService)
Phil Burk97350f92017-07-21 15:59:44 -070046 , mTimestampPositionOffset(0)
Phil Burk39f02dd2017-08-04 09:13:31 -070047 , mXRunCount(0) {
Phil Burkc0c70e32017-02-09 13:18:38 -080048}
49
Phil Burka5222e22017-07-28 13:31:14 -070050std::string AAudioServiceStreamShared::dumpHeader() {
51 std::stringstream result;
52 result << AAudioServiceStreamBase::dumpHeader();
53 result << " Write# Read# Avail XRuns";
54 return result.str();
55}
56
57std::string AAudioServiceStreamShared::dump() const {
58 std::stringstream result;
Phil Burk39f02dd2017-08-04 09:13:31 -070059
Phil Burka5222e22017-07-28 13:31:14 -070060 result << AAudioServiceStreamBase::dump();
61
Phil Burk8f4fe502020-07-15 23:54:50 +000062 result << mAudioDataQueue->dump();
Phil Burka5222e22017-07-28 13:31:14 -070063 result << std::setw(8) << getXRunCount();
64
65 return result.str();
66}
67
Phil Burkec89b2e2017-06-20 15:05:06 -070068int32_t AAudioServiceStreamShared::calculateBufferCapacity(int32_t requestedCapacityFrames,
69 int32_t framesPerBurst) {
70
71 if (requestedCapacityFrames > MAX_FRAMES_PER_BUFFER) {
Phil Burkfbf031e2017-10-12 15:58:31 -070072 ALOGE("calculateBufferCapacity() requested capacity %d > max %d",
Phil Burkec89b2e2017-06-20 15:05:06 -070073 requestedCapacityFrames, MAX_FRAMES_PER_BUFFER);
74 return AAUDIO_ERROR_OUT_OF_RANGE;
75 }
76
77 // Determine how many bursts will fit in the buffer.
78 int32_t numBursts;
79 if (requestedCapacityFrames == AAUDIO_UNSPECIFIED) {
80 // Use fewer bursts if default is too many.
81 if ((DEFAULT_BURSTS_PER_BUFFER * framesPerBurst) > MAX_FRAMES_PER_BUFFER) {
82 numBursts = MAX_FRAMES_PER_BUFFER / framesPerBurst;
83 } else {
84 numBursts = DEFAULT_BURSTS_PER_BUFFER;
85 }
86 } else {
87 // round up to nearest burst boundary
88 numBursts = (requestedCapacityFrames + framesPerBurst - 1) / framesPerBurst;
89 }
90
91 // Clip to bare minimum.
92 if (numBursts < MIN_BURSTS_PER_BUFFER) {
93 numBursts = MIN_BURSTS_PER_BUFFER;
94 }
95 // Check for numeric overflow.
96 if (numBursts > 0x8000 || framesPerBurst > 0x8000) {
Phil Burkfbf031e2017-10-12 15:58:31 -070097 ALOGE("calculateBufferCapacity() overflow, capacity = %d * %d",
Phil Burkec89b2e2017-06-20 15:05:06 -070098 numBursts, framesPerBurst);
99 return AAUDIO_ERROR_OUT_OF_RANGE;
100 }
101 int32_t capacityInFrames = numBursts * framesPerBurst;
102
Phil Burk18142ae2020-07-28 12:44:37 -0700103 // Final range check.
Phil Burkec89b2e2017-06-20 15:05:06 -0700104 if (capacityInFrames > MAX_FRAMES_PER_BUFFER) {
Phil Burkfbf031e2017-10-12 15:58:31 -0700105 ALOGE("calculateBufferCapacity() calc capacity %d > max %d",
Phil Burkec89b2e2017-06-20 15:05:06 -0700106 capacityInFrames, MAX_FRAMES_PER_BUFFER);
107 return AAUDIO_ERROR_OUT_OF_RANGE;
108 }
Phil Burk7ba46552019-04-15 08:58:08 -0700109 ALOGV("calculateBufferCapacity() requested %d frames, actual = %d",
Phil Burkec89b2e2017-06-20 15:05:06 -0700110 requestedCapacityFrames, capacityInFrames);
111 return capacityInFrames;
112}
113
Phil Burk39f02dd2017-08-04 09:13:31 -0700114aaudio_result_t AAudioServiceStreamShared::open(const aaudio::AAudioStreamRequest &request) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800115
Phil Burk11e8d332017-05-24 09:59:02 -0700116 sp<AAudioServiceStreamShared> keep(this);
117
Phil Burk15f97c92018-09-04 14:06:27 -0700118 if (request.getConstantConfiguration().getSharingMode() != AAUDIO_SHARING_MODE_SHARED) {
119 ALOGE("%s() sharingMode mismatch %d", __func__,
120 request.getConstantConfiguration().getSharingMode());
121 return AAUDIO_ERROR_INTERNAL;
122 }
123
124 aaudio_result_t result = AAudioServiceStreamBase::open(request);
Phil Burkc0c70e32017-02-09 13:18:38 -0800125 if (result != AAUDIO_OK) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800126 return result;
127 }
128
129 const AAudioStreamConfiguration &configurationInput = request.getConstantConfiguration();
Phil Burkc0c70e32017-02-09 13:18:38 -0800130
Phil Burk6e2770e2018-05-01 13:03:52 -0700131 sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote();
132 if (endpoint == nullptr) {
133 result = AAUDIO_ERROR_INVALID_STATE;
134 goto error;
135 }
136
Phil Burkc0c70e32017-02-09 13:18:38 -0800137 // Is the request compatible with the shared endpoint?
Phil Burk39f02dd2017-08-04 09:13:31 -0700138 setFormat(configurationInput.getFormat());
Phil Burk0127c1b2018-03-29 13:48:06 -0700139 if (getFormat() == AUDIO_FORMAT_DEFAULT) {
140 setFormat(AUDIO_FORMAT_PCM_FLOAT);
141 } else if (getFormat() != AUDIO_FORMAT_PCM_FLOAT) {
Phil Burk7ba46552019-04-15 08:58:08 -0700142 ALOGD("%s() audio_format_t mAudioFormat = %d, need FLOAT", __func__, getFormat());
Phil Burkec89b2e2017-06-20 15:05:06 -0700143 result = AAUDIO_ERROR_INVALID_FORMAT;
144 goto error;
Phil Burkc0c70e32017-02-09 13:18:38 -0800145 }
146
Phil Burk39f02dd2017-08-04 09:13:31 -0700147 setSampleRate(configurationInput.getSampleRate());
148 if (getSampleRate() == AAUDIO_UNSPECIFIED) {
Phil Burk6e2770e2018-05-01 13:03:52 -0700149 setSampleRate(endpoint->getSampleRate());
150 } else if (getSampleRate() != endpoint->getSampleRate()) {
Phil Burk7ba46552019-04-15 08:58:08 -0700151 ALOGD("%s() mSampleRate = %d, need %d",
Phil Burk6e2770e2018-05-01 13:03:52 -0700152 __func__, getSampleRate(), endpoint->getSampleRate());
Phil Burkec89b2e2017-06-20 15:05:06 -0700153 result = AAUDIO_ERROR_INVALID_RATE;
154 goto error;
Phil Burkc0c70e32017-02-09 13:18:38 -0800155 }
156
Phil Burk39f02dd2017-08-04 09:13:31 -0700157 setSamplesPerFrame(configurationInput.getSamplesPerFrame());
158 if (getSamplesPerFrame() == AAUDIO_UNSPECIFIED) {
Phil Burk6e2770e2018-05-01 13:03:52 -0700159 setSamplesPerFrame(endpoint->getSamplesPerFrame());
160 } else if (getSamplesPerFrame() != endpoint->getSamplesPerFrame()) {
Phil Burk7ba46552019-04-15 08:58:08 -0700161 ALOGD("%s() mSamplesPerFrame = %d, need %d",
Phil Burk6e2770e2018-05-01 13:03:52 -0700162 __func__, getSamplesPerFrame(), endpoint->getSamplesPerFrame());
Phil Burkec89b2e2017-06-20 15:05:06 -0700163 result = AAUDIO_ERROR_OUT_OF_RANGE;
164 goto error;
Phil Burkc0c70e32017-02-09 13:18:38 -0800165 }
166
Phil Burk39f02dd2017-08-04 09:13:31 -0700167 setBufferCapacity(calculateBufferCapacity(configurationInput.getBufferCapacity(),
168 mFramesPerBurst));
169 if (getBufferCapacity() < 0) {
170 result = getBufferCapacity(); // negative error code
171 setBufferCapacity(0);
Phil Burkec89b2e2017-06-20 15:05:06 -0700172 goto error;
Phil Burkc0c70e32017-02-09 13:18:38 -0800173 }
Phil Burkc0c70e32017-02-09 13:18:38 -0800174
Phil Burk523b3042017-09-13 13:03:08 -0700175 {
176 std::lock_guard<std::mutex> lock(mAudioDataQueueLock);
177 // Create audio data shared memory buffer for client.
Phil Burk8f4fe502020-07-15 23:54:50 +0000178 mAudioDataQueue = std::make_shared<SharedRingBuffer>();
Phil Burk523b3042017-09-13 13:03:08 -0700179 result = mAudioDataQueue->allocate(calculateBytesPerFrame(), getBufferCapacity());
180 if (result != AAUDIO_OK) {
Phil Burk55e5eab2018-04-10 15:16:38 -0700181 ALOGE("%s() could not allocate FIFO with %d frames",
182 __func__, getBufferCapacity());
Phil Burk523b3042017-09-13 13:03:08 -0700183 result = AAUDIO_ERROR_NO_MEMORY;
184 goto error;
185 }
Phil Burkec89b2e2017-06-20 15:05:06 -0700186 }
187
Phil Burk6e2770e2018-05-01 13:03:52 -0700188 result = endpoint->registerStream(keep);
Phil Burkec89b2e2017-06-20 15:05:06 -0700189 if (result != AAUDIO_OK) {
190 goto error;
191 }
Phil Burkc0c70e32017-02-09 13:18:38 -0800192
Phil Burk5a26e662017-07-07 12:44:48 -0700193 setState(AAUDIO_STREAM_STATE_OPEN);
Phil Burkc0c70e32017-02-09 13:18:38 -0800194 return AAUDIO_OK;
Phil Burkec89b2e2017-06-20 15:05:06 -0700195
196error:
197 close();
198 return result;
Phil Burkc0c70e32017-02-09 13:18:38 -0800199}
200
Phil Burkc0c70e32017-02-09 13:18:38 -0800201/**
202 * Get an immutable description of the data queue created by this service.
203 */
Phil Burk523b3042017-09-13 13:03:08 -0700204aaudio_result_t AAudioServiceStreamShared::getAudioDataDescription(
205 AudioEndpointParcelable &parcelable)
Phil Burkc0c70e32017-02-09 13:18:38 -0800206{
Phil Burk523b3042017-09-13 13:03:08 -0700207 std::lock_guard<std::mutex> lock(mAudioDataQueueLock);
208 if (mAudioDataQueue == nullptr) {
Phil Burk7ba46552019-04-15 08:58:08 -0700209 ALOGW("%s(): mUpMessageQueue null! - stream not open", __func__);
Phil Burk523b3042017-09-13 13:03:08 -0700210 return AAUDIO_ERROR_NULL;
211 }
Phil Burkc0c70e32017-02-09 13:18:38 -0800212 // Gather information on the data queue.
213 mAudioDataQueue->fillParcelable(parcelable,
214 parcelable.mDownDataQueueParcelable);
215 parcelable.mDownDataQueueParcelable.setFramesPerBurst(getFramesPerBurst());
216 return AAUDIO_OK;
217}
218
Phil Burk97350f92017-07-21 15:59:44 -0700219void AAudioServiceStreamShared::markTransferTime(Timestamp &timestamp) {
Phil Burka53ffa62018-10-10 16:21:37 -0700220 mAtomicStreamTimestamp.write(timestamp);
Phil Burk71f35bb2017-04-13 16:05:07 -0700221}
Phil Burkc0c70e32017-02-09 13:18:38 -0800222
Phil Burk39f02dd2017-08-04 09:13:31 -0700223// Get timestamp that was written by mixer or distributor.
Phil Burkc0c70e32017-02-09 13:18:38 -0800224aaudio_result_t AAudioServiceStreamShared::getFreeRunningPosition(int64_t *positionFrames,
Phil Burk39f02dd2017-08-04 09:13:31 -0700225 int64_t *timeNanos) {
226 // TODO Get presentation timestamp from the HAL
Phil Burka53ffa62018-10-10 16:21:37 -0700227 if (mAtomicStreamTimestamp.isValid()) {
228 Timestamp timestamp = mAtomicStreamTimestamp.read();
Phil Burk97350f92017-07-21 15:59:44 -0700229 *positionFrames = timestamp.getPosition();
230 *timeNanos = timestamp.getNanoseconds();
231 return AAUDIO_OK;
232 } else {
233 return AAUDIO_ERROR_UNAVAILABLE;
234 }
235}
236
237// Get timestamp from lower level service.
238aaudio_result_t AAudioServiceStreamShared::getHardwareTimestamp(int64_t *positionFrames,
Phil Burk39f02dd2017-08-04 09:13:31 -0700239 int64_t *timeNanos) {
Phil Burk97350f92017-07-21 15:59:44 -0700240
Phil Burkbcc36742017-08-31 17:24:51 -0700241 int64_t position = 0;
Phil Burk6e2770e2018-05-01 13:03:52 -0700242 sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote();
243 if (endpoint == nullptr) {
Phil Burk7ba46552019-04-15 08:58:08 -0700244 ALOGW("%s() has no endpoint", __func__);
Phil Burk6e2770e2018-05-01 13:03:52 -0700245 return AAUDIO_ERROR_INVALID_STATE;
246 }
247
248 aaudio_result_t result = endpoint->getTimestamp(&position, timeNanos);
Phil Burk97350f92017-07-21 15:59:44 -0700249 if (result == AAUDIO_OK) {
Phil Burkbcc36742017-08-31 17:24:51 -0700250 int64_t offset = mTimestampPositionOffset.load();
251 // TODO, do not go below starting value
252 position -= offset; // Offset from shared MMAP stream
Phil Burk55e5eab2018-04-10 15:16:38 -0700253 ALOGV("%s() %8lld = %8lld - %8lld",
254 __func__, (long long) position, (long long) (position + offset), (long long) offset);
Phil Burk97350f92017-07-21 15:59:44 -0700255 }
Phil Burkbcc36742017-08-31 17:24:51 -0700256 *positionFrames = position;
Phil Burk97350f92017-07-21 15:59:44 -0700257 return result;
Phil Burkc0c70e32017-02-09 13:18:38 -0800258}
Phil Burk8f4fe502020-07-15 23:54:50 +0000259
260void AAudioServiceStreamShared::writeDataIfRoom(int64_t mmapFramesRead,
261 const void *buffer, int32_t numFrames) {
262 int64_t clientFramesWritten = 0;
263
264 // Lock the AudioFifo to protect against close.
265 std::lock_guard <std::mutex> lock(mAudioDataQueueLock);
266
267 if (mAudioDataQueue != nullptr) {
268 std::shared_ptr<FifoBuffer> fifo = mAudioDataQueue->getFifoBuffer();
269 // Determine offset between framePosition in client's stream
270 // vs the underlying MMAP stream.
271 clientFramesWritten = fifo->getWriteCounter();
272 // There are two indices that refer to the same frame.
273 int64_t positionOffset = mmapFramesRead - clientFramesWritten;
274 setTimestampPositionOffset(positionOffset);
275
276 // Is the buffer too full to write a burst?
277 if (fifo->getEmptyFramesAvailable() < getFramesPerBurst()) {
278 incrementXRunCount();
279 } else {
280 fifo->write(buffer, numFrames);
281 }
282 clientFramesWritten = fifo->getWriteCounter();
283 }
284
285 if (clientFramesWritten > 0) {
286 // This timestamp represents the completion of data being written into the
287 // client buffer. It is sent to the client and used in the timing model
288 // to decide when data will be available to read.
289 Timestamp timestamp(clientFramesWritten, AudioClock::getNanoseconds());
290 markTransferTime(timestamp);
291 }
292}