blob: 556710d9610bd8aab8258a032ba06048ac4393d7 [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());
Philip P. Moltmannbda45752020-07-17 16:41:18 -070076 mMmapClient.identity = request.getIdentity();
77 // TODO b/182392769: use identity util
78 mMmapClient.identity.uid = VALUE_OR_FATAL(
79 legacy2aidl_uid_t_int32_t(IPCThreadState::self()->getCallingUid()));
80 mMmapClient.identity.pid = VALUE_OR_FATAL(
81 legacy2aidl_pid_t_int32_t(IPCThreadState::self()->getCallingPid()));
Phil Burk39f02dd2017-08-04 09:13:31 -070082
Phil Burk04e805b2018-03-27 09:13:53 -070083 audio_format_t audioFormat = getFormat();
84
85 // FLOAT is not directly supported by the HAL so ask for a 24-bit.
86 bool isHighResRequested = audioFormat == AUDIO_FORMAT_PCM_FLOAT
87 || audioFormat == AUDIO_FORMAT_PCM_32_BIT;
88 if (isHighResRequested) {
89 // TODO remove these logs when finished debugging.
90 ALOGD("%s() change format from %d to 24_BIT_PACKED", __func__, audioFormat);
91 audioFormat = AUDIO_FORMAT_PCM_24_BIT_PACKED;
92 }
93
94 result = openWithFormat(audioFormat);
95 if (result == AAUDIO_OK) return result;
96
97 // TODO The HAL and AudioFlinger should be recommending a format if the open fails.
98 // But that recommendation is not propagating back from the HAL.
99 // So for now just try something very likely to work.
100 if (result == AAUDIO_ERROR_UNAVAILABLE && audioFormat == AUDIO_FORMAT_PCM_24_BIT_PACKED) {
101 ALOGD("%s() 24_BIT failed, perhaps due to format. Try again with 16_BIT", __func__);
102 audioFormat = AUDIO_FORMAT_PCM_16_BIT;
103 result = openWithFormat(audioFormat);
104 }
105 return result;
106}
107
108aaudio_result_t AAudioServiceEndpointMMAP::openWithFormat(audio_format_t audioFormat) {
109 aaudio_result_t result = AAUDIO_OK;
110 audio_config_base_t config;
111 audio_port_handle_t deviceId;
112
113 const audio_attributes_t attributes = getAudioAttributesFrom(this);
114
Phil Burk39f02dd2017-08-04 09:13:31 -0700115 mRequestedDeviceId = deviceId = getDeviceId();
116
117 // Fill in config
Phil Burk0127c1b2018-03-29 13:48:06 -0700118 config.format = audioFormat;
Phil Burk39f02dd2017-08-04 09:13:31 -0700119
120 int32_t aaudioSampleRate = getSampleRate();
121 if (aaudioSampleRate == AAUDIO_UNSPECIFIED) {
122 aaudioSampleRate = AAUDIO_SAMPLE_RATE_DEFAULT;
123 }
124 config.sample_rate = aaudioSampleRate;
125
126 int32_t aaudioSamplesPerFrame = getSamplesPerFrame();
127
jiabind1f1cb62020-03-24 11:57:57 -0700128 const aaudio_direction_t direction = getDirection();
129
Phil Burk39f02dd2017-08-04 09:13:31 -0700130 if (direction == AAUDIO_DIRECTION_OUTPUT) {
131 config.channel_mask = (aaudioSamplesPerFrame == AAUDIO_UNSPECIFIED)
132 ? AUDIO_CHANNEL_OUT_STEREO
133 : audio_channel_out_mask_from_count(aaudioSamplesPerFrame);
134 mHardwareTimeOffsetNanos = OUTPUT_ESTIMATED_HARDWARE_OFFSET_NANOS; // frames at DAC later
135
136 } else if (direction == AAUDIO_DIRECTION_INPUT) {
137 config.channel_mask = (aaudioSamplesPerFrame == AAUDIO_UNSPECIFIED)
138 ? AUDIO_CHANNEL_IN_STEREO
139 : audio_channel_in_mask_from_count(aaudioSamplesPerFrame);
140 mHardwareTimeOffsetNanos = INPUT_ESTIMATED_HARDWARE_OFFSET_NANOS; // frames at ADC earlier
141
142 } else {
Phil Burk19e990e2018-03-22 13:59:34 -0700143 ALOGE("%s() invalid direction = %d", __func__, direction);
Phil Burk39f02dd2017-08-04 09:13:31 -0700144 return AAUDIO_ERROR_ILLEGAL_ARGUMENT;
145 }
146
147 MmapStreamInterface::stream_direction_t streamDirection =
148 (direction == AAUDIO_DIRECTION_OUTPUT)
149 ? MmapStreamInterface::DIRECTION_OUTPUT
150 : MmapStreamInterface::DIRECTION_INPUT;
151
Phil Burk4e1af9f2018-01-03 15:54:35 -0800152 aaudio_session_id_t requestedSessionId = getSessionId();
153 audio_session_t sessionId = AAudioConvert_aaudioToAndroidSessionId(requestedSessionId);
154
Phil Burk39f02dd2017-08-04 09:13:31 -0700155 // Open HAL stream. Set mMmapStream
156 status_t status = MmapStreamInterface::openMmapStream(streamDirection,
157 &attributes,
158 &config,
159 mMmapClient,
160 &deviceId,
Phil Burk4e1af9f2018-01-03 15:54:35 -0800161 &sessionId,
Phil Burk39f02dd2017-08-04 09:13:31 -0700162 this, // callback
163 mMmapStream,
164 &mPortHandle);
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700165 ALOGD("%s() mMapClient.identity = %s => portHandle = %d\n",
166 __func__, mMmapClient.identity.toString().c_str(), mPortHandle);
Phil Burk39f02dd2017-08-04 09:13:31 -0700167 if (status != OK) {
Phil Burk29ccc292019-04-15 08:58:08 -0700168 // This can happen if the resource is busy or the config does
169 // not match the hardware.
170 ALOGD("%s() - openMmapStream() returned status %d", __func__, status);
Phil Burk39f02dd2017-08-04 09:13:31 -0700171 return AAUDIO_ERROR_UNAVAILABLE;
172 }
173
174 if (deviceId == AAUDIO_UNSPECIFIED) {
Phil Burka3901e92018-10-08 13:54:38 -0700175 ALOGW("%s() - openMmapStream() failed to set deviceId", __func__);
Phil Burk39f02dd2017-08-04 09:13:31 -0700176 }
177 setDeviceId(deviceId);
178
Phil Burk4e1af9f2018-01-03 15:54:35 -0800179 if (sessionId == AUDIO_SESSION_ALLOCATE) {
Phil Burk19e990e2018-03-22 13:59:34 -0700180 ALOGW("%s() - openMmapStream() failed to set sessionId", __func__);
Phil Burk4e1af9f2018-01-03 15:54:35 -0800181 }
182
183 aaudio_session_id_t actualSessionId =
184 (requestedSessionId == AAUDIO_SESSION_ID_NONE)
185 ? AAUDIO_SESSION_ID_NONE
186 : (aaudio_session_id_t) sessionId;
187 setSessionId(actualSessionId);
Phil Burk19e990e2018-03-22 13:59:34 -0700188 ALOGD("%s() deviceId = %d, sessionId = %d", __func__, getDeviceId(), getSessionId());
Phil Burk4e1af9f2018-01-03 15:54:35 -0800189
Phil Burk39f02dd2017-08-04 09:13:31 -0700190 // Create MMAP/NOIRQ buffer.
191 int32_t minSizeFrames = getBufferCapacity();
192 if (minSizeFrames <= 0) { // zero will get rejected
193 minSizeFrames = AAUDIO_BUFFER_CAPACITY_MIN;
194 }
195 status = mMmapStream->createMmapBuffer(minSizeFrames, &mMmapBufferinfo);
Kevin Rocard734334f2018-07-12 19:37:41 -0700196 bool isBufferShareable = mMmapBufferinfo.flags & AUDIO_MMAP_APPLICATION_SHAREABLE;
Phil Burk39f02dd2017-08-04 09:13:31 -0700197 if (status != OK) {
Phil Burk19e990e2018-03-22 13:59:34 -0700198 ALOGE("%s() - createMmapBuffer() failed with status %d %s",
199 __func__, status, strerror(-status));
Phil Burk39f02dd2017-08-04 09:13:31 -0700200 result = AAUDIO_ERROR_UNAVAILABLE;
201 goto error;
202 } else {
Phil Burk29ccc292019-04-15 08:58:08 -0700203 ALOGD("%s() createMmapBuffer() buffer_size = %d fr, burst_size %d fr"
Phil Burk39f02dd2017-08-04 09:13:31 -0700204 ", Sharable FD: %s",
Phil Burk29ccc292019-04-15 08:58:08 -0700205 __func__,
Kevin Rocard734334f2018-07-12 19:37:41 -0700206 mMmapBufferinfo.buffer_size_frames,
Phil Burk39f02dd2017-08-04 09:13:31 -0700207 mMmapBufferinfo.burst_size_frames,
Kevin Rocard734334f2018-07-12 19:37:41 -0700208 isBufferShareable ? "Yes" : "No");
Phil Burk39f02dd2017-08-04 09:13:31 -0700209 }
210
211 setBufferCapacity(mMmapBufferinfo.buffer_size_frames);
Kevin Rocard734334f2018-07-12 19:37:41 -0700212 if (!isBufferShareable) {
Phil Burk39f02dd2017-08-04 09:13:31 -0700213 // Exclusive mode can only be used by the service because the FD cannot be shared.
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700214 int32_t audioServiceUid =
215 VALUE_OR_FATAL(legacy2aidl_uid_t_int32_t(getuid()));
216 if ((mMmapClient.identity.uid != audioServiceUid) &&
Phil Burk39f02dd2017-08-04 09:13:31 -0700217 getSharingMode() == AAUDIO_SHARING_MODE_EXCLUSIVE) {
Phil Burk19e990e2018-03-22 13:59:34 -0700218 ALOGW("%s() - exclusive FD cannot be used by client", __func__);
Phil Burk39f02dd2017-08-04 09:13:31 -0700219 result = AAUDIO_ERROR_UNAVAILABLE;
220 goto error;
221 }
222 }
223
224 // Get information about the stream and pass it back to the caller.
225 setSamplesPerFrame((direction == AAUDIO_DIRECTION_OUTPUT)
226 ? audio_channel_count_from_out_mask(config.channel_mask)
227 : audio_channel_count_from_in_mask(config.channel_mask));
228
229 // AAudio creates a copy of this FD and retains ownership of the copy.
230 // Assume that AudioFlinger will close the original shared_memory_fd.
231 mAudioDataFileDescriptor.reset(dup(mMmapBufferinfo.shared_memory_fd));
232 if (mAudioDataFileDescriptor.get() == -1) {
Phil Burk19e990e2018-03-22 13:59:34 -0700233 ALOGE("%s() - could not dup shared_memory_fd", __func__);
Phil Burk39f02dd2017-08-04 09:13:31 -0700234 result = AAUDIO_ERROR_INTERNAL;
235 goto error;
236 }
237 mFramesPerBurst = mMmapBufferinfo.burst_size_frames;
Phil Burk0127c1b2018-03-29 13:48:06 -0700238 setFormat(config.format);
Phil Burk39f02dd2017-08-04 09:13:31 -0700239 setSampleRate(config.sample_rate);
240
Phil Burk3c4e6b52019-01-22 15:53:36 -0800241 ALOGD("%s() actual rate = %d, channels = %d"
242 ", deviceId = %d, capacity = %d\n",
243 __func__, getSampleRate(), getSamplesPerFrame(), deviceId, getBufferCapacity());
Phil Burk39f02dd2017-08-04 09:13:31 -0700244
Phil Burk3c4e6b52019-01-22 15:53:36 -0800245 ALOGD("%s() format = 0x%08x, frame size = %d, burst size = %d",
246 __func__, getFormat(), calculateBytesPerFrame(), mFramesPerBurst);
Phil Burk0127c1b2018-03-29 13:48:06 -0700247
Phil Burk39f02dd2017-08-04 09:13:31 -0700248 return result;
249
250error:
251 close();
252 return result;
253}
254
Phil Burk320910f2020-08-12 14:29:10 +0000255void AAudioServiceEndpointMMAP::close() {
Phil Burk6e463ce2020-04-13 10:20:20 -0700256 if (mMmapStream != nullptr) {
Phil Burk39f02dd2017-08-04 09:13:31 -0700257 // Needs to be explicitly cleared or CTS will fail but it is not clear why.
258 mMmapStream.clear();
259 // Apparently the above close is asynchronous. An attempt to open a new device
260 // right after a close can fail. Also some callbacks may still be in flight!
261 // FIXME Make closing synchronous.
262 AudioClock::sleepForNanos(100 * AAUDIO_NANOS_PER_MILLISECOND);
263 }
Phil Burk39f02dd2017-08-04 09:13:31 -0700264}
265
266aaudio_result_t AAudioServiceEndpointMMAP::startStream(sp<AAudioServiceStreamBase> stream,
Phil Burkbbd52862018-04-13 11:37:42 -0700267 audio_port_handle_t *clientHandle __unused) {
Phil Burkbcc36742017-08-31 17:24:51 -0700268 // Start the client on behalf of the AAudio service.
269 // Use the port handle that was provided by openMmapStream().
Phil Burkbbd52862018-04-13 11:37:42 -0700270 audio_port_handle_t tempHandle = mPortHandle;
jiabind1f1cb62020-03-24 11:57:57 -0700271 audio_attributes_t attr = {};
272 if (stream != nullptr) {
273 attr = getAudioAttributesFrom(stream.get());
274 }
275 aaudio_result_t result = startClient(
276 mMmapClient, stream == nullptr ? nullptr : &attr, &tempHandle);
Phil Burkbbd52862018-04-13 11:37:42 -0700277 // When AudioFlinger is passed a valid port handle then it should not change it.
278 LOG_ALWAYS_FATAL_IF(tempHandle != mPortHandle,
279 "%s() port handle not expected to change from %d to %d",
280 __func__, mPortHandle, tempHandle);
Phil Burk29ccc292019-04-15 08:58:08 -0700281 ALOGV("%s() mPortHandle = %d", __func__, mPortHandle);
Phil Burkbbd52862018-04-13 11:37:42 -0700282 return result;
Phil Burk39f02dd2017-08-04 09:13:31 -0700283}
284
285aaudio_result_t AAudioServiceEndpointMMAP::stopStream(sp<AAudioServiceStreamBase> stream,
Phil Burkbbd52862018-04-13 11:37:42 -0700286 audio_port_handle_t clientHandle __unused) {
Phil Burk39f02dd2017-08-04 09:13:31 -0700287 mFramesTransferred.reset32();
Phil Burk73af62a2017-10-26 12:11:47 -0700288
289 // Round 64-bit counter up to a multiple of the buffer capacity.
290 // This is required because the 64-bit counter is used as an index
291 // into a circular buffer and the actual HW position is reset to zero
292 // when the stream is stopped.
293 mFramesTransferred.roundUp64(getBufferCapacity());
294
Phil Burkbbd52862018-04-13 11:37:42 -0700295 // Use the port handle that was provided by openMmapStream().
Phil Burk29ccc292019-04-15 08:58:08 -0700296 ALOGV("%s() mPortHandle = %d", __func__, mPortHandle);
Phil Burk39f02dd2017-08-04 09:13:31 -0700297 return stopClient(mPortHandle);
298}
299
300aaudio_result_t AAudioServiceEndpointMMAP::startClient(const android::AudioClient& client,
jiabind1f1cb62020-03-24 11:57:57 -0700301 const audio_attributes_t *attr,
Phil Burk39f02dd2017-08-04 09:13:31 -0700302 audio_port_handle_t *clientHandle) {
303 if (mMmapStream == nullptr) return AAUDIO_ERROR_NULL;
jiabind1f1cb62020-03-24 11:57:57 -0700304 status_t status = mMmapStream->start(client, attr, clientHandle);
Phil Burk29ccc292019-04-15 08:58:08 -0700305 return AAudioConvert_androidToAAudioResult(status);
Phil Burk39f02dd2017-08-04 09:13:31 -0700306}
307
308aaudio_result_t AAudioServiceEndpointMMAP::stopClient(audio_port_handle_t clientHandle) {
309 if (mMmapStream == nullptr) return AAUDIO_ERROR_NULL;
310 aaudio_result_t result = AAudioConvert_androidToAAudioResult(mMmapStream->stop(clientHandle));
Phil Burk39f02dd2017-08-04 09:13:31 -0700311 return result;
312}
313
314// Get free-running DSP or DMA hardware position from the HAL.
315aaudio_result_t AAudioServiceEndpointMMAP::getFreeRunningPosition(int64_t *positionFrames,
316 int64_t *timeNanos) {
317 struct audio_mmap_position position;
318 if (mMmapStream == nullptr) {
319 return AAUDIO_ERROR_NULL;
320 }
321 status_t status = mMmapStream->getMmapPosition(&position);
Phil Burk19e990e2018-03-22 13:59:34 -0700322 ALOGV("%s() status= %d, pos = %d, nanos = %lld\n",
323 __func__, status, position.position_frames, (long long) position.time_nanoseconds);
Phil Burk39f02dd2017-08-04 09:13:31 -0700324 aaudio_result_t result = AAudioConvert_androidToAAudioResult(status);
325 if (result == AAUDIO_ERROR_UNAVAILABLE) {
Phil Burk19e990e2018-03-22 13:59:34 -0700326 ALOGW("%s(): getMmapPosition() has no position data available", __func__);
Phil Burk39f02dd2017-08-04 09:13:31 -0700327 } else if (result != AAUDIO_OK) {
Phil Burk19e990e2018-03-22 13:59:34 -0700328 ALOGE("%s(): getMmapPosition() returned status %d", __func__, status);
Phil Burk39f02dd2017-08-04 09:13:31 -0700329 } else {
330 // Convert 32-bit position to 64-bit position.
331 mFramesTransferred.update32(position.position_frames);
332 *positionFrames = mFramesTransferred.get();
333 *timeNanos = position.time_nanoseconds;
334 }
335 return result;
336}
337
338aaudio_result_t AAudioServiceEndpointMMAP::getTimestamp(int64_t *positionFrames,
339 int64_t *timeNanos) {
340 return 0; // TODO
341}
342
Phil Burka77869d2020-05-07 10:39:47 -0700343// This is called by onTearDown() in a separate thread to avoid deadlocks.
344void AAudioServiceEndpointMMAP::handleTearDownAsync(audio_port_handle_t portHandle) {
Phil Burkbbd52862018-04-13 11:37:42 -0700345 // Are we tearing down the EXCLUSIVE MMAP stream?
346 if (isStreamRegistered(portHandle)) {
347 ALOGD("%s(%d) tearing down this entire MMAP endpoint", __func__, portHandle);
348 disconnectRegisteredStreams();
349 } else {
350 // Must be a SHARED stream?
351 ALOGD("%s(%d) disconnect a specific stream", __func__, portHandle);
352 aaudio_result_t result = mAAudioService.disconnectStreamByPortHandle(portHandle);
353 ALOGD("%s(%d) disconnectStreamByPortHandle returned %d", __func__, portHandle, result);
354 }
Phil Burk39f02dd2017-08-04 09:13:31 -0700355};
356
Phil Burka77869d2020-05-07 10:39:47 -0700357// This is called by AudioFlinger when it wants to destroy a stream.
358void AAudioServiceEndpointMMAP::onTearDown(audio_port_handle_t portHandle) {
359 ALOGD("%s(portHandle = %d) called", __func__, portHandle);
360 std::thread asyncTask(&AAudioServiceEndpointMMAP::handleTearDownAsync, this, portHandle);
361 asyncTask.detach();
362}
363
Phil Burk39f02dd2017-08-04 09:13:31 -0700364void AAudioServiceEndpointMMAP::onVolumeChanged(audio_channel_mask_t channels,
365 android::Vector<float> values) {
Phil Burk19e990e2018-03-22 13:59:34 -0700366 // TODO Do we really need a different volume for each channel?
367 // We get called with an array filled with a single value!
Phil Burk39f02dd2017-08-04 09:13:31 -0700368 float volume = values[0];
Phil Burk29ccc292019-04-15 08:58:08 -0700369 ALOGD("%s() volume[0] = %f", __func__, volume);
Phil Burk39f02dd2017-08-04 09:13:31 -0700370 std::lock_guard<std::mutex> lock(mLockStreams);
Chih-Hung Hsieh3ef324d2018-12-11 11:48:12 -0800371 for(const auto& stream : mRegisteredStreams) {
Phil Burk39f02dd2017-08-04 09:13:31 -0700372 stream->onVolumeChanged(volume);
373 }
374};
375
Phil Burka77869d2020-05-07 10:39:47 -0700376void AAudioServiceEndpointMMAP::onRoutingChanged(audio_port_handle_t portHandle) {
377 const int32_t deviceId = static_cast<int32_t>(portHandle);
Phil Burk29ccc292019-04-15 08:58:08 -0700378 ALOGD("%s() called with dev %d, old = %d", __func__, deviceId, getDeviceId());
Phil Burka77869d2020-05-07 10:39:47 -0700379 if (getDeviceId() != deviceId) {
380 if (getDeviceId() != AUDIO_PORT_HANDLE_NONE) {
381 std::thread asyncTask([this, deviceId]() {
382 disconnectRegisteredStreams();
383 setDeviceId(deviceId);
384 });
385 asyncTask.detach();
386 } else {
387 setDeviceId(deviceId);
388 }
Phil Burk39f02dd2017-08-04 09:13:31 -0700389 }
Phil Burk39f02dd2017-08-04 09:13:31 -0700390};
391
392/**
393 * Get an immutable description of the data queue from the HAL.
394 */
395aaudio_result_t AAudioServiceEndpointMMAP::getDownDataDescription(AudioEndpointParcelable &parcelable)
396{
397 // Gather information on the data queue based on HAL info.
398 int32_t bytesPerFrame = calculateBytesPerFrame();
399 int32_t capacityInBytes = getBufferCapacity() * bytesPerFrame;
400 int fdIndex = parcelable.addFileDescriptor(mAudioDataFileDescriptor, capacityInBytes);
401 parcelable.mDownDataQueueParcelable.setupMemory(fdIndex, 0, capacityInBytes);
402 parcelable.mDownDataQueueParcelable.setBytesPerFrame(bytesPerFrame);
403 parcelable.mDownDataQueueParcelable.setFramesPerBurst(mFramesPerBurst);
404 parcelable.mDownDataQueueParcelable.setCapacityInFrames(getBufferCapacity());
405 return AAUDIO_OK;
406}
jiabinb7d8c5a2020-08-26 17:24:52 -0700407
408aaudio_result_t AAudioServiceEndpointMMAP::getExternalPosition(uint64_t *positionFrames,
409 int64_t *timeNanos)
410{
411 if (!mExternalPositionSupported) {
412 return AAUDIO_ERROR_INVALID_STATE;
413 }
414 status_t status = mMmapStream->getExternalPosition(positionFrames, timeNanos);
415 if (status == INVALID_OPERATION) {
416 // getExternalPosition is not supported. Set mExternalPositionSupported as false
417 // so that the call will not go to the HAL next time.
418 mExternalPositionSupported = false;
419 }
420 return AAudioConvert_androidToAAudioResult(status);
421}