blob: 7294a5800982436f4a99d603cf12a991518e990d [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
Phil Burk39f02dd2017-08-04 09:13:31 -070054std::string AAudioServiceEndpointMMAP::dump() const {
55 std::stringstream result;
56
57 result << " MMAP: framesTransferred = " << mFramesTransferred.get();
58 result << ", HW nanos = " << mHardwareTimeOffsetNanos;
59 result << ", port handle = " << mPortHandle;
60 result << ", audio data FD = " << mAudioDataFileDescriptor;
61 result << "\n";
62
63 result << " HW Offset Micros: " <<
64 (getHardwareTimeOffsetNanos()
65 / AAUDIO_NANOS_PER_MICROSECOND) << "\n";
66
67 result << AAudioServiceEndpoint::dump();
68 return result.str();
69}
70
71aaudio_result_t AAudioServiceEndpointMMAP::open(const aaudio::AAudioStreamRequest &request) {
72 aaudio_result_t result = AAUDIO_OK;
Phil Burk39f02dd2017-08-04 09:13:31 -070073 copyFrom(request.getConstantConfiguration());
Philip P. Moltmannbda45752020-07-17 16:41:18 -070074 mMmapClient.identity = request.getIdentity();
75 // TODO b/182392769: use identity util
76 mMmapClient.identity.uid = VALUE_OR_FATAL(
77 legacy2aidl_uid_t_int32_t(IPCThreadState::self()->getCallingUid()));
78 mMmapClient.identity.pid = VALUE_OR_FATAL(
79 legacy2aidl_pid_t_int32_t(IPCThreadState::self()->getCallingPid()));
Phil Burk39f02dd2017-08-04 09:13:31 -070080
Phil Burk04e805b2018-03-27 09:13:53 -070081 audio_format_t audioFormat = getFormat();
82
83 // FLOAT is not directly supported by the HAL so ask for a 24-bit.
84 bool isHighResRequested = audioFormat == AUDIO_FORMAT_PCM_FLOAT
85 || audioFormat == AUDIO_FORMAT_PCM_32_BIT;
86 if (isHighResRequested) {
87 // TODO remove these logs when finished debugging.
88 ALOGD("%s() change format from %d to 24_BIT_PACKED", __func__, audioFormat);
89 audioFormat = AUDIO_FORMAT_PCM_24_BIT_PACKED;
90 }
91
92 result = openWithFormat(audioFormat);
93 if (result == AAUDIO_OK) return result;
94
95 // TODO The HAL and AudioFlinger should be recommending a format if the open fails.
96 // But that recommendation is not propagating back from the HAL.
97 // So for now just try something very likely to work.
98 if (result == AAUDIO_ERROR_UNAVAILABLE && audioFormat == AUDIO_FORMAT_PCM_24_BIT_PACKED) {
99 ALOGD("%s() 24_BIT failed, perhaps due to format. Try again with 16_BIT", __func__);
100 audioFormat = AUDIO_FORMAT_PCM_16_BIT;
101 result = openWithFormat(audioFormat);
102 }
103 return result;
104}
105
106aaudio_result_t AAudioServiceEndpointMMAP::openWithFormat(audio_format_t audioFormat) {
107 aaudio_result_t result = AAUDIO_OK;
108 audio_config_base_t config;
109 audio_port_handle_t deviceId;
110
111 const audio_attributes_t attributes = getAudioAttributesFrom(this);
112
Phil Burk39f02dd2017-08-04 09:13:31 -0700113 mRequestedDeviceId = deviceId = getDeviceId();
114
115 // Fill in config
Phil Burk0127c1b2018-03-29 13:48:06 -0700116 config.format = audioFormat;
Phil Burk39f02dd2017-08-04 09:13:31 -0700117
118 int32_t aaudioSampleRate = getSampleRate();
119 if (aaudioSampleRate == AAUDIO_UNSPECIFIED) {
120 aaudioSampleRate = AAUDIO_SAMPLE_RATE_DEFAULT;
121 }
122 config.sample_rate = aaudioSampleRate;
123
124 int32_t aaudioSamplesPerFrame = getSamplesPerFrame();
125
jiabind1f1cb62020-03-24 11:57:57 -0700126 const aaudio_direction_t direction = getDirection();
127
Phil Burk39f02dd2017-08-04 09:13:31 -0700128 if (direction == AAUDIO_DIRECTION_OUTPUT) {
129 config.channel_mask = (aaudioSamplesPerFrame == AAUDIO_UNSPECIFIED)
130 ? AUDIO_CHANNEL_OUT_STEREO
131 : audio_channel_out_mask_from_count(aaudioSamplesPerFrame);
132 mHardwareTimeOffsetNanos = OUTPUT_ESTIMATED_HARDWARE_OFFSET_NANOS; // frames at DAC later
133
134 } else if (direction == AAUDIO_DIRECTION_INPUT) {
135 config.channel_mask = (aaudioSamplesPerFrame == AAUDIO_UNSPECIFIED)
136 ? AUDIO_CHANNEL_IN_STEREO
137 : audio_channel_in_mask_from_count(aaudioSamplesPerFrame);
138 mHardwareTimeOffsetNanos = INPUT_ESTIMATED_HARDWARE_OFFSET_NANOS; // frames at ADC earlier
139
140 } else {
Phil Burk19e990e2018-03-22 13:59:34 -0700141 ALOGE("%s() invalid direction = %d", __func__, direction);
Phil Burk39f02dd2017-08-04 09:13:31 -0700142 return AAUDIO_ERROR_ILLEGAL_ARGUMENT;
143 }
144
145 MmapStreamInterface::stream_direction_t streamDirection =
146 (direction == AAUDIO_DIRECTION_OUTPUT)
147 ? MmapStreamInterface::DIRECTION_OUTPUT
148 : MmapStreamInterface::DIRECTION_INPUT;
149
Phil Burk4e1af9f2018-01-03 15:54:35 -0800150 aaudio_session_id_t requestedSessionId = getSessionId();
151 audio_session_t sessionId = AAudioConvert_aaudioToAndroidSessionId(requestedSessionId);
152
Phil Burk39f02dd2017-08-04 09:13:31 -0700153 // Open HAL stream. Set mMmapStream
154 status_t status = MmapStreamInterface::openMmapStream(streamDirection,
155 &attributes,
156 &config,
157 mMmapClient,
158 &deviceId,
Phil Burk4e1af9f2018-01-03 15:54:35 -0800159 &sessionId,
Phil Burk39f02dd2017-08-04 09:13:31 -0700160 this, // callback
161 mMmapStream,
162 &mPortHandle);
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700163 ALOGD("%s() mMapClient.identity = %s => portHandle = %d\n",
164 __func__, mMmapClient.identity.toString().c_str(), mPortHandle);
Phil Burk39f02dd2017-08-04 09:13:31 -0700165 if (status != OK) {
Phil Burk29ccc292019-04-15 08:58:08 -0700166 // This can happen if the resource is busy or the config does
167 // not match the hardware.
168 ALOGD("%s() - openMmapStream() returned status %d", __func__, status);
Phil Burk39f02dd2017-08-04 09:13:31 -0700169 return AAUDIO_ERROR_UNAVAILABLE;
170 }
171
172 if (deviceId == AAUDIO_UNSPECIFIED) {
Phil Burka3901e92018-10-08 13:54:38 -0700173 ALOGW("%s() - openMmapStream() failed to set deviceId", __func__);
Phil Burk39f02dd2017-08-04 09:13:31 -0700174 }
175 setDeviceId(deviceId);
176
Phil Burk4e1af9f2018-01-03 15:54:35 -0800177 if (sessionId == AUDIO_SESSION_ALLOCATE) {
Phil Burk19e990e2018-03-22 13:59:34 -0700178 ALOGW("%s() - openMmapStream() failed to set sessionId", __func__);
Phil Burk4e1af9f2018-01-03 15:54:35 -0800179 }
180
181 aaudio_session_id_t actualSessionId =
182 (requestedSessionId == AAUDIO_SESSION_ID_NONE)
183 ? AAUDIO_SESSION_ID_NONE
184 : (aaudio_session_id_t) sessionId;
185 setSessionId(actualSessionId);
Phil Burk19e990e2018-03-22 13:59:34 -0700186 ALOGD("%s() deviceId = %d, sessionId = %d", __func__, getDeviceId(), getSessionId());
Phil Burk4e1af9f2018-01-03 15:54:35 -0800187
Phil Burk39f02dd2017-08-04 09:13:31 -0700188 // Create MMAP/NOIRQ buffer.
189 int32_t minSizeFrames = getBufferCapacity();
190 if (minSizeFrames <= 0) { // zero will get rejected
191 minSizeFrames = AAUDIO_BUFFER_CAPACITY_MIN;
192 }
193 status = mMmapStream->createMmapBuffer(minSizeFrames, &mMmapBufferinfo);
Kevin Rocard734334f2018-07-12 19:37:41 -0700194 bool isBufferShareable = mMmapBufferinfo.flags & AUDIO_MMAP_APPLICATION_SHAREABLE;
Phil Burk39f02dd2017-08-04 09:13:31 -0700195 if (status != OK) {
Phil Burk19e990e2018-03-22 13:59:34 -0700196 ALOGE("%s() - createMmapBuffer() failed with status %d %s",
197 __func__, status, strerror(-status));
Phil Burk39f02dd2017-08-04 09:13:31 -0700198 result = AAUDIO_ERROR_UNAVAILABLE;
199 goto error;
200 } else {
Phil Burk29ccc292019-04-15 08:58:08 -0700201 ALOGD("%s() createMmapBuffer() buffer_size = %d fr, burst_size %d fr"
Phil Burk39f02dd2017-08-04 09:13:31 -0700202 ", Sharable FD: %s",
Phil Burk29ccc292019-04-15 08:58:08 -0700203 __func__,
Kevin Rocard734334f2018-07-12 19:37:41 -0700204 mMmapBufferinfo.buffer_size_frames,
Phil Burk39f02dd2017-08-04 09:13:31 -0700205 mMmapBufferinfo.burst_size_frames,
Kevin Rocard734334f2018-07-12 19:37:41 -0700206 isBufferShareable ? "Yes" : "No");
Phil Burk39f02dd2017-08-04 09:13:31 -0700207 }
208
209 setBufferCapacity(mMmapBufferinfo.buffer_size_frames);
Kevin Rocard734334f2018-07-12 19:37:41 -0700210 if (!isBufferShareable) {
Phil Burk39f02dd2017-08-04 09:13:31 -0700211 // Exclusive mode can only be used by the service because the FD cannot be shared.
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700212 int32_t audioServiceUid =
213 VALUE_OR_FATAL(legacy2aidl_uid_t_int32_t(getuid()));
214 if ((mMmapClient.identity.uid != audioServiceUid) &&
Phil Burk39f02dd2017-08-04 09:13:31 -0700215 getSharingMode() == AAUDIO_SHARING_MODE_EXCLUSIVE) {
Phil Burk19e990e2018-03-22 13:59:34 -0700216 ALOGW("%s() - exclusive FD cannot be used by client", __func__);
Phil Burk39f02dd2017-08-04 09:13:31 -0700217 result = AAUDIO_ERROR_UNAVAILABLE;
218 goto error;
219 }
220 }
221
222 // Get information about the stream and pass it back to the caller.
223 setSamplesPerFrame((direction == AAUDIO_DIRECTION_OUTPUT)
224 ? audio_channel_count_from_out_mask(config.channel_mask)
225 : audio_channel_count_from_in_mask(config.channel_mask));
226
227 // AAudio creates a copy of this FD and retains ownership of the copy.
228 // Assume that AudioFlinger will close the original shared_memory_fd.
229 mAudioDataFileDescriptor.reset(dup(mMmapBufferinfo.shared_memory_fd));
230 if (mAudioDataFileDescriptor.get() == -1) {
Phil Burk19e990e2018-03-22 13:59:34 -0700231 ALOGE("%s() - could not dup shared_memory_fd", __func__);
Phil Burk39f02dd2017-08-04 09:13:31 -0700232 result = AAUDIO_ERROR_INTERNAL;
233 goto error;
234 }
235 mFramesPerBurst = mMmapBufferinfo.burst_size_frames;
Phil Burk0127c1b2018-03-29 13:48:06 -0700236 setFormat(config.format);
Phil Burk39f02dd2017-08-04 09:13:31 -0700237 setSampleRate(config.sample_rate);
238
Phil Burk3c4e6b52019-01-22 15:53:36 -0800239 ALOGD("%s() actual rate = %d, channels = %d"
240 ", deviceId = %d, capacity = %d\n",
241 __func__, getSampleRate(), getSamplesPerFrame(), deviceId, getBufferCapacity());
Phil Burk39f02dd2017-08-04 09:13:31 -0700242
Phil Burk3c4e6b52019-01-22 15:53:36 -0800243 ALOGD("%s() format = 0x%08x, frame size = %d, burst size = %d",
244 __func__, getFormat(), calculateBytesPerFrame(), mFramesPerBurst);
Phil Burk0127c1b2018-03-29 13:48:06 -0700245
Phil Burk39f02dd2017-08-04 09:13:31 -0700246 return result;
247
248error:
249 close();
250 return result;
251}
252
Phil Burk320910f2020-08-12 14:29:10 +0000253void AAudioServiceEndpointMMAP::close() {
Phil Burk6e463ce2020-04-13 10:20:20 -0700254 if (mMmapStream != nullptr) {
Phil Burk39f02dd2017-08-04 09:13:31 -0700255 // Needs to be explicitly cleared or CTS will fail but it is not clear why.
256 mMmapStream.clear();
257 // Apparently the above close is asynchronous. An attempt to open a new device
258 // right after a close can fail. Also some callbacks may still be in flight!
259 // FIXME Make closing synchronous.
260 AudioClock::sleepForNanos(100 * AAUDIO_NANOS_PER_MILLISECOND);
261 }
Phil Burk39f02dd2017-08-04 09:13:31 -0700262}
263
264aaudio_result_t AAudioServiceEndpointMMAP::startStream(sp<AAudioServiceStreamBase> stream,
Phil Burkbbd52862018-04-13 11:37:42 -0700265 audio_port_handle_t *clientHandle __unused) {
Phil Burkbcc36742017-08-31 17:24:51 -0700266 // Start the client on behalf of the AAudio service.
267 // Use the port handle that was provided by openMmapStream().
Phil Burkbbd52862018-04-13 11:37:42 -0700268 audio_port_handle_t tempHandle = mPortHandle;
jiabind1f1cb62020-03-24 11:57:57 -0700269 audio_attributes_t attr = {};
270 if (stream != nullptr) {
271 attr = getAudioAttributesFrom(stream.get());
272 }
273 aaudio_result_t result = startClient(
274 mMmapClient, stream == nullptr ? nullptr : &attr, &tempHandle);
Phil Burkbbd52862018-04-13 11:37:42 -0700275 // When AudioFlinger is passed a valid port handle then it should not change it.
276 LOG_ALWAYS_FATAL_IF(tempHandle != mPortHandle,
277 "%s() port handle not expected to change from %d to %d",
278 __func__, mPortHandle, tempHandle);
Phil Burk29ccc292019-04-15 08:58:08 -0700279 ALOGV("%s() mPortHandle = %d", __func__, mPortHandle);
Phil Burkbbd52862018-04-13 11:37:42 -0700280 return result;
Phil Burk39f02dd2017-08-04 09:13:31 -0700281}
282
283aaudio_result_t AAudioServiceEndpointMMAP::stopStream(sp<AAudioServiceStreamBase> stream,
Phil Burkbbd52862018-04-13 11:37:42 -0700284 audio_port_handle_t clientHandle __unused) {
Phil Burk39f02dd2017-08-04 09:13:31 -0700285 mFramesTransferred.reset32();
Phil Burk73af62a2017-10-26 12:11:47 -0700286
287 // Round 64-bit counter up to a multiple of the buffer capacity.
288 // This is required because the 64-bit counter is used as an index
289 // into a circular buffer and the actual HW position is reset to zero
290 // when the stream is stopped.
291 mFramesTransferred.roundUp64(getBufferCapacity());
292
Phil Burkbbd52862018-04-13 11:37:42 -0700293 // Use the port handle that was provided by openMmapStream().
Phil Burk29ccc292019-04-15 08:58:08 -0700294 ALOGV("%s() mPortHandle = %d", __func__, mPortHandle);
Phil Burk39f02dd2017-08-04 09:13:31 -0700295 return stopClient(mPortHandle);
296}
297
298aaudio_result_t AAudioServiceEndpointMMAP::startClient(const android::AudioClient& client,
jiabind1f1cb62020-03-24 11:57:57 -0700299 const audio_attributes_t *attr,
Phil Burk39f02dd2017-08-04 09:13:31 -0700300 audio_port_handle_t *clientHandle) {
301 if (mMmapStream == nullptr) return AAUDIO_ERROR_NULL;
jiabind1f1cb62020-03-24 11:57:57 -0700302 status_t status = mMmapStream->start(client, attr, clientHandle);
Phil Burk29ccc292019-04-15 08:58:08 -0700303 return AAudioConvert_androidToAAudioResult(status);
Phil Burk39f02dd2017-08-04 09:13:31 -0700304}
305
306aaudio_result_t AAudioServiceEndpointMMAP::stopClient(audio_port_handle_t clientHandle) {
307 if (mMmapStream == nullptr) return AAUDIO_ERROR_NULL;
308 aaudio_result_t result = AAudioConvert_androidToAAudioResult(mMmapStream->stop(clientHandle));
Phil Burk39f02dd2017-08-04 09:13:31 -0700309 return result;
310}
311
312// Get free-running DSP or DMA hardware position from the HAL.
313aaudio_result_t AAudioServiceEndpointMMAP::getFreeRunningPosition(int64_t *positionFrames,
314 int64_t *timeNanos) {
315 struct audio_mmap_position position;
316 if (mMmapStream == nullptr) {
317 return AAUDIO_ERROR_NULL;
318 }
319 status_t status = mMmapStream->getMmapPosition(&position);
Phil Burk19e990e2018-03-22 13:59:34 -0700320 ALOGV("%s() status= %d, pos = %d, nanos = %lld\n",
321 __func__, status, position.position_frames, (long long) position.time_nanoseconds);
Phil Burk39f02dd2017-08-04 09:13:31 -0700322 aaudio_result_t result = AAudioConvert_androidToAAudioResult(status);
323 if (result == AAUDIO_ERROR_UNAVAILABLE) {
Phil Burk19e990e2018-03-22 13:59:34 -0700324 ALOGW("%s(): getMmapPosition() has no position data available", __func__);
Phil Burk39f02dd2017-08-04 09:13:31 -0700325 } else if (result != AAUDIO_OK) {
Phil Burk19e990e2018-03-22 13:59:34 -0700326 ALOGE("%s(): getMmapPosition() returned status %d", __func__, status);
Phil Burk39f02dd2017-08-04 09:13:31 -0700327 } else {
328 // Convert 32-bit position to 64-bit position.
329 mFramesTransferred.update32(position.position_frames);
330 *positionFrames = mFramesTransferred.get();
331 *timeNanos = position.time_nanoseconds;
332 }
333 return result;
334}
335
336aaudio_result_t AAudioServiceEndpointMMAP::getTimestamp(int64_t *positionFrames,
337 int64_t *timeNanos) {
338 return 0; // TODO
339}
340
Phil Burka77869d2020-05-07 10:39:47 -0700341// This is called by onTearDown() in a separate thread to avoid deadlocks.
342void AAudioServiceEndpointMMAP::handleTearDownAsync(audio_port_handle_t portHandle) {
Phil Burkbbd52862018-04-13 11:37:42 -0700343 // Are we tearing down the EXCLUSIVE MMAP stream?
344 if (isStreamRegistered(portHandle)) {
345 ALOGD("%s(%d) tearing down this entire MMAP endpoint", __func__, portHandle);
346 disconnectRegisteredStreams();
347 } else {
348 // Must be a SHARED stream?
349 ALOGD("%s(%d) disconnect a specific stream", __func__, portHandle);
350 aaudio_result_t result = mAAudioService.disconnectStreamByPortHandle(portHandle);
351 ALOGD("%s(%d) disconnectStreamByPortHandle returned %d", __func__, portHandle, result);
352 }
Phil Burk39f02dd2017-08-04 09:13:31 -0700353};
354
Phil Burka77869d2020-05-07 10:39:47 -0700355// This is called by AudioFlinger when it wants to destroy a stream.
356void AAudioServiceEndpointMMAP::onTearDown(audio_port_handle_t portHandle) {
357 ALOGD("%s(portHandle = %d) called", __func__, portHandle);
Phil Burk3d201942021-04-08 23:27:04 +0000358 android::sp<AAudioServiceEndpointMMAP> holdEndpoint(this);
359 std::thread asyncTask([holdEndpoint, portHandle]() {
360 holdEndpoint->handleTearDownAsync(portHandle);
361 });
Phil Burka77869d2020-05-07 10:39:47 -0700362 asyncTask.detach();
363}
364
Phil Burk39f02dd2017-08-04 09:13:31 -0700365void AAudioServiceEndpointMMAP::onVolumeChanged(audio_channel_mask_t channels,
366 android::Vector<float> values) {
Phil Burk19e990e2018-03-22 13:59:34 -0700367 // TODO Do we really need a different volume for each channel?
368 // We get called with an array filled with a single value!
Phil Burk39f02dd2017-08-04 09:13:31 -0700369 float volume = values[0];
Phil Burk29ccc292019-04-15 08:58:08 -0700370 ALOGD("%s() volume[0] = %f", __func__, volume);
Phil Burk39f02dd2017-08-04 09:13:31 -0700371 std::lock_guard<std::mutex> lock(mLockStreams);
Chih-Hung Hsieh3ef324d2018-12-11 11:48:12 -0800372 for(const auto& stream : mRegisteredStreams) {
Phil Burk39f02dd2017-08-04 09:13:31 -0700373 stream->onVolumeChanged(volume);
374 }
375};
376
Phil Burka77869d2020-05-07 10:39:47 -0700377void AAudioServiceEndpointMMAP::onRoutingChanged(audio_port_handle_t portHandle) {
378 const int32_t deviceId = static_cast<int32_t>(portHandle);
Phil Burk29ccc292019-04-15 08:58:08 -0700379 ALOGD("%s() called with dev %d, old = %d", __func__, deviceId, getDeviceId());
Phil Burka77869d2020-05-07 10:39:47 -0700380 if (getDeviceId() != deviceId) {
381 if (getDeviceId() != AUDIO_PORT_HANDLE_NONE) {
Phil Burk3d201942021-04-08 23:27:04 +0000382 android::sp<AAudioServiceEndpointMMAP> holdEndpoint(this);
383 std::thread asyncTask([holdEndpoint, deviceId]() {
384 ALOGD("onRoutingChanged() asyncTask launched");
385 holdEndpoint->disconnectRegisteredStreams();
386 holdEndpoint->setDeviceId(deviceId);
Phil Burka77869d2020-05-07 10:39:47 -0700387 });
388 asyncTask.detach();
389 } else {
390 setDeviceId(deviceId);
391 }
Phil Burk39f02dd2017-08-04 09:13:31 -0700392 }
Phil Burk39f02dd2017-08-04 09:13:31 -0700393};
394
395/**
396 * Get an immutable description of the data queue from the HAL.
397 */
398aaudio_result_t AAudioServiceEndpointMMAP::getDownDataDescription(AudioEndpointParcelable &parcelable)
399{
400 // Gather information on the data queue based on HAL info.
401 int32_t bytesPerFrame = calculateBytesPerFrame();
402 int32_t capacityInBytes = getBufferCapacity() * bytesPerFrame;
403 int fdIndex = parcelable.addFileDescriptor(mAudioDataFileDescriptor, capacityInBytes);
404 parcelable.mDownDataQueueParcelable.setupMemory(fdIndex, 0, capacityInBytes);
405 parcelable.mDownDataQueueParcelable.setBytesPerFrame(bytesPerFrame);
406 parcelable.mDownDataQueueParcelable.setFramesPerBurst(mFramesPerBurst);
407 parcelable.mDownDataQueueParcelable.setCapacityInFrames(getBufferCapacity());
408 return AAUDIO_OK;
409}
jiabinb7d8c5a2020-08-26 17:24:52 -0700410
411aaudio_result_t AAudioServiceEndpointMMAP::getExternalPosition(uint64_t *positionFrames,
412 int64_t *timeNanos)
413{
414 if (!mExternalPositionSupported) {
415 return AAUDIO_ERROR_INVALID_STATE;
416 }
417 status_t status = mMmapStream->getExternalPosition(positionFrames, timeNanos);
418 if (status == INVALID_OPERATION) {
419 // getExternalPosition is not supported. Set mExternalPositionSupported as false
420 // so that the call will not go to the HAL next time.
421 mExternalPositionSupported = false;
422 }
423 return AAudioConvert_androidToAAudioResult(status);
424}