blob: 7515ea731f32506813782ba5e819cf3dfbfbcf95 [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) {
Phil Burk11e8d332017-05-24 09:59:02 -070055 ALOGE("AAudioServiceStreamShared::calculateBufferCapacity() requested capacity %d > max %d",
Phil Burkec89b2e2017-06-20 15:05:06 -070056 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) {
Phil Burk11e8d332017-05-24 09:59:02 -070080 ALOGE("AAudioServiceStreamShared::calculateBufferCapacity() overflow, capacity = %d * %d",
Phil Burkec89b2e2017-06-20 15:05:06 -070081 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) {
Phil Burk11e8d332017-05-24 09:59:02 -070088 ALOGE("AAudioServiceStreamShared::calculateBufferCapacity() calc capacity %d > max %d",
Phil Burkec89b2e2017-06-20 15:05:06 -070089 capacityInFrames, MAX_FRAMES_PER_BUFFER);
90 return AAUDIO_ERROR_OUT_OF_RANGE;
91 }
Phil Burk11e8d332017-05-24 09:59:02 -070092 ALOGD("AAudioServiceStreamShared::calculateBufferCapacity() requested %d frames, actual = %d",
Phil Burkec89b2e2017-06-20 15:05:06 -070093 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
Phil Burk11e8d332017-05-24 09:59:02 -0700100 sp<AAudioServiceStreamShared> keep(this);
101
Phil Burkc0c70e32017-02-09 13:18:38 -0800102 aaudio_result_t result = AAudioServiceStreamBase::open(request, configurationOutput);
103 if (result != AAUDIO_OK) {
Phil Burkec89b2e2017-06-20 15:05:06 -0700104 ALOGE("AAudioServiceStreamBase open() returned %d", result);
Phil Burkc0c70e32017-02-09 13:18:38 -0800105 return result;
106 }
107
108 const AAudioStreamConfiguration &configurationInput = request.getConstantConfiguration();
109 int32_t deviceId = configurationInput.getDeviceId();
110 aaudio_direction_t direction = request.getDirection();
111
Phil Burkc0c70e32017-02-09 13:18:38 -0800112 AAudioEndpointManager &mEndpointManager = AAudioEndpointManager::getInstance();
Phil Burk71f35bb2017-04-13 16:05:07 -0700113 mServiceEndpoint = mEndpointManager.openEndpoint(mAudioService, deviceId, direction);
Phil Burkc0c70e32017-02-09 13:18:38 -0800114 if (mServiceEndpoint == nullptr) {
Phil Burk11e8d332017-05-24 09:59:02 -0700115 ALOGE("AAudioServiceStreamShared::open() mServiceEndPoint = %p", mServiceEndpoint);
Phil Burkc0c70e32017-02-09 13:18:38 -0800116 return AAUDIO_ERROR_UNAVAILABLE;
117 }
118
119 // Is the request compatible with the shared endpoint?
120 mAudioFormat = configurationInput.getAudioFormat();
121 if (mAudioFormat == AAUDIO_FORMAT_UNSPECIFIED) {
122 mAudioFormat = AAUDIO_FORMAT_PCM_FLOAT;
123 } else if (mAudioFormat != AAUDIO_FORMAT_PCM_FLOAT) {
Phil Burk11e8d332017-05-24 09:59:02 -0700124 ALOGE("AAudioServiceStreamShared::open() mAudioFormat = %d, need FLOAT", mAudioFormat);
Phil Burkec89b2e2017-06-20 15:05:06 -0700125 result = AAUDIO_ERROR_INVALID_FORMAT;
126 goto error;
Phil Burkc0c70e32017-02-09 13:18:38 -0800127 }
128
129 mSampleRate = configurationInput.getSampleRate();
Glenn Kasten37a466a2017-05-30 15:53:14 -0700130 if (mSampleRate == AAUDIO_UNSPECIFIED) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800131 mSampleRate = mServiceEndpoint->getSampleRate();
132 } else if (mSampleRate != mServiceEndpoint->getSampleRate()) {
Phil Burk11e8d332017-05-24 09:59:02 -0700133 ALOGE("AAudioServiceStreamShared::open() mSampleRate = %d, need %d",
Phil Burk71f35bb2017-04-13 16:05:07 -0700134 mSampleRate, mServiceEndpoint->getSampleRate());
Phil Burkec89b2e2017-06-20 15:05:06 -0700135 result = AAUDIO_ERROR_INVALID_RATE;
136 goto error;
Phil Burkc0c70e32017-02-09 13:18:38 -0800137 }
138
139 mSamplesPerFrame = configurationInput.getSamplesPerFrame();
Glenn Kasten37a466a2017-05-30 15:53:14 -0700140 if (mSamplesPerFrame == AAUDIO_UNSPECIFIED) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800141 mSamplesPerFrame = mServiceEndpoint->getSamplesPerFrame();
142 } else if (mSamplesPerFrame != mServiceEndpoint->getSamplesPerFrame()) {
Phil Burk11e8d332017-05-24 09:59:02 -0700143 ALOGE("AAudioServiceStreamShared::open() mSamplesPerFrame = %d, need %d",
Phil Burk71f35bb2017-04-13 16:05:07 -0700144 mSamplesPerFrame, mServiceEndpoint->getSamplesPerFrame());
Phil Burkec89b2e2017-06-20 15:05:06 -0700145 result = AAUDIO_ERROR_OUT_OF_RANGE;
146 goto error;
Phil Burkc0c70e32017-02-09 13:18:38 -0800147 }
148
Phil Burkc0c70e32017-02-09 13:18:38 -0800149 mFramesPerBurst = mServiceEndpoint->getFramesPerBurst();
Phil Burk11e8d332017-05-24 09:59:02 -0700150 ALOGD("AAudioServiceStreamShared::open() mSampleRate = %d, mFramesPerBurst = %d",
Phil Burkec89b2e2017-06-20 15:05:06 -0700151 mSampleRate, mFramesPerBurst);
152
153 mCapacityInFrames = calculateBufferCapacity(configurationInput.getBufferCapacity(),
154 mFramesPerBurst);
155 if (mCapacityInFrames < 0) {
156 result = mCapacityInFrames; // negative error code
157 mCapacityInFrames = 0;
158 goto error;
Phil Burkc0c70e32017-02-09 13:18:38 -0800159 }
Phil Burkc0c70e32017-02-09 13:18:38 -0800160
161 // Create audio data shared memory buffer for client.
162 mAudioDataQueue = new SharedRingBuffer();
Phil Burkec89b2e2017-06-20 15:05:06 -0700163 result = mAudioDataQueue->allocate(calculateBytesPerFrame(), mCapacityInFrames);
164 if (result != AAUDIO_OK) {
Phil Burk11e8d332017-05-24 09:59:02 -0700165 ALOGE("AAudioServiceStreamShared::open() could not allocate FIFO with %d frames",
Phil Burkec89b2e2017-06-20 15:05:06 -0700166 mCapacityInFrames);
167 result = AAUDIO_ERROR_NO_MEMORY;
168 goto error;
169 }
170
171 ALOGD("AAudioServiceStreamShared::open() actual rate = %d, channels = %d, deviceId = %d",
172 mSampleRate, mSamplesPerFrame, mServiceEndpoint->getDeviceId());
Phil Burkc0c70e32017-02-09 13:18:38 -0800173
174 // Fill in configuration for client.
175 configurationOutput.setSampleRate(mSampleRate);
176 configurationOutput.setSamplesPerFrame(mSamplesPerFrame);
177 configurationOutput.setAudioFormat(mAudioFormat);
Phil Burkec89b2e2017-06-20 15:05:06 -0700178 configurationOutput.setDeviceId(mServiceEndpoint->getDeviceId());
Phil Burkc0c70e32017-02-09 13:18:38 -0800179
Phil Burk11e8d332017-05-24 09:59:02 -0700180 result = mServiceEndpoint->registerStream(keep);
Phil Burkec89b2e2017-06-20 15:05:06 -0700181 if (result != AAUDIO_OK) {
182 goto error;
183 }
Phil Burkc0c70e32017-02-09 13:18:38 -0800184
185 return AAUDIO_OK;
Phil Burkec89b2e2017-06-20 15:05:06 -0700186
187error:
188 close();
189 return result;
Phil Burkc0c70e32017-02-09 13:18:38 -0800190}
191
192/**
193 * Start the flow of audio data.
194 *
195 * An AAUDIO_SERVICE_EVENT_STARTED will be sent to the client when complete.
196 */
197aaudio_result_t AAudioServiceStreamShared::start() {
Phil Burk71f35bb2017-04-13 16:05:07 -0700198 AAudioServiceEndpoint *endpoint = mServiceEndpoint;
199 if (endpoint == nullptr) {
200 return AAUDIO_ERROR_INVALID_STATE;
201 }
Phil Burk87c9f642017-05-17 07:22:39 -0700202 // For output streams, this will add the stream to the mixer.
Phil Burk71f35bb2017-04-13 16:05:07 -0700203 aaudio_result_t result = endpoint->startStream(this);
Phil Burkc0c70e32017-02-09 13:18:38 -0800204 if (result != AAUDIO_OK) {
205 ALOGE("AAudioServiceStreamShared::start() mServiceEndpoint returned %d", result);
Phil Burkec89b2e2017-06-20 15:05:06 -0700206 processFatalError();
Phil Burkc0c70e32017-02-09 13:18:38 -0800207 } else {
208 result = AAudioServiceStreamBase::start();
209 }
Phil Burkec89b2e2017-06-20 15:05:06 -0700210 return result;
Phil Burkc0c70e32017-02-09 13:18:38 -0800211}
212
213/**
214 * Stop the flow of data so that start() can resume without loss of data.
215 *
216 * An AAUDIO_SERVICE_EVENT_PAUSED will be sent to the client when complete.
217*/
218aaudio_result_t AAudioServiceStreamShared::pause() {
Phil Burk71f35bb2017-04-13 16:05:07 -0700219 AAudioServiceEndpoint *endpoint = mServiceEndpoint;
220 if (endpoint == nullptr) {
221 return AAUDIO_ERROR_INVALID_STATE;
222 }
Phil Burk71f35bb2017-04-13 16:05:07 -0700223 aaudio_result_t result = endpoint->stopStream(this);
224 if (result != AAUDIO_OK) {
225 ALOGE("AAudioServiceStreamShared::pause() mServiceEndpoint returned %d", result);
Phil Burkec89b2e2017-06-20 15:05:06 -0700226 processFatalError();
Phil Burk71f35bb2017-04-13 16:05:07 -0700227 }
228 return AAudioServiceStreamBase::pause();
229}
230
231aaudio_result_t AAudioServiceStreamShared::stop() {
232 AAudioServiceEndpoint *endpoint = mServiceEndpoint;
233 if (endpoint == nullptr) {
234 return AAUDIO_ERROR_INVALID_STATE;
235 }
Phil Burk71f35bb2017-04-13 16:05:07 -0700236 aaudio_result_t result = endpoint->stopStream(this);
Phil Burkc0c70e32017-02-09 13:18:38 -0800237 if (result != AAUDIO_OK) {
238 ALOGE("AAudioServiceStreamShared::stop() mServiceEndpoint returned %d", result);
Phil Burkec89b2e2017-06-20 15:05:06 -0700239 processFatalError();
Phil Burkc0c70e32017-02-09 13:18:38 -0800240 }
Phil Burk71f35bb2017-04-13 16:05:07 -0700241 return AAudioServiceStreamBase::stop();
Phil Burkc0c70e32017-02-09 13:18:38 -0800242}
243
244/**
245 * Discard any data held by the underlying HAL or Service.
246 *
247 * An AAUDIO_SERVICE_EVENT_FLUSHED will be sent to the client when complete.
248 */
249aaudio_result_t AAudioServiceStreamShared::flush() {
Phil Burkec89b2e2017-06-20 15:05:06 -0700250 AAudioServiceEndpoint *endpoint = mServiceEndpoint;
251 if (endpoint == nullptr) {
252 return AAUDIO_ERROR_INVALID_STATE;
253 }
254 if (mState != AAUDIO_STREAM_STATE_PAUSED) {
255 ALOGE("AAudioServiceStreamShared::flush() stream not paused, state = %s",
256 AAudio_convertStreamStateToText(mState));
257 return AAUDIO_ERROR_INVALID_STATE;
258 }
259 // Data will get flushed when the client receives the FLUSHED event.
260 return AAudioServiceStreamBase::flush();
Phil Burkc0c70e32017-02-09 13:18:38 -0800261}
262
263aaudio_result_t AAudioServiceStreamShared::close() {
264 pause();
265 // TODO wait for pause() to synchronize
Phil Burk71f35bb2017-04-13 16:05:07 -0700266 AAudioServiceEndpoint *endpoint = mServiceEndpoint;
267 if (endpoint != nullptr) {
Phil Burk11e8d332017-05-24 09:59:02 -0700268 sp<AAudioServiceStreamShared> keep(this);
269 endpoint->unregisterStream(keep);
Phil Burk71f35bb2017-04-13 16:05:07 -0700270
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 Burkc0c70e32017-02-09 13:18:38 -0800279 return AAudioServiceStreamBase::close();
280}
281
282/**
283 * Get an immutable description of the data queue created by this service.
284 */
285aaudio_result_t AAudioServiceStreamShared::getDownDataDescription(AudioEndpointParcelable &parcelable)
286{
287 // Gather information on the data queue.
288 mAudioDataQueue->fillParcelable(parcelable,
289 parcelable.mDownDataQueueParcelable);
290 parcelable.mDownDataQueueParcelable.setFramesPerBurst(getFramesPerBurst());
291 return AAUDIO_OK;
292}
293
294void AAudioServiceStreamShared::onStop() {
295}
296
297void AAudioServiceStreamShared::onDisconnect() {
Phil Burk11e8d332017-05-24 09:59:02 -0700298 processFatalError();
Phil Burkc0c70e32017-02-09 13:18:38 -0800299}
300
Phil Burk71f35bb2017-04-13 16:05:07 -0700301void AAudioServiceStreamShared::markTransferTime(int64_t nanoseconds) {
302 mMarkedPosition = mAudioDataQueue->getFifoBuffer()->getReadCounter();
303 mMarkedTime = nanoseconds;
304}
Phil Burkc0c70e32017-02-09 13:18:38 -0800305
306aaudio_result_t AAudioServiceStreamShared::getFreeRunningPosition(int64_t *positionFrames,
307 int64_t *timeNanos) {
Phil Burk71f35bb2017-04-13 16:05:07 -0700308 // TODO get these two numbers as an atomic pair
309 *positionFrames = mMarkedPosition;
310 *timeNanos = mMarkedTime;
Phil Burkc0c70e32017-02-09 13:18:38 -0800311 return AAUDIO_OK;
312}