blob: d648c6d64af296ed5c8b18c6f717dfe5045ea2de [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
Phil Burka5222e22017-07-28 13:31:14 -070021#include <iomanip>
22#include <iostream>
Phil Burkc0c70e32017-02-09 13:18:38 -080023#include <mutex>
24
25#include <aaudio/AAudio.h>
26
27#include "binding/IAAudioService.h"
28
29#include "binding/AAudioServiceMessage.h"
30#include "AAudioServiceStreamBase.h"
31#include "AAudioServiceStreamShared.h"
32#include "AAudioEndpointManager.h"
33#include "AAudioService.h"
34#include "AAudioServiceEndpoint.h"
35
36using namespace android;
37using namespace aaudio;
38
Phil Burkec89b2e2017-06-20 15:05:06 -070039#define MIN_BURSTS_PER_BUFFER 2
40#define DEFAULT_BURSTS_PER_BUFFER 16
41// This is an arbitrary range. TODO review.
42#define MAX_FRAMES_PER_BUFFER (32 * 1024)
Phil Burkc0c70e32017-02-09 13:18:38 -080043
44AAudioServiceStreamShared::AAudioServiceStreamShared(AAudioService &audioService)
45 : mAudioService(audioService)
Phil Burk97350f92017-07-21 15:59:44 -070046 , mTimestampPositionOffset(0)
Phil Burka5222e22017-07-28 13:31:14 -070047 , mXRunCount(0)
Phil Burkc0c70e32017-02-09 13:18:38 -080048 {
49}
50
Phil Burka5222e22017-07-28 13:31:14 -070051std::string AAudioServiceStreamShared::dumpHeader() {
52 std::stringstream result;
53 result << AAudioServiceStreamBase::dumpHeader();
54 result << " Write# Read# Avail XRuns";
55 return result.str();
56}
57
58std::string AAudioServiceStreamShared::dump() const {
59 std::stringstream result;
60 result << AAudioServiceStreamBase::dump();
61
62 auto fifo = mAudioDataQueue->getFifoBuffer();
63 int32_t readCounter = fifo->getReadCounter();
64 int32_t writeCounter = fifo->getWriteCounter();
65 result << std::setw(10) << writeCounter;
66 result << std::setw(10) << readCounter;
67 result << std::setw(8) << (writeCounter - readCounter);
68 result << std::setw(8) << getXRunCount();
69
70 return result.str();
71}
72
Phil Burkec89b2e2017-06-20 15:05:06 -070073int32_t AAudioServiceStreamShared::calculateBufferCapacity(int32_t requestedCapacityFrames,
74 int32_t framesPerBurst) {
75
76 if (requestedCapacityFrames > MAX_FRAMES_PER_BUFFER) {
Phil Burk11e8d332017-05-24 09:59:02 -070077 ALOGE("AAudioServiceStreamShared::calculateBufferCapacity() requested capacity %d > max %d",
Phil Burkec89b2e2017-06-20 15:05:06 -070078 requestedCapacityFrames, MAX_FRAMES_PER_BUFFER);
79 return AAUDIO_ERROR_OUT_OF_RANGE;
80 }
81
82 // Determine how many bursts will fit in the buffer.
83 int32_t numBursts;
84 if (requestedCapacityFrames == AAUDIO_UNSPECIFIED) {
85 // Use fewer bursts if default is too many.
86 if ((DEFAULT_BURSTS_PER_BUFFER * framesPerBurst) > MAX_FRAMES_PER_BUFFER) {
87 numBursts = MAX_FRAMES_PER_BUFFER / framesPerBurst;
88 } else {
89 numBursts = DEFAULT_BURSTS_PER_BUFFER;
90 }
91 } else {
92 // round up to nearest burst boundary
93 numBursts = (requestedCapacityFrames + framesPerBurst - 1) / framesPerBurst;
94 }
95
96 // Clip to bare minimum.
97 if (numBursts < MIN_BURSTS_PER_BUFFER) {
98 numBursts = MIN_BURSTS_PER_BUFFER;
99 }
100 // Check for numeric overflow.
101 if (numBursts > 0x8000 || framesPerBurst > 0x8000) {
Phil Burk11e8d332017-05-24 09:59:02 -0700102 ALOGE("AAudioServiceStreamShared::calculateBufferCapacity() overflow, capacity = %d * %d",
Phil Burkec89b2e2017-06-20 15:05:06 -0700103 numBursts, framesPerBurst);
104 return AAUDIO_ERROR_OUT_OF_RANGE;
105 }
106 int32_t capacityInFrames = numBursts * framesPerBurst;
107
108 // Final sanity check.
109 if (capacityInFrames > MAX_FRAMES_PER_BUFFER) {
Phil Burk11e8d332017-05-24 09:59:02 -0700110 ALOGE("AAudioServiceStreamShared::calculateBufferCapacity() calc capacity %d > max %d",
Phil Burkec89b2e2017-06-20 15:05:06 -0700111 capacityInFrames, MAX_FRAMES_PER_BUFFER);
112 return AAUDIO_ERROR_OUT_OF_RANGE;
113 }
Phil Burk11e8d332017-05-24 09:59:02 -0700114 ALOGD("AAudioServiceStreamShared::calculateBufferCapacity() requested %d frames, actual = %d",
Phil Burkec89b2e2017-06-20 15:05:06 -0700115 requestedCapacityFrames, capacityInFrames);
116 return capacityInFrames;
117}
118
Phil Burkc0c70e32017-02-09 13:18:38 -0800119aaudio_result_t AAudioServiceStreamShared::open(const aaudio::AAudioStreamRequest &request,
120 aaudio::AAudioStreamConfiguration &configurationOutput) {
121
Phil Burk11e8d332017-05-24 09:59:02 -0700122 sp<AAudioServiceStreamShared> keep(this);
123
Phil Burkc0c70e32017-02-09 13:18:38 -0800124 aaudio_result_t result = AAudioServiceStreamBase::open(request, configurationOutput);
125 if (result != AAUDIO_OK) {
Phil Burkec89b2e2017-06-20 15:05:06 -0700126 ALOGE("AAudioServiceStreamBase open() returned %d", result);
Phil Burkc0c70e32017-02-09 13:18:38 -0800127 return result;
128 }
129
130 const AAudioStreamConfiguration &configurationInput = request.getConstantConfiguration();
Phil Burkc0c70e32017-02-09 13:18:38 -0800131 aaudio_direction_t direction = request.getDirection();
132
Phil Burkc0c70e32017-02-09 13:18:38 -0800133 AAudioEndpointManager &mEndpointManager = AAudioEndpointManager::getInstance();
Eric Laurenta17ae742017-06-29 15:43:55 -0700134 mServiceEndpoint = mEndpointManager.openEndpoint(mAudioService, configurationOutput, direction);
Phil Burkc0c70e32017-02-09 13:18:38 -0800135 if (mServiceEndpoint == nullptr) {
Phil Burk11e8d332017-05-24 09:59:02 -0700136 ALOGE("AAudioServiceStreamShared::open() mServiceEndPoint = %p", mServiceEndpoint);
Phil Burkc0c70e32017-02-09 13:18:38 -0800137 return AAUDIO_ERROR_UNAVAILABLE;
138 }
139
140 // Is the request compatible with the shared endpoint?
jiabin901f65d2017-07-12 17:56:35 -0700141 mAudioFormat = configurationInput.getFormat();
Phil Burkc0c70e32017-02-09 13:18:38 -0800142 if (mAudioFormat == AAUDIO_FORMAT_UNSPECIFIED) {
143 mAudioFormat = AAUDIO_FORMAT_PCM_FLOAT;
144 } else if (mAudioFormat != AAUDIO_FORMAT_PCM_FLOAT) {
Phil Burk11e8d332017-05-24 09:59:02 -0700145 ALOGE("AAudioServiceStreamShared::open() mAudioFormat = %d, need FLOAT", mAudioFormat);
Phil Burkec89b2e2017-06-20 15:05:06 -0700146 result = AAUDIO_ERROR_INVALID_FORMAT;
147 goto error;
Phil Burkc0c70e32017-02-09 13:18:38 -0800148 }
149
150 mSampleRate = configurationInput.getSampleRate();
Glenn Kasten37a466a2017-05-30 15:53:14 -0700151 if (mSampleRate == AAUDIO_UNSPECIFIED) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800152 mSampleRate = mServiceEndpoint->getSampleRate();
153 } else if (mSampleRate != mServiceEndpoint->getSampleRate()) {
Phil Burk11e8d332017-05-24 09:59:02 -0700154 ALOGE("AAudioServiceStreamShared::open() mSampleRate = %d, need %d",
Phil Burk71f35bb2017-04-13 16:05:07 -0700155 mSampleRate, mServiceEndpoint->getSampleRate());
Phil Burkec89b2e2017-06-20 15:05:06 -0700156 result = AAUDIO_ERROR_INVALID_RATE;
157 goto error;
Phil Burkc0c70e32017-02-09 13:18:38 -0800158 }
159
160 mSamplesPerFrame = configurationInput.getSamplesPerFrame();
Glenn Kasten37a466a2017-05-30 15:53:14 -0700161 if (mSamplesPerFrame == AAUDIO_UNSPECIFIED) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800162 mSamplesPerFrame = mServiceEndpoint->getSamplesPerFrame();
163 } else if (mSamplesPerFrame != mServiceEndpoint->getSamplesPerFrame()) {
Phil Burk11e8d332017-05-24 09:59:02 -0700164 ALOGE("AAudioServiceStreamShared::open() mSamplesPerFrame = %d, need %d",
Phil Burk71f35bb2017-04-13 16:05:07 -0700165 mSamplesPerFrame, mServiceEndpoint->getSamplesPerFrame());
Phil Burkec89b2e2017-06-20 15:05:06 -0700166 result = AAUDIO_ERROR_OUT_OF_RANGE;
167 goto error;
Phil Burkc0c70e32017-02-09 13:18:38 -0800168 }
169
Phil Burkc0c70e32017-02-09 13:18:38 -0800170 mFramesPerBurst = mServiceEndpoint->getFramesPerBurst();
Phil Burk11e8d332017-05-24 09:59:02 -0700171 ALOGD("AAudioServiceStreamShared::open() mSampleRate = %d, mFramesPerBurst = %d",
Phil Burkec89b2e2017-06-20 15:05:06 -0700172 mSampleRate, mFramesPerBurst);
173
174 mCapacityInFrames = calculateBufferCapacity(configurationInput.getBufferCapacity(),
175 mFramesPerBurst);
176 if (mCapacityInFrames < 0) {
177 result = mCapacityInFrames; // negative error code
178 mCapacityInFrames = 0;
179 goto error;
Phil Burkc0c70e32017-02-09 13:18:38 -0800180 }
Phil Burkc0c70e32017-02-09 13:18:38 -0800181
182 // Create audio data shared memory buffer for client.
183 mAudioDataQueue = new SharedRingBuffer();
Phil Burkec89b2e2017-06-20 15:05:06 -0700184 result = mAudioDataQueue->allocate(calculateBytesPerFrame(), mCapacityInFrames);
185 if (result != AAUDIO_OK) {
Phil Burk11e8d332017-05-24 09:59:02 -0700186 ALOGE("AAudioServiceStreamShared::open() could not allocate FIFO with %d frames",
Phil Burkec89b2e2017-06-20 15:05:06 -0700187 mCapacityInFrames);
188 result = AAUDIO_ERROR_NO_MEMORY;
189 goto error;
190 }
191
192 ALOGD("AAudioServiceStreamShared::open() actual rate = %d, channels = %d, deviceId = %d",
193 mSampleRate, mSamplesPerFrame, mServiceEndpoint->getDeviceId());
Phil Burkc0c70e32017-02-09 13:18:38 -0800194
195 // Fill in configuration for client.
196 configurationOutput.setSampleRate(mSampleRate);
197 configurationOutput.setSamplesPerFrame(mSamplesPerFrame);
jiabin901f65d2017-07-12 17:56:35 -0700198 configurationOutput.setFormat(mAudioFormat);
Phil Burkec89b2e2017-06-20 15:05:06 -0700199 configurationOutput.setDeviceId(mServiceEndpoint->getDeviceId());
Phil Burkc0c70e32017-02-09 13:18:38 -0800200
Phil Burk11e8d332017-05-24 09:59:02 -0700201 result = mServiceEndpoint->registerStream(keep);
Phil Burkec89b2e2017-06-20 15:05:06 -0700202 if (result != AAUDIO_OK) {
203 goto error;
204 }
Phil Burkc0c70e32017-02-09 13:18:38 -0800205
Phil Burk5a26e662017-07-07 12:44:48 -0700206 setState(AAUDIO_STREAM_STATE_OPEN);
Phil Burkc0c70e32017-02-09 13:18:38 -0800207 return AAUDIO_OK;
Phil Burkec89b2e2017-06-20 15:05:06 -0700208
209error:
210 close();
211 return result;
Phil Burkc0c70e32017-02-09 13:18:38 -0800212}
213
214/**
215 * Start the flow of audio data.
216 *
217 * An AAUDIO_SERVICE_EVENT_STARTED will be sent to the client when complete.
218 */
219aaudio_result_t AAudioServiceStreamShared::start() {
Eric Laurentcb4dae22017-07-01 19:39:32 -0700220 if (isRunning()) {
221 return AAUDIO_OK;
222 }
Phil Burk71f35bb2017-04-13 16:05:07 -0700223 AAudioServiceEndpoint *endpoint = mServiceEndpoint;
224 if (endpoint == nullptr) {
Phil Burka5222e22017-07-28 13:31:14 -0700225 ALOGE("AAudioServiceStreamShared::start() missing endpoint");
Phil Burk71f35bb2017-04-13 16:05:07 -0700226 return AAUDIO_ERROR_INVALID_STATE;
227 }
Phil Burk87c9f642017-05-17 07:22:39 -0700228 // For output streams, this will add the stream to the mixer.
Phil Burk71f35bb2017-04-13 16:05:07 -0700229 aaudio_result_t result = endpoint->startStream(this);
Phil Burkc0c70e32017-02-09 13:18:38 -0800230 if (result != AAUDIO_OK) {
231 ALOGE("AAudioServiceStreamShared::start() mServiceEndpoint returned %d", result);
Phil Burk5ef003b2017-06-30 11:43:37 -0700232 disconnect();
Phil Burkc0c70e32017-02-09 13:18:38 -0800233 } else {
Eric Laurentcb4dae22017-07-01 19:39:32 -0700234 result = endpoint->getStreamInternal()->startClient(mMmapClient, &mClientHandle);
235 if (result == AAUDIO_OK) {
236 result = AAudioServiceStreamBase::start();
237 }
Phil Burkc0c70e32017-02-09 13:18:38 -0800238 }
Phil Burkec89b2e2017-06-20 15:05:06 -0700239 return result;
Phil Burkc0c70e32017-02-09 13:18:38 -0800240}
241
242/**
243 * Stop the flow of data so that start() can resume without loss of data.
244 *
245 * An AAUDIO_SERVICE_EVENT_PAUSED will be sent to the client when complete.
246*/
247aaudio_result_t AAudioServiceStreamShared::pause() {
Eric Laurentcb4dae22017-07-01 19:39:32 -0700248 if (!isRunning()) {
249 return AAUDIO_OK;
250 }
Phil Burk71f35bb2017-04-13 16:05:07 -0700251 AAudioServiceEndpoint *endpoint = mServiceEndpoint;
252 if (endpoint == nullptr) {
Phil Burka5222e22017-07-28 13:31:14 -0700253 ALOGE("AAudioServiceStreamShared::pause() missing endpoint");
Phil Burk71f35bb2017-04-13 16:05:07 -0700254 return AAUDIO_ERROR_INVALID_STATE;
255 }
Eric Laurentcb4dae22017-07-01 19:39:32 -0700256 endpoint->getStreamInternal()->stopClient(mClientHandle);
Phil Burk71f35bb2017-04-13 16:05:07 -0700257 aaudio_result_t result = endpoint->stopStream(this);
258 if (result != AAUDIO_OK) {
259 ALOGE("AAudioServiceStreamShared::pause() mServiceEndpoint returned %d", result);
Phil Burk5ef003b2017-06-30 11:43:37 -0700260 disconnect(); // TODO should we return or pause Base first?
Phil Burk71f35bb2017-04-13 16:05:07 -0700261 }
262 return AAudioServiceStreamBase::pause();
263}
264
265aaudio_result_t AAudioServiceStreamShared::stop() {
Eric Laurentcb4dae22017-07-01 19:39:32 -0700266 if (!isRunning()) {
267 return AAUDIO_OK;
268 }
Phil Burk71f35bb2017-04-13 16:05:07 -0700269 AAudioServiceEndpoint *endpoint = mServiceEndpoint;
270 if (endpoint == nullptr) {
Phil Burka5222e22017-07-28 13:31:14 -0700271 ALOGE("AAudioServiceStreamShared::stop() missing endpoint");
Phil Burk71f35bb2017-04-13 16:05:07 -0700272 return AAUDIO_ERROR_INVALID_STATE;
273 }
Eric Laurentcb4dae22017-07-01 19:39:32 -0700274 endpoint->getStreamInternal()->stopClient(mClientHandle);
Phil Burk71f35bb2017-04-13 16:05:07 -0700275 aaudio_result_t result = endpoint->stopStream(this);
Phil Burkc0c70e32017-02-09 13:18:38 -0800276 if (result != AAUDIO_OK) {
277 ALOGE("AAudioServiceStreamShared::stop() mServiceEndpoint returned %d", result);
Phil Burk5ef003b2017-06-30 11:43:37 -0700278 disconnect();
Phil Burkc0c70e32017-02-09 13:18:38 -0800279 }
Phil Burk71f35bb2017-04-13 16:05:07 -0700280 return AAudioServiceStreamBase::stop();
Phil Burkc0c70e32017-02-09 13:18:38 -0800281}
282
283/**
284 * Discard any data held by the underlying HAL or Service.
285 *
286 * An AAUDIO_SERVICE_EVENT_FLUSHED will be sent to the client when complete.
287 */
288aaudio_result_t AAudioServiceStreamShared::flush() {
Phil Burkec89b2e2017-06-20 15:05:06 -0700289 AAudioServiceEndpoint *endpoint = mServiceEndpoint;
290 if (endpoint == nullptr) {
Phil Burka5222e22017-07-28 13:31:14 -0700291 ALOGE("AAudioServiceStreamShared::flush() missing endpoint");
Phil Burkec89b2e2017-06-20 15:05:06 -0700292 return AAUDIO_ERROR_INVALID_STATE;
293 }
294 if (mState != AAUDIO_STREAM_STATE_PAUSED) {
Eric Laurentcb4dae22017-07-01 19:39:32 -0700295 ALOGE("AAudioServiceStreamShared::flush() stream not paused, state = %s",
Phil Burkec89b2e2017-06-20 15:05:06 -0700296 AAudio_convertStreamStateToText(mState));
297 return AAUDIO_ERROR_INVALID_STATE;
298 }
299 // Data will get flushed when the client receives the FLUSHED event.
300 return AAudioServiceStreamBase::flush();
Phil Burkc0c70e32017-02-09 13:18:38 -0800301}
302
303aaudio_result_t AAudioServiceStreamShared::close() {
Phil Burk98d6d922017-07-06 11:52:45 -0700304 if (mState == AAUDIO_STREAM_STATE_CLOSED) {
305 return AAUDIO_OK;
Phil Burk71f35bb2017-04-13 16:05:07 -0700306 }
Phil Burk98d6d922017-07-06 11:52:45 -0700307
Eric Laurentcb4dae22017-07-01 19:39:32 -0700308 stop();
309
Phil Burk98d6d922017-07-06 11:52:45 -0700310 AAudioServiceEndpoint *endpoint = mServiceEndpoint;
311 if (endpoint == nullptr) {
312 return AAUDIO_ERROR_INVALID_STATE;
313 }
Phil Burk98d6d922017-07-06 11:52:45 -0700314
315 endpoint->unregisterStream(this);
316
317 AAudioEndpointManager &mEndpointManager = AAudioEndpointManager::getInstance();
318 mEndpointManager.closeEndpoint(endpoint);
319 mServiceEndpoint = nullptr;
320
Phil Burk942bdc02017-05-03 11:36:50 -0700321 if (mAudioDataQueue != nullptr) {
322 delete mAudioDataQueue;
323 mAudioDataQueue = nullptr;
324 }
Phil Burkc0c70e32017-02-09 13:18:38 -0800325 return AAudioServiceStreamBase::close();
326}
327
328/**
329 * Get an immutable description of the data queue created by this service.
330 */
331aaudio_result_t AAudioServiceStreamShared::getDownDataDescription(AudioEndpointParcelable &parcelable)
332{
333 // Gather information on the data queue.
334 mAudioDataQueue->fillParcelable(parcelable,
335 parcelable.mDownDataQueueParcelable);
336 parcelable.mDownDataQueueParcelable.setFramesPerBurst(getFramesPerBurst());
337 return AAUDIO_OK;
338}
339
Phil Burk97350f92017-07-21 15:59:44 -0700340void AAudioServiceStreamShared::markTransferTime(Timestamp &timestamp) {
341 mAtomicTimestamp.write(timestamp);
Phil Burk71f35bb2017-04-13 16:05:07 -0700342}
Phil Burkc0c70e32017-02-09 13:18:38 -0800343
Phil Burk97350f92017-07-21 15:59:44 -0700344// Get timestamp that was written by the real-time service thread, eg. mixer.
Phil Burkc0c70e32017-02-09 13:18:38 -0800345aaudio_result_t AAudioServiceStreamShared::getFreeRunningPosition(int64_t *positionFrames,
346 int64_t *timeNanos) {
Phil Burk97350f92017-07-21 15:59:44 -0700347 if (mAtomicTimestamp.isValid()) {
348 Timestamp timestamp = mAtomicTimestamp.read();
349 *positionFrames = timestamp.getPosition();
350 *timeNanos = timestamp.getNanoseconds();
351 return AAUDIO_OK;
352 } else {
353 return AAUDIO_ERROR_UNAVAILABLE;
354 }
355}
356
357// Get timestamp from lower level service.
358aaudio_result_t AAudioServiceStreamShared::getHardwareTimestamp(int64_t *positionFrames,
359 int64_t *timeNanos) {
360
361 aaudio_result_t result = mServiceEndpoint->getTimestamp(positionFrames, timeNanos);
362 if (result == AAUDIO_OK) {
363 *positionFrames -= mTimestampPositionOffset.load(); // Offset from shared MMAP stream
364 }
365 return result;
Phil Burkc0c70e32017-02-09 13:18:38 -0800366}