Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 1 | /* |
| 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> |
jiabin | f1c7397 | 2022-04-14 16:28:52 -0700 | [diff] [blame] | 25 | #include <set> |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 26 | #include <sstream> |
Phil Burk | a77869d | 2020-05-07 10:39:47 -0700 | [diff] [blame] | 27 | #include <thread> |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 28 | #include <utils/Singleton.h> |
| 29 | #include <vector> |
| 30 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 31 | #include "AAudioEndpointManager.h" |
| 32 | #include "AAudioServiceEndpoint.h" |
| 33 | |
| 34 | #include "core/AudioStreamBuilder.h" |
| 35 | #include "AAudioServiceEndpoint.h" |
| 36 | #include "AAudioServiceStreamShared.h" |
| 37 | #include "AAudioServiceEndpointPlay.h" |
| 38 | #include "AAudioServiceEndpointMMAP.h" |
| 39 | |
jiabin | 613e6ae | 2022-12-21 20:20:11 +0000 | [diff] [blame^] | 40 | #define AAUDIO_BUFFER_CAPACITY_MIN (4 * 512) |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 41 | #define AAUDIO_SAMPLE_RATE_DEFAULT 48000 |
| 42 | |
| 43 | // This is an estimate of the time difference between the HW and the MMAP time. |
| 44 | // TODO Get presentation timestamps from the HAL instead of using these estimates. |
| 45 | #define OUTPUT_ESTIMATED_HARDWARE_OFFSET_NANOS (3 * AAUDIO_NANOS_PER_MILLISECOND) |
| 46 | #define INPUT_ESTIMATED_HARDWARE_OFFSET_NANOS (-1 * AAUDIO_NANOS_PER_MILLISECOND) |
| 47 | |
| 48 | using namespace android; // TODO just import names needed |
| 49 | using namespace aaudio; // TODO just import names needed |
| 50 | |
Phil Burk | bbd5286 | 2018-04-13 11:37:42 -0700 | [diff] [blame] | 51 | AAudioServiceEndpointMMAP::AAudioServiceEndpointMMAP(AAudioService &audioService) |
| 52 | : mMmapStream(nullptr) |
| 53 | , mAAudioService(audioService) {} |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 54 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 55 | std::string AAudioServiceEndpointMMAP::dump() const { |
| 56 | std::stringstream result; |
| 57 | |
| 58 | result << " MMAP: framesTransferred = " << mFramesTransferred.get(); |
| 59 | result << ", HW nanos = " << mHardwareTimeOffsetNanos; |
| 60 | result << ", port handle = " << mPortHandle; |
| 61 | result << ", audio data FD = " << mAudioDataFileDescriptor; |
| 62 | result << "\n"; |
| 63 | |
| 64 | result << " HW Offset Micros: " << |
| 65 | (getHardwareTimeOffsetNanos() |
| 66 | / AAUDIO_NANOS_PER_MICROSECOND) << "\n"; |
| 67 | |
| 68 | result << AAudioServiceEndpoint::dump(); |
| 69 | return result.str(); |
| 70 | } |
| 71 | |
jiabin | f1c7397 | 2022-04-14 16:28:52 -0700 | [diff] [blame] | 72 | namespace { |
| 73 | |
| 74 | const static std::map<audio_format_t, audio_format_t> NEXT_FORMAT_TO_TRY = { |
| 75 | {AUDIO_FORMAT_PCM_FLOAT, AUDIO_FORMAT_PCM_32_BIT}, |
| 76 | {AUDIO_FORMAT_PCM_32_BIT, AUDIO_FORMAT_PCM_24_BIT_PACKED}, |
| 77 | {AUDIO_FORMAT_PCM_24_BIT_PACKED, AUDIO_FORMAT_PCM_16_BIT} |
| 78 | }; |
| 79 | |
| 80 | audio_format_t getNextFormatToTry(audio_format_t curFormat, audio_format_t returnedFromAPM) { |
| 81 | if (returnedFromAPM != AUDIO_FORMAT_DEFAULT) { |
| 82 | return returnedFromAPM; |
| 83 | } |
| 84 | const auto it = NEXT_FORMAT_TO_TRY.find(curFormat); |
| 85 | return it != NEXT_FORMAT_TO_TRY.end() ? it->second : AUDIO_FORMAT_DEFAULT; |
| 86 | } |
| 87 | |
jiabin | 613e6ae | 2022-12-21 20:20:11 +0000 | [diff] [blame^] | 88 | } // namespace |
jiabin | f1c7397 | 2022-04-14 16:28:52 -0700 | [diff] [blame] | 89 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 90 | aaudio_result_t AAudioServiceEndpointMMAP::open(const aaudio::AAudioStreamRequest &request) { |
| 91 | aaudio_result_t result = AAUDIO_OK; |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 92 | copyFrom(request.getConstantConfiguration()); |
Phil Burk | 7bc710b | 2022-09-01 16:57:00 +0000 | [diff] [blame] | 93 | mRequestedDeviceId = getDeviceId(); |
| 94 | |
Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 95 | mMmapClient.attributionSource = request.getAttributionSource(); |
| 96 | // TODO b/182392769: use attribution source util |
| 97 | mMmapClient.attributionSource.uid = VALUE_OR_FATAL( |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 98 | legacy2aidl_uid_t_int32_t(IPCThreadState::self()->getCallingUid())); |
Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 99 | mMmapClient.attributionSource.pid = VALUE_OR_FATAL( |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 100 | legacy2aidl_pid_t_int32_t(IPCThreadState::self()->getCallingPid())); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 101 | |
Phil Burk | 04e805b | 2018-03-27 09:13:53 -0700 | [diff] [blame] | 102 | audio_format_t audioFormat = getFormat(); |
jiabin | f1c7397 | 2022-04-14 16:28:52 -0700 | [diff] [blame] | 103 | std::set<audio_format_t> formatsTried; |
| 104 | while (true) { |
| 105 | if (formatsTried.find(audioFormat) != formatsTried.end()) { |
| 106 | // APM returning something that has already tried. |
| 107 | ALOGW("Have already tried to open #x, but failed before"); |
| 108 | break; |
| 109 | } |
| 110 | formatsTried.insert(audioFormat); |
Phil Burk | 04e805b | 2018-03-27 09:13:53 -0700 | [diff] [blame] | 111 | |
jiabin | f1c7397 | 2022-04-14 16:28:52 -0700 | [diff] [blame] | 112 | audio_format_t nextFormatToTry = AUDIO_FORMAT_DEFAULT; |
| 113 | result = openWithFormat(audioFormat, &nextFormatToTry); |
jiabin | 613e6ae | 2022-12-21 20:20:11 +0000 | [diff] [blame^] | 114 | if (result != AAUDIO_ERROR_UNAVAILABLE) { |
jiabin | f1c7397 | 2022-04-14 16:28:52 -0700 | [diff] [blame] | 115 | // Return if it is successful or there is an error that is not |
| 116 | // AAUDIO_ERROR_UNAVAILABLE happens. |
| 117 | ALOGI("Opened format=%#x with result=%d", audioFormat, result); |
| 118 | break; |
| 119 | } |
Phil Burk | 04e805b | 2018-03-27 09:13:53 -0700 | [diff] [blame] | 120 | |
jiabin | f1c7397 | 2022-04-14 16:28:52 -0700 | [diff] [blame] | 121 | nextFormatToTry = getNextFormatToTry(audioFormat, nextFormatToTry); |
| 122 | ALOGD("%s() %#x failed, perhaps due to format. Try again with %#x", |
| 123 | __func__, audioFormat, nextFormatToTry); |
| 124 | audioFormat = nextFormatToTry; |
| 125 | if (audioFormat == AUDIO_FORMAT_DEFAULT) { |
| 126 | // Nothing else to try |
| 127 | break; |
| 128 | } |
Phil Burk | 04e805b | 2018-03-27 09:13:53 -0700 | [diff] [blame] | 129 | } |
| 130 | return result; |
| 131 | } |
| 132 | |
jiabin | f1c7397 | 2022-04-14 16:28:52 -0700 | [diff] [blame] | 133 | aaudio_result_t AAudioServiceEndpointMMAP::openWithFormat( |
| 134 | audio_format_t audioFormat, audio_format_t* nextFormatToTry) { |
Phil Burk | 04e805b | 2018-03-27 09:13:53 -0700 | [diff] [blame] | 135 | aaudio_result_t result = AAUDIO_OK; |
| 136 | audio_config_base_t config; |
| 137 | audio_port_handle_t deviceId; |
| 138 | |
| 139 | const audio_attributes_t attributes = getAudioAttributesFrom(this); |
| 140 | |
Phil Burk | 7bc710b | 2022-09-01 16:57:00 +0000 | [diff] [blame] | 141 | deviceId = mRequestedDeviceId; |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 142 | |
| 143 | // Fill in config |
Phil Burk | 0127c1b | 2018-03-29 13:48:06 -0700 | [diff] [blame] | 144 | config.format = audioFormat; |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 145 | |
| 146 | int32_t aaudioSampleRate = getSampleRate(); |
| 147 | if (aaudioSampleRate == AAUDIO_UNSPECIFIED) { |
| 148 | aaudioSampleRate = AAUDIO_SAMPLE_RATE_DEFAULT; |
| 149 | } |
| 150 | config.sample_rate = aaudioSampleRate; |
| 151 | |
jiabin | d1f1cb6 | 2020-03-24 11:57:57 -0700 | [diff] [blame] | 152 | const aaudio_direction_t direction = getDirection(); |
| 153 | |
jiabin | a909409 | 2021-06-28 20:36:45 +0000 | [diff] [blame] | 154 | config.channel_mask = AAudio_getChannelMaskForOpen( |
| 155 | getChannelMask(), getSamplesPerFrame(), direction == AAUDIO_DIRECTION_INPUT); |
| 156 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 157 | if (direction == AAUDIO_DIRECTION_OUTPUT) { |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 158 | mHardwareTimeOffsetNanos = OUTPUT_ESTIMATED_HARDWARE_OFFSET_NANOS; // frames at DAC later |
| 159 | |
| 160 | } else if (direction == AAUDIO_DIRECTION_INPUT) { |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 161 | mHardwareTimeOffsetNanos = INPUT_ESTIMATED_HARDWARE_OFFSET_NANOS; // frames at ADC earlier |
| 162 | |
| 163 | } else { |
Phil Burk | 19e990e | 2018-03-22 13:59:34 -0700 | [diff] [blame] | 164 | ALOGE("%s() invalid direction = %d", __func__, direction); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 165 | return AAUDIO_ERROR_ILLEGAL_ARGUMENT; |
| 166 | } |
| 167 | |
jiabin | 613e6ae | 2022-12-21 20:20:11 +0000 | [diff] [blame^] | 168 | const MmapStreamInterface::stream_direction_t streamDirection = |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 169 | (direction == AAUDIO_DIRECTION_OUTPUT) |
| 170 | ? MmapStreamInterface::DIRECTION_OUTPUT |
| 171 | : MmapStreamInterface::DIRECTION_INPUT; |
| 172 | |
jiabin | 613e6ae | 2022-12-21 20:20:11 +0000 | [diff] [blame^] | 173 | const aaudio_session_id_t requestedSessionId = getSessionId(); |
Phil Burk | 4e1af9f | 2018-01-03 15:54:35 -0800 | [diff] [blame] | 174 | audio_session_t sessionId = AAudioConvert_aaudioToAndroidSessionId(requestedSessionId); |
| 175 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 176 | // Open HAL stream. Set mMmapStream |
Phil Burk | 7bc710b | 2022-09-01 16:57:00 +0000 | [diff] [blame] | 177 | ALOGD("%s trying to open MMAP stream with format=%#x, " |
| 178 | "sample_rate=%u, channel_mask=%#x, device=%d", |
| 179 | __func__, config.format, config.sample_rate, |
| 180 | config.channel_mask, deviceId); |
jiabin | 613e6ae | 2022-12-21 20:20:11 +0000 | [diff] [blame^] | 181 | const status_t status = MmapStreamInterface::openMmapStream(streamDirection, |
| 182 | &attributes, |
| 183 | &config, |
| 184 | mMmapClient, |
| 185 | &deviceId, |
| 186 | &sessionId, |
| 187 | this, // callback |
| 188 | mMmapStream, |
| 189 | &mPortHandle); |
Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 190 | ALOGD("%s() mMapClient.attributionSource = %s => portHandle = %d\n", |
| 191 | __func__, mMmapClient.attributionSource.toString().c_str(), mPortHandle); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 192 | if (status != OK) { |
Phil Burk | 29ccc29 | 2019-04-15 08:58:08 -0700 | [diff] [blame] | 193 | // This can happen if the resource is busy or the config does |
| 194 | // not match the hardware. |
jiabin | f1c7397 | 2022-04-14 16:28:52 -0700 | [diff] [blame] | 195 | ALOGD("%s() - openMmapStream() returned status=%d, suggested format=%#x, sample_rate=%u, " |
| 196 | "channel_mask=%#x", |
| 197 | __func__, status, config.format, config.sample_rate, config.format); |
| 198 | *nextFormatToTry = config.format != audioFormat ? config.format |
| 199 | : *nextFormatToTry; |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 200 | return AAUDIO_ERROR_UNAVAILABLE; |
| 201 | } |
| 202 | |
| 203 | if (deviceId == AAUDIO_UNSPECIFIED) { |
Phil Burk | a3901e9 | 2018-10-08 13:54:38 -0700 | [diff] [blame] | 204 | ALOGW("%s() - openMmapStream() failed to set deviceId", __func__); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 205 | } |
| 206 | setDeviceId(deviceId); |
| 207 | |
Phil Burk | 4e1af9f | 2018-01-03 15:54:35 -0800 | [diff] [blame] | 208 | if (sessionId == AUDIO_SESSION_ALLOCATE) { |
Phil Burk | 19e990e | 2018-03-22 13:59:34 -0700 | [diff] [blame] | 209 | ALOGW("%s() - openMmapStream() failed to set sessionId", __func__); |
Phil Burk | 4e1af9f | 2018-01-03 15:54:35 -0800 | [diff] [blame] | 210 | } |
| 211 | |
jiabin | 613e6ae | 2022-12-21 20:20:11 +0000 | [diff] [blame^] | 212 | const aaudio_session_id_t actualSessionId = |
Phil Burk | 4e1af9f | 2018-01-03 15:54:35 -0800 | [diff] [blame] | 213 | (requestedSessionId == AAUDIO_SESSION_ID_NONE) |
| 214 | ? AAUDIO_SESSION_ID_NONE |
| 215 | : (aaudio_session_id_t) sessionId; |
| 216 | setSessionId(actualSessionId); |
Phil Burk | ed782c8 | 2022-02-08 21:43:53 +0000 | [diff] [blame] | 217 | |
| 218 | ALOGD("%s(format = 0x%X) deviceId = %d, sessionId = %d", |
| 219 | __func__, audioFormat, getDeviceId(), getSessionId()); |
Phil Burk | 4e1af9f | 2018-01-03 15:54:35 -0800 | [diff] [blame] | 220 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 221 | // Create MMAP/NOIRQ buffer. |
millerliang | 18d1e6c | 2022-02-08 15:43:40 +0800 | [diff] [blame] | 222 | result = createMmapBuffer(&mAudioDataFileDescriptor); |
| 223 | if (result != AAUDIO_OK) { |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 224 | goto error; |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | // Get information about the stream and pass it back to the caller. |
jiabin | a909409 | 2021-06-28 20:36:45 +0000 | [diff] [blame] | 228 | setChannelMask(AAudioConvert_androidToAAudioChannelMask( |
| 229 | config.channel_mask, getDirection() == AAUDIO_DIRECTION_INPUT, |
| 230 | AAudio_isChannelIndexMask(config.channel_mask))); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 231 | |
Phil Burk | 0127c1b | 2018-03-29 13:48:06 -0700 | [diff] [blame] | 232 | setFormat(config.format); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 233 | setSampleRate(config.sample_rate); |
Robert Wu | 310037a | 2022-09-06 21:48:18 +0000 | [diff] [blame] | 234 | setHardwareSampleRate(getSampleRate()); |
| 235 | setHardwareFormat(getFormat()); |
| 236 | setHardwareSamplesPerFrame(AAudioConvert_channelMaskToCount(getChannelMask())); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 237 | |
jiabin | a5df87b | 2020-12-29 10:45:19 -0800 | [diff] [blame] | 238 | // If the position is not updated while the timestamp is updated for more than a certain amount, |
| 239 | // the timestamp reported from the HAL may not be accurate. Here, a timestamp grace period is |
| 240 | // set as 5 burst size. We may want to update this value if there is any report from OEMs saying |
| 241 | // that is too short. |
| 242 | static constexpr int kTimestampGraceBurstCount = 5; |
| 243 | mTimestampGracePeriodMs = ((int64_t) kTimestampGraceBurstCount * mFramesPerBurst |
| 244 | * AAUDIO_MILLIS_PER_SECOND) / getSampleRate(); |
| 245 | |
Phil Burk | ed782c8 | 2022-02-08 21:43:53 +0000 | [diff] [blame] | 246 | ALOGD("%s() got rate = %d, channels = %d channelMask = %#x, deviceId = %d, capacity = %d\n", |
jiabin | a909409 | 2021-06-28 20:36:45 +0000 | [diff] [blame] | 247 | __func__, getSampleRate(), getSamplesPerFrame(), getChannelMask(), |
| 248 | deviceId, getBufferCapacity()); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 249 | |
Phil Burk | ed782c8 | 2022-02-08 21:43:53 +0000 | [diff] [blame] | 250 | ALOGD("%s() got format = 0x%X = %s, frame size = %d, burst size = %d", |
| 251 | __func__, getFormat(), audio_format_to_string(getFormat()), |
| 252 | calculateBytesPerFrame(), mFramesPerBurst); |
Phil Burk | 0127c1b | 2018-03-29 13:48:06 -0700 | [diff] [blame] | 253 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 254 | return result; |
| 255 | |
| 256 | error: |
| 257 | close(); |
Phil Burk | 7bc710b | 2022-09-01 16:57:00 +0000 | [diff] [blame] | 258 | // restore original requests |
| 259 | setDeviceId(mRequestedDeviceId); |
| 260 | setSessionId(requestedSessionId); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 261 | return result; |
| 262 | } |
| 263 | |
Phil Burk | 320910f | 2020-08-12 14:29:10 +0000 | [diff] [blame] | 264 | void AAudioServiceEndpointMMAP::close() { |
Phil Burk | 6e463ce | 2020-04-13 10:20:20 -0700 | [diff] [blame] | 265 | if (mMmapStream != nullptr) { |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 266 | // Needs to be explicitly cleared or CTS will fail but it is not clear why. |
| 267 | mMmapStream.clear(); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 268 | AudioClock::sleepForNanos(100 * AAUDIO_NANOS_PER_MILLISECOND); |
| 269 | } |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | aaudio_result_t AAudioServiceEndpointMMAP::startStream(sp<AAudioServiceStreamBase> stream, |
Phil Burk | bbd5286 | 2018-04-13 11:37:42 -0700 | [diff] [blame] | 273 | audio_port_handle_t *clientHandle __unused) { |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 274 | // Start the client on behalf of the AAudio service. |
| 275 | // Use the port handle that was provided by openMmapStream(). |
Phil Burk | bbd5286 | 2018-04-13 11:37:42 -0700 | [diff] [blame] | 276 | audio_port_handle_t tempHandle = mPortHandle; |
jiabin | d1f1cb6 | 2020-03-24 11:57:57 -0700 | [diff] [blame] | 277 | audio_attributes_t attr = {}; |
| 278 | if (stream != nullptr) { |
| 279 | attr = getAudioAttributesFrom(stream.get()); |
| 280 | } |
jiabin | 613e6ae | 2022-12-21 20:20:11 +0000 | [diff] [blame^] | 281 | const aaudio_result_t result = startClient( |
jiabin | d1f1cb6 | 2020-03-24 11:57:57 -0700 | [diff] [blame] | 282 | mMmapClient, stream == nullptr ? nullptr : &attr, &tempHandle); |
Phil Burk | bbd5286 | 2018-04-13 11:37:42 -0700 | [diff] [blame] | 283 | // When AudioFlinger is passed a valid port handle then it should not change it. |
| 284 | LOG_ALWAYS_FATAL_IF(tempHandle != mPortHandle, |
| 285 | "%s() port handle not expected to change from %d to %d", |
| 286 | __func__, mPortHandle, tempHandle); |
Phil Burk | 29ccc29 | 2019-04-15 08:58:08 -0700 | [diff] [blame] | 287 | ALOGV("%s() mPortHandle = %d", __func__, mPortHandle); |
Phil Burk | bbd5286 | 2018-04-13 11:37:42 -0700 | [diff] [blame] | 288 | return result; |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 289 | } |
| 290 | |
jiabin | 613e6ae | 2022-12-21 20:20:11 +0000 | [diff] [blame^] | 291 | aaudio_result_t AAudioServiceEndpointMMAP::stopStream(sp<AAudioServiceStreamBase> /*stream*/, |
| 292 | audio_port_handle_t /*clientHandle*/) { |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 293 | mFramesTransferred.reset32(); |
Phil Burk | 73af62a | 2017-10-26 12:11:47 -0700 | [diff] [blame] | 294 | |
| 295 | // Round 64-bit counter up to a multiple of the buffer capacity. |
| 296 | // This is required because the 64-bit counter is used as an index |
| 297 | // into a circular buffer and the actual HW position is reset to zero |
| 298 | // when the stream is stopped. |
| 299 | mFramesTransferred.roundUp64(getBufferCapacity()); |
| 300 | |
Phil Burk | bbd5286 | 2018-04-13 11:37:42 -0700 | [diff] [blame] | 301 | // Use the port handle that was provided by openMmapStream(). |
Phil Burk | 29ccc29 | 2019-04-15 08:58:08 -0700 | [diff] [blame] | 302 | ALOGV("%s() mPortHandle = %d", __func__, mPortHandle); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 303 | return stopClient(mPortHandle); |
| 304 | } |
| 305 | |
| 306 | aaudio_result_t AAudioServiceEndpointMMAP::startClient(const android::AudioClient& client, |
jiabin | d1f1cb6 | 2020-03-24 11:57:57 -0700 | [diff] [blame] | 307 | const audio_attributes_t *attr, |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 308 | audio_port_handle_t *clientHandle) { |
jiabin | 613e6ae | 2022-12-21 20:20:11 +0000 | [diff] [blame^] | 309 | return mMmapStream == nullptr |
| 310 | ? AAUDIO_ERROR_NULL |
| 311 | : AAudioConvert_androidToAAudioResult(mMmapStream->start(client, attr, clientHandle)); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | aaudio_result_t AAudioServiceEndpointMMAP::stopClient(audio_port_handle_t clientHandle) { |
jiabin | 613e6ae | 2022-12-21 20:20:11 +0000 | [diff] [blame^] | 315 | return mMmapStream == nullptr |
| 316 | ? AAUDIO_ERROR_NULL |
| 317 | : AAudioConvert_androidToAAudioResult(mMmapStream->stop(clientHandle)); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 318 | } |
| 319 | |
jiabin | f7f0615 | 2021-11-22 18:10:14 +0000 | [diff] [blame] | 320 | aaudio_result_t AAudioServiceEndpointMMAP::standby() { |
jiabin | 613e6ae | 2022-12-21 20:20:11 +0000 | [diff] [blame^] | 321 | return mMmapStream == nullptr |
| 322 | ? AAUDIO_ERROR_NULL |
| 323 | : AAudioConvert_androidToAAudioResult(mMmapStream->standby()); |
jiabin | f7f0615 | 2021-11-22 18:10:14 +0000 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | aaudio_result_t AAudioServiceEndpointMMAP::exitStandby(AudioEndpointParcelable* parcelable) { |
| 327 | if (mMmapStream == nullptr) { |
| 328 | return AAUDIO_ERROR_NULL; |
| 329 | } |
| 330 | mAudioDataFileDescriptor.reset(); |
jiabin | 613e6ae | 2022-12-21 20:20:11 +0000 | [diff] [blame^] | 331 | const aaudio_result_t result = createMmapBuffer(&mAudioDataFileDescriptor); |
jiabin | f7f0615 | 2021-11-22 18:10:14 +0000 | [diff] [blame] | 332 | if (result == AAUDIO_OK) { |
jiabin | 613e6ae | 2022-12-21 20:20:11 +0000 | [diff] [blame^] | 333 | const int32_t bytesPerFrame = calculateBytesPerFrame(); |
| 334 | const int32_t capacityInBytes = getBufferCapacity() * bytesPerFrame; |
| 335 | const int fdIndex = parcelable->addFileDescriptor( |
| 336 | mAudioDataFileDescriptor, capacityInBytes); |
jiabin | f7f0615 | 2021-11-22 18:10:14 +0000 | [diff] [blame] | 337 | parcelable->mDownDataQueueParcelable.setupMemory(fdIndex, 0, capacityInBytes); |
| 338 | parcelable->mDownDataQueueParcelable.setBytesPerFrame(bytesPerFrame); |
| 339 | parcelable->mDownDataQueueParcelable.setFramesPerBurst(mFramesPerBurst); |
| 340 | parcelable->mDownDataQueueParcelable.setCapacityInFrames(getBufferCapacity()); |
| 341 | } |
| 342 | return result; |
| 343 | } |
| 344 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 345 | // Get free-running DSP or DMA hardware position from the HAL. |
| 346 | aaudio_result_t AAudioServiceEndpointMMAP::getFreeRunningPosition(int64_t *positionFrames, |
| 347 | int64_t *timeNanos) { |
| 348 | struct audio_mmap_position position; |
| 349 | if (mMmapStream == nullptr) { |
| 350 | return AAUDIO_ERROR_NULL; |
| 351 | } |
jiabin | 613e6ae | 2022-12-21 20:20:11 +0000 | [diff] [blame^] | 352 | const status_t status = mMmapStream->getMmapPosition(&position); |
Phil Burk | 19e990e | 2018-03-22 13:59:34 -0700 | [diff] [blame] | 353 | ALOGV("%s() status= %d, pos = %d, nanos = %lld\n", |
| 354 | __func__, status, position.position_frames, (long long) position.time_nanoseconds); |
jiabin | 613e6ae | 2022-12-21 20:20:11 +0000 | [diff] [blame^] | 355 | const aaudio_result_t result = AAudioConvert_androidToAAudioResult(status); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 356 | if (result == AAUDIO_ERROR_UNAVAILABLE) { |
Phil Burk | 19e990e | 2018-03-22 13:59:34 -0700 | [diff] [blame] | 357 | ALOGW("%s(): getMmapPosition() has no position data available", __func__); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 358 | } else if (result != AAUDIO_OK) { |
Phil Burk | 19e990e | 2018-03-22 13:59:34 -0700 | [diff] [blame] | 359 | ALOGE("%s(): getMmapPosition() returned status %d", __func__, status); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 360 | } else { |
| 361 | // Convert 32-bit position to 64-bit position. |
| 362 | mFramesTransferred.update32(position.position_frames); |
| 363 | *positionFrames = mFramesTransferred.get(); |
| 364 | *timeNanos = position.time_nanoseconds; |
| 365 | } |
| 366 | return result; |
| 367 | } |
| 368 | |
jiabin | 613e6ae | 2022-12-21 20:20:11 +0000 | [diff] [blame^] | 369 | aaudio_result_t AAudioServiceEndpointMMAP::getTimestamp(int64_t* /*positionFrames*/, |
| 370 | int64_t* /*timeNanos*/) { |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 371 | return 0; // TODO |
| 372 | } |
| 373 | |
Phil Burk | a77869d | 2020-05-07 10:39:47 -0700 | [diff] [blame] | 374 | // This is called by onTearDown() in a separate thread to avoid deadlocks. |
| 375 | void AAudioServiceEndpointMMAP::handleTearDownAsync(audio_port_handle_t portHandle) { |
Phil Burk | bbd5286 | 2018-04-13 11:37:42 -0700 | [diff] [blame] | 376 | // Are we tearing down the EXCLUSIVE MMAP stream? |
| 377 | if (isStreamRegistered(portHandle)) { |
| 378 | ALOGD("%s(%d) tearing down this entire MMAP endpoint", __func__, portHandle); |
| 379 | disconnectRegisteredStreams(); |
| 380 | } else { |
| 381 | // Must be a SHARED stream? |
| 382 | ALOGD("%s(%d) disconnect a specific stream", __func__, portHandle); |
jiabin | 613e6ae | 2022-12-21 20:20:11 +0000 | [diff] [blame^] | 383 | const aaudio_result_t result = mAAudioService.disconnectStreamByPortHandle(portHandle); |
Phil Burk | bbd5286 | 2018-04-13 11:37:42 -0700 | [diff] [blame] | 384 | ALOGD("%s(%d) disconnectStreamByPortHandle returned %d", __func__, portHandle, result); |
| 385 | } |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 386 | }; |
| 387 | |
Phil Burk | a77869d | 2020-05-07 10:39:47 -0700 | [diff] [blame] | 388 | // This is called by AudioFlinger when it wants to destroy a stream. |
| 389 | void AAudioServiceEndpointMMAP::onTearDown(audio_port_handle_t portHandle) { |
| 390 | ALOGD("%s(portHandle = %d) called", __func__, portHandle); |
jiabin | 613e6ae | 2022-12-21 20:20:11 +0000 | [diff] [blame^] | 391 | const android::sp<AAudioServiceEndpointMMAP> holdEndpoint(this); |
Phil Burk | 3d20194 | 2021-04-08 23:27:04 +0000 | [diff] [blame] | 392 | std::thread asyncTask([holdEndpoint, portHandle]() { |
| 393 | holdEndpoint->handleTearDownAsync(portHandle); |
| 394 | }); |
Phil Burk | a77869d | 2020-05-07 10:39:47 -0700 | [diff] [blame] | 395 | asyncTask.detach(); |
| 396 | } |
| 397 | |
Robert Wu | 4389ae6 | 2022-02-17 18:39:41 +0000 | [diff] [blame] | 398 | void AAudioServiceEndpointMMAP::onVolumeChanged(float volume) { |
| 399 | ALOGD("%s() volume = %f", __func__, volume); |
jiabin | 613e6ae | 2022-12-21 20:20:11 +0000 | [diff] [blame^] | 400 | const std::lock_guard<std::mutex> lock(mLockStreams); |
Chih-Hung Hsieh | 3ef324d | 2018-12-11 11:48:12 -0800 | [diff] [blame] | 401 | for(const auto& stream : mRegisteredStreams) { |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 402 | stream->onVolumeChanged(volume); |
| 403 | } |
| 404 | }; |
| 405 | |
Phil Burk | a77869d | 2020-05-07 10:39:47 -0700 | [diff] [blame] | 406 | void AAudioServiceEndpointMMAP::onRoutingChanged(audio_port_handle_t portHandle) { |
jiabin | 613e6ae | 2022-12-21 20:20:11 +0000 | [diff] [blame^] | 407 | const auto deviceId = static_cast<int32_t>(portHandle); |
Phil Burk | 29ccc29 | 2019-04-15 08:58:08 -0700 | [diff] [blame] | 408 | ALOGD("%s() called with dev %d, old = %d", __func__, deviceId, getDeviceId()); |
Phil Burk | a77869d | 2020-05-07 10:39:47 -0700 | [diff] [blame] | 409 | if (getDeviceId() != deviceId) { |
| 410 | if (getDeviceId() != AUDIO_PORT_HANDLE_NONE) { |
jiabin | 613e6ae | 2022-12-21 20:20:11 +0000 | [diff] [blame^] | 411 | const android::sp<AAudioServiceEndpointMMAP> holdEndpoint(this); |
Phil Burk | 3d20194 | 2021-04-08 23:27:04 +0000 | [diff] [blame] | 412 | std::thread asyncTask([holdEndpoint, deviceId]() { |
| 413 | ALOGD("onRoutingChanged() asyncTask launched"); |
| 414 | holdEndpoint->disconnectRegisteredStreams(); |
| 415 | holdEndpoint->setDeviceId(deviceId); |
Phil Burk | a77869d | 2020-05-07 10:39:47 -0700 | [diff] [blame] | 416 | }); |
| 417 | asyncTask.detach(); |
| 418 | } else { |
| 419 | setDeviceId(deviceId); |
| 420 | } |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 421 | } |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 422 | }; |
| 423 | |
| 424 | /** |
| 425 | * Get an immutable description of the data queue from the HAL. |
| 426 | */ |
jiabin | 2a59462 | 2021-10-14 00:32:25 +0000 | [diff] [blame] | 427 | aaudio_result_t AAudioServiceEndpointMMAP::getDownDataDescription( |
| 428 | AudioEndpointParcelable* parcelable) |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 429 | { |
| 430 | // Gather information on the data queue based on HAL info. |
jiabin | 613e6ae | 2022-12-21 20:20:11 +0000 | [diff] [blame^] | 431 | const int32_t bytesPerFrame = calculateBytesPerFrame(); |
| 432 | const int32_t capacityInBytes = getBufferCapacity() * bytesPerFrame; |
| 433 | const int fdIndex = parcelable->addFileDescriptor(mAudioDataFileDescriptor, capacityInBytes); |
jiabin | 2a59462 | 2021-10-14 00:32:25 +0000 | [diff] [blame] | 434 | parcelable->mDownDataQueueParcelable.setupMemory(fdIndex, 0, capacityInBytes); |
| 435 | parcelable->mDownDataQueueParcelable.setBytesPerFrame(bytesPerFrame); |
| 436 | parcelable->mDownDataQueueParcelable.setFramesPerBurst(mFramesPerBurst); |
| 437 | parcelable->mDownDataQueueParcelable.setCapacityInFrames(getBufferCapacity()); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 438 | return AAUDIO_OK; |
| 439 | } |
jiabin | b7d8c5a | 2020-08-26 17:24:52 -0700 | [diff] [blame] | 440 | |
| 441 | aaudio_result_t AAudioServiceEndpointMMAP::getExternalPosition(uint64_t *positionFrames, |
| 442 | int64_t *timeNanos) |
| 443 | { |
jiabin | a5df87b | 2020-12-29 10:45:19 -0800 | [diff] [blame] | 444 | if (mHalExternalPositionStatus != AAUDIO_OK) { |
| 445 | return mHalExternalPositionStatus; |
jiabin | b7d8c5a | 2020-08-26 17:24:52 -0700 | [diff] [blame] | 446 | } |
jiabin | a5df87b | 2020-12-29 10:45:19 -0800 | [diff] [blame] | 447 | uint64_t tempPositionFrames; |
| 448 | int64_t tempTimeNanos; |
jiabin | 613e6ae | 2022-12-21 20:20:11 +0000 | [diff] [blame^] | 449 | const status_t status = mMmapStream->getExternalPosition(&tempPositionFrames, &tempTimeNanos); |
jiabin | a5df87b | 2020-12-29 10:45:19 -0800 | [diff] [blame] | 450 | if (status != OK) { |
| 451 | // getExternalPosition reports error. The HAL may not support the API. Cache the result |
jiabin | b7d8c5a | 2020-08-26 17:24:52 -0700 | [diff] [blame] | 452 | // so that the call will not go to the HAL next time. |
jiabin | a5df87b | 2020-12-29 10:45:19 -0800 | [diff] [blame] | 453 | mHalExternalPositionStatus = AAudioConvert_androidToAAudioResult(status); |
| 454 | return mHalExternalPositionStatus; |
jiabin | b7d8c5a | 2020-08-26 17:24:52 -0700 | [diff] [blame] | 455 | } |
jiabin | a5df87b | 2020-12-29 10:45:19 -0800 | [diff] [blame] | 456 | |
| 457 | // If the HAL keeps reporting the same position or timestamp, the HAL may be having some issues |
| 458 | // to report correct external position. In that case, we will not trust the values reported from |
| 459 | // the HAL. Ideally, we may want to stop querying external position if the HAL cannot report |
| 460 | // correct position within a period. But it may not be a good idea to get system time too often. |
| 461 | // In that case, a maximum number of frozen external position is defined so that if the |
| 462 | // count of the same timestamp or position is reported by the HAL continuously, the values from |
| 463 | // the HAL will no longer be trusted. |
| 464 | static constexpr int kMaxFrozenCount = 20; |
| 465 | // If the HAL version is less than 7.0, the getPresentationPosition is an optional API. |
| 466 | // If the HAL version is 7.0 or later, the getPresentationPosition is a mandatory API. |
| 467 | // In that case, even the returned status is NO_ERROR, it doesn't indicate the returned |
| 468 | // position is a valid one. Do a simple validation, which is checking if the position is |
| 469 | // forward within half a second or not, here so that this function can return error if |
| 470 | // the validation fails. Note that we don't only apply this validation logic to HAL API |
| 471 | // less than 7.0. The reason is that there is a chance the HAL is not reporting the |
| 472 | // timestamp and position correctly. |
| 473 | if (mLastPositionFrames > tempPositionFrames) { |
| 474 | // If the position is going backwards, there must be something wrong with the HAL. |
| 475 | // In that case, we do not trust the values reported by the HAL. |
| 476 | ALOGW("%s position is going backwards, last position(%jd) current position(%jd)", |
| 477 | __func__, mLastPositionFrames, tempPositionFrames); |
| 478 | mHalExternalPositionStatus = AAUDIO_ERROR_INTERNAL; |
| 479 | return mHalExternalPositionStatus; |
| 480 | } else if (mLastPositionFrames == tempPositionFrames) { |
| 481 | if (tempTimeNanos - mTimestampNanosForLastPosition > |
| 482 | AAUDIO_NANOS_PER_MILLISECOND * mTimestampGracePeriodMs) { |
| 483 | ALOGW("%s, the reported position is not changed within %d msec. " |
| 484 | "Set the external position as not supported", __func__, mTimestampGracePeriodMs); |
| 485 | mHalExternalPositionStatus = AAUDIO_ERROR_INTERNAL; |
| 486 | return mHalExternalPositionStatus; |
| 487 | } |
| 488 | mFrozenPositionCount++; |
| 489 | } else { |
| 490 | mFrozenPositionCount = 0; |
| 491 | } |
| 492 | |
| 493 | if (mTimestampNanosForLastPosition > tempTimeNanos) { |
| 494 | // If the timestamp is going backwards, there must be something wrong with the HAL. |
| 495 | // In that case, we do not trust the values reported by the HAL. |
| 496 | ALOGW("%s timestamp is going backwards, last timestamp(%jd), current timestamp(%jd)", |
| 497 | __func__, mTimestampNanosForLastPosition, tempTimeNanos); |
| 498 | mHalExternalPositionStatus = AAUDIO_ERROR_INTERNAL; |
| 499 | return mHalExternalPositionStatus; |
| 500 | } else if (mTimestampNanosForLastPosition == tempTimeNanos) { |
| 501 | mFrozenTimestampCount++; |
| 502 | } else { |
| 503 | mFrozenTimestampCount = 0; |
| 504 | } |
| 505 | |
| 506 | if (mFrozenTimestampCount + mFrozenPositionCount > kMaxFrozenCount) { |
| 507 | ALOGW("%s too many frozen external position from HAL.", __func__); |
| 508 | mHalExternalPositionStatus = AAUDIO_ERROR_INTERNAL; |
| 509 | return mHalExternalPositionStatus; |
| 510 | } |
| 511 | |
| 512 | mLastPositionFrames = tempPositionFrames; |
| 513 | mTimestampNanosForLastPosition = tempTimeNanos; |
| 514 | |
| 515 | // Only update the timestamp and position when they looks valid. |
| 516 | *positionFrames = tempPositionFrames; |
| 517 | *timeNanos = tempTimeNanos; |
| 518 | return mHalExternalPositionStatus; |
jiabin | b7d8c5a | 2020-08-26 17:24:52 -0700 | [diff] [blame] | 519 | } |
jiabin | f7f0615 | 2021-11-22 18:10:14 +0000 | [diff] [blame] | 520 | |
| 521 | aaudio_result_t AAudioServiceEndpointMMAP::createMmapBuffer( |
| 522 | android::base::unique_fd* fileDescriptor) |
| 523 | { |
| 524 | memset(&mMmapBufferinfo, 0, sizeof(struct audio_mmap_buffer_info)); |
| 525 | int32_t minSizeFrames = getBufferCapacity(); |
| 526 | if (minSizeFrames <= 0) { // zero will get rejected |
| 527 | minSizeFrames = AAUDIO_BUFFER_CAPACITY_MIN; |
| 528 | } |
jiabin | 613e6ae | 2022-12-21 20:20:11 +0000 | [diff] [blame^] | 529 | const status_t status = mMmapStream->createMmapBuffer(minSizeFrames, &mMmapBufferinfo); |
| 530 | const bool isBufferShareable = mMmapBufferinfo.flags & AUDIO_MMAP_APPLICATION_SHAREABLE; |
jiabin | f7f0615 | 2021-11-22 18:10:14 +0000 | [diff] [blame] | 531 | if (status != OK) { |
| 532 | ALOGE("%s() - createMmapBuffer() failed with status %d %s", |
| 533 | __func__, status, strerror(-status)); |
| 534 | return AAUDIO_ERROR_UNAVAILABLE; |
| 535 | } else { |
| 536 | ALOGD("%s() createMmapBuffer() buffer_size = %d fr, burst_size %d fr" |
| 537 | ", Sharable FD: %s", |
| 538 | __func__, |
| 539 | mMmapBufferinfo.buffer_size_frames, |
| 540 | mMmapBufferinfo.burst_size_frames, |
| 541 | isBufferShareable ? "Yes" : "No"); |
| 542 | } |
| 543 | |
| 544 | setBufferCapacity(mMmapBufferinfo.buffer_size_frames); |
| 545 | if (!isBufferShareable) { |
| 546 | // Exclusive mode can only be used by the service because the FD cannot be shared. |
jiabin | 613e6ae | 2022-12-21 20:20:11 +0000 | [diff] [blame^] | 547 | const int32_t audioServiceUid = |
jiabin | f7f0615 | 2021-11-22 18:10:14 +0000 | [diff] [blame] | 548 | VALUE_OR_FATAL(legacy2aidl_uid_t_int32_t(getuid())); |
| 549 | if ((mMmapClient.attributionSource.uid != audioServiceUid) && |
| 550 | getSharingMode() == AAUDIO_SHARING_MODE_EXCLUSIVE) { |
| 551 | ALOGW("%s() - exclusive FD cannot be used by client", __func__); |
| 552 | return AAUDIO_ERROR_UNAVAILABLE; |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | // AAudio creates a copy of this FD and retains ownership of the copy. |
| 557 | // Assume that AudioFlinger will close the original shared_memory_fd. |
| 558 | fileDescriptor->reset(dup(mMmapBufferinfo.shared_memory_fd)); |
| 559 | if (fileDescriptor->get() == -1) { |
| 560 | ALOGE("%s() - could not dup shared_memory_fd", __func__); |
| 561 | return AAUDIO_ERROR_INTERNAL; |
| 562 | } |
| 563 | |
| 564 | // Call to HAL to make sure the transport FD was able to be closed by binder. |
| 565 | // This is a tricky workaround for a problem in Binder. |
| 566 | // TODO:[b/192048842] When that problem is fixed we may be able to remove or change this code. |
| 567 | struct audio_mmap_position position; |
| 568 | mMmapStream->getMmapPosition(&position); |
| 569 | |
| 570 | mFramesPerBurst = mMmapBufferinfo.burst_size_frames; |
| 571 | |
| 572 | return AAUDIO_OK; |
| 573 | } |