blob: 2ca847a05209924943c13027ad99d6c29069c777 [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
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 Burkec89b2e2017-06-20 15:05:06 -070073int32_t AAudioServiceStreamShared::calculateBufferCapacity(int32_t requestedCapacityFrames,
74 int32_t framesPerBurst) {
75
76 if (requestedCapacityFrames > MAX_FRAMES_PER_BUFFER) {
Phil Burkfbf031e2017-10-12 15:58:31 -070077 ALOGE("calculateBufferCapacity() requested capacity %d > max %d",
Phil Burkec89b2e2017-06-20 15:05:06 -070078 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 Burkfbf031e2017-10-12 15:58:31 -0700102 ALOGE("calculateBufferCapacity() overflow, capacity = %d * %d",
Phil Burkec89b2e2017-06-20 15:05:06 -0700103 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 Burkfbf031e2017-10-12 15:58:31 -0700110 ALOGE("calculateBufferCapacity() calc capacity %d > max %d",
Phil Burkec89b2e2017-06-20 15:05:06 -0700111 capacityInFrames, MAX_FRAMES_PER_BUFFER);
112 return AAUDIO_ERROR_OUT_OF_RANGE;
113 }
Phil Burk29ccc292019-04-15 08:58:08 -0700114 ALOGV("calculateBufferCapacity() requested %d frames, actual = %d",
Phil Burkec89b2e2017-06-20 15:05:06 -0700115 requestedCapacityFrames, capacityInFrames);
116 return capacityInFrames;
117}
118
Phil Burk39f02dd2017-08-04 09:13:31 -0700119aaudio_result_t AAudioServiceStreamShared::open(const aaudio::AAudioStreamRequest &request) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800120
Phil Burk11e8d332017-05-24 09:59:02 -0700121 sp<AAudioServiceStreamShared> keep(this);
122
Phil Burk15f97c92018-09-04 14:06:27 -0700123 if (request.getConstantConfiguration().getSharingMode() != AAUDIO_SHARING_MODE_SHARED) {
124 ALOGE("%s() sharingMode mismatch %d", __func__,
125 request.getConstantConfiguration().getSharingMode());
126 return AAUDIO_ERROR_INTERNAL;
127 }
128
129 aaudio_result_t result = AAudioServiceStreamBase::open(request);
Phil Burkc0c70e32017-02-09 13:18:38 -0800130 if (result != AAUDIO_OK) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800131 return result;
132 }
133
134 const AAudioStreamConfiguration &configurationInput = request.getConstantConfiguration();
Phil Burkc0c70e32017-02-09 13:18:38 -0800135
Phil Burk6e2770e2018-05-01 13:03:52 -0700136 sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote();
137 if (endpoint == nullptr) {
138 result = AAUDIO_ERROR_INVALID_STATE;
139 goto error;
140 }
141
Phil Burkc0c70e32017-02-09 13:18:38 -0800142 // Is the request compatible with the shared endpoint?
Phil Burk39f02dd2017-08-04 09:13:31 -0700143 setFormat(configurationInput.getFormat());
Phil Burk0127c1b2018-03-29 13:48:06 -0700144 if (getFormat() == AUDIO_FORMAT_DEFAULT) {
145 setFormat(AUDIO_FORMAT_PCM_FLOAT);
146 } else if (getFormat() != AUDIO_FORMAT_PCM_FLOAT) {
Phil Burk29ccc292019-04-15 08:58:08 -0700147 ALOGD("%s() audio_format_t mAudioFormat = %d, need FLOAT", __func__, getFormat());
Phil Burkec89b2e2017-06-20 15:05:06 -0700148 result = AAUDIO_ERROR_INVALID_FORMAT;
149 goto error;
Phil Burkc0c70e32017-02-09 13:18:38 -0800150 }
151
Phil Burk39f02dd2017-08-04 09:13:31 -0700152 setSampleRate(configurationInput.getSampleRate());
153 if (getSampleRate() == AAUDIO_UNSPECIFIED) {
Phil Burk6e2770e2018-05-01 13:03:52 -0700154 setSampleRate(endpoint->getSampleRate());
155 } else if (getSampleRate() != endpoint->getSampleRate()) {
Phil Burk29ccc292019-04-15 08:58:08 -0700156 ALOGD("%s() mSampleRate = %d, need %d",
Phil Burk6e2770e2018-05-01 13:03:52 -0700157 __func__, getSampleRate(), endpoint->getSampleRate());
Phil Burkec89b2e2017-06-20 15:05:06 -0700158 result = AAUDIO_ERROR_INVALID_RATE;
159 goto error;
Phil Burkc0c70e32017-02-09 13:18:38 -0800160 }
161
Phil Burk39f02dd2017-08-04 09:13:31 -0700162 setSamplesPerFrame(configurationInput.getSamplesPerFrame());
163 if (getSamplesPerFrame() == AAUDIO_UNSPECIFIED) {
Phil Burk6e2770e2018-05-01 13:03:52 -0700164 setSamplesPerFrame(endpoint->getSamplesPerFrame());
165 } else if (getSamplesPerFrame() != endpoint->getSamplesPerFrame()) {
Phil Burk29ccc292019-04-15 08:58:08 -0700166 ALOGD("%s() mSamplesPerFrame = %d, need %d",
Phil Burk6e2770e2018-05-01 13:03:52 -0700167 __func__, getSamplesPerFrame(), endpoint->getSamplesPerFrame());
Phil Burkec89b2e2017-06-20 15:05:06 -0700168 result = AAUDIO_ERROR_OUT_OF_RANGE;
169 goto error;
Phil Burkc0c70e32017-02-09 13:18:38 -0800170 }
171
Phil Burk39f02dd2017-08-04 09:13:31 -0700172 setBufferCapacity(calculateBufferCapacity(configurationInput.getBufferCapacity(),
173 mFramesPerBurst));
174 if (getBufferCapacity() < 0) {
175 result = getBufferCapacity(); // negative error code
176 setBufferCapacity(0);
Phil Burkec89b2e2017-06-20 15:05:06 -0700177 goto error;
Phil Burkc0c70e32017-02-09 13:18:38 -0800178 }
Phil Burkc0c70e32017-02-09 13:18:38 -0800179
Phil Burk523b3042017-09-13 13:03:08 -0700180 {
181 std::lock_guard<std::mutex> lock(mAudioDataQueueLock);
182 // Create audio data shared memory buffer for client.
183 mAudioDataQueue = new SharedRingBuffer();
184 result = mAudioDataQueue->allocate(calculateBytesPerFrame(), getBufferCapacity());
185 if (result != AAUDIO_OK) {
Phil Burk55e5eab2018-04-10 15:16:38 -0700186 ALOGE("%s() could not allocate FIFO with %d frames",
187 __func__, getBufferCapacity());
Phil Burk523b3042017-09-13 13:03:08 -0700188 result = AAUDIO_ERROR_NO_MEMORY;
189 goto error;
190 }
Phil Burkec89b2e2017-06-20 15:05:06 -0700191 }
192
Phil Burk6e2770e2018-05-01 13:03:52 -0700193 result = endpoint->registerStream(keep);
Phil Burkec89b2e2017-06-20 15:05:06 -0700194 if (result != AAUDIO_OK) {
195 goto error;
196 }
Phil Burkc0c70e32017-02-09 13:18:38 -0800197
Phil Burk5a26e662017-07-07 12:44:48 -0700198 setState(AAUDIO_STREAM_STATE_OPEN);
Phil Burkc0c70e32017-02-09 13:18:38 -0800199 return AAUDIO_OK;
Phil Burkec89b2e2017-06-20 15:05:06 -0700200
201error:
202 close();
203 return result;
Phil Burkc0c70e32017-02-09 13:18:38 -0800204}
205
Phil Burkc0c70e32017-02-09 13:18:38 -0800206
207aaudio_result_t AAudioServiceStreamShared::close() {
Phil Burk39f02dd2017-08-04 09:13:31 -0700208 aaudio_result_t result = AAudioServiceStreamBase::close();
Phil Burk98d6d922017-07-06 11:52:45 -0700209
Phil Burk523b3042017-09-13 13:03:08 -0700210 {
211 std::lock_guard<std::mutex> lock(mAudioDataQueueLock);
212 delete mAudioDataQueue;
213 mAudioDataQueue = nullptr;
214 }
Eric Laurentcb4dae22017-07-01 19:39:32 -0700215
Phil Burk39f02dd2017-08-04 09:13:31 -0700216 return result;
Phil Burkc0c70e32017-02-09 13:18:38 -0800217}
218
219/**
220 * Get an immutable description of the data queue created by this service.
221 */
Phil Burk523b3042017-09-13 13:03:08 -0700222aaudio_result_t AAudioServiceStreamShared::getAudioDataDescription(
223 AudioEndpointParcelable &parcelable)
Phil Burkc0c70e32017-02-09 13:18:38 -0800224{
Phil Burk523b3042017-09-13 13:03:08 -0700225 std::lock_guard<std::mutex> lock(mAudioDataQueueLock);
226 if (mAudioDataQueue == nullptr) {
Phil Burk29ccc292019-04-15 08:58:08 -0700227 ALOGW("%s(): mUpMessageQueue null! - stream not open", __func__);
Phil Burk523b3042017-09-13 13:03:08 -0700228 return AAUDIO_ERROR_NULL;
229 }
Phil Burkc0c70e32017-02-09 13:18:38 -0800230 // Gather information on the data queue.
231 mAudioDataQueue->fillParcelable(parcelable,
232 parcelable.mDownDataQueueParcelable);
233 parcelable.mDownDataQueueParcelable.setFramesPerBurst(getFramesPerBurst());
234 return AAUDIO_OK;
235}
236
Phil Burk97350f92017-07-21 15:59:44 -0700237void AAudioServiceStreamShared::markTransferTime(Timestamp &timestamp) {
Phil Burka53ffa62018-10-10 16:21:37 -0700238 mAtomicStreamTimestamp.write(timestamp);
Phil Burk71f35bb2017-04-13 16:05:07 -0700239}
Phil Burkc0c70e32017-02-09 13:18:38 -0800240
Phil Burk39f02dd2017-08-04 09:13:31 -0700241// Get timestamp that was written by mixer or distributor.
Phil Burkc0c70e32017-02-09 13:18:38 -0800242aaudio_result_t AAudioServiceStreamShared::getFreeRunningPosition(int64_t *positionFrames,
Phil Burk39f02dd2017-08-04 09:13:31 -0700243 int64_t *timeNanos) {
244 // TODO Get presentation timestamp from the HAL
Phil Burka53ffa62018-10-10 16:21:37 -0700245 if (mAtomicStreamTimestamp.isValid()) {
246 Timestamp timestamp = mAtomicStreamTimestamp.read();
Phil Burk97350f92017-07-21 15:59:44 -0700247 *positionFrames = timestamp.getPosition();
248 *timeNanos = timestamp.getNanoseconds();
249 return AAUDIO_OK;
250 } else {
251 return AAUDIO_ERROR_UNAVAILABLE;
252 }
253}
254
255// Get timestamp from lower level service.
256aaudio_result_t AAudioServiceStreamShared::getHardwareTimestamp(int64_t *positionFrames,
Phil Burk39f02dd2017-08-04 09:13:31 -0700257 int64_t *timeNanos) {
Phil Burk97350f92017-07-21 15:59:44 -0700258
Phil Burkbcc36742017-08-31 17:24:51 -0700259 int64_t position = 0;
Phil Burk6e2770e2018-05-01 13:03:52 -0700260 sp<AAudioServiceEndpoint> endpoint = mServiceEndpointWeak.promote();
261 if (endpoint == nullptr) {
Phil Burk29ccc292019-04-15 08:58:08 -0700262 ALOGW("%s() has no endpoint", __func__);
Phil Burk6e2770e2018-05-01 13:03:52 -0700263 return AAUDIO_ERROR_INVALID_STATE;
264 }
265
266 aaudio_result_t result = endpoint->getTimestamp(&position, timeNanos);
Phil Burk97350f92017-07-21 15:59:44 -0700267 if (result == AAUDIO_OK) {
Phil Burkbcc36742017-08-31 17:24:51 -0700268 int64_t offset = mTimestampPositionOffset.load();
269 // TODO, do not go below starting value
270 position -= offset; // Offset from shared MMAP stream
Phil Burk55e5eab2018-04-10 15:16:38 -0700271 ALOGV("%s() %8lld = %8lld - %8lld",
272 __func__, (long long) position, (long long) (position + offset), (long long) offset);
Phil Burk97350f92017-07-21 15:59:44 -0700273 }
Phil Burkbcc36742017-08-31 17:24:51 -0700274 *positionFrames = position;
Phil Burk97350f92017-07-21 15:59:44 -0700275 return result;
Phil Burkc0c70e32017-02-09 13:18:38 -0800276}