blob: 5654113208d2b89054562483cb94004a62e0300b [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
Eric Laurentcb4dae22017-07-01 19:39:32 -070017#define LOG_TAG "AAudioServiceStreamShared"
Phil Burkc0c70e32017-02-09 13:18:38 -080018//#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?
jiabin901f65d2017-07-12 17:56:35 -0700115 mAudioFormat = configurationInput.getFormat();
Phil Burkc0c70e32017-02-09 13:18:38 -0800116 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);
jiabin901f65d2017-07-12 17:56:35 -0700172 configurationOutput.setFormat(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() {
Eric Laurentcb4dae22017-07-01 19:39:32 -0700194 if (isRunning()) {
195 return AAUDIO_OK;
196 }
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 Burk5ef003b2017-06-30 11:43:37 -0700205 disconnect();
Phil Burkc0c70e32017-02-09 13:18:38 -0800206 } else {
Eric Laurentcb4dae22017-07-01 19:39:32 -0700207 result = endpoint->getStreamInternal()->startClient(mMmapClient, &mClientHandle);
208 if (result == AAUDIO_OK) {
209 result = AAudioServiceStreamBase::start();
210 }
Phil Burkc0c70e32017-02-09 13:18:38 -0800211 }
Phil Burkec89b2e2017-06-20 15:05:06 -0700212 return result;
Phil Burkc0c70e32017-02-09 13:18:38 -0800213}
214
215/**
216 * Stop the flow of data so that start() can resume without loss of data.
217 *
218 * An AAUDIO_SERVICE_EVENT_PAUSED will be sent to the client when complete.
219*/
220aaudio_result_t AAudioServiceStreamShared::pause() {
Eric Laurentcb4dae22017-07-01 19:39:32 -0700221 if (!isRunning()) {
222 return AAUDIO_OK;
223 }
Phil Burk71f35bb2017-04-13 16:05:07 -0700224 AAudioServiceEndpoint *endpoint = mServiceEndpoint;
225 if (endpoint == nullptr) {
226 return AAUDIO_ERROR_INVALID_STATE;
227 }
Eric Laurentcb4dae22017-07-01 19:39:32 -0700228 endpoint->getStreamInternal()->stopClient(mClientHandle);
Phil Burk71f35bb2017-04-13 16:05:07 -0700229 aaudio_result_t result = endpoint->stopStream(this);
230 if (result != AAUDIO_OK) {
231 ALOGE("AAudioServiceStreamShared::pause() mServiceEndpoint returned %d", result);
Phil Burk5ef003b2017-06-30 11:43:37 -0700232 disconnect(); // TODO should we return or pause Base first?
Phil Burk71f35bb2017-04-13 16:05:07 -0700233 }
234 return AAudioServiceStreamBase::pause();
235}
236
237aaudio_result_t AAudioServiceStreamShared::stop() {
Eric Laurentcb4dae22017-07-01 19:39:32 -0700238 if (!isRunning()) {
239 return AAUDIO_OK;
240 }
Phil Burk71f35bb2017-04-13 16:05:07 -0700241 AAudioServiceEndpoint *endpoint = mServiceEndpoint;
242 if (endpoint == nullptr) {
243 return AAUDIO_ERROR_INVALID_STATE;
244 }
Eric Laurentcb4dae22017-07-01 19:39:32 -0700245 endpoint->getStreamInternal()->stopClient(mClientHandle);
Phil Burk71f35bb2017-04-13 16:05:07 -0700246 aaudio_result_t result = endpoint->stopStream(this);
Phil Burkc0c70e32017-02-09 13:18:38 -0800247 if (result != AAUDIO_OK) {
248 ALOGE("AAudioServiceStreamShared::stop() mServiceEndpoint returned %d", result);
Phil Burk5ef003b2017-06-30 11:43:37 -0700249 disconnect();
Phil Burkc0c70e32017-02-09 13:18:38 -0800250 }
Phil Burk71f35bb2017-04-13 16:05:07 -0700251 return AAudioServiceStreamBase::stop();
Phil Burkc0c70e32017-02-09 13:18:38 -0800252}
253
254/**
255 * Discard any data held by the underlying HAL or Service.
256 *
257 * An AAUDIO_SERVICE_EVENT_FLUSHED will be sent to the client when complete.
258 */
259aaudio_result_t AAudioServiceStreamShared::flush() {
Phil Burkec89b2e2017-06-20 15:05:06 -0700260 AAudioServiceEndpoint *endpoint = mServiceEndpoint;
261 if (endpoint == nullptr) {
262 return AAUDIO_ERROR_INVALID_STATE;
263 }
264 if (mState != AAUDIO_STREAM_STATE_PAUSED) {
Eric Laurentcb4dae22017-07-01 19:39:32 -0700265 ALOGE("AAudioServiceStreamShared::flush() stream not paused, state = %s",
Phil Burkec89b2e2017-06-20 15:05:06 -0700266 AAudio_convertStreamStateToText(mState));
267 return AAUDIO_ERROR_INVALID_STATE;
268 }
269 // Data will get flushed when the client receives the FLUSHED event.
270 return AAudioServiceStreamBase::flush();
Phil Burkc0c70e32017-02-09 13:18:38 -0800271}
272
273aaudio_result_t AAudioServiceStreamShared::close() {
Phil Burk98d6d922017-07-06 11:52:45 -0700274 if (mState == AAUDIO_STREAM_STATE_CLOSED) {
275 return AAUDIO_OK;
Phil Burk71f35bb2017-04-13 16:05:07 -0700276 }
Phil Burk98d6d922017-07-06 11:52:45 -0700277
Eric Laurentcb4dae22017-07-01 19:39:32 -0700278 stop();
279
Phil Burk98d6d922017-07-06 11:52:45 -0700280 AAudioServiceEndpoint *endpoint = mServiceEndpoint;
281 if (endpoint == nullptr) {
282 return AAUDIO_ERROR_INVALID_STATE;
283 }
Phil Burk98d6d922017-07-06 11:52:45 -0700284
285 endpoint->unregisterStream(this);
286
287 AAudioEndpointManager &mEndpointManager = AAudioEndpointManager::getInstance();
288 mEndpointManager.closeEndpoint(endpoint);
289 mServiceEndpoint = nullptr;
290
Phil Burk942bdc02017-05-03 11:36:50 -0700291 if (mAudioDataQueue != nullptr) {
292 delete mAudioDataQueue;
293 mAudioDataQueue = nullptr;
294 }
Phil Burkc0c70e32017-02-09 13:18:38 -0800295 return AAudioServiceStreamBase::close();
296}
297
298/**
299 * Get an immutable description of the data queue created by this service.
300 */
301aaudio_result_t AAudioServiceStreamShared::getDownDataDescription(AudioEndpointParcelable &parcelable)
302{
303 // Gather information on the data queue.
304 mAudioDataQueue->fillParcelable(parcelable,
305 parcelable.mDownDataQueueParcelable);
306 parcelable.mDownDataQueueParcelable.setFramesPerBurst(getFramesPerBurst());
307 return AAUDIO_OK;
308}
309
Phil Burk71f35bb2017-04-13 16:05:07 -0700310void AAudioServiceStreamShared::markTransferTime(int64_t nanoseconds) {
311 mMarkedPosition = mAudioDataQueue->getFifoBuffer()->getReadCounter();
312 mMarkedTime = nanoseconds;
313}
Phil Burkc0c70e32017-02-09 13:18:38 -0800314
315aaudio_result_t AAudioServiceStreamShared::getFreeRunningPosition(int64_t *positionFrames,
316 int64_t *timeNanos) {
Phil Burk71f35bb2017-04-13 16:05:07 -0700317 // TODO get these two numbers as an atomic pair
318 *positionFrames = mMarkedPosition;
319 *timeNanos = mMarkedTime;
Phil Burkc0c70e32017-02-09 13:18:38 -0800320 return AAUDIO_OK;
321}