blob: 75d88cf54a86b08a57ec56bdcde695686ab05dbb [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)
Phil Burk39f02dd2017-08-04 09:13:31 -070045 : AAudioServiceStreamBase(audioService)
Phil Burk97350f92017-07-21 15:59:44 -070046 , mTimestampPositionOffset(0)
Phil Burk39f02dd2017-08-04 09:13:31 -070047 , mXRunCount(0) {
Phil Burkc0c70e32017-02-09 13:18:38 -080048}
49
Phil Burka5222e22017-07-28 13:31:14 -070050std::string AAudioServiceStreamShared::dumpHeader() {
51 std::stringstream result;
52 result << AAudioServiceStreamBase::dumpHeader();
53 result << " Write# Read# Avail XRuns";
54 return result.str();
55}
56
57std::string AAudioServiceStreamShared::dump() const {
58 std::stringstream result;
Phil Burk39f02dd2017-08-04 09:13:31 -070059
Phil Burka5222e22017-07-28 13:31:14 -070060 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 Burkfbf031e2017-10-12 15:58:31 -070077 ALOGE("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 Burkfbf031e2017-10-12 15:58:31 -0700102 ALOGE("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 Burkfbf031e2017-10-12 15:58:31 -0700110 ALOGE("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 Burkfbf031e2017-10-12 15:58:31 -0700114 ALOGD("calculateBufferCapacity() requested %d frames, actual = %d",
Phil Burkec89b2e2017-06-20 15:05:06 -0700115 requestedCapacityFrames, capacityInFrames);
116 return capacityInFrames;
117}
118
Phil Burk39f02dd2017-08-04 09:13:31 -0700119aaudio_result_t AAudioServiceStreamShared::open(const aaudio::AAudioStreamRequest &request) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800120
Phil Burk11e8d332017-05-24 09:59:02 -0700121 sp<AAudioServiceStreamShared> keep(this);
122
Phil Burk39f02dd2017-08-04 09:13:31 -0700123 aaudio_result_t result = AAudioServiceStreamBase::open(request, AAUDIO_SHARING_MODE_SHARED);
Phil Burkc0c70e32017-02-09 13:18:38 -0800124 if (result != AAUDIO_OK) {
Phil Burkfbf031e2017-10-12 15:58:31 -0700125 ALOGE("open() returned %d", result);
Phil Burkc0c70e32017-02-09 13:18:38 -0800126 return result;
127 }
128
129 const AAudioStreamConfiguration &configurationInput = request.getConstantConfiguration();
Phil Burkc0c70e32017-02-09 13:18:38 -0800130
Phil Burkc0c70e32017-02-09 13:18:38 -0800131
132 // Is the request compatible with the shared endpoint?
Phil Burk39f02dd2017-08-04 09:13:31 -0700133 setFormat(configurationInput.getFormat());
134 if (getFormat() == AAUDIO_FORMAT_UNSPECIFIED) {
135 setFormat(AAUDIO_FORMAT_PCM_FLOAT);
136 } else if (getFormat() != AAUDIO_FORMAT_PCM_FLOAT) {
Phil Burkfbf031e2017-10-12 15:58:31 -0700137 ALOGE("open() mAudioFormat = %d, need FLOAT", getFormat());
Phil Burkec89b2e2017-06-20 15:05:06 -0700138 result = AAUDIO_ERROR_INVALID_FORMAT;
139 goto error;
Phil Burkc0c70e32017-02-09 13:18:38 -0800140 }
141
Phil Burk39f02dd2017-08-04 09:13:31 -0700142 setSampleRate(configurationInput.getSampleRate());
143 if (getSampleRate() == AAUDIO_UNSPECIFIED) {
144 setSampleRate(mServiceEndpoint->getSampleRate());
145 } else if (getSampleRate() != mServiceEndpoint->getSampleRate()) {
Phil Burkfbf031e2017-10-12 15:58:31 -0700146 ALOGE("open() mSampleRate = %d, need %d",
Phil Burk39f02dd2017-08-04 09:13:31 -0700147 getSampleRate(), mServiceEndpoint->getSampleRate());
Phil Burkec89b2e2017-06-20 15:05:06 -0700148 result = AAUDIO_ERROR_INVALID_RATE;
149 goto error;
Phil Burkc0c70e32017-02-09 13:18:38 -0800150 }
151
Phil Burk39f02dd2017-08-04 09:13:31 -0700152 setSamplesPerFrame(configurationInput.getSamplesPerFrame());
153 if (getSamplesPerFrame() == AAUDIO_UNSPECIFIED) {
154 setSamplesPerFrame(mServiceEndpoint->getSamplesPerFrame());
155 } else if (getSamplesPerFrame() != mServiceEndpoint->getSamplesPerFrame()) {
Phil Burkfbf031e2017-10-12 15:58:31 -0700156 ALOGE("open() mSamplesPerFrame = %d, need %d",
Phil Burk39f02dd2017-08-04 09:13:31 -0700157 getSamplesPerFrame(), mServiceEndpoint->getSamplesPerFrame());
Phil Burkec89b2e2017-06-20 15:05:06 -0700158 result = AAUDIO_ERROR_OUT_OF_RANGE;
159 goto error;
Phil Burkc0c70e32017-02-09 13:18:38 -0800160 }
161
Phil Burk39f02dd2017-08-04 09:13:31 -0700162 setBufferCapacity(calculateBufferCapacity(configurationInput.getBufferCapacity(),
163 mFramesPerBurst));
164 if (getBufferCapacity() < 0) {
165 result = getBufferCapacity(); // negative error code
166 setBufferCapacity(0);
Phil Burkec89b2e2017-06-20 15:05:06 -0700167 goto error;
Phil Burkc0c70e32017-02-09 13:18:38 -0800168 }
Phil Burkc0c70e32017-02-09 13:18:38 -0800169
Phil Burk523b3042017-09-13 13:03:08 -0700170 {
171 std::lock_guard<std::mutex> lock(mAudioDataQueueLock);
172 // Create audio data shared memory buffer for client.
173 mAudioDataQueue = new SharedRingBuffer();
174 result = mAudioDataQueue->allocate(calculateBytesPerFrame(), getBufferCapacity());
175 if (result != AAUDIO_OK) {
Phil Burkfbf031e2017-10-12 15:58:31 -0700176 ALOGE("open() could not allocate FIFO with %d frames",
Phil Burk523b3042017-09-13 13:03:08 -0700177 getBufferCapacity());
178 result = AAUDIO_ERROR_NO_MEMORY;
179 goto error;
180 }
Phil Burkec89b2e2017-06-20 15:05:06 -0700181 }
182
Phil Burkfbf031e2017-10-12 15:58:31 -0700183 ALOGD("open() actual rate = %d, channels = %d, deviceId = %d",
Phil Burk39f02dd2017-08-04 09:13:31 -0700184 getSampleRate(), getSamplesPerFrame(), mServiceEndpoint->getDeviceId());
Phil Burkc0c70e32017-02-09 13:18:38 -0800185
Phil Burk11e8d332017-05-24 09:59:02 -0700186 result = mServiceEndpoint->registerStream(keep);
Phil Burkec89b2e2017-06-20 15:05:06 -0700187 if (result != AAUDIO_OK) {
188 goto error;
189 }
Phil Burkc0c70e32017-02-09 13:18:38 -0800190
Phil Burk5a26e662017-07-07 12:44:48 -0700191 setState(AAUDIO_STREAM_STATE_OPEN);
Phil Burkc0c70e32017-02-09 13:18:38 -0800192 return AAUDIO_OK;
Phil Burkec89b2e2017-06-20 15:05:06 -0700193
194error:
195 close();
196 return result;
Phil Burkc0c70e32017-02-09 13:18:38 -0800197}
198
Phil Burkc0c70e32017-02-09 13:18:38 -0800199
200aaudio_result_t AAudioServiceStreamShared::close() {
Phil Burk39f02dd2017-08-04 09:13:31 -0700201 aaudio_result_t result = AAudioServiceStreamBase::close();
Phil Burk98d6d922017-07-06 11:52:45 -0700202
Phil Burk523b3042017-09-13 13:03:08 -0700203 {
204 std::lock_guard<std::mutex> lock(mAudioDataQueueLock);
205 delete mAudioDataQueue;
206 mAudioDataQueue = nullptr;
207 }
Eric Laurentcb4dae22017-07-01 19:39:32 -0700208
Phil Burk39f02dd2017-08-04 09:13:31 -0700209 return result;
Phil Burkc0c70e32017-02-09 13:18:38 -0800210}
211
212/**
213 * Get an immutable description of the data queue created by this service.
214 */
Phil Burk523b3042017-09-13 13:03:08 -0700215aaudio_result_t AAudioServiceStreamShared::getAudioDataDescription(
216 AudioEndpointParcelable &parcelable)
Phil Burkc0c70e32017-02-09 13:18:38 -0800217{
Phil Burk523b3042017-09-13 13:03:08 -0700218 std::lock_guard<std::mutex> lock(mAudioDataQueueLock);
219 if (mAudioDataQueue == nullptr) {
220 ALOGE("getAudioDataDescription(): mUpMessageQueue null! - stream not open");
221 return AAUDIO_ERROR_NULL;
222 }
Phil Burkc0c70e32017-02-09 13:18:38 -0800223 // Gather information on the data queue.
224 mAudioDataQueue->fillParcelable(parcelable,
225 parcelable.mDownDataQueueParcelable);
226 parcelable.mDownDataQueueParcelable.setFramesPerBurst(getFramesPerBurst());
227 return AAUDIO_OK;
228}
229
Phil Burk97350f92017-07-21 15:59:44 -0700230void AAudioServiceStreamShared::markTransferTime(Timestamp &timestamp) {
231 mAtomicTimestamp.write(timestamp);
Phil Burk71f35bb2017-04-13 16:05:07 -0700232}
Phil Burkc0c70e32017-02-09 13:18:38 -0800233
Phil Burk39f02dd2017-08-04 09:13:31 -0700234// Get timestamp that was written by mixer or distributor.
Phil Burkc0c70e32017-02-09 13:18:38 -0800235aaudio_result_t AAudioServiceStreamShared::getFreeRunningPosition(int64_t *positionFrames,
Phil Burk39f02dd2017-08-04 09:13:31 -0700236 int64_t *timeNanos) {
237 // TODO Get presentation timestamp from the HAL
Phil Burk97350f92017-07-21 15:59:44 -0700238 if (mAtomicTimestamp.isValid()) {
239 Timestamp timestamp = mAtomicTimestamp.read();
240 *positionFrames = timestamp.getPosition();
241 *timeNanos = timestamp.getNanoseconds();
242 return AAUDIO_OK;
243 } else {
244 return AAUDIO_ERROR_UNAVAILABLE;
245 }
246}
247
248// Get timestamp from lower level service.
249aaudio_result_t AAudioServiceStreamShared::getHardwareTimestamp(int64_t *positionFrames,
Phil Burk39f02dd2017-08-04 09:13:31 -0700250 int64_t *timeNanos) {
Phil Burk97350f92017-07-21 15:59:44 -0700251
Phil Burkbcc36742017-08-31 17:24:51 -0700252 int64_t position = 0;
253 aaudio_result_t result = mServiceEndpoint->getTimestamp(&position, timeNanos);
Phil Burk97350f92017-07-21 15:59:44 -0700254 if (result == AAUDIO_OK) {
Phil Burkbcc36742017-08-31 17:24:51 -0700255 int64_t offset = mTimestampPositionOffset.load();
256 // TODO, do not go below starting value
257 position -= offset; // Offset from shared MMAP stream
258 ALOGV("getHardwareTimestamp() %8lld = %8lld - %8lld",
259 (long long) position, (long long) (position + offset), (long long) offset);
Phil Burk97350f92017-07-21 15:59:44 -0700260 }
Phil Burkbcc36742017-08-31 17:24:51 -0700261 *positionFrames = position;
Phil Burk97350f92017-07-21 15:59:44 -0700262 return result;
Phil Burkc0c70e32017-02-09 13:18:38 -0800263}