blob: 8bb34d162b909f566b262c4f5744194f5232dbe4 [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
Phil Burk5a26e662017-07-07 12:44:48 -0700180 setState(AAUDIO_STREAM_STATE_OPEN);
Phil Burkc0c70e32017-02-09 13:18:38 -0800181 return AAUDIO_OK;
Phil Burkec89b2e2017-06-20 15:05:06 -0700182
183error:
184 close();
185 return result;
Phil Burkc0c70e32017-02-09 13:18:38 -0800186}
187
188/**
189 * Start the flow of audio data.
190 *
191 * An AAUDIO_SERVICE_EVENT_STARTED will be sent to the client when complete.
192 */
193aaudio_result_t AAudioServiceStreamShared::start() {
Phil Burk71f35bb2017-04-13 16:05:07 -0700194 AAudioServiceEndpoint *endpoint = mServiceEndpoint;
195 if (endpoint == nullptr) {
196 return AAUDIO_ERROR_INVALID_STATE;
197 }
Phil Burk87c9f642017-05-17 07:22:39 -0700198 // For output streams, this will add the stream to the mixer.
Phil Burk71f35bb2017-04-13 16:05:07 -0700199 aaudio_result_t result = endpoint->startStream(this);
Phil Burkc0c70e32017-02-09 13:18:38 -0800200 if (result != AAUDIO_OK) {
201 ALOGE("AAudioServiceStreamShared::start() mServiceEndpoint returned %d", result);
Phil Burk5ef003b2017-06-30 11:43:37 -0700202 disconnect();
Phil Burkc0c70e32017-02-09 13:18:38 -0800203 } else {
204 result = AAudioServiceStreamBase::start();
205 }
Phil Burkec89b2e2017-06-20 15:05:06 -0700206 return result;
Phil Burkc0c70e32017-02-09 13:18:38 -0800207}
208
209/**
210 * Stop the flow of data so that start() can resume without loss of data.
211 *
212 * An AAUDIO_SERVICE_EVENT_PAUSED will be sent to the client when complete.
213*/
214aaudio_result_t AAudioServiceStreamShared::pause() {
Phil Burk71f35bb2017-04-13 16:05:07 -0700215 AAudioServiceEndpoint *endpoint = mServiceEndpoint;
216 if (endpoint == nullptr) {
217 return AAUDIO_ERROR_INVALID_STATE;
218 }
Phil Burk71f35bb2017-04-13 16:05:07 -0700219 aaudio_result_t result = endpoint->stopStream(this);
220 if (result != AAUDIO_OK) {
221 ALOGE("AAudioServiceStreamShared::pause() mServiceEndpoint returned %d", result);
Phil Burk5ef003b2017-06-30 11:43:37 -0700222 disconnect(); // TODO should we return or pause Base first?
Phil Burk71f35bb2017-04-13 16:05:07 -0700223 }
224 return AAudioServiceStreamBase::pause();
225}
226
227aaudio_result_t AAudioServiceStreamShared::stop() {
228 AAudioServiceEndpoint *endpoint = mServiceEndpoint;
229 if (endpoint == nullptr) {
230 return AAUDIO_ERROR_INVALID_STATE;
231 }
Phil Burk71f35bb2017-04-13 16:05:07 -0700232 aaudio_result_t result = endpoint->stopStream(this);
Phil Burkc0c70e32017-02-09 13:18:38 -0800233 if (result != AAUDIO_OK) {
234 ALOGE("AAudioServiceStreamShared::stop() mServiceEndpoint returned %d", result);
Phil Burk5ef003b2017-06-30 11:43:37 -0700235 disconnect();
Phil Burkc0c70e32017-02-09 13:18:38 -0800236 }
Phil Burk71f35bb2017-04-13 16:05:07 -0700237 return AAudioServiceStreamBase::stop();
Phil Burkc0c70e32017-02-09 13:18:38 -0800238}
239
240/**
241 * Discard any data held by the underlying HAL or Service.
242 *
243 * An AAUDIO_SERVICE_EVENT_FLUSHED will be sent to the client when complete.
244 */
245aaudio_result_t AAudioServiceStreamShared::flush() {
Phil Burkec89b2e2017-06-20 15:05:06 -0700246 AAudioServiceEndpoint *endpoint = mServiceEndpoint;
247 if (endpoint == nullptr) {
248 return AAUDIO_ERROR_INVALID_STATE;
249 }
250 if (mState != AAUDIO_STREAM_STATE_PAUSED) {
251 ALOGE("AAudioServiceStreamShared::flush() stream not paused, state = %s",
252 AAudio_convertStreamStateToText(mState));
253 return AAUDIO_ERROR_INVALID_STATE;
254 }
255 // Data will get flushed when the client receives the FLUSHED event.
256 return AAudioServiceStreamBase::flush();
Phil Burkc0c70e32017-02-09 13:18:38 -0800257}
258
259aaudio_result_t AAudioServiceStreamShared::close() {
Phil Burk98d6d922017-07-06 11:52:45 -0700260 if (mState == AAUDIO_STREAM_STATE_CLOSED) {
261 return AAUDIO_OK;
Phil Burk71f35bb2017-04-13 16:05:07 -0700262 }
Phil Burk98d6d922017-07-06 11:52:45 -0700263
264 AAudioServiceEndpoint *endpoint = mServiceEndpoint;
265 if (endpoint == nullptr) {
266 return AAUDIO_ERROR_INVALID_STATE;
267 }
268 endpoint->stopStream(this);
269
270 endpoint->unregisterStream(this);
271
272 AAudioEndpointManager &mEndpointManager = AAudioEndpointManager::getInstance();
273 mEndpointManager.closeEndpoint(endpoint);
274 mServiceEndpoint = nullptr;
275
Phil Burk942bdc02017-05-03 11:36:50 -0700276 if (mAudioDataQueue != nullptr) {
277 delete mAudioDataQueue;
278 mAudioDataQueue = nullptr;
279 }
Phil Burk98d6d922017-07-06 11:52:45 -0700280
Phil Burkc0c70e32017-02-09 13:18:38 -0800281 return AAudioServiceStreamBase::close();
282}
283
284/**
285 * Get an immutable description of the data queue created by this service.
286 */
287aaudio_result_t AAudioServiceStreamShared::getDownDataDescription(AudioEndpointParcelable &parcelable)
288{
289 // Gather information on the data queue.
290 mAudioDataQueue->fillParcelable(parcelable,
291 parcelable.mDownDataQueueParcelable);
292 parcelable.mDownDataQueueParcelable.setFramesPerBurst(getFramesPerBurst());
293 return AAUDIO_OK;
294}
295
296void AAudioServiceStreamShared::onStop() {
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}