blob: 7475692f67c7fcc47812e2e30a87f4fcd06ef44f [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();
Phil Burkc0c70e32017-02-09 13:18:38 -0800109 aaudio_direction_t direction = request.getDirection();
110
Phil Burkc0c70e32017-02-09 13:18:38 -0800111 AAudioEndpointManager &mEndpointManager = AAudioEndpointManager::getInstance();
Eric Laurenta17ae742017-06-29 15:43:55 -0700112 mServiceEndpoint = mEndpointManager.openEndpoint(mAudioService, configurationOutput, direction);
Phil Burkc0c70e32017-02-09 13:18:38 -0800113 if (mServiceEndpoint == nullptr) {
Phil Burk11e8d332017-05-24 09:59:02 -0700114 ALOGE("AAudioServiceStreamShared::open() mServiceEndPoint = %p", mServiceEndpoint);
Phil Burkc0c70e32017-02-09 13:18:38 -0800115 return AAUDIO_ERROR_UNAVAILABLE;
116 }
117
118 // Is the request compatible with the shared endpoint?
119 mAudioFormat = configurationInput.getAudioFormat();
120 if (mAudioFormat == AAUDIO_FORMAT_UNSPECIFIED) {
121 mAudioFormat = AAUDIO_FORMAT_PCM_FLOAT;
122 } else if (mAudioFormat != AAUDIO_FORMAT_PCM_FLOAT) {
Phil Burk11e8d332017-05-24 09:59:02 -0700123 ALOGE("AAudioServiceStreamShared::open() mAudioFormat = %d, need FLOAT", mAudioFormat);
Phil Burkec89b2e2017-06-20 15:05:06 -0700124 result = AAUDIO_ERROR_INVALID_FORMAT;
125 goto error;
Phil Burkc0c70e32017-02-09 13:18:38 -0800126 }
127
128 mSampleRate = configurationInput.getSampleRate();
Glenn Kasten37a466a2017-05-30 15:53:14 -0700129 if (mSampleRate == AAUDIO_UNSPECIFIED) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800130 mSampleRate = mServiceEndpoint->getSampleRate();
131 } else if (mSampleRate != mServiceEndpoint->getSampleRate()) {
Phil Burk11e8d332017-05-24 09:59:02 -0700132 ALOGE("AAudioServiceStreamShared::open() mSampleRate = %d, need %d",
Phil Burk71f35bb2017-04-13 16:05:07 -0700133 mSampleRate, mServiceEndpoint->getSampleRate());
Phil Burkec89b2e2017-06-20 15:05:06 -0700134 result = AAUDIO_ERROR_INVALID_RATE;
135 goto error;
Phil Burkc0c70e32017-02-09 13:18:38 -0800136 }
137
138 mSamplesPerFrame = configurationInput.getSamplesPerFrame();
Glenn Kasten37a466a2017-05-30 15:53:14 -0700139 if (mSamplesPerFrame == AAUDIO_UNSPECIFIED) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800140 mSamplesPerFrame = mServiceEndpoint->getSamplesPerFrame();
141 } else if (mSamplesPerFrame != mServiceEndpoint->getSamplesPerFrame()) {
Phil Burk11e8d332017-05-24 09:59:02 -0700142 ALOGE("AAudioServiceStreamShared::open() mSamplesPerFrame = %d, need %d",
Phil Burk71f35bb2017-04-13 16:05:07 -0700143 mSamplesPerFrame, mServiceEndpoint->getSamplesPerFrame());
Phil Burkec89b2e2017-06-20 15:05:06 -0700144 result = AAUDIO_ERROR_OUT_OF_RANGE;
145 goto error;
Phil Burkc0c70e32017-02-09 13:18:38 -0800146 }
147
Phil Burkc0c70e32017-02-09 13:18:38 -0800148 mFramesPerBurst = mServiceEndpoint->getFramesPerBurst();
Phil Burk11e8d332017-05-24 09:59:02 -0700149 ALOGD("AAudioServiceStreamShared::open() mSampleRate = %d, mFramesPerBurst = %d",
Phil Burkec89b2e2017-06-20 15:05:06 -0700150 mSampleRate, mFramesPerBurst);
151
152 mCapacityInFrames = calculateBufferCapacity(configurationInput.getBufferCapacity(),
153 mFramesPerBurst);
154 if (mCapacityInFrames < 0) {
155 result = mCapacityInFrames; // negative error code
156 mCapacityInFrames = 0;
157 goto error;
Phil Burkc0c70e32017-02-09 13:18:38 -0800158 }
Phil Burkc0c70e32017-02-09 13:18:38 -0800159
160 // Create audio data shared memory buffer for client.
161 mAudioDataQueue = new SharedRingBuffer();
Phil Burkec89b2e2017-06-20 15:05:06 -0700162 result = mAudioDataQueue->allocate(calculateBytesPerFrame(), mCapacityInFrames);
163 if (result != AAUDIO_OK) {
Phil Burk11e8d332017-05-24 09:59:02 -0700164 ALOGE("AAudioServiceStreamShared::open() could not allocate FIFO with %d frames",
Phil Burkec89b2e2017-06-20 15:05:06 -0700165 mCapacityInFrames);
166 result = AAUDIO_ERROR_NO_MEMORY;
167 goto error;
168 }
169
170 ALOGD("AAudioServiceStreamShared::open() actual rate = %d, channels = %d, deviceId = %d",
171 mSampleRate, mSamplesPerFrame, mServiceEndpoint->getDeviceId());
Phil Burkc0c70e32017-02-09 13:18:38 -0800172
173 // Fill in configuration for client.
174 configurationOutput.setSampleRate(mSampleRate);
175 configurationOutput.setSamplesPerFrame(mSamplesPerFrame);
176 configurationOutput.setAudioFormat(mAudioFormat);
Phil Burkec89b2e2017-06-20 15:05:06 -0700177 configurationOutput.setDeviceId(mServiceEndpoint->getDeviceId());
Phil Burkc0c70e32017-02-09 13:18:38 -0800178
Phil Burk11e8d332017-05-24 09:59:02 -0700179 result = mServiceEndpoint->registerStream(keep);
Phil Burkec89b2e2017-06-20 15:05:06 -0700180 if (result != AAUDIO_OK) {
181 goto error;
182 }
Phil Burkc0c70e32017-02-09 13:18:38 -0800183
184 return AAUDIO_OK;
Phil Burkec89b2e2017-06-20 15:05:06 -0700185
186error:
187 close();
188 return result;
Phil Burkc0c70e32017-02-09 13:18:38 -0800189}
190
191/**
192 * Start the flow of audio data.
193 *
194 * An AAUDIO_SERVICE_EVENT_STARTED will be sent to the client when complete.
195 */
196aaudio_result_t AAudioServiceStreamShared::start() {
Phil Burk71f35bb2017-04-13 16:05:07 -0700197 AAudioServiceEndpoint *endpoint = mServiceEndpoint;
198 if (endpoint == nullptr) {
199 return AAUDIO_ERROR_INVALID_STATE;
200 }
Phil Burk87c9f642017-05-17 07:22:39 -0700201 // For output streams, this will add the stream to the mixer.
Phil Burk71f35bb2017-04-13 16:05:07 -0700202 aaudio_result_t result = endpoint->startStream(this);
Phil Burkc0c70e32017-02-09 13:18:38 -0800203 if (result != AAUDIO_OK) {
204 ALOGE("AAudioServiceStreamShared::start() mServiceEndpoint returned %d", result);
Phil Burkec89b2e2017-06-20 15:05:06 -0700205 processFatalError();
Phil Burkc0c70e32017-02-09 13:18:38 -0800206 } else {
207 result = AAudioServiceStreamBase::start();
208 }
Phil Burkec89b2e2017-06-20 15:05:06 -0700209 return result;
Phil Burkc0c70e32017-02-09 13:18:38 -0800210}
211
212/**
213 * Stop the flow of data so that start() can resume without loss of data.
214 *
215 * An AAUDIO_SERVICE_EVENT_PAUSED will be sent to the client when complete.
216*/
217aaudio_result_t AAudioServiceStreamShared::pause() {
Phil Burk71f35bb2017-04-13 16:05:07 -0700218 AAudioServiceEndpoint *endpoint = mServiceEndpoint;
219 if (endpoint == nullptr) {
220 return AAUDIO_ERROR_INVALID_STATE;
221 }
Phil Burk71f35bb2017-04-13 16:05:07 -0700222 aaudio_result_t result = endpoint->stopStream(this);
223 if (result != AAUDIO_OK) {
224 ALOGE("AAudioServiceStreamShared::pause() mServiceEndpoint returned %d", result);
Phil Burkec89b2e2017-06-20 15:05:06 -0700225 processFatalError();
Phil Burk71f35bb2017-04-13 16:05:07 -0700226 }
227 return AAudioServiceStreamBase::pause();
228}
229
230aaudio_result_t AAudioServiceStreamShared::stop() {
231 AAudioServiceEndpoint *endpoint = mServiceEndpoint;
232 if (endpoint == nullptr) {
233 return AAUDIO_ERROR_INVALID_STATE;
234 }
Phil Burk71f35bb2017-04-13 16:05:07 -0700235 aaudio_result_t result = endpoint->stopStream(this);
Phil Burkc0c70e32017-02-09 13:18:38 -0800236 if (result != AAUDIO_OK) {
237 ALOGE("AAudioServiceStreamShared::stop() mServiceEndpoint returned %d", result);
Phil Burkec89b2e2017-06-20 15:05:06 -0700238 processFatalError();
Phil Burkc0c70e32017-02-09 13:18:38 -0800239 }
Phil Burk71f35bb2017-04-13 16:05:07 -0700240 return AAudioServiceStreamBase::stop();
Phil Burkc0c70e32017-02-09 13:18:38 -0800241}
242
243/**
244 * Discard any data held by the underlying HAL or Service.
245 *
246 * An AAUDIO_SERVICE_EVENT_FLUSHED will be sent to the client when complete.
247 */
248aaudio_result_t AAudioServiceStreamShared::flush() {
Phil Burkec89b2e2017-06-20 15:05:06 -0700249 AAudioServiceEndpoint *endpoint = mServiceEndpoint;
250 if (endpoint == nullptr) {
251 return AAUDIO_ERROR_INVALID_STATE;
252 }
253 if (mState != AAUDIO_STREAM_STATE_PAUSED) {
254 ALOGE("AAudioServiceStreamShared::flush() stream not paused, state = %s",
255 AAudio_convertStreamStateToText(mState));
256 return AAUDIO_ERROR_INVALID_STATE;
257 }
258 // Data will get flushed when the client receives the FLUSHED event.
259 return AAudioServiceStreamBase::flush();
Phil Burkc0c70e32017-02-09 13:18:38 -0800260}
261
262aaudio_result_t AAudioServiceStreamShared::close() {
263 pause();
264 // TODO wait for pause() to synchronize
Phil Burk71f35bb2017-04-13 16:05:07 -0700265 AAudioServiceEndpoint *endpoint = mServiceEndpoint;
266 if (endpoint != nullptr) {
Phil Burk11e8d332017-05-24 09:59:02 -0700267 sp<AAudioServiceStreamShared> keep(this);
268 endpoint->unregisterStream(keep);
Phil Burk71f35bb2017-04-13 16:05:07 -0700269
270 AAudioEndpointManager &mEndpointManager = AAudioEndpointManager::getInstance();
271 mEndpointManager.closeEndpoint(endpoint);
272 mServiceEndpoint = nullptr;
273 }
Phil Burk942bdc02017-05-03 11:36:50 -0700274 if (mAudioDataQueue != nullptr) {
275 delete mAudioDataQueue;
276 mAudioDataQueue = nullptr;
277 }
Phil Burkc0c70e32017-02-09 13:18:38 -0800278 return AAudioServiceStreamBase::close();
279}
280
281/**
282 * Get an immutable description of the data queue created by this service.
283 */
284aaudio_result_t AAudioServiceStreamShared::getDownDataDescription(AudioEndpointParcelable &parcelable)
285{
286 // Gather information on the data queue.
287 mAudioDataQueue->fillParcelable(parcelable,
288 parcelable.mDownDataQueueParcelable);
289 parcelable.mDownDataQueueParcelable.setFramesPerBurst(getFramesPerBurst());
290 return AAUDIO_OK;
291}
292
293void AAudioServiceStreamShared::onStop() {
294}
295
296void AAudioServiceStreamShared::onDisconnect() {
Phil Burk11e8d332017-05-24 09:59:02 -0700297 processFatalError();
Phil Burkc0c70e32017-02-09 13:18:38 -0800298}
299
Phil Burk71f35bb2017-04-13 16:05:07 -0700300void AAudioServiceStreamShared::markTransferTime(int64_t nanoseconds) {
301 mMarkedPosition = mAudioDataQueue->getFifoBuffer()->getReadCounter();
302 mMarkedTime = nanoseconds;
303}
Phil Burkc0c70e32017-02-09 13:18:38 -0800304
305aaudio_result_t AAudioServiceStreamShared::getFreeRunningPosition(int64_t *positionFrames,
306 int64_t *timeNanos) {
Phil Burk71f35bb2017-04-13 16:05:07 -0700307 // TODO get these two numbers as an atomic pair
308 *positionFrames = mMarkedPosition;
309 *timeNanos = mMarkedTime;
Phil Burkc0c70e32017-02-09 13:18:38 -0800310 return AAUDIO_OK;
311}