blob: 85b2057b1ce18de0beebf2077d4e9ef612976223 [file] [log] [blame]
Phil Burk39f02dd2017-08-04 09:13:31 -07001/*
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 "AAudioServiceEndpointMMAP"
18//#define LOG_NDEBUG 0
19#include <utils/Log.h>
20
21#include <algorithm>
22#include <assert.h>
23#include <map>
24#include <mutex>
25#include <sstream>
Phil Burka77869d2020-05-07 10:39:47 -070026#include <thread>
Phil Burk39f02dd2017-08-04 09:13:31 -070027#include <utils/Singleton.h>
28#include <vector>
29
Phil Burk39f02dd2017-08-04 09:13:31 -070030#include "AAudioEndpointManager.h"
31#include "AAudioServiceEndpoint.h"
32
33#include "core/AudioStreamBuilder.h"
34#include "AAudioServiceEndpoint.h"
35#include "AAudioServiceStreamShared.h"
36#include "AAudioServiceEndpointPlay.h"
37#include "AAudioServiceEndpointMMAP.h"
38
Phil Burk39f02dd2017-08-04 09:13:31 -070039#define AAUDIO_BUFFER_CAPACITY_MIN 4 * 512
40#define AAUDIO_SAMPLE_RATE_DEFAULT 48000
41
42// This is an estimate of the time difference between the HW and the MMAP time.
43// TODO Get presentation timestamps from the HAL instead of using these estimates.
44#define OUTPUT_ESTIMATED_HARDWARE_OFFSET_NANOS (3 * AAUDIO_NANOS_PER_MILLISECOND)
45#define INPUT_ESTIMATED_HARDWARE_OFFSET_NANOS (-1 * AAUDIO_NANOS_PER_MILLISECOND)
46
47using namespace android; // TODO just import names needed
48using namespace aaudio; // TODO just import names needed
49
Phil Burkbbd52862018-04-13 11:37:42 -070050AAudioServiceEndpointMMAP::AAudioServiceEndpointMMAP(AAudioService &audioService)
51 : mMmapStream(nullptr)
52 , mAAudioService(audioService) {}
Phil Burk39f02dd2017-08-04 09:13:31 -070053
54AAudioServiceEndpointMMAP::~AAudioServiceEndpointMMAP() {}
55
56std::string AAudioServiceEndpointMMAP::dump() const {
57 std::stringstream result;
58
59 result << " MMAP: framesTransferred = " << mFramesTransferred.get();
60 result << ", HW nanos = " << mHardwareTimeOffsetNanos;
61 result << ", port handle = " << mPortHandle;
62 result << ", audio data FD = " << mAudioDataFileDescriptor;
63 result << "\n";
64
65 result << " HW Offset Micros: " <<
66 (getHardwareTimeOffsetNanos()
67 / AAUDIO_NANOS_PER_MICROSECOND) << "\n";
68
69 result << AAudioServiceEndpoint::dump();
70 return result.str();
71}
72
73aaudio_result_t AAudioServiceEndpointMMAP::open(const aaudio::AAudioStreamRequest &request) {
74 aaudio_result_t result = AAUDIO_OK;
Phil Burk39f02dd2017-08-04 09:13:31 -070075 copyFrom(request.getConstantConfiguration());
Phil Burk39f02dd2017-08-04 09:13:31 -070076 mMmapClient.clientUid = request.getUserId();
77 mMmapClient.clientPid = request.getProcessId();
78 mMmapClient.packageName.setTo(String16(""));
79
Phil Burk04e805b2018-03-27 09:13:53 -070080 audio_format_t audioFormat = getFormat();
81
82 // FLOAT is not directly supported by the HAL so ask for a 24-bit.
83 bool isHighResRequested = audioFormat == AUDIO_FORMAT_PCM_FLOAT
84 || audioFormat == AUDIO_FORMAT_PCM_32_BIT;
85 if (isHighResRequested) {
86 // TODO remove these logs when finished debugging.
87 ALOGD("%s() change format from %d to 24_BIT_PACKED", __func__, audioFormat);
88 audioFormat = AUDIO_FORMAT_PCM_24_BIT_PACKED;
89 }
90
91 result = openWithFormat(audioFormat);
92 if (result == AAUDIO_OK) return result;
93
94 // TODO The HAL and AudioFlinger should be recommending a format if the open fails.
95 // But that recommendation is not propagating back from the HAL.
96 // So for now just try something very likely to work.
97 if (result == AAUDIO_ERROR_UNAVAILABLE && audioFormat == AUDIO_FORMAT_PCM_24_BIT_PACKED) {
98 ALOGD("%s() 24_BIT failed, perhaps due to format. Try again with 16_BIT", __func__);
99 audioFormat = AUDIO_FORMAT_PCM_16_BIT;
100 result = openWithFormat(audioFormat);
101 }
102 return result;
103}
104
105aaudio_result_t AAudioServiceEndpointMMAP::openWithFormat(audio_format_t audioFormat) {
106 aaudio_result_t result = AAUDIO_OK;
107 audio_config_base_t config;
108 audio_port_handle_t deviceId;
109
110 const audio_attributes_t attributes = getAudioAttributesFrom(this);
111
Phil Burk39f02dd2017-08-04 09:13:31 -0700112 mRequestedDeviceId = deviceId = getDeviceId();
113
114 // Fill in config
Phil Burk0127c1b2018-03-29 13:48:06 -0700115 config.format = audioFormat;
Phil Burk39f02dd2017-08-04 09:13:31 -0700116
117 int32_t aaudioSampleRate = getSampleRate();
118 if (aaudioSampleRate == AAUDIO_UNSPECIFIED) {
119 aaudioSampleRate = AAUDIO_SAMPLE_RATE_DEFAULT;
120 }
121 config.sample_rate = aaudioSampleRate;
122
123 int32_t aaudioSamplesPerFrame = getSamplesPerFrame();
124
jiabind1f1cb62020-03-24 11:57:57 -0700125 const aaudio_direction_t direction = getDirection();
126
Phil Burk39f02dd2017-08-04 09:13:31 -0700127 if (direction == AAUDIO_DIRECTION_OUTPUT) {
128 config.channel_mask = (aaudioSamplesPerFrame == AAUDIO_UNSPECIFIED)
129 ? AUDIO_CHANNEL_OUT_STEREO
130 : audio_channel_out_mask_from_count(aaudioSamplesPerFrame);
131 mHardwareTimeOffsetNanos = OUTPUT_ESTIMATED_HARDWARE_OFFSET_NANOS; // frames at DAC later
132
133 } else if (direction == AAUDIO_DIRECTION_INPUT) {
134 config.channel_mask = (aaudioSamplesPerFrame == AAUDIO_UNSPECIFIED)
135 ? AUDIO_CHANNEL_IN_STEREO
136 : audio_channel_in_mask_from_count(aaudioSamplesPerFrame);
137 mHardwareTimeOffsetNanos = INPUT_ESTIMATED_HARDWARE_OFFSET_NANOS; // frames at ADC earlier
138
139 } else {
Phil Burk19e990e2018-03-22 13:59:34 -0700140 ALOGE("%s() invalid direction = %d", __func__, direction);
Phil Burk39f02dd2017-08-04 09:13:31 -0700141 return AAUDIO_ERROR_ILLEGAL_ARGUMENT;
142 }
143
144 MmapStreamInterface::stream_direction_t streamDirection =
145 (direction == AAUDIO_DIRECTION_OUTPUT)
146 ? MmapStreamInterface::DIRECTION_OUTPUT
147 : MmapStreamInterface::DIRECTION_INPUT;
148
Phil Burk4e1af9f2018-01-03 15:54:35 -0800149 aaudio_session_id_t requestedSessionId = getSessionId();
150 audio_session_t sessionId = AAudioConvert_aaudioToAndroidSessionId(requestedSessionId);
151
Phil Burk39f02dd2017-08-04 09:13:31 -0700152 // Open HAL stream. Set mMmapStream
153 status_t status = MmapStreamInterface::openMmapStream(streamDirection,
154 &attributes,
155 &config,
156 mMmapClient,
157 &deviceId,
Phil Burk4e1af9f2018-01-03 15:54:35 -0800158 &sessionId,
Phil Burk39f02dd2017-08-04 09:13:31 -0700159 this, // callback
160 mMmapStream,
161 &mPortHandle);
Phil Burk19e990e2018-03-22 13:59:34 -0700162 ALOGD("%s() mMapClient.uid = %d, pid = %d => portHandle = %d\n",
163 __func__, mMmapClient.clientUid, mMmapClient.clientPid, mPortHandle);
Phil Burk39f02dd2017-08-04 09:13:31 -0700164 if (status != OK) {
Phil Burk29ccc292019-04-15 08:58:08 -0700165 // This can happen if the resource is busy or the config does
166 // not match the hardware.
167 ALOGD("%s() - openMmapStream() returned status %d", __func__, status);
Phil Burk39f02dd2017-08-04 09:13:31 -0700168 return AAUDIO_ERROR_UNAVAILABLE;
169 }
170
171 if (deviceId == AAUDIO_UNSPECIFIED) {
Phil Burka3901e92018-10-08 13:54:38 -0700172 ALOGW("%s() - openMmapStream() failed to set deviceId", __func__);
Phil Burk39f02dd2017-08-04 09:13:31 -0700173 }
174 setDeviceId(deviceId);
175
Phil Burk4e1af9f2018-01-03 15:54:35 -0800176 if (sessionId == AUDIO_SESSION_ALLOCATE) {
Phil Burk19e990e2018-03-22 13:59:34 -0700177 ALOGW("%s() - openMmapStream() failed to set sessionId", __func__);
Phil Burk4e1af9f2018-01-03 15:54:35 -0800178 }
179
180 aaudio_session_id_t actualSessionId =
181 (requestedSessionId == AAUDIO_SESSION_ID_NONE)
182 ? AAUDIO_SESSION_ID_NONE
183 : (aaudio_session_id_t) sessionId;
184 setSessionId(actualSessionId);
Phil Burk19e990e2018-03-22 13:59:34 -0700185 ALOGD("%s() deviceId = %d, sessionId = %d", __func__, getDeviceId(), getSessionId());
Phil Burk4e1af9f2018-01-03 15:54:35 -0800186
Phil Burk39f02dd2017-08-04 09:13:31 -0700187 // Create MMAP/NOIRQ buffer.
188 int32_t minSizeFrames = getBufferCapacity();
189 if (minSizeFrames <= 0) { // zero will get rejected
190 minSizeFrames = AAUDIO_BUFFER_CAPACITY_MIN;
191 }
192 status = mMmapStream->createMmapBuffer(minSizeFrames, &mMmapBufferinfo);
Kevin Rocard734334f2018-07-12 19:37:41 -0700193 bool isBufferShareable = mMmapBufferinfo.flags & AUDIO_MMAP_APPLICATION_SHAREABLE;
Phil Burk39f02dd2017-08-04 09:13:31 -0700194 if (status != OK) {
Phil Burk19e990e2018-03-22 13:59:34 -0700195 ALOGE("%s() - createMmapBuffer() failed with status %d %s",
196 __func__, status, strerror(-status));
Phil Burk39f02dd2017-08-04 09:13:31 -0700197 result = AAUDIO_ERROR_UNAVAILABLE;
198 goto error;
199 } else {
Phil Burk29ccc292019-04-15 08:58:08 -0700200 ALOGD("%s() createMmapBuffer() buffer_size = %d fr, burst_size %d fr"
Phil Burk39f02dd2017-08-04 09:13:31 -0700201 ", Sharable FD: %s",
Phil Burk29ccc292019-04-15 08:58:08 -0700202 __func__,
Kevin Rocard734334f2018-07-12 19:37:41 -0700203 mMmapBufferinfo.buffer_size_frames,
Phil Burk39f02dd2017-08-04 09:13:31 -0700204 mMmapBufferinfo.burst_size_frames,
Kevin Rocard734334f2018-07-12 19:37:41 -0700205 isBufferShareable ? "Yes" : "No");
Phil Burk39f02dd2017-08-04 09:13:31 -0700206 }
207
208 setBufferCapacity(mMmapBufferinfo.buffer_size_frames);
Kevin Rocard734334f2018-07-12 19:37:41 -0700209 if (!isBufferShareable) {
Phil Burk39f02dd2017-08-04 09:13:31 -0700210 // Exclusive mode can only be used by the service because the FD cannot be shared.
211 uid_t audioServiceUid = getuid();
212 if ((mMmapClient.clientUid != audioServiceUid) &&
213 getSharingMode() == AAUDIO_SHARING_MODE_EXCLUSIVE) {
Phil Burk19e990e2018-03-22 13:59:34 -0700214 ALOGW("%s() - exclusive FD cannot be used by client", __func__);
Phil Burk39f02dd2017-08-04 09:13:31 -0700215 result = AAUDIO_ERROR_UNAVAILABLE;
216 goto error;
217 }
218 }
219
220 // Get information about the stream and pass it back to the caller.
221 setSamplesPerFrame((direction == AAUDIO_DIRECTION_OUTPUT)
222 ? audio_channel_count_from_out_mask(config.channel_mask)
223 : audio_channel_count_from_in_mask(config.channel_mask));
224
225 // AAudio creates a copy of this FD and retains ownership of the copy.
226 // Assume that AudioFlinger will close the original shared_memory_fd.
227 mAudioDataFileDescriptor.reset(dup(mMmapBufferinfo.shared_memory_fd));
228 if (mAudioDataFileDescriptor.get() == -1) {
Phil Burk19e990e2018-03-22 13:59:34 -0700229 ALOGE("%s() - could not dup shared_memory_fd", __func__);
Phil Burk39f02dd2017-08-04 09:13:31 -0700230 result = AAUDIO_ERROR_INTERNAL;
231 goto error;
232 }
233 mFramesPerBurst = mMmapBufferinfo.burst_size_frames;
Phil Burk0127c1b2018-03-29 13:48:06 -0700234 setFormat(config.format);
Phil Burk39f02dd2017-08-04 09:13:31 -0700235 setSampleRate(config.sample_rate);
236
Phil Burk3c4e6b52019-01-22 15:53:36 -0800237 ALOGD("%s() actual rate = %d, channels = %d"
238 ", deviceId = %d, capacity = %d\n",
239 __func__, getSampleRate(), getSamplesPerFrame(), deviceId, getBufferCapacity());
Phil Burk39f02dd2017-08-04 09:13:31 -0700240
Phil Burk3c4e6b52019-01-22 15:53:36 -0800241 ALOGD("%s() format = 0x%08x, frame size = %d, burst size = %d",
242 __func__, getFormat(), calculateBytesPerFrame(), mFramesPerBurst);
Phil Burk0127c1b2018-03-29 13:48:06 -0700243
Phil Burk39f02dd2017-08-04 09:13:31 -0700244 return result;
245
246error:
247 close();
248 return result;
249}
250
Phil Burk320910f2020-08-12 14:29:10 +0000251void AAudioServiceEndpointMMAP::close() {
Phil Burk6e463ce2020-04-13 10:20:20 -0700252 if (mMmapStream != nullptr) {
Phil Burk39f02dd2017-08-04 09:13:31 -0700253 // Needs to be explicitly cleared or CTS will fail but it is not clear why.
254 mMmapStream.clear();
255 // Apparently the above close is asynchronous. An attempt to open a new device
256 // right after a close can fail. Also some callbacks may still be in flight!
257 // FIXME Make closing synchronous.
258 AudioClock::sleepForNanos(100 * AAUDIO_NANOS_PER_MILLISECOND);
259 }
Phil Burk39f02dd2017-08-04 09:13:31 -0700260}
261
262aaudio_result_t AAudioServiceEndpointMMAP::startStream(sp<AAudioServiceStreamBase> stream,
Phil Burkbbd52862018-04-13 11:37:42 -0700263 audio_port_handle_t *clientHandle __unused) {
Phil Burkbcc36742017-08-31 17:24:51 -0700264 // Start the client on behalf of the AAudio service.
265 // Use the port handle that was provided by openMmapStream().
Phil Burkbbd52862018-04-13 11:37:42 -0700266 audio_port_handle_t tempHandle = mPortHandle;
jiabind1f1cb62020-03-24 11:57:57 -0700267 audio_attributes_t attr = {};
268 if (stream != nullptr) {
269 attr = getAudioAttributesFrom(stream.get());
270 }
271 aaudio_result_t result = startClient(
272 mMmapClient, stream == nullptr ? nullptr : &attr, &tempHandle);
Phil Burkbbd52862018-04-13 11:37:42 -0700273 // When AudioFlinger is passed a valid port handle then it should not change it.
274 LOG_ALWAYS_FATAL_IF(tempHandle != mPortHandle,
275 "%s() port handle not expected to change from %d to %d",
276 __func__, mPortHandle, tempHandle);
Phil Burk29ccc292019-04-15 08:58:08 -0700277 ALOGV("%s() mPortHandle = %d", __func__, mPortHandle);
Phil Burkbbd52862018-04-13 11:37:42 -0700278 return result;
Phil Burk39f02dd2017-08-04 09:13:31 -0700279}
280
281aaudio_result_t AAudioServiceEndpointMMAP::stopStream(sp<AAudioServiceStreamBase> stream,
Phil Burkbbd52862018-04-13 11:37:42 -0700282 audio_port_handle_t clientHandle __unused) {
Phil Burk39f02dd2017-08-04 09:13:31 -0700283 mFramesTransferred.reset32();
Phil Burk73af62a2017-10-26 12:11:47 -0700284
285 // Round 64-bit counter up to a multiple of the buffer capacity.
286 // This is required because the 64-bit counter is used as an index
287 // into a circular buffer and the actual HW position is reset to zero
288 // when the stream is stopped.
289 mFramesTransferred.roundUp64(getBufferCapacity());
290
Phil Burkbbd52862018-04-13 11:37:42 -0700291 // Use the port handle that was provided by openMmapStream().
Phil Burk29ccc292019-04-15 08:58:08 -0700292 ALOGV("%s() mPortHandle = %d", __func__, mPortHandle);
Phil Burk39f02dd2017-08-04 09:13:31 -0700293 return stopClient(mPortHandle);
294}
295
296aaudio_result_t AAudioServiceEndpointMMAP::startClient(const android::AudioClient& client,
jiabind1f1cb62020-03-24 11:57:57 -0700297 const audio_attributes_t *attr,
Phil Burk39f02dd2017-08-04 09:13:31 -0700298 audio_port_handle_t *clientHandle) {
299 if (mMmapStream == nullptr) return AAUDIO_ERROR_NULL;
jiabind1f1cb62020-03-24 11:57:57 -0700300 status_t status = mMmapStream->start(client, attr, clientHandle);
Phil Burk29ccc292019-04-15 08:58:08 -0700301 return AAudioConvert_androidToAAudioResult(status);
Phil Burk39f02dd2017-08-04 09:13:31 -0700302}
303
304aaudio_result_t AAudioServiceEndpointMMAP::stopClient(audio_port_handle_t clientHandle) {
305 if (mMmapStream == nullptr) return AAUDIO_ERROR_NULL;
306 aaudio_result_t result = AAudioConvert_androidToAAudioResult(mMmapStream->stop(clientHandle));
Phil Burk39f02dd2017-08-04 09:13:31 -0700307 return result;
308}
309
310// Get free-running DSP or DMA hardware position from the HAL.
311aaudio_result_t AAudioServiceEndpointMMAP::getFreeRunningPosition(int64_t *positionFrames,
312 int64_t *timeNanos) {
313 struct audio_mmap_position position;
314 if (mMmapStream == nullptr) {
315 return AAUDIO_ERROR_NULL;
316 }
317 status_t status = mMmapStream->getMmapPosition(&position);
Phil Burk19e990e2018-03-22 13:59:34 -0700318 ALOGV("%s() status= %d, pos = %d, nanos = %lld\n",
319 __func__, status, position.position_frames, (long long) position.time_nanoseconds);
Phil Burk39f02dd2017-08-04 09:13:31 -0700320 aaudio_result_t result = AAudioConvert_androidToAAudioResult(status);
321 if (result == AAUDIO_ERROR_UNAVAILABLE) {
Phil Burk19e990e2018-03-22 13:59:34 -0700322 ALOGW("%s(): getMmapPosition() has no position data available", __func__);
Phil Burk39f02dd2017-08-04 09:13:31 -0700323 } else if (result != AAUDIO_OK) {
Phil Burk19e990e2018-03-22 13:59:34 -0700324 ALOGE("%s(): getMmapPosition() returned status %d", __func__, status);
Phil Burk39f02dd2017-08-04 09:13:31 -0700325 } else {
326 // Convert 32-bit position to 64-bit position.
327 mFramesTransferred.update32(position.position_frames);
328 *positionFrames = mFramesTransferred.get();
329 *timeNanos = position.time_nanoseconds;
330 }
331 return result;
332}
333
334aaudio_result_t AAudioServiceEndpointMMAP::getTimestamp(int64_t *positionFrames,
335 int64_t *timeNanos) {
336 return 0; // TODO
337}
338
Phil Burka77869d2020-05-07 10:39:47 -0700339// This is called by onTearDown() in a separate thread to avoid deadlocks.
340void AAudioServiceEndpointMMAP::handleTearDownAsync(audio_port_handle_t portHandle) {
Phil Burkbbd52862018-04-13 11:37:42 -0700341 // Are we tearing down the EXCLUSIVE MMAP stream?
342 if (isStreamRegistered(portHandle)) {
343 ALOGD("%s(%d) tearing down this entire MMAP endpoint", __func__, portHandle);
344 disconnectRegisteredStreams();
345 } else {
346 // Must be a SHARED stream?
347 ALOGD("%s(%d) disconnect a specific stream", __func__, portHandle);
348 aaudio_result_t result = mAAudioService.disconnectStreamByPortHandle(portHandle);
349 ALOGD("%s(%d) disconnectStreamByPortHandle returned %d", __func__, portHandle, result);
350 }
Phil Burk39f02dd2017-08-04 09:13:31 -0700351};
352
Phil Burka77869d2020-05-07 10:39:47 -0700353// This is called by AudioFlinger when it wants to destroy a stream.
354void AAudioServiceEndpointMMAP::onTearDown(audio_port_handle_t portHandle) {
355 ALOGD("%s(portHandle = %d) called", __func__, portHandle);
356 std::thread asyncTask(&AAudioServiceEndpointMMAP::handleTearDownAsync, this, portHandle);
357 asyncTask.detach();
358}
359
Phil Burk39f02dd2017-08-04 09:13:31 -0700360void AAudioServiceEndpointMMAP::onVolumeChanged(audio_channel_mask_t channels,
361 android::Vector<float> values) {
Phil Burk19e990e2018-03-22 13:59:34 -0700362 // TODO Do we really need a different volume for each channel?
363 // We get called with an array filled with a single value!
Phil Burk39f02dd2017-08-04 09:13:31 -0700364 float volume = values[0];
Phil Burk29ccc292019-04-15 08:58:08 -0700365 ALOGD("%s() volume[0] = %f", __func__, volume);
Phil Burk39f02dd2017-08-04 09:13:31 -0700366 std::lock_guard<std::mutex> lock(mLockStreams);
Chih-Hung Hsieh3ef324d2018-12-11 11:48:12 -0800367 for(const auto& stream : mRegisteredStreams) {
Phil Burk39f02dd2017-08-04 09:13:31 -0700368 stream->onVolumeChanged(volume);
369 }
370};
371
Phil Burka77869d2020-05-07 10:39:47 -0700372void AAudioServiceEndpointMMAP::onRoutingChanged(audio_port_handle_t portHandle) {
373 const int32_t deviceId = static_cast<int32_t>(portHandle);
Phil Burk29ccc292019-04-15 08:58:08 -0700374 ALOGD("%s() called with dev %d, old = %d", __func__, deviceId, getDeviceId());
Phil Burka77869d2020-05-07 10:39:47 -0700375 if (getDeviceId() != deviceId) {
376 if (getDeviceId() != AUDIO_PORT_HANDLE_NONE) {
377 std::thread asyncTask([this, deviceId]() {
378 disconnectRegisteredStreams();
379 setDeviceId(deviceId);
380 });
381 asyncTask.detach();
382 } else {
383 setDeviceId(deviceId);
384 }
Phil Burk39f02dd2017-08-04 09:13:31 -0700385 }
Phil Burk39f02dd2017-08-04 09:13:31 -0700386};
387
388/**
389 * Get an immutable description of the data queue from the HAL.
390 */
391aaudio_result_t AAudioServiceEndpointMMAP::getDownDataDescription(AudioEndpointParcelable &parcelable)
392{
393 // Gather information on the data queue based on HAL info.
394 int32_t bytesPerFrame = calculateBytesPerFrame();
395 int32_t capacityInBytes = getBufferCapacity() * bytesPerFrame;
396 int fdIndex = parcelable.addFileDescriptor(mAudioDataFileDescriptor, capacityInBytes);
397 parcelable.mDownDataQueueParcelable.setupMemory(fdIndex, 0, capacityInBytes);
398 parcelable.mDownDataQueueParcelable.setBytesPerFrame(bytesPerFrame);
399 parcelable.mDownDataQueueParcelable.setFramesPerBurst(mFramesPerBurst);
400 parcelable.mDownDataQueueParcelable.setCapacityInFrames(getBufferCapacity());
401 return AAUDIO_OK;
402}
jiabinb7d8c5a2020-08-26 17:24:52 -0700403
404aaudio_result_t AAudioServiceEndpointMMAP::getExternalPosition(uint64_t *positionFrames,
405 int64_t *timeNanos)
406{
407 if (!mExternalPositionSupported) {
408 return AAUDIO_ERROR_INVALID_STATE;
409 }
410 status_t status = mMmapStream->getExternalPosition(positionFrames, timeNanos);
411 if (status == INVALID_OPERATION) {
412 // getExternalPosition is not supported. Set mExternalPositionSupported as false
413 // so that the call will not go to the HAL next time.
414 mExternalPositionSupported = false;
415 }
416 return AAudioConvert_androidToAAudioResult(status);
417}