blob: b4efd1ae767c51dc7c3665278cfa9d516620ae1a [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
millerlianga75a83f2021-04-30 17:43:00 +080083 // FLOAT is not directly supported by the HAL so ask for a 32-bit.
84 if (audioFormat == AUDIO_FORMAT_PCM_FLOAT) {
Phil Burk04e805b2018-03-27 09:13:53 -070085 // TODO remove these logs when finished debugging.
millerlianga75a83f2021-04-30 17:43:00 +080086 ALOGD("%s() change format from %d to 32_BIT", __func__, audioFormat);
87 audioFormat = AUDIO_FORMAT_PCM_32_BIT;
Phil Burk04e805b2018-03-27 09:13:53 -070088 }
89
90 result = openWithFormat(audioFormat);
91 if (result == AAUDIO_OK) return result;
92
millerlianga75a83f2021-04-30 17:43:00 +080093 if (result == AAUDIO_ERROR_UNAVAILABLE && audioFormat == AUDIO_FORMAT_PCM_32_BIT) {
94 ALOGD("%s() 32_BIT failed, perhaps due to format. Try again with 24_BIT_PACKED", __func__);
95 audioFormat = AUDIO_FORMAT_PCM_24_BIT_PACKED;
96 result = openWithFormat(audioFormat);
97 }
98 if (result == AAUDIO_OK) return result;
99
Phil Burk04e805b2018-03-27 09:13:53 -0700100 // TODO The HAL and AudioFlinger should be recommending a format if the open fails.
101 // But that recommendation is not propagating back from the HAL.
102 // So for now just try something very likely to work.
103 if (result == AAUDIO_ERROR_UNAVAILABLE && audioFormat == AUDIO_FORMAT_PCM_24_BIT_PACKED) {
104 ALOGD("%s() 24_BIT failed, perhaps due to format. Try again with 16_BIT", __func__);
105 audioFormat = AUDIO_FORMAT_PCM_16_BIT;
106 result = openWithFormat(audioFormat);
107 }
108 return result;
109}
110
111aaudio_result_t AAudioServiceEndpointMMAP::openWithFormat(audio_format_t audioFormat) {
112 aaudio_result_t result = AAUDIO_OK;
113 audio_config_base_t config;
114 audio_port_handle_t deviceId;
115
116 const audio_attributes_t attributes = getAudioAttributesFrom(this);
117
Phil Burk39f02dd2017-08-04 09:13:31 -0700118 mRequestedDeviceId = deviceId = getDeviceId();
119
120 // Fill in config
Phil Burk0127c1b2018-03-29 13:48:06 -0700121 config.format = audioFormat;
Phil Burk39f02dd2017-08-04 09:13:31 -0700122
123 int32_t aaudioSampleRate = getSampleRate();
124 if (aaudioSampleRate == AAUDIO_UNSPECIFIED) {
125 aaudioSampleRate = AAUDIO_SAMPLE_RATE_DEFAULT;
126 }
127 config.sample_rate = aaudioSampleRate;
128
129 int32_t aaudioSamplesPerFrame = getSamplesPerFrame();
130
jiabind1f1cb62020-03-24 11:57:57 -0700131 const aaudio_direction_t direction = getDirection();
132
Phil Burk39f02dd2017-08-04 09:13:31 -0700133 if (direction == AAUDIO_DIRECTION_OUTPUT) {
134 config.channel_mask = (aaudioSamplesPerFrame == AAUDIO_UNSPECIFIED)
135 ? AUDIO_CHANNEL_OUT_STEREO
136 : audio_channel_out_mask_from_count(aaudioSamplesPerFrame);
137 mHardwareTimeOffsetNanos = OUTPUT_ESTIMATED_HARDWARE_OFFSET_NANOS; // frames at DAC later
138
139 } else if (direction == AAUDIO_DIRECTION_INPUT) {
140 config.channel_mask = (aaudioSamplesPerFrame == AAUDIO_UNSPECIFIED)
141 ? AUDIO_CHANNEL_IN_STEREO
142 : audio_channel_in_mask_from_count(aaudioSamplesPerFrame);
143 mHardwareTimeOffsetNanos = INPUT_ESTIMATED_HARDWARE_OFFSET_NANOS; // frames at ADC earlier
144
145 } else {
Phil Burk19e990e2018-03-22 13:59:34 -0700146 ALOGE("%s() invalid direction = %d", __func__, direction);
Phil Burk39f02dd2017-08-04 09:13:31 -0700147 return AAUDIO_ERROR_ILLEGAL_ARGUMENT;
148 }
149
150 MmapStreamInterface::stream_direction_t streamDirection =
151 (direction == AAUDIO_DIRECTION_OUTPUT)
152 ? MmapStreamInterface::DIRECTION_OUTPUT
153 : MmapStreamInterface::DIRECTION_INPUT;
154
Phil Burk4e1af9f2018-01-03 15:54:35 -0800155 aaudio_session_id_t requestedSessionId = getSessionId();
156 audio_session_t sessionId = AAudioConvert_aaudioToAndroidSessionId(requestedSessionId);
157
Phil Burk39f02dd2017-08-04 09:13:31 -0700158 // Open HAL stream. Set mMmapStream
159 status_t status = MmapStreamInterface::openMmapStream(streamDirection,
160 &attributes,
161 &config,
162 mMmapClient,
163 &deviceId,
Phil Burk4e1af9f2018-01-03 15:54:35 -0800164 &sessionId,
Phil Burk39f02dd2017-08-04 09:13:31 -0700165 this, // callback
166 mMmapStream,
167 &mPortHandle);
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700168 ALOGD("%s() mMapClient.identity = %s => portHandle = %d\n",
169 __func__, mMmapClient.identity.toString().c_str(), mPortHandle);
Phil Burk39f02dd2017-08-04 09:13:31 -0700170 if (status != OK) {
Phil Burk29ccc292019-04-15 08:58:08 -0700171 // This can happen if the resource is busy or the config does
172 // not match the hardware.
173 ALOGD("%s() - openMmapStream() returned status %d", __func__, status);
Phil Burk39f02dd2017-08-04 09:13:31 -0700174 return AAUDIO_ERROR_UNAVAILABLE;
175 }
176
177 if (deviceId == AAUDIO_UNSPECIFIED) {
Phil Burka3901e92018-10-08 13:54:38 -0700178 ALOGW("%s() - openMmapStream() failed to set deviceId", __func__);
Phil Burk39f02dd2017-08-04 09:13:31 -0700179 }
180 setDeviceId(deviceId);
181
Phil Burk4e1af9f2018-01-03 15:54:35 -0800182 if (sessionId == AUDIO_SESSION_ALLOCATE) {
Phil Burk19e990e2018-03-22 13:59:34 -0700183 ALOGW("%s() - openMmapStream() failed to set sessionId", __func__);
Phil Burk4e1af9f2018-01-03 15:54:35 -0800184 }
185
186 aaudio_session_id_t actualSessionId =
187 (requestedSessionId == AAUDIO_SESSION_ID_NONE)
188 ? AAUDIO_SESSION_ID_NONE
189 : (aaudio_session_id_t) sessionId;
190 setSessionId(actualSessionId);
Phil Burk19e990e2018-03-22 13:59:34 -0700191 ALOGD("%s() deviceId = %d, sessionId = %d", __func__, getDeviceId(), getSessionId());
Phil Burk4e1af9f2018-01-03 15:54:35 -0800192
Phil Burk39f02dd2017-08-04 09:13:31 -0700193 // Create MMAP/NOIRQ buffer.
194 int32_t minSizeFrames = getBufferCapacity();
195 if (minSizeFrames <= 0) { // zero will get rejected
196 minSizeFrames = AAUDIO_BUFFER_CAPACITY_MIN;
197 }
198 status = mMmapStream->createMmapBuffer(minSizeFrames, &mMmapBufferinfo);
Kevin Rocard734334f2018-07-12 19:37:41 -0700199 bool isBufferShareable = mMmapBufferinfo.flags & AUDIO_MMAP_APPLICATION_SHAREABLE;
Phil Burk39f02dd2017-08-04 09:13:31 -0700200 if (status != OK) {
Phil Burk19e990e2018-03-22 13:59:34 -0700201 ALOGE("%s() - createMmapBuffer() failed with status %d %s",
202 __func__, status, strerror(-status));
Phil Burk39f02dd2017-08-04 09:13:31 -0700203 result = AAUDIO_ERROR_UNAVAILABLE;
204 goto error;
205 } else {
Phil Burk29ccc292019-04-15 08:58:08 -0700206 ALOGD("%s() createMmapBuffer() buffer_size = %d fr, burst_size %d fr"
Phil Burk39f02dd2017-08-04 09:13:31 -0700207 ", Sharable FD: %s",
Phil Burk29ccc292019-04-15 08:58:08 -0700208 __func__,
Kevin Rocard734334f2018-07-12 19:37:41 -0700209 mMmapBufferinfo.buffer_size_frames,
Phil Burk39f02dd2017-08-04 09:13:31 -0700210 mMmapBufferinfo.burst_size_frames,
Kevin Rocard734334f2018-07-12 19:37:41 -0700211 isBufferShareable ? "Yes" : "No");
Phil Burk39f02dd2017-08-04 09:13:31 -0700212 }
213
214 setBufferCapacity(mMmapBufferinfo.buffer_size_frames);
Kevin Rocard734334f2018-07-12 19:37:41 -0700215 if (!isBufferShareable) {
Phil Burk39f02dd2017-08-04 09:13:31 -0700216 // Exclusive mode can only be used by the service because the FD cannot be shared.
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700217 int32_t audioServiceUid =
218 VALUE_OR_FATAL(legacy2aidl_uid_t_int32_t(getuid()));
219 if ((mMmapClient.identity.uid != audioServiceUid) &&
Phil Burk39f02dd2017-08-04 09:13:31 -0700220 getSharingMode() == AAUDIO_SHARING_MODE_EXCLUSIVE) {
Phil Burk19e990e2018-03-22 13:59:34 -0700221 ALOGW("%s() - exclusive FD cannot be used by client", __func__);
Phil Burk39f02dd2017-08-04 09:13:31 -0700222 result = AAUDIO_ERROR_UNAVAILABLE;
223 goto error;
224 }
225 }
226
227 // Get information about the stream and pass it back to the caller.
228 setSamplesPerFrame((direction == AAUDIO_DIRECTION_OUTPUT)
229 ? audio_channel_count_from_out_mask(config.channel_mask)
230 : audio_channel_count_from_in_mask(config.channel_mask));
231
232 // AAudio creates a copy of this FD and retains ownership of the copy.
233 // Assume that AudioFlinger will close the original shared_memory_fd.
234 mAudioDataFileDescriptor.reset(dup(mMmapBufferinfo.shared_memory_fd));
235 if (mAudioDataFileDescriptor.get() == -1) {
Phil Burk19e990e2018-03-22 13:59:34 -0700236 ALOGE("%s() - could not dup shared_memory_fd", __func__);
Phil Burk39f02dd2017-08-04 09:13:31 -0700237 result = AAUDIO_ERROR_INTERNAL;
238 goto error;
239 }
240 mFramesPerBurst = mMmapBufferinfo.burst_size_frames;
Phil Burk0127c1b2018-03-29 13:48:06 -0700241 setFormat(config.format);
Phil Burk39f02dd2017-08-04 09:13:31 -0700242 setSampleRate(config.sample_rate);
243
Phil Burk3c4e6b52019-01-22 15:53:36 -0800244 ALOGD("%s() actual rate = %d, channels = %d"
245 ", deviceId = %d, capacity = %d\n",
246 __func__, getSampleRate(), getSamplesPerFrame(), deviceId, getBufferCapacity());
Phil Burk39f02dd2017-08-04 09:13:31 -0700247
Phil Burk3c4e6b52019-01-22 15:53:36 -0800248 ALOGD("%s() format = 0x%08x, frame size = %d, burst size = %d",
249 __func__, getFormat(), calculateBytesPerFrame(), mFramesPerBurst);
Phil Burk0127c1b2018-03-29 13:48:06 -0700250
Phil Burk39f02dd2017-08-04 09:13:31 -0700251 return result;
252
253error:
254 close();
255 return result;
256}
257
Phil Burk320910f2020-08-12 14:29:10 +0000258void AAudioServiceEndpointMMAP::close() {
Phil Burk6e463ce2020-04-13 10:20:20 -0700259 if (mMmapStream != nullptr) {
Phil Burk39f02dd2017-08-04 09:13:31 -0700260 // Needs to be explicitly cleared or CTS will fail but it is not clear why.
261 mMmapStream.clear();
262 // Apparently the above close is asynchronous. An attempt to open a new device
263 // right after a close can fail. Also some callbacks may still be in flight!
264 // FIXME Make closing synchronous.
265 AudioClock::sleepForNanos(100 * AAUDIO_NANOS_PER_MILLISECOND);
266 }
Phil Burk39f02dd2017-08-04 09:13:31 -0700267}
268
269aaudio_result_t AAudioServiceEndpointMMAP::startStream(sp<AAudioServiceStreamBase> stream,
Phil Burkbbd52862018-04-13 11:37:42 -0700270 audio_port_handle_t *clientHandle __unused) {
Phil Burkbcc36742017-08-31 17:24:51 -0700271 // Start the client on behalf of the AAudio service.
272 // Use the port handle that was provided by openMmapStream().
Phil Burkbbd52862018-04-13 11:37:42 -0700273 audio_port_handle_t tempHandle = mPortHandle;
jiabind1f1cb62020-03-24 11:57:57 -0700274 audio_attributes_t attr = {};
275 if (stream != nullptr) {
276 attr = getAudioAttributesFrom(stream.get());
277 }
278 aaudio_result_t result = startClient(
279 mMmapClient, stream == nullptr ? nullptr : &attr, &tempHandle);
Phil Burkbbd52862018-04-13 11:37:42 -0700280 // When AudioFlinger is passed a valid port handle then it should not change it.
281 LOG_ALWAYS_FATAL_IF(tempHandle != mPortHandle,
282 "%s() port handle not expected to change from %d to %d",
283 __func__, mPortHandle, tempHandle);
Phil Burk29ccc292019-04-15 08:58:08 -0700284 ALOGV("%s() mPortHandle = %d", __func__, mPortHandle);
Phil Burkbbd52862018-04-13 11:37:42 -0700285 return result;
Phil Burk39f02dd2017-08-04 09:13:31 -0700286}
287
288aaudio_result_t AAudioServiceEndpointMMAP::stopStream(sp<AAudioServiceStreamBase> stream,
Phil Burkbbd52862018-04-13 11:37:42 -0700289 audio_port_handle_t clientHandle __unused) {
Phil Burk39f02dd2017-08-04 09:13:31 -0700290 mFramesTransferred.reset32();
Phil Burk73af62a2017-10-26 12:11:47 -0700291
292 // Round 64-bit counter up to a multiple of the buffer capacity.
293 // This is required because the 64-bit counter is used as an index
294 // into a circular buffer and the actual HW position is reset to zero
295 // when the stream is stopped.
296 mFramesTransferred.roundUp64(getBufferCapacity());
297
Phil Burkbbd52862018-04-13 11:37:42 -0700298 // Use the port handle that was provided by openMmapStream().
Phil Burk29ccc292019-04-15 08:58:08 -0700299 ALOGV("%s() mPortHandle = %d", __func__, mPortHandle);
Phil Burk39f02dd2017-08-04 09:13:31 -0700300 return stopClient(mPortHandle);
301}
302
303aaudio_result_t AAudioServiceEndpointMMAP::startClient(const android::AudioClient& client,
jiabind1f1cb62020-03-24 11:57:57 -0700304 const audio_attributes_t *attr,
Phil Burk39f02dd2017-08-04 09:13:31 -0700305 audio_port_handle_t *clientHandle) {
306 if (mMmapStream == nullptr) return AAUDIO_ERROR_NULL;
jiabind1f1cb62020-03-24 11:57:57 -0700307 status_t status = mMmapStream->start(client, attr, clientHandle);
Phil Burk29ccc292019-04-15 08:58:08 -0700308 return AAudioConvert_androidToAAudioResult(status);
Phil Burk39f02dd2017-08-04 09:13:31 -0700309}
310
311aaudio_result_t AAudioServiceEndpointMMAP::stopClient(audio_port_handle_t clientHandle) {
312 if (mMmapStream == nullptr) return AAUDIO_ERROR_NULL;
313 aaudio_result_t result = AAudioConvert_androidToAAudioResult(mMmapStream->stop(clientHandle));
Phil Burk39f02dd2017-08-04 09:13:31 -0700314 return result;
315}
316
317// Get free-running DSP or DMA hardware position from the HAL.
318aaudio_result_t AAudioServiceEndpointMMAP::getFreeRunningPosition(int64_t *positionFrames,
319 int64_t *timeNanos) {
320 struct audio_mmap_position position;
321 if (mMmapStream == nullptr) {
322 return AAUDIO_ERROR_NULL;
323 }
324 status_t status = mMmapStream->getMmapPosition(&position);
Phil Burk19e990e2018-03-22 13:59:34 -0700325 ALOGV("%s() status= %d, pos = %d, nanos = %lld\n",
326 __func__, status, position.position_frames, (long long) position.time_nanoseconds);
Phil Burk39f02dd2017-08-04 09:13:31 -0700327 aaudio_result_t result = AAudioConvert_androidToAAudioResult(status);
328 if (result == AAUDIO_ERROR_UNAVAILABLE) {
Phil Burk19e990e2018-03-22 13:59:34 -0700329 ALOGW("%s(): getMmapPosition() has no position data available", __func__);
Phil Burk39f02dd2017-08-04 09:13:31 -0700330 } else if (result != AAUDIO_OK) {
Phil Burk19e990e2018-03-22 13:59:34 -0700331 ALOGE("%s(): getMmapPosition() returned status %d", __func__, status);
Phil Burk39f02dd2017-08-04 09:13:31 -0700332 } else {
333 // Convert 32-bit position to 64-bit position.
334 mFramesTransferred.update32(position.position_frames);
335 *positionFrames = mFramesTransferred.get();
336 *timeNanos = position.time_nanoseconds;
337 }
338 return result;
339}
340
341aaudio_result_t AAudioServiceEndpointMMAP::getTimestamp(int64_t *positionFrames,
342 int64_t *timeNanos) {
343 return 0; // TODO
344}
345
Phil Burka77869d2020-05-07 10:39:47 -0700346// This is called by onTearDown() in a separate thread to avoid deadlocks.
347void AAudioServiceEndpointMMAP::handleTearDownAsync(audio_port_handle_t portHandle) {
Phil Burkbbd52862018-04-13 11:37:42 -0700348 // Are we tearing down the EXCLUSIVE MMAP stream?
349 if (isStreamRegistered(portHandle)) {
350 ALOGD("%s(%d) tearing down this entire MMAP endpoint", __func__, portHandle);
351 disconnectRegisteredStreams();
352 } else {
353 // Must be a SHARED stream?
354 ALOGD("%s(%d) disconnect a specific stream", __func__, portHandle);
355 aaudio_result_t result = mAAudioService.disconnectStreamByPortHandle(portHandle);
356 ALOGD("%s(%d) disconnectStreamByPortHandle returned %d", __func__, portHandle, result);
357 }
Phil Burk39f02dd2017-08-04 09:13:31 -0700358};
359
Phil Burka77869d2020-05-07 10:39:47 -0700360// This is called by AudioFlinger when it wants to destroy a stream.
361void AAudioServiceEndpointMMAP::onTearDown(audio_port_handle_t portHandle) {
362 ALOGD("%s(portHandle = %d) called", __func__, portHandle);
Phil Burk3d201942021-04-08 23:27:04 +0000363 android::sp<AAudioServiceEndpointMMAP> holdEndpoint(this);
364 std::thread asyncTask([holdEndpoint, portHandle]() {
365 holdEndpoint->handleTearDownAsync(portHandle);
366 });
Phil Burka77869d2020-05-07 10:39:47 -0700367 asyncTask.detach();
368}
369
Phil Burk39f02dd2017-08-04 09:13:31 -0700370void AAudioServiceEndpointMMAP::onVolumeChanged(audio_channel_mask_t channels,
371 android::Vector<float> values) {
Phil Burk19e990e2018-03-22 13:59:34 -0700372 // TODO Do we really need a different volume for each channel?
373 // We get called with an array filled with a single value!
Phil Burk39f02dd2017-08-04 09:13:31 -0700374 float volume = values[0];
Phil Burk29ccc292019-04-15 08:58:08 -0700375 ALOGD("%s() volume[0] = %f", __func__, volume);
Phil Burk39f02dd2017-08-04 09:13:31 -0700376 std::lock_guard<std::mutex> lock(mLockStreams);
Chih-Hung Hsieh3ef324d2018-12-11 11:48:12 -0800377 for(const auto& stream : mRegisteredStreams) {
Phil Burk39f02dd2017-08-04 09:13:31 -0700378 stream->onVolumeChanged(volume);
379 }
380};
381
Phil Burka77869d2020-05-07 10:39:47 -0700382void AAudioServiceEndpointMMAP::onRoutingChanged(audio_port_handle_t portHandle) {
383 const int32_t deviceId = static_cast<int32_t>(portHandle);
Phil Burk29ccc292019-04-15 08:58:08 -0700384 ALOGD("%s() called with dev %d, old = %d", __func__, deviceId, getDeviceId());
Phil Burka77869d2020-05-07 10:39:47 -0700385 if (getDeviceId() != deviceId) {
386 if (getDeviceId() != AUDIO_PORT_HANDLE_NONE) {
Phil Burk3d201942021-04-08 23:27:04 +0000387 android::sp<AAudioServiceEndpointMMAP> holdEndpoint(this);
388 std::thread asyncTask([holdEndpoint, deviceId]() {
389 ALOGD("onRoutingChanged() asyncTask launched");
390 holdEndpoint->disconnectRegisteredStreams();
391 holdEndpoint->setDeviceId(deviceId);
Phil Burka77869d2020-05-07 10:39:47 -0700392 });
393 asyncTask.detach();
394 } else {
395 setDeviceId(deviceId);
396 }
Phil Burk39f02dd2017-08-04 09:13:31 -0700397 }
Phil Burk39f02dd2017-08-04 09:13:31 -0700398};
399
400/**
401 * Get an immutable description of the data queue from the HAL.
402 */
403aaudio_result_t AAudioServiceEndpointMMAP::getDownDataDescription(AudioEndpointParcelable &parcelable)
404{
405 // Gather information on the data queue based on HAL info.
406 int32_t bytesPerFrame = calculateBytesPerFrame();
407 int32_t capacityInBytes = getBufferCapacity() * bytesPerFrame;
408 int fdIndex = parcelable.addFileDescriptor(mAudioDataFileDescriptor, capacityInBytes);
409 parcelable.mDownDataQueueParcelable.setupMemory(fdIndex, 0, capacityInBytes);
410 parcelable.mDownDataQueueParcelable.setBytesPerFrame(bytesPerFrame);
411 parcelable.mDownDataQueueParcelable.setFramesPerBurst(mFramesPerBurst);
412 parcelable.mDownDataQueueParcelable.setCapacityInFrames(getBufferCapacity());
413 return AAUDIO_OK;
414}
jiabinb7d8c5a2020-08-26 17:24:52 -0700415
416aaudio_result_t AAudioServiceEndpointMMAP::getExternalPosition(uint64_t *positionFrames,
417 int64_t *timeNanos)
418{
419 if (!mExternalPositionSupported) {
420 return AAUDIO_ERROR_INVALID_STATE;
421 }
422 status_t status = mMmapStream->getExternalPosition(positionFrames, timeNanos);
423 if (status == INVALID_OPERATION) {
424 // getExternalPosition is not supported. Set mExternalPositionSupported as false
425 // so that the call will not go to the HAL next time.
426 mExternalPositionSupported = false;
427 }
428 return AAudioConvert_androidToAAudioResult(status);
429}