blob: 9cc5cbcc6062874264407a917e0e878097e13033 [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
Phil Burkec89b2e2017-06-20 15:05:06 -070047int32_t AAudioServiceStreamShared::calculateBufferCapacity(int32_t requestedCapacityFrames,
48 int32_t framesPerBurst) {
49
50 if (requestedCapacityFrames > MAX_FRAMES_PER_BUFFER) {
Phil Burk11e8d332017-05-24 09:59:02 -070051 ALOGE("AAudioServiceStreamShared::calculateBufferCapacity() requested capacity %d > max %d",
Phil Burkec89b2e2017-06-20 15:05:06 -070052 requestedCapacityFrames, MAX_FRAMES_PER_BUFFER);
53 return AAUDIO_ERROR_OUT_OF_RANGE;
54 }
55
56 // Determine how many bursts will fit in the buffer.
57 int32_t numBursts;
58 if (requestedCapacityFrames == AAUDIO_UNSPECIFIED) {
59 // Use fewer bursts if default is too many.
60 if ((DEFAULT_BURSTS_PER_BUFFER * framesPerBurst) > MAX_FRAMES_PER_BUFFER) {
61 numBursts = MAX_FRAMES_PER_BUFFER / framesPerBurst;
62 } else {
63 numBursts = DEFAULT_BURSTS_PER_BUFFER;
64 }
65 } else {
66 // round up to nearest burst boundary
67 numBursts = (requestedCapacityFrames + framesPerBurst - 1) / framesPerBurst;
68 }
69
70 // Clip to bare minimum.
71 if (numBursts < MIN_BURSTS_PER_BUFFER) {
72 numBursts = MIN_BURSTS_PER_BUFFER;
73 }
74 // Check for numeric overflow.
75 if (numBursts > 0x8000 || framesPerBurst > 0x8000) {
Phil Burk11e8d332017-05-24 09:59:02 -070076 ALOGE("AAudioServiceStreamShared::calculateBufferCapacity() overflow, capacity = %d * %d",
Phil Burkec89b2e2017-06-20 15:05:06 -070077 numBursts, framesPerBurst);
78 return AAUDIO_ERROR_OUT_OF_RANGE;
79 }
80 int32_t capacityInFrames = numBursts * framesPerBurst;
81
82 // Final sanity check.
83 if (capacityInFrames > MAX_FRAMES_PER_BUFFER) {
Phil Burk11e8d332017-05-24 09:59:02 -070084 ALOGE("AAudioServiceStreamShared::calculateBufferCapacity() calc capacity %d > max %d",
Phil Burkec89b2e2017-06-20 15:05:06 -070085 capacityInFrames, MAX_FRAMES_PER_BUFFER);
86 return AAUDIO_ERROR_OUT_OF_RANGE;
87 }
Phil Burk11e8d332017-05-24 09:59:02 -070088 ALOGD("AAudioServiceStreamShared::calculateBufferCapacity() requested %d frames, actual = %d",
Phil Burkec89b2e2017-06-20 15:05:06 -070089 requestedCapacityFrames, capacityInFrames);
90 return capacityInFrames;
91}
92
Phil Burkc0c70e32017-02-09 13:18:38 -080093aaudio_result_t AAudioServiceStreamShared::open(const aaudio::AAudioStreamRequest &request,
94 aaudio::AAudioStreamConfiguration &configurationOutput) {
95
Phil Burk11e8d332017-05-24 09:59:02 -070096 sp<AAudioServiceStreamShared> keep(this);
97
Phil Burkc0c70e32017-02-09 13:18:38 -080098 aaudio_result_t result = AAudioServiceStreamBase::open(request, configurationOutput);
99 if (result != AAUDIO_OK) {
Phil Burkec89b2e2017-06-20 15:05:06 -0700100 ALOGE("AAudioServiceStreamBase open() returned %d", result);
Phil Burkc0c70e32017-02-09 13:18:38 -0800101 return result;
102 }
103
104 const AAudioStreamConfiguration &configurationInput = request.getConstantConfiguration();
Phil Burkc0c70e32017-02-09 13:18:38 -0800105 aaudio_direction_t direction = request.getDirection();
106
Phil Burkc0c70e32017-02-09 13:18:38 -0800107 AAudioEndpointManager &mEndpointManager = AAudioEndpointManager::getInstance();
Eric Laurenta17ae742017-06-29 15:43:55 -0700108 mServiceEndpoint = mEndpointManager.openEndpoint(mAudioService, configurationOutput, direction);
Phil Burkc0c70e32017-02-09 13:18:38 -0800109 if (mServiceEndpoint == nullptr) {
Phil Burk11e8d332017-05-24 09:59:02 -0700110 ALOGE("AAudioServiceStreamShared::open() mServiceEndPoint = %p", mServiceEndpoint);
Phil Burkc0c70e32017-02-09 13:18:38 -0800111 return AAUDIO_ERROR_UNAVAILABLE;
112 }
113
114 // Is the request compatible with the shared endpoint?
115 mAudioFormat = configurationInput.getAudioFormat();
116 if (mAudioFormat == AAUDIO_FORMAT_UNSPECIFIED) {
117 mAudioFormat = AAUDIO_FORMAT_PCM_FLOAT;
118 } else if (mAudioFormat != AAUDIO_FORMAT_PCM_FLOAT) {
Phil Burk11e8d332017-05-24 09:59:02 -0700119 ALOGE("AAudioServiceStreamShared::open() mAudioFormat = %d, need FLOAT", mAudioFormat);
Phil Burkec89b2e2017-06-20 15:05:06 -0700120 result = AAUDIO_ERROR_INVALID_FORMAT;
121 goto error;
Phil Burkc0c70e32017-02-09 13:18:38 -0800122 }
123
124 mSampleRate = configurationInput.getSampleRate();
Glenn Kasten37a466a2017-05-30 15:53:14 -0700125 if (mSampleRate == AAUDIO_UNSPECIFIED) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800126 mSampleRate = mServiceEndpoint->getSampleRate();
127 } else if (mSampleRate != mServiceEndpoint->getSampleRate()) {
Phil Burk11e8d332017-05-24 09:59:02 -0700128 ALOGE("AAudioServiceStreamShared::open() mSampleRate = %d, need %d",
Phil Burk71f35bb2017-04-13 16:05:07 -0700129 mSampleRate, mServiceEndpoint->getSampleRate());
Phil Burkec89b2e2017-06-20 15:05:06 -0700130 result = AAUDIO_ERROR_INVALID_RATE;
131 goto error;
Phil Burkc0c70e32017-02-09 13:18:38 -0800132 }
133
134 mSamplesPerFrame = configurationInput.getSamplesPerFrame();
Glenn Kasten37a466a2017-05-30 15:53:14 -0700135 if (mSamplesPerFrame == AAUDIO_UNSPECIFIED) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800136 mSamplesPerFrame = mServiceEndpoint->getSamplesPerFrame();
137 } else if (mSamplesPerFrame != mServiceEndpoint->getSamplesPerFrame()) {
Phil Burk11e8d332017-05-24 09:59:02 -0700138 ALOGE("AAudioServiceStreamShared::open() mSamplesPerFrame = %d, need %d",
Phil Burk71f35bb2017-04-13 16:05:07 -0700139 mSamplesPerFrame, mServiceEndpoint->getSamplesPerFrame());
Phil Burkec89b2e2017-06-20 15:05:06 -0700140 result = AAUDIO_ERROR_OUT_OF_RANGE;
141 goto error;
Phil Burkc0c70e32017-02-09 13:18:38 -0800142 }
143
Phil Burkc0c70e32017-02-09 13:18:38 -0800144 mFramesPerBurst = mServiceEndpoint->getFramesPerBurst();
Phil Burk11e8d332017-05-24 09:59:02 -0700145 ALOGD("AAudioServiceStreamShared::open() mSampleRate = %d, mFramesPerBurst = %d",
Phil Burkec89b2e2017-06-20 15:05:06 -0700146 mSampleRate, mFramesPerBurst);
147
148 mCapacityInFrames = calculateBufferCapacity(configurationInput.getBufferCapacity(),
149 mFramesPerBurst);
150 if (mCapacityInFrames < 0) {
151 result = mCapacityInFrames; // negative error code
152 mCapacityInFrames = 0;
153 goto error;
Phil Burkc0c70e32017-02-09 13:18:38 -0800154 }
Phil Burkc0c70e32017-02-09 13:18:38 -0800155
156 // Create audio data shared memory buffer for client.
157 mAudioDataQueue = new SharedRingBuffer();
Phil Burkec89b2e2017-06-20 15:05:06 -0700158 result = mAudioDataQueue->allocate(calculateBytesPerFrame(), mCapacityInFrames);
159 if (result != AAUDIO_OK) {
Phil Burk11e8d332017-05-24 09:59:02 -0700160 ALOGE("AAudioServiceStreamShared::open() could not allocate FIFO with %d frames",
Phil Burkec89b2e2017-06-20 15:05:06 -0700161 mCapacityInFrames);
162 result = AAUDIO_ERROR_NO_MEMORY;
163 goto error;
164 }
165
166 ALOGD("AAudioServiceStreamShared::open() actual rate = %d, channels = %d, deviceId = %d",
167 mSampleRate, mSamplesPerFrame, mServiceEndpoint->getDeviceId());
Phil Burkc0c70e32017-02-09 13:18:38 -0800168
169 // Fill in configuration for client.
170 configurationOutput.setSampleRate(mSampleRate);
171 configurationOutput.setSamplesPerFrame(mSamplesPerFrame);
172 configurationOutput.setAudioFormat(mAudioFormat);
Phil Burkec89b2e2017-06-20 15:05:06 -0700173 configurationOutput.setDeviceId(mServiceEndpoint->getDeviceId());
Phil Burkc0c70e32017-02-09 13:18:38 -0800174
Phil Burk11e8d332017-05-24 09:59:02 -0700175 result = mServiceEndpoint->registerStream(keep);
Phil Burkec89b2e2017-06-20 15:05:06 -0700176 if (result != AAUDIO_OK) {
177 goto error;
178 }
Phil Burkc0c70e32017-02-09 13:18:38 -0800179
180 return AAUDIO_OK;
Phil Burkec89b2e2017-06-20 15:05:06 -0700181
182error:
183 close();
184 return result;
Phil Burkc0c70e32017-02-09 13:18:38 -0800185}
186
187/**
188 * Start the flow of audio data.
189 *
190 * An AAUDIO_SERVICE_EVENT_STARTED will be sent to the client when complete.
191 */
192aaudio_result_t AAudioServiceStreamShared::start() {
Phil Burk71f35bb2017-04-13 16:05:07 -0700193 AAudioServiceEndpoint *endpoint = mServiceEndpoint;
194 if (endpoint == nullptr) {
195 return AAUDIO_ERROR_INVALID_STATE;
196 }
Phil Burk87c9f642017-05-17 07:22:39 -0700197 // For output streams, this will add the stream to the mixer.
Phil Burk71f35bb2017-04-13 16:05:07 -0700198 aaudio_result_t result = endpoint->startStream(this);
Phil Burkc0c70e32017-02-09 13:18:38 -0800199 if (result != AAUDIO_OK) {
200 ALOGE("AAudioServiceStreamShared::start() mServiceEndpoint returned %d", result);
Phil Burk5ef003b2017-06-30 11:43:37 -0700201 disconnect();
Phil Burkc0c70e32017-02-09 13:18:38 -0800202 } else {
203 result = AAudioServiceStreamBase::start();
204 }
Phil Burkec89b2e2017-06-20 15:05:06 -0700205 return result;
Phil Burkc0c70e32017-02-09 13:18:38 -0800206}
207
208/**
209 * Stop the flow of data so that start() can resume without loss of data.
210 *
211 * An AAUDIO_SERVICE_EVENT_PAUSED will be sent to the client when complete.
212*/
213aaudio_result_t AAudioServiceStreamShared::pause() {
Phil Burk71f35bb2017-04-13 16:05:07 -0700214 AAudioServiceEndpoint *endpoint = mServiceEndpoint;
215 if (endpoint == nullptr) {
216 return AAUDIO_ERROR_INVALID_STATE;
217 }
Phil Burk71f35bb2017-04-13 16:05:07 -0700218 aaudio_result_t result = endpoint->stopStream(this);
219 if (result != AAUDIO_OK) {
220 ALOGE("AAudioServiceStreamShared::pause() mServiceEndpoint returned %d", result);
Phil Burk5ef003b2017-06-30 11:43:37 -0700221 disconnect(); // TODO should we return or pause Base first?
Phil Burk71f35bb2017-04-13 16:05:07 -0700222 }
223 return AAudioServiceStreamBase::pause();
224}
225
226aaudio_result_t AAudioServiceStreamShared::stop() {
227 AAudioServiceEndpoint *endpoint = mServiceEndpoint;
228 if (endpoint == nullptr) {
229 return AAUDIO_ERROR_INVALID_STATE;
230 }
Phil Burk71f35bb2017-04-13 16:05:07 -0700231 aaudio_result_t result = endpoint->stopStream(this);
Phil Burkc0c70e32017-02-09 13:18:38 -0800232 if (result != AAUDIO_OK) {
233 ALOGE("AAudioServiceStreamShared::stop() mServiceEndpoint returned %d", result);
Phil Burk5ef003b2017-06-30 11:43:37 -0700234 disconnect();
Phil Burkc0c70e32017-02-09 13:18:38 -0800235 }
Phil Burk71f35bb2017-04-13 16:05:07 -0700236 return AAudioServiceStreamBase::stop();
Phil Burkc0c70e32017-02-09 13:18:38 -0800237}
238
239/**
240 * Discard any data held by the underlying HAL or Service.
241 *
242 * An AAUDIO_SERVICE_EVENT_FLUSHED will be sent to the client when complete.
243 */
244aaudio_result_t AAudioServiceStreamShared::flush() {
Phil Burkec89b2e2017-06-20 15:05:06 -0700245 AAudioServiceEndpoint *endpoint = mServiceEndpoint;
246 if (endpoint == nullptr) {
247 return AAUDIO_ERROR_INVALID_STATE;
248 }
249 if (mState != AAUDIO_STREAM_STATE_PAUSED) {
250 ALOGE("AAudioServiceStreamShared::flush() stream not paused, state = %s",
251 AAudio_convertStreamStateToText(mState));
252 return AAUDIO_ERROR_INVALID_STATE;
253 }
254 // Data will get flushed when the client receives the FLUSHED event.
255 return AAudioServiceStreamBase::flush();
Phil Burkc0c70e32017-02-09 13:18:38 -0800256}
257
258aaudio_result_t AAudioServiceStreamShared::close() {
Phil Burk98d6d922017-07-06 11:52:45 -0700259 if (mState == AAUDIO_STREAM_STATE_CLOSED) {
260 return AAUDIO_OK;
Phil Burk71f35bb2017-04-13 16:05:07 -0700261 }
Phil Burk98d6d922017-07-06 11:52:45 -0700262
263 AAudioServiceEndpoint *endpoint = mServiceEndpoint;
264 if (endpoint == nullptr) {
265 return AAUDIO_ERROR_INVALID_STATE;
266 }
267 endpoint->stopStream(this);
268
269 endpoint->unregisterStream(this);
270
271 AAudioEndpointManager &mEndpointManager = AAudioEndpointManager::getInstance();
272 mEndpointManager.closeEndpoint(endpoint);
273 mServiceEndpoint = nullptr;
274
Phil Burk942bdc02017-05-03 11:36:50 -0700275 if (mAudioDataQueue != nullptr) {
276 delete mAudioDataQueue;
277 mAudioDataQueue = nullptr;
278 }
Phil Burk98d6d922017-07-06 11:52:45 -0700279
Phil Burkc0c70e32017-02-09 13:18:38 -0800280 return AAudioServiceStreamBase::close();
281}
282
283/**
284 * Get an immutable description of the data queue created by this service.
285 */
286aaudio_result_t AAudioServiceStreamShared::getDownDataDescription(AudioEndpointParcelable &parcelable)
287{
288 // Gather information on the data queue.
289 mAudioDataQueue->fillParcelable(parcelable,
290 parcelable.mDownDataQueueParcelable);
291 parcelable.mDownDataQueueParcelable.setFramesPerBurst(getFramesPerBurst());
292 return AAUDIO_OK;
293}
294
295void AAudioServiceStreamShared::onStop() {
296}
297
Phil Burk71f35bb2017-04-13 16:05:07 -0700298void AAudioServiceStreamShared::markTransferTime(int64_t nanoseconds) {
299 mMarkedPosition = mAudioDataQueue->getFifoBuffer()->getReadCounter();
300 mMarkedTime = nanoseconds;
301}
Phil Burkc0c70e32017-02-09 13:18:38 -0800302
303aaudio_result_t AAudioServiceStreamShared::getFreeRunningPosition(int64_t *positionFrames,
304 int64_t *timeNanos) {
Phil Burk71f35bb2017-04-13 16:05:07 -0700305 // TODO get these two numbers as an atomic pair
306 *positionFrames = mMarkedPosition;
307 *timeNanos = mMarkedTime;
Phil Burkc0c70e32017-02-09 13:18:38 -0800308 return AAUDIO_OK;
309}