blob: f246fc0211769a5cb536f3d148f7c7e330c73cf1 [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
17#define LOG_TAG "AAudioService"
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
34using namespace android;
35using namespace aaudio;
36
Phil Burkec89b2e2017-06-20 15:05:06 -070037#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 Burkc0c70e32017-02-09 13:18:38 -080041
42AAudioServiceStreamShared::AAudioServiceStreamShared(AAudioService &audioService)
43 : mAudioService(audioService)
44 {
45}
46
47AAudioServiceStreamShared::~AAudioServiceStreamShared() {
48 close();
49}
50
Phil Burkec89b2e2017-06-20 15:05:06 -070051int32_t AAudioServiceStreamShared::calculateBufferCapacity(int32_t requestedCapacityFrames,
52 int32_t framesPerBurst) {
53
54 if (requestedCapacityFrames > MAX_FRAMES_PER_BUFFER) {
55 ALOGE("AAudioServiceStreamShared::open(), requested capacity %d > max %d",
56 requestedCapacityFrames, MAX_FRAMES_PER_BUFFER);
57 return AAUDIO_ERROR_OUT_OF_RANGE;
58 }
59
60 // Determine how many bursts will fit in the buffer.
61 int32_t numBursts;
62 if (requestedCapacityFrames == AAUDIO_UNSPECIFIED) {
63 // Use fewer bursts if default is too many.
64 if ((DEFAULT_BURSTS_PER_BUFFER * framesPerBurst) > MAX_FRAMES_PER_BUFFER) {
65 numBursts = MAX_FRAMES_PER_BUFFER / framesPerBurst;
66 } else {
67 numBursts = DEFAULT_BURSTS_PER_BUFFER;
68 }
69 } else {
70 // round up to nearest burst boundary
71 numBursts = (requestedCapacityFrames + framesPerBurst - 1) / framesPerBurst;
72 }
73
74 // Clip to bare minimum.
75 if (numBursts < MIN_BURSTS_PER_BUFFER) {
76 numBursts = MIN_BURSTS_PER_BUFFER;
77 }
78 // Check for numeric overflow.
79 if (numBursts > 0x8000 || framesPerBurst > 0x8000) {
80 ALOGE("AAudioServiceStreamShared::open(), numeric overflow, capacity = %d * %d",
81 numBursts, framesPerBurst);
82 return AAUDIO_ERROR_OUT_OF_RANGE;
83 }
84 int32_t capacityInFrames = numBursts * framesPerBurst;
85
86 // Final sanity check.
87 if (capacityInFrames > MAX_FRAMES_PER_BUFFER) {
88 ALOGE("AAudioServiceStreamShared::open(), calculated capacity %d > max %d",
89 capacityInFrames, MAX_FRAMES_PER_BUFFER);
90 return AAUDIO_ERROR_OUT_OF_RANGE;
91 }
92 ALOGD("AAudioServiceStreamShared::open(), requested capacity = %d frames, actual = %d",
93 requestedCapacityFrames, capacityInFrames);
94 return capacityInFrames;
95}
96
Phil Burkc0c70e32017-02-09 13:18:38 -080097aaudio_result_t AAudioServiceStreamShared::open(const aaudio::AAudioStreamRequest &request,
98 aaudio::AAudioStreamConfiguration &configurationOutput) {
99
100 aaudio_result_t result = AAudioServiceStreamBase::open(request, configurationOutput);
101 if (result != AAUDIO_OK) {
Phil Burkec89b2e2017-06-20 15:05:06 -0700102 ALOGE("AAudioServiceStreamBase open() returned %d", result);
Phil Burkc0c70e32017-02-09 13:18:38 -0800103 return result;
104 }
105
106 const AAudioStreamConfiguration &configurationInput = request.getConstantConfiguration();
107 int32_t deviceId = configurationInput.getDeviceId();
108 aaudio_direction_t direction = request.getDirection();
109
Phil Burkc0c70e32017-02-09 13:18:38 -0800110 AAudioEndpointManager &mEndpointManager = AAudioEndpointManager::getInstance();
Phil Burk71f35bb2017-04-13 16:05:07 -0700111 mServiceEndpoint = mEndpointManager.openEndpoint(mAudioService, deviceId, direction);
Phil Burkc0c70e32017-02-09 13:18:38 -0800112 if (mServiceEndpoint == nullptr) {
Phil Burk87c9f642017-05-17 07:22:39 -0700113 ALOGE("AAudioServiceStreamShared::open(), mServiceEndPoint = %p", mServiceEndpoint);
Phil Burkc0c70e32017-02-09 13:18:38 -0800114 return AAUDIO_ERROR_UNAVAILABLE;
115 }
116
117 // Is the request compatible with the shared endpoint?
118 mAudioFormat = configurationInput.getAudioFormat();
119 if (mAudioFormat == AAUDIO_FORMAT_UNSPECIFIED) {
120 mAudioFormat = AAUDIO_FORMAT_PCM_FLOAT;
121 } else if (mAudioFormat != AAUDIO_FORMAT_PCM_FLOAT) {
Phil Burk71f35bb2017-04-13 16:05:07 -0700122 ALOGE("AAudioServiceStreamShared::open(), mAudioFormat = %d, need FLOAT", mAudioFormat);
Phil Burkec89b2e2017-06-20 15:05:06 -0700123 result = AAUDIO_ERROR_INVALID_FORMAT;
124 goto error;
Phil Burkc0c70e32017-02-09 13:18:38 -0800125 }
126
127 mSampleRate = configurationInput.getSampleRate();
Glenn Kasten37a466a2017-05-30 15:53:14 -0700128 if (mSampleRate == AAUDIO_UNSPECIFIED) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800129 mSampleRate = mServiceEndpoint->getSampleRate();
130 } else if (mSampleRate != mServiceEndpoint->getSampleRate()) {
Phil Burkec89b2e2017-06-20 15:05:06 -0700131 ALOGE("AAudioServiceStreamShared::open(), mSampleRate = %d, need %d",
Phil Burk71f35bb2017-04-13 16:05:07 -0700132 mSampleRate, mServiceEndpoint->getSampleRate());
Phil Burkec89b2e2017-06-20 15:05:06 -0700133 result = AAUDIO_ERROR_INVALID_RATE;
134 goto error;
Phil Burkc0c70e32017-02-09 13:18:38 -0800135 }
136
137 mSamplesPerFrame = configurationInput.getSamplesPerFrame();
Glenn Kasten37a466a2017-05-30 15:53:14 -0700138 if (mSamplesPerFrame == AAUDIO_UNSPECIFIED) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800139 mSamplesPerFrame = mServiceEndpoint->getSamplesPerFrame();
140 } else if (mSamplesPerFrame != mServiceEndpoint->getSamplesPerFrame()) {
Phil Burk71f35bb2017-04-13 16:05:07 -0700141 ALOGE("AAudioServiceStreamShared::open(), mSamplesPerFrame = %d, need %d",
142 mSamplesPerFrame, mServiceEndpoint->getSamplesPerFrame());
Phil Burkec89b2e2017-06-20 15:05:06 -0700143 result = AAUDIO_ERROR_OUT_OF_RANGE;
144 goto error;
Phil Burkc0c70e32017-02-09 13:18:38 -0800145 }
146
Phil Burkc0c70e32017-02-09 13:18:38 -0800147 mFramesPerBurst = mServiceEndpoint->getFramesPerBurst();
Phil Burkec89b2e2017-06-20 15:05:06 -0700148 ALOGD("AAudioServiceStreamShared::open(), mSampleRate = %d, mFramesPerBurst = %d",
149 mSampleRate, mFramesPerBurst);
150
151 mCapacityInFrames = calculateBufferCapacity(configurationInput.getBufferCapacity(),
152 mFramesPerBurst);
153 if (mCapacityInFrames < 0) {
154 result = mCapacityInFrames; // negative error code
155 mCapacityInFrames = 0;
156 goto error;
Phil Burkc0c70e32017-02-09 13:18:38 -0800157 }
Phil Burkc0c70e32017-02-09 13:18:38 -0800158
159 // Create audio data shared memory buffer for client.
160 mAudioDataQueue = new SharedRingBuffer();
Phil Burkec89b2e2017-06-20 15:05:06 -0700161 result = mAudioDataQueue->allocate(calculateBytesPerFrame(), mCapacityInFrames);
162 if (result != AAUDIO_OK) {
163 ALOGE("AAudioServiceStreamShared::open(), could not allocate FIFO with %d frames",
164 mCapacityInFrames);
165 result = AAUDIO_ERROR_NO_MEMORY;
166 goto error;
167 }
168
169 ALOGD("AAudioServiceStreamShared::open() actual rate = %d, channels = %d, deviceId = %d",
170 mSampleRate, mSamplesPerFrame, mServiceEndpoint->getDeviceId());
Phil Burkc0c70e32017-02-09 13:18:38 -0800171
172 // Fill in configuration for client.
173 configurationOutput.setSampleRate(mSampleRate);
174 configurationOutput.setSamplesPerFrame(mSamplesPerFrame);
175 configurationOutput.setAudioFormat(mAudioFormat);
Phil Burkec89b2e2017-06-20 15:05:06 -0700176 configurationOutput.setDeviceId(mServiceEndpoint->getDeviceId());
Phil Burkc0c70e32017-02-09 13:18:38 -0800177
Phil Burkec89b2e2017-06-20 15:05:06 -0700178 result = mServiceEndpoint->registerStream(this);
179 if (result != AAUDIO_OK) {
180 goto error;
181 }
Phil Burkc0c70e32017-02-09 13:18:38 -0800182
183 return AAUDIO_OK;
Phil Burkec89b2e2017-06-20 15:05:06 -0700184
185error:
186 close();
187 return result;
Phil Burkc0c70e32017-02-09 13:18:38 -0800188}
189
190/**
191 * Start the flow of audio data.
192 *
193 * An AAUDIO_SERVICE_EVENT_STARTED will be sent to the client when complete.
194 */
195aaudio_result_t AAudioServiceStreamShared::start() {
Phil Burk71f35bb2017-04-13 16:05:07 -0700196 AAudioServiceEndpoint *endpoint = mServiceEndpoint;
197 if (endpoint == nullptr) {
198 return AAUDIO_ERROR_INVALID_STATE;
199 }
Phil Burk87c9f642017-05-17 07:22:39 -0700200 // For output streams, this will add the stream to the mixer.
Phil Burk71f35bb2017-04-13 16:05:07 -0700201 aaudio_result_t result = endpoint->startStream(this);
Phil Burkc0c70e32017-02-09 13:18:38 -0800202 if (result != AAUDIO_OK) {
203 ALOGE("AAudioServiceStreamShared::start() mServiceEndpoint returned %d", result);
Phil Burkec89b2e2017-06-20 15:05:06 -0700204 processFatalError();
Phil Burkc0c70e32017-02-09 13:18:38 -0800205 } else {
206 result = AAudioServiceStreamBase::start();
207 }
Phil Burkec89b2e2017-06-20 15:05:06 -0700208 return result;
Phil Burkc0c70e32017-02-09 13:18:38 -0800209}
210
211/**
212 * Stop the flow of data so that start() can resume without loss of data.
213 *
214 * An AAUDIO_SERVICE_EVENT_PAUSED will be sent to the client when complete.
215*/
216aaudio_result_t AAudioServiceStreamShared::pause() {
Phil Burk71f35bb2017-04-13 16:05:07 -0700217 AAudioServiceEndpoint *endpoint = mServiceEndpoint;
218 if (endpoint == nullptr) {
219 return AAUDIO_ERROR_INVALID_STATE;
220 }
Phil Burk71f35bb2017-04-13 16:05:07 -0700221 aaudio_result_t result = endpoint->stopStream(this);
222 if (result != AAUDIO_OK) {
223 ALOGE("AAudioServiceStreamShared::pause() mServiceEndpoint returned %d", result);
Phil Burkec89b2e2017-06-20 15:05:06 -0700224 processFatalError();
Phil Burk71f35bb2017-04-13 16:05:07 -0700225 }
226 return AAudioServiceStreamBase::pause();
227}
228
229aaudio_result_t AAudioServiceStreamShared::stop() {
230 AAudioServiceEndpoint *endpoint = mServiceEndpoint;
231 if (endpoint == nullptr) {
232 return AAUDIO_ERROR_INVALID_STATE;
233 }
Phil Burk71f35bb2017-04-13 16:05:07 -0700234 aaudio_result_t result = endpoint->stopStream(this);
Phil Burkc0c70e32017-02-09 13:18:38 -0800235 if (result != AAUDIO_OK) {
236 ALOGE("AAudioServiceStreamShared::stop() mServiceEndpoint returned %d", result);
Phil Burkec89b2e2017-06-20 15:05:06 -0700237 processFatalError();
Phil Burkc0c70e32017-02-09 13:18:38 -0800238 }
Phil Burk71f35bb2017-04-13 16:05:07 -0700239 return AAudioServiceStreamBase::stop();
Phil Burkc0c70e32017-02-09 13:18:38 -0800240}
241
242/**
243 * Discard any data held by the underlying HAL or Service.
244 *
245 * An AAUDIO_SERVICE_EVENT_FLUSHED will be sent to the client when complete.
246 */
247aaudio_result_t AAudioServiceStreamShared::flush() {
Phil Burkec89b2e2017-06-20 15:05:06 -0700248 AAudioServiceEndpoint *endpoint = mServiceEndpoint;
249 if (endpoint == nullptr) {
250 return AAUDIO_ERROR_INVALID_STATE;
251 }
252 if (mState != AAUDIO_STREAM_STATE_PAUSED) {
253 ALOGE("AAudioServiceStreamShared::flush() stream not paused, state = %s",
254 AAudio_convertStreamStateToText(mState));
255 return AAUDIO_ERROR_INVALID_STATE;
256 }
257 // Data will get flushed when the client receives the FLUSHED event.
258 return AAudioServiceStreamBase::flush();
Phil Burkc0c70e32017-02-09 13:18:38 -0800259}
260
261aaudio_result_t AAudioServiceStreamShared::close() {
262 pause();
263 // TODO wait for pause() to synchronize
Phil Burk71f35bb2017-04-13 16:05:07 -0700264 AAudioServiceEndpoint *endpoint = mServiceEndpoint;
265 if (endpoint != nullptr) {
266 endpoint->unregisterStream(this);
267
268 AAudioEndpointManager &mEndpointManager = AAudioEndpointManager::getInstance();
269 mEndpointManager.closeEndpoint(endpoint);
270 mServiceEndpoint = nullptr;
271 }
Phil Burk942bdc02017-05-03 11:36:50 -0700272 if (mAudioDataQueue != nullptr) {
273 delete mAudioDataQueue;
274 mAudioDataQueue = nullptr;
275 }
Phil Burkc0c70e32017-02-09 13:18:38 -0800276 return AAudioServiceStreamBase::close();
277}
278
279/**
280 * Get an immutable description of the data queue created by this service.
281 */
282aaudio_result_t AAudioServiceStreamShared::getDownDataDescription(AudioEndpointParcelable &parcelable)
283{
284 // Gather information on the data queue.
285 mAudioDataQueue->fillParcelable(parcelable,
286 parcelable.mDownDataQueueParcelable);
287 parcelable.mDownDataQueueParcelable.setFramesPerBurst(getFramesPerBurst());
288 return AAUDIO_OK;
289}
290
291void AAudioServiceStreamShared::onStop() {
292}
293
294void AAudioServiceStreamShared::onDisconnect() {
295 mServiceEndpoint->close();
296 mServiceEndpoint = nullptr;
297}
298
Phil Burk71f35bb2017-04-13 16:05:07 -0700299void AAudioServiceStreamShared::markTransferTime(int64_t nanoseconds) {
300 mMarkedPosition = mAudioDataQueue->getFifoBuffer()->getReadCounter();
301 mMarkedTime = nanoseconds;
302}
Phil Burkc0c70e32017-02-09 13:18:38 -0800303
304aaudio_result_t AAudioServiceStreamShared::getFreeRunningPosition(int64_t *positionFrames,
305 int64_t *timeNanos) {
Phil Burk71f35bb2017-04-13 16:05:07 -0700306 // TODO get these two numbers as an atomic pair
307 *positionFrames = mMarkedPosition;
308 *timeNanos = mMarkedTime;
Phil Burkc0c70e32017-02-09 13:18:38 -0800309 return AAUDIO_OK;
310}