blob: 07c4faf9fbd62fe3c104287f4a3fae4455b8396b [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)
Phil Burk97350f92017-07-21 15:59:44 -070044 , mTimestampPositionOffset(0)
Phil Burkc0c70e32017-02-09 13:18:38 -080045 {
46}
47
Phil Burkec89b2e2017-06-20 15:05:06 -070048int32_t AAudioServiceStreamShared::calculateBufferCapacity(int32_t requestedCapacityFrames,
49 int32_t framesPerBurst) {
50
51 if (requestedCapacityFrames > MAX_FRAMES_PER_BUFFER) {
Phil Burk11e8d332017-05-24 09:59:02 -070052 ALOGE("AAudioServiceStreamShared::calculateBufferCapacity() requested capacity %d > max %d",
Phil Burkec89b2e2017-06-20 15:05:06 -070053 requestedCapacityFrames, MAX_FRAMES_PER_BUFFER);
54 return AAUDIO_ERROR_OUT_OF_RANGE;
55 }
56
57 // Determine how many bursts will fit in the buffer.
58 int32_t numBursts;
59 if (requestedCapacityFrames == AAUDIO_UNSPECIFIED) {
60 // Use fewer bursts if default is too many.
61 if ((DEFAULT_BURSTS_PER_BUFFER * framesPerBurst) > MAX_FRAMES_PER_BUFFER) {
62 numBursts = MAX_FRAMES_PER_BUFFER / framesPerBurst;
63 } else {
64 numBursts = DEFAULT_BURSTS_PER_BUFFER;
65 }
66 } else {
67 // round up to nearest burst boundary
68 numBursts = (requestedCapacityFrames + framesPerBurst - 1) / framesPerBurst;
69 }
70
71 // Clip to bare minimum.
72 if (numBursts < MIN_BURSTS_PER_BUFFER) {
73 numBursts = MIN_BURSTS_PER_BUFFER;
74 }
75 // Check for numeric overflow.
76 if (numBursts > 0x8000 || framesPerBurst > 0x8000) {
Phil Burk11e8d332017-05-24 09:59:02 -070077 ALOGE("AAudioServiceStreamShared::calculateBufferCapacity() overflow, capacity = %d * %d",
Phil Burkec89b2e2017-06-20 15:05:06 -070078 numBursts, framesPerBurst);
79 return AAUDIO_ERROR_OUT_OF_RANGE;
80 }
81 int32_t capacityInFrames = numBursts * framesPerBurst;
82
83 // Final sanity check.
84 if (capacityInFrames > MAX_FRAMES_PER_BUFFER) {
Phil Burk11e8d332017-05-24 09:59:02 -070085 ALOGE("AAudioServiceStreamShared::calculateBufferCapacity() calc capacity %d > max %d",
Phil Burkec89b2e2017-06-20 15:05:06 -070086 capacityInFrames, MAX_FRAMES_PER_BUFFER);
87 return AAUDIO_ERROR_OUT_OF_RANGE;
88 }
Phil Burk11e8d332017-05-24 09:59:02 -070089 ALOGD("AAudioServiceStreamShared::calculateBufferCapacity() requested %d frames, actual = %d",
Phil Burkec89b2e2017-06-20 15:05:06 -070090 requestedCapacityFrames, capacityInFrames);
91 return capacityInFrames;
92}
93
Phil Burkc0c70e32017-02-09 13:18:38 -080094aaudio_result_t AAudioServiceStreamShared::open(const aaudio::AAudioStreamRequest &request,
95 aaudio::AAudioStreamConfiguration &configurationOutput) {
96
Phil Burk11e8d332017-05-24 09:59:02 -070097 sp<AAudioServiceStreamShared> keep(this);
98
Phil Burkc0c70e32017-02-09 13:18:38 -080099 aaudio_result_t result = AAudioServiceStreamBase::open(request, configurationOutput);
100 if (result != AAUDIO_OK) {
Phil Burkec89b2e2017-06-20 15:05:06 -0700101 ALOGE("AAudioServiceStreamBase open() returned %d", result);
Phil Burkc0c70e32017-02-09 13:18:38 -0800102 return result;
103 }
104
105 const AAudioStreamConfiguration &configurationInput = request.getConstantConfiguration();
Phil Burkc0c70e32017-02-09 13:18:38 -0800106 aaudio_direction_t direction = request.getDirection();
107
Phil Burkc0c70e32017-02-09 13:18:38 -0800108 AAudioEndpointManager &mEndpointManager = AAudioEndpointManager::getInstance();
Eric Laurenta17ae742017-06-29 15:43:55 -0700109 mServiceEndpoint = mEndpointManager.openEndpoint(mAudioService, configurationOutput, direction);
Phil Burkc0c70e32017-02-09 13:18:38 -0800110 if (mServiceEndpoint == nullptr) {
Phil Burk11e8d332017-05-24 09:59:02 -0700111 ALOGE("AAudioServiceStreamShared::open() mServiceEndPoint = %p", mServiceEndpoint);
Phil Burkc0c70e32017-02-09 13:18:38 -0800112 return AAUDIO_ERROR_UNAVAILABLE;
113 }
114
115 // Is the request compatible with the shared endpoint?
jiabin901f65d2017-07-12 17:56:35 -0700116 mAudioFormat = configurationInput.getFormat();
Phil Burkc0c70e32017-02-09 13:18:38 -0800117 if (mAudioFormat == AAUDIO_FORMAT_UNSPECIFIED) {
118 mAudioFormat = AAUDIO_FORMAT_PCM_FLOAT;
119 } else if (mAudioFormat != AAUDIO_FORMAT_PCM_FLOAT) {
Phil Burk11e8d332017-05-24 09:59:02 -0700120 ALOGE("AAudioServiceStreamShared::open() mAudioFormat = %d, need FLOAT", mAudioFormat);
Phil Burkec89b2e2017-06-20 15:05:06 -0700121 result = AAUDIO_ERROR_INVALID_FORMAT;
122 goto error;
Phil Burkc0c70e32017-02-09 13:18:38 -0800123 }
124
125 mSampleRate = configurationInput.getSampleRate();
Glenn Kasten37a466a2017-05-30 15:53:14 -0700126 if (mSampleRate == AAUDIO_UNSPECIFIED) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800127 mSampleRate = mServiceEndpoint->getSampleRate();
128 } else if (mSampleRate != mServiceEndpoint->getSampleRate()) {
Phil Burk11e8d332017-05-24 09:59:02 -0700129 ALOGE("AAudioServiceStreamShared::open() mSampleRate = %d, need %d",
Phil Burk71f35bb2017-04-13 16:05:07 -0700130 mSampleRate, mServiceEndpoint->getSampleRate());
Phil Burkec89b2e2017-06-20 15:05:06 -0700131 result = AAUDIO_ERROR_INVALID_RATE;
132 goto error;
Phil Burkc0c70e32017-02-09 13:18:38 -0800133 }
134
135 mSamplesPerFrame = configurationInput.getSamplesPerFrame();
Glenn Kasten37a466a2017-05-30 15:53:14 -0700136 if (mSamplesPerFrame == AAUDIO_UNSPECIFIED) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800137 mSamplesPerFrame = mServiceEndpoint->getSamplesPerFrame();
138 } else if (mSamplesPerFrame != mServiceEndpoint->getSamplesPerFrame()) {
Phil Burk11e8d332017-05-24 09:59:02 -0700139 ALOGE("AAudioServiceStreamShared::open() mSamplesPerFrame = %d, need %d",
Phil Burk71f35bb2017-04-13 16:05:07 -0700140 mSamplesPerFrame, mServiceEndpoint->getSamplesPerFrame());
Phil Burkec89b2e2017-06-20 15:05:06 -0700141 result = AAUDIO_ERROR_OUT_OF_RANGE;
142 goto error;
Phil Burkc0c70e32017-02-09 13:18:38 -0800143 }
144
Phil Burkc0c70e32017-02-09 13:18:38 -0800145 mFramesPerBurst = mServiceEndpoint->getFramesPerBurst();
Phil Burk11e8d332017-05-24 09:59:02 -0700146 ALOGD("AAudioServiceStreamShared::open() mSampleRate = %d, mFramesPerBurst = %d",
Phil Burkec89b2e2017-06-20 15:05:06 -0700147 mSampleRate, mFramesPerBurst);
148
149 mCapacityInFrames = calculateBufferCapacity(configurationInput.getBufferCapacity(),
150 mFramesPerBurst);
151 if (mCapacityInFrames < 0) {
152 result = mCapacityInFrames; // negative error code
153 mCapacityInFrames = 0;
154 goto error;
Phil Burkc0c70e32017-02-09 13:18:38 -0800155 }
Phil Burkc0c70e32017-02-09 13:18:38 -0800156
157 // Create audio data shared memory buffer for client.
158 mAudioDataQueue = new SharedRingBuffer();
Phil Burkec89b2e2017-06-20 15:05:06 -0700159 result = mAudioDataQueue->allocate(calculateBytesPerFrame(), mCapacityInFrames);
160 if (result != AAUDIO_OK) {
Phil Burk11e8d332017-05-24 09:59:02 -0700161 ALOGE("AAudioServiceStreamShared::open() could not allocate FIFO with %d frames",
Phil Burkec89b2e2017-06-20 15:05:06 -0700162 mCapacityInFrames);
163 result = AAUDIO_ERROR_NO_MEMORY;
164 goto error;
165 }
166
167 ALOGD("AAudioServiceStreamShared::open() actual rate = %d, channels = %d, deviceId = %d",
168 mSampleRate, mSamplesPerFrame, mServiceEndpoint->getDeviceId());
Phil Burkc0c70e32017-02-09 13:18:38 -0800169
170 // Fill in configuration for client.
171 configurationOutput.setSampleRate(mSampleRate);
172 configurationOutput.setSamplesPerFrame(mSamplesPerFrame);
jiabin901f65d2017-07-12 17:56:35 -0700173 configurationOutput.setFormat(mAudioFormat);
Phil Burkec89b2e2017-06-20 15:05:06 -0700174 configurationOutput.setDeviceId(mServiceEndpoint->getDeviceId());
Phil Burkc0c70e32017-02-09 13:18:38 -0800175
Phil Burk11e8d332017-05-24 09:59:02 -0700176 result = mServiceEndpoint->registerStream(keep);
Phil Burkec89b2e2017-06-20 15:05:06 -0700177 if (result != AAUDIO_OK) {
178 goto error;
179 }
Phil Burkc0c70e32017-02-09 13:18:38 -0800180
Phil Burk5a26e662017-07-07 12:44:48 -0700181 setState(AAUDIO_STREAM_STATE_OPEN);
Phil Burkc0c70e32017-02-09 13:18:38 -0800182 return AAUDIO_OK;
Phil Burkec89b2e2017-06-20 15:05:06 -0700183
184error:
185 close();
186 return result;
Phil Burkc0c70e32017-02-09 13:18:38 -0800187}
188
189/**
190 * Start the flow of audio data.
191 *
192 * An AAUDIO_SERVICE_EVENT_STARTED will be sent to the client when complete.
193 */
194aaudio_result_t AAudioServiceStreamShared::start() {
Eric Laurentcb4dae22017-07-01 19:39:32 -0700195 if (isRunning()) {
196 return AAUDIO_OK;
197 }
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 Burk5ef003b2017-06-30 11:43:37 -0700206 disconnect();
Phil Burkc0c70e32017-02-09 13:18:38 -0800207 } else {
Eric Laurentcb4dae22017-07-01 19:39:32 -0700208 result = endpoint->getStreamInternal()->startClient(mMmapClient, &mClientHandle);
209 if (result == AAUDIO_OK) {
210 result = AAudioServiceStreamBase::start();
211 }
Phil Burkc0c70e32017-02-09 13:18:38 -0800212 }
Phil Burkec89b2e2017-06-20 15:05:06 -0700213 return result;
Phil Burkc0c70e32017-02-09 13:18:38 -0800214}
215
216/**
217 * Stop the flow of data so that start() can resume without loss of data.
218 *
219 * An AAUDIO_SERVICE_EVENT_PAUSED will be sent to the client when complete.
220*/
221aaudio_result_t AAudioServiceStreamShared::pause() {
Eric Laurentcb4dae22017-07-01 19:39:32 -0700222 if (!isRunning()) {
223 return AAUDIO_OK;
224 }
Phil Burk71f35bb2017-04-13 16:05:07 -0700225 AAudioServiceEndpoint *endpoint = mServiceEndpoint;
226 if (endpoint == nullptr) {
227 return AAUDIO_ERROR_INVALID_STATE;
228 }
Eric Laurentcb4dae22017-07-01 19:39:32 -0700229 endpoint->getStreamInternal()->stopClient(mClientHandle);
Phil Burk71f35bb2017-04-13 16:05:07 -0700230 aaudio_result_t result = endpoint->stopStream(this);
231 if (result != AAUDIO_OK) {
232 ALOGE("AAudioServiceStreamShared::pause() mServiceEndpoint returned %d", result);
Phil Burk5ef003b2017-06-30 11:43:37 -0700233 disconnect(); // TODO should we return or pause Base first?
Phil Burk71f35bb2017-04-13 16:05:07 -0700234 }
235 return AAudioServiceStreamBase::pause();
236}
237
238aaudio_result_t AAudioServiceStreamShared::stop() {
Eric Laurentcb4dae22017-07-01 19:39:32 -0700239 if (!isRunning()) {
240 return AAUDIO_OK;
241 }
Phil Burk71f35bb2017-04-13 16:05:07 -0700242 AAudioServiceEndpoint *endpoint = mServiceEndpoint;
243 if (endpoint == nullptr) {
244 return AAUDIO_ERROR_INVALID_STATE;
245 }
Eric Laurentcb4dae22017-07-01 19:39:32 -0700246 endpoint->getStreamInternal()->stopClient(mClientHandle);
Phil Burk71f35bb2017-04-13 16:05:07 -0700247 aaudio_result_t result = endpoint->stopStream(this);
Phil Burkc0c70e32017-02-09 13:18:38 -0800248 if (result != AAUDIO_OK) {
249 ALOGE("AAudioServiceStreamShared::stop() mServiceEndpoint returned %d", result);
Phil Burk5ef003b2017-06-30 11:43:37 -0700250 disconnect();
Phil Burkc0c70e32017-02-09 13:18:38 -0800251 }
Phil Burk71f35bb2017-04-13 16:05:07 -0700252 return AAudioServiceStreamBase::stop();
Phil Burkc0c70e32017-02-09 13:18:38 -0800253}
254
255/**
256 * Discard any data held by the underlying HAL or Service.
257 *
258 * An AAUDIO_SERVICE_EVENT_FLUSHED will be sent to the client when complete.
259 */
260aaudio_result_t AAudioServiceStreamShared::flush() {
Phil Burkec89b2e2017-06-20 15:05:06 -0700261 AAudioServiceEndpoint *endpoint = mServiceEndpoint;
262 if (endpoint == nullptr) {
263 return AAUDIO_ERROR_INVALID_STATE;
264 }
265 if (mState != AAUDIO_STREAM_STATE_PAUSED) {
Eric Laurentcb4dae22017-07-01 19:39:32 -0700266 ALOGE("AAudioServiceStreamShared::flush() stream not paused, state = %s",
Phil Burkec89b2e2017-06-20 15:05:06 -0700267 AAudio_convertStreamStateToText(mState));
268 return AAUDIO_ERROR_INVALID_STATE;
269 }
270 // Data will get flushed when the client receives the FLUSHED event.
271 return AAudioServiceStreamBase::flush();
Phil Burkc0c70e32017-02-09 13:18:38 -0800272}
273
274aaudio_result_t AAudioServiceStreamShared::close() {
Phil Burk98d6d922017-07-06 11:52:45 -0700275 if (mState == AAUDIO_STREAM_STATE_CLOSED) {
276 return AAUDIO_OK;
Phil Burk71f35bb2017-04-13 16:05:07 -0700277 }
Phil Burk98d6d922017-07-06 11:52:45 -0700278
Eric Laurentcb4dae22017-07-01 19:39:32 -0700279 stop();
280
Phil Burk98d6d922017-07-06 11:52:45 -0700281 AAudioServiceEndpoint *endpoint = mServiceEndpoint;
282 if (endpoint == nullptr) {
283 return AAUDIO_ERROR_INVALID_STATE;
284 }
Phil Burk98d6d922017-07-06 11:52:45 -0700285
286 endpoint->unregisterStream(this);
287
288 AAudioEndpointManager &mEndpointManager = AAudioEndpointManager::getInstance();
289 mEndpointManager.closeEndpoint(endpoint);
290 mServiceEndpoint = nullptr;
291
Phil Burk942bdc02017-05-03 11:36:50 -0700292 if (mAudioDataQueue != nullptr) {
293 delete mAudioDataQueue;
294 mAudioDataQueue = nullptr;
295 }
Phil Burkc0c70e32017-02-09 13:18:38 -0800296 return AAudioServiceStreamBase::close();
297}
298
299/**
300 * Get an immutable description of the data queue created by this service.
301 */
302aaudio_result_t AAudioServiceStreamShared::getDownDataDescription(AudioEndpointParcelable &parcelable)
303{
304 // Gather information on the data queue.
305 mAudioDataQueue->fillParcelable(parcelable,
306 parcelable.mDownDataQueueParcelable);
307 parcelable.mDownDataQueueParcelable.setFramesPerBurst(getFramesPerBurst());
308 return AAUDIO_OK;
309}
310
Phil Burk97350f92017-07-21 15:59:44 -0700311void AAudioServiceStreamShared::markTransferTime(Timestamp &timestamp) {
312 mAtomicTimestamp.write(timestamp);
Phil Burk71f35bb2017-04-13 16:05:07 -0700313}
Phil Burkc0c70e32017-02-09 13:18:38 -0800314
Phil Burk97350f92017-07-21 15:59:44 -0700315// Get timestamp that was written by the real-time service thread, eg. mixer.
Phil Burkc0c70e32017-02-09 13:18:38 -0800316aaudio_result_t AAudioServiceStreamShared::getFreeRunningPosition(int64_t *positionFrames,
317 int64_t *timeNanos) {
Phil Burk97350f92017-07-21 15:59:44 -0700318 if (mAtomicTimestamp.isValid()) {
319 Timestamp timestamp = mAtomicTimestamp.read();
320 *positionFrames = timestamp.getPosition();
321 *timeNanos = timestamp.getNanoseconds();
322 return AAUDIO_OK;
323 } else {
324 return AAUDIO_ERROR_UNAVAILABLE;
325 }
326}
327
328// Get timestamp from lower level service.
329aaudio_result_t AAudioServiceStreamShared::getHardwareTimestamp(int64_t *positionFrames,
330 int64_t *timeNanos) {
331
332 aaudio_result_t result = mServiceEndpoint->getTimestamp(positionFrames, timeNanos);
333 if (result == AAUDIO_OK) {
334 *positionFrames -= mTimestampPositionOffset.load(); // Offset from shared MMAP stream
335 }
336 return result;
Phil Burkc0c70e32017-02-09 13:18:38 -0800337}