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 | |
Phil Burk | bf05e94 | 2023-12-21 00:03:09 +0000 | [diff] [blame] | 40 | #include <com_android_media_aaudio.h> |
| 41 | |
jiabin | 613e6ae | 2022-12-21 20:20:11 +0000 | [diff] [blame] | 42 | #define AAUDIO_BUFFER_CAPACITY_MIN (4 * 512) |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 43 | #define AAUDIO_SAMPLE_RATE_DEFAULT 48000 |
| 44 | |
| 45 | // This is an estimate of the time difference between the HW and the MMAP time. |
| 46 | // TODO Get presentation timestamps from the HAL instead of using these estimates. |
| 47 | #define OUTPUT_ESTIMATED_HARDWARE_OFFSET_NANOS (3 * AAUDIO_NANOS_PER_MILLISECOND) |
| 48 | #define INPUT_ESTIMATED_HARDWARE_OFFSET_NANOS (-1 * AAUDIO_NANOS_PER_MILLISECOND) |
| 49 | |
Robert Wu | d559ba5 | 2023-06-29 00:08:51 +0000 | [diff] [blame] | 50 | #define AAUDIO_MAX_OPEN_ATTEMPTS 10 |
| 51 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 52 | using namespace android; // TODO just import names needed |
| 53 | using namespace aaudio; // TODO just import names needed |
| 54 | |
Phil Burk | bbd5286 | 2018-04-13 11:37:42 -0700 | [diff] [blame] | 55 | AAudioServiceEndpointMMAP::AAudioServiceEndpointMMAP(AAudioService &audioService) |
| 56 | : mMmapStream(nullptr) |
| 57 | , mAAudioService(audioService) {} |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 58 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 59 | std::string AAudioServiceEndpointMMAP::dump() const { |
| 60 | std::stringstream result; |
| 61 | |
| 62 | result << " MMAP: framesTransferred = " << mFramesTransferred.get(); |
| 63 | result << ", HW nanos = " << mHardwareTimeOffsetNanos; |
| 64 | result << ", port handle = " << mPortHandle; |
jiabin | fc791ee | 2023-02-15 19:43:40 +0000 | [diff] [blame] | 65 | result << ", audio data FD = " << mAudioDataWrapper->getDataFileDescriptor(); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 66 | result << "\n"; |
| 67 | |
| 68 | result << " HW Offset Micros: " << |
| 69 | (getHardwareTimeOffsetNanos() |
| 70 | / AAUDIO_NANOS_PER_MICROSECOND) << "\n"; |
| 71 | |
| 72 | result << AAudioServiceEndpoint::dump(); |
| 73 | return result.str(); |
| 74 | } |
| 75 | |
jiabin | f1c7397 | 2022-04-14 16:28:52 -0700 | [diff] [blame] | 76 | namespace { |
| 77 | |
| 78 | const static std::map<audio_format_t, audio_format_t> NEXT_FORMAT_TO_TRY = { |
| 79 | {AUDIO_FORMAT_PCM_FLOAT, AUDIO_FORMAT_PCM_32_BIT}, |
| 80 | {AUDIO_FORMAT_PCM_32_BIT, AUDIO_FORMAT_PCM_24_BIT_PACKED}, |
Robert Wu | c59b4c9 | 2023-11-30 02:10:29 +0000 | [diff] [blame] | 81 | {AUDIO_FORMAT_PCM_24_BIT_PACKED, AUDIO_FORMAT_PCM_8_24_BIT}, |
| 82 | {AUDIO_FORMAT_PCM_8_24_BIT, AUDIO_FORMAT_PCM_16_BIT} |
jiabin | f1c7397 | 2022-04-14 16:28:52 -0700 | [diff] [blame] | 83 | }; |
| 84 | |
Robert Wu | d559ba5 | 2023-06-29 00:08:51 +0000 | [diff] [blame] | 85 | audio_format_t getNextFormatToTry(audio_format_t curFormat) { |
jiabin | f1c7397 | 2022-04-14 16:28:52 -0700 | [diff] [blame] | 86 | const auto it = NEXT_FORMAT_TO_TRY.find(curFormat); |
Robert Wu | d559ba5 | 2023-06-29 00:08:51 +0000 | [diff] [blame] | 87 | return it != NEXT_FORMAT_TO_TRY.end() ? it->second : curFormat; |
jiabin | f1c7397 | 2022-04-14 16:28:52 -0700 | [diff] [blame] | 88 | } |
| 89 | |
Robert Wu | d559ba5 | 2023-06-29 00:08:51 +0000 | [diff] [blame] | 90 | struct configComp { |
| 91 | bool operator() (const audio_config_base_t& lhs, const audio_config_base_t& rhs) const { |
| 92 | if (lhs.sample_rate != rhs.sample_rate) { |
| 93 | return lhs.sample_rate < rhs.sample_rate; |
| 94 | } else if (lhs.channel_mask != rhs.channel_mask) { |
| 95 | return lhs.channel_mask < rhs.channel_mask; |
| 96 | } else { |
| 97 | return lhs.format < rhs.format; |
| 98 | } |
| 99 | } |
| 100 | }; |
| 101 | |
jiabin | 613e6ae | 2022-12-21 20:20:11 +0000 | [diff] [blame] | 102 | } // namespace |
jiabin | f1c7397 | 2022-04-14 16:28:52 -0700 | [diff] [blame] | 103 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 104 | aaudio_result_t AAudioServiceEndpointMMAP::open(const aaudio::AAudioStreamRequest &request) { |
| 105 | aaudio_result_t result = AAUDIO_OK; |
jiabin | fc791ee | 2023-02-15 19:43:40 +0000 | [diff] [blame] | 106 | mAudioDataWrapper = std::make_unique<SharedMemoryWrapper>(); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 107 | copyFrom(request.getConstantConfiguration()); |
Robert Wu | b7f8edc | 2024-11-04 19:54:38 +0000 | [diff] [blame] | 108 | mRequestedDeviceId = android::getFirstDeviceId(getDeviceIds()); |
Phil Burk | 7bc710b | 2022-09-01 16:57:00 +0000 | [diff] [blame] | 109 | |
Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 110 | mMmapClient.attributionSource = request.getAttributionSource(); |
| 111 | // TODO b/182392769: use attribution source util |
| 112 | mMmapClient.attributionSource.uid = VALUE_OR_FATAL( |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 113 | legacy2aidl_uid_t_int32_t(IPCThreadState::self()->getCallingUid())); |
Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 114 | mMmapClient.attributionSource.pid = VALUE_OR_FATAL( |
Philip P. Moltmann | bda4575 | 2020-07-17 16:41:18 -0700 | [diff] [blame] | 115 | legacy2aidl_pid_t_int32_t(IPCThreadState::self()->getCallingPid())); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 116 | |
Phil Burk | 04e805b | 2018-03-27 09:13:53 -0700 | [diff] [blame] | 117 | audio_format_t audioFormat = getFormat(); |
Robert Wu | d559ba5 | 2023-06-29 00:08:51 +0000 | [diff] [blame] | 118 | int32_t sampleRate = getSampleRate(); |
| 119 | if (sampleRate == AAUDIO_UNSPECIFIED) { |
| 120 | sampleRate = AAUDIO_SAMPLE_RATE_DEFAULT; |
| 121 | } |
| 122 | |
| 123 | const aaudio_direction_t direction = getDirection(); |
| 124 | audio_config_base_t config; |
| 125 | config.format = audioFormat; |
| 126 | config.sample_rate = sampleRate; |
| 127 | config.channel_mask = AAudio_getChannelMaskForOpen( |
| 128 | getChannelMask(), getSamplesPerFrame(), direction == AAUDIO_DIRECTION_INPUT); |
| 129 | |
| 130 | std::set<audio_config_base_t, configComp> configsTried; |
| 131 | int32_t numberOfAttempts = 0; |
| 132 | while (numberOfAttempts < AAUDIO_MAX_OPEN_ATTEMPTS) { |
| 133 | if (configsTried.find(config) != configsTried.end()) { |
jiabin | f1c7397 | 2022-04-14 16:28:52 -0700 | [diff] [blame] | 134 | // APM returning something that has already tried. |
Robert Wu | d559ba5 | 2023-06-29 00:08:51 +0000 | [diff] [blame] | 135 | ALOGW("Have already tried to open with format=%#x and sr=%d, but failed before", |
| 136 | config.format, config.sample_rate); |
jiabin | f1c7397 | 2022-04-14 16:28:52 -0700 | [diff] [blame] | 137 | break; |
| 138 | } |
Robert Wu | d559ba5 | 2023-06-29 00:08:51 +0000 | [diff] [blame] | 139 | configsTried.insert(config); |
Phil Burk | 04e805b | 2018-03-27 09:13:53 -0700 | [diff] [blame] | 140 | |
Robert Wu | d559ba5 | 2023-06-29 00:08:51 +0000 | [diff] [blame] | 141 | audio_config_base_t previousConfig = config; |
| 142 | result = openWithConfig(&config); |
jiabin | 613e6ae | 2022-12-21 20:20:11 +0000 | [diff] [blame] | 143 | if (result != AAUDIO_ERROR_UNAVAILABLE) { |
jiabin | f1c7397 | 2022-04-14 16:28:52 -0700 | [diff] [blame] | 144 | // Return if it is successful or there is an error that is not |
| 145 | // AAUDIO_ERROR_UNAVAILABLE happens. |
Robert Wu | d559ba5 | 2023-06-29 00:08:51 +0000 | [diff] [blame] | 146 | ALOGI("Opened format=%#x sr=%d, with result=%d", previousConfig.format, |
| 147 | previousConfig.sample_rate, result); |
jiabin | f1c7397 | 2022-04-14 16:28:52 -0700 | [diff] [blame] | 148 | break; |
| 149 | } |
Phil Burk | 04e805b | 2018-03-27 09:13:53 -0700 | [diff] [blame] | 150 | |
Robert Wu | d559ba5 | 2023-06-29 00:08:51 +0000 | [diff] [blame] | 151 | // Try other formats if the config from APM is the same as our current config. |
| 152 | // Some HALs may report its format support incorrectly. |
Robert Wu | b91f7d2 | 2024-11-20 19:56:16 +0000 | [diff] [blame] | 153 | if ((previousConfig.format == config.format) && |
| 154 | (previousConfig.sample_rate == config.sample_rate)) { |
| 155 | config.format = getNextFormatToTry(config.format); |
jiabin | f1c7397 | 2022-04-14 16:28:52 -0700 | [diff] [blame] | 156 | } |
Robert Wu | d559ba5 | 2023-06-29 00:08:51 +0000 | [diff] [blame] | 157 | |
| 158 | ALOGD("%s() %#x %d failed, perhaps due to format or sample rate. Try again with %#x %d", |
| 159 | __func__, previousConfig.format, previousConfig.sample_rate, config.format, |
| 160 | config.sample_rate); |
| 161 | numberOfAttempts++; |
Phil Burk | 04e805b | 2018-03-27 09:13:53 -0700 | [diff] [blame] | 162 | } |
| 163 | return result; |
| 164 | } |
| 165 | |
Robert Wu | d559ba5 | 2023-06-29 00:08:51 +0000 | [diff] [blame] | 166 | aaudio_result_t AAudioServiceEndpointMMAP::openWithConfig( |
| 167 | audio_config_base_t* config) { |
Phil Burk | 04e805b | 2018-03-27 09:13:53 -0700 | [diff] [blame] | 168 | aaudio_result_t result = AAUDIO_OK; |
Robert Wu | d559ba5 | 2023-06-29 00:08:51 +0000 | [diff] [blame] | 169 | audio_config_base_t currentConfig = *config; |
Robert Wu | b7f8edc | 2024-11-04 19:54:38 +0000 | [diff] [blame] | 170 | android::DeviceIdVector deviceIds; |
Phil Burk | 04e805b | 2018-03-27 09:13:53 -0700 | [diff] [blame] | 171 | |
| 172 | const audio_attributes_t attributes = getAudioAttributesFrom(this); |
| 173 | |
Robert Wu | b7f8edc | 2024-11-04 19:54:38 +0000 | [diff] [blame] | 174 | if (mRequestedDeviceId != AAUDIO_UNSPECIFIED) { |
| 175 | deviceIds.push_back(mRequestedDeviceId); |
| 176 | } |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 177 | |
jiabin | d1f1cb6 | 2020-03-24 11:57:57 -0700 | [diff] [blame] | 178 | const aaudio_direction_t direction = getDirection(); |
| 179 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 180 | if (direction == AAUDIO_DIRECTION_OUTPUT) { |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 181 | mHardwareTimeOffsetNanos = OUTPUT_ESTIMATED_HARDWARE_OFFSET_NANOS; // frames at DAC later |
| 182 | |
| 183 | } else if (direction == AAUDIO_DIRECTION_INPUT) { |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 184 | mHardwareTimeOffsetNanos = INPUT_ESTIMATED_HARDWARE_OFFSET_NANOS; // frames at ADC earlier |
| 185 | |
| 186 | } else { |
Phil Burk | 19e990e | 2018-03-22 13:59:34 -0700 | [diff] [blame] | 187 | ALOGE("%s() invalid direction = %d", __func__, direction); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 188 | return AAUDIO_ERROR_ILLEGAL_ARGUMENT; |
| 189 | } |
| 190 | |
jiabin | 613e6ae | 2022-12-21 20:20:11 +0000 | [diff] [blame] | 191 | const MmapStreamInterface::stream_direction_t streamDirection = |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 192 | (direction == AAUDIO_DIRECTION_OUTPUT) |
| 193 | ? MmapStreamInterface::DIRECTION_OUTPUT |
| 194 | : MmapStreamInterface::DIRECTION_INPUT; |
| 195 | |
jiabin | 613e6ae | 2022-12-21 20:20:11 +0000 | [diff] [blame] | 196 | const aaudio_session_id_t requestedSessionId = getSessionId(); |
Phil Burk | 4e1af9f | 2018-01-03 15:54:35 -0800 | [diff] [blame] | 197 | audio_session_t sessionId = AAudioConvert_aaudioToAndroidSessionId(requestedSessionId); |
| 198 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 199 | // Open HAL stream. Set mMmapStream |
Phil Burk | 7bc710b | 2022-09-01 16:57:00 +0000 | [diff] [blame] | 200 | ALOGD("%s trying to open MMAP stream with format=%#x, " |
Robert Wu | b7f8edc | 2024-11-04 19:54:38 +0000 | [diff] [blame] | 201 | "sample_rate=%u, channel_mask=%#x, device=%s", |
Robert Wu | d559ba5 | 2023-06-29 00:08:51 +0000 | [diff] [blame] | 202 | __func__, config->format, config->sample_rate, |
Robert Wu | b7f8edc | 2024-11-04 19:54:38 +0000 | [diff] [blame] | 203 | config->channel_mask, android::toString(deviceIds).c_str()); |
Robert Wu | aeb1d00 | 2024-10-30 23:19:44 +0000 | [diff] [blame] | 204 | |
Phil Burk | cffd50f | 2024-06-03 23:52:19 +0000 | [diff] [blame] | 205 | const std::lock_guard<std::mutex> lock(mMmapStreamLock); |
jiabin | 613e6ae | 2022-12-21 20:20:11 +0000 | [diff] [blame] | 206 | const status_t status = MmapStreamInterface::openMmapStream(streamDirection, |
| 207 | &attributes, |
Robert Wu | d559ba5 | 2023-06-29 00:08:51 +0000 | [diff] [blame] | 208 | config, |
jiabin | 613e6ae | 2022-12-21 20:20:11 +0000 | [diff] [blame] | 209 | mMmapClient, |
Robert Wu | aeb1d00 | 2024-10-30 23:19:44 +0000 | [diff] [blame] | 210 | &deviceIds, |
jiabin | 613e6ae | 2022-12-21 20:20:11 +0000 | [diff] [blame] | 211 | &sessionId, |
| 212 | this, // callback |
| 213 | mMmapStream, |
| 214 | &mPortHandle); |
Svet Ganov | 3376113 | 2021-05-13 22:51:08 +0000 | [diff] [blame] | 215 | ALOGD("%s() mMapClient.attributionSource = %s => portHandle = %d\n", |
| 216 | __func__, mMmapClient.attributionSource.toString().c_str(), mPortHandle); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 217 | if (status != OK) { |
Phil Burk | 29ccc29 | 2019-04-15 08:58:08 -0700 | [diff] [blame] | 218 | // This can happen if the resource is busy or the config does |
| 219 | // not match the hardware. |
jiabin | f1c7397 | 2022-04-14 16:28:52 -0700 | [diff] [blame] | 220 | ALOGD("%s() - openMmapStream() returned status=%d, suggested format=%#x, sample_rate=%u, " |
| 221 | "channel_mask=%#x", |
Robert Wu | d559ba5 | 2023-06-29 00:08:51 +0000 | [diff] [blame] | 222 | __func__, status, config->format, config->sample_rate, config->channel_mask); |
| 223 | // Keep the channel mask of the current config |
| 224 | config->channel_mask = currentConfig.channel_mask; |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 225 | return AAUDIO_ERROR_UNAVAILABLE; |
| 226 | } |
| 227 | |
Robert Wu | b7f8edc | 2024-11-04 19:54:38 +0000 | [diff] [blame] | 228 | if (deviceIds.empty()) { |
| 229 | ALOGW("%s() - openMmapStream() failed to set deviceIds", __func__); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 230 | } |
Robert Wu | b7f8edc | 2024-11-04 19:54:38 +0000 | [diff] [blame] | 231 | setDeviceIds(deviceIds); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 232 | |
Phil Burk | 4e1af9f | 2018-01-03 15:54:35 -0800 | [diff] [blame] | 233 | if (sessionId == AUDIO_SESSION_ALLOCATE) { |
Phil Burk | 19e990e | 2018-03-22 13:59:34 -0700 | [diff] [blame] | 234 | ALOGW("%s() - openMmapStream() failed to set sessionId", __func__); |
Phil Burk | 4e1af9f | 2018-01-03 15:54:35 -0800 | [diff] [blame] | 235 | } |
| 236 | |
jiabin | 613e6ae | 2022-12-21 20:20:11 +0000 | [diff] [blame] | 237 | const aaudio_session_id_t actualSessionId = |
Phil Burk | 4e1af9f | 2018-01-03 15:54:35 -0800 | [diff] [blame] | 238 | (requestedSessionId == AAUDIO_SESSION_ID_NONE) |
| 239 | ? AAUDIO_SESSION_ID_NONE |
| 240 | : (aaudio_session_id_t) sessionId; |
| 241 | setSessionId(actualSessionId); |
Phil Burk | ed782c8 | 2022-02-08 21:43:53 +0000 | [diff] [blame] | 242 | |
Robert Wu | b7f8edc | 2024-11-04 19:54:38 +0000 | [diff] [blame] | 243 | ALOGD("%s(format = 0x%X) deviceIds = %s, sessionId = %d", |
| 244 | __func__, config->format, toString(getDeviceIds()).c_str(), getSessionId()); |
Phil Burk | 4e1af9f | 2018-01-03 15:54:35 -0800 | [diff] [blame] | 245 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 246 | // Create MMAP/NOIRQ buffer. |
Phil Burk | cffd50f | 2024-06-03 23:52:19 +0000 | [diff] [blame] | 247 | result = createMmapBuffer_l(); |
millerliang | 18d1e6c | 2022-02-08 15:43:40 +0800 | [diff] [blame] | 248 | if (result != AAUDIO_OK) { |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 249 | goto error; |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | // Get information about the stream and pass it back to the caller. |
jiabin | a909409 | 2021-06-28 20:36:45 +0000 | [diff] [blame] | 253 | setChannelMask(AAudioConvert_androidToAAudioChannelMask( |
Robert Wu | d559ba5 | 2023-06-29 00:08:51 +0000 | [diff] [blame] | 254 | config->channel_mask, getDirection() == AAUDIO_DIRECTION_INPUT, |
| 255 | AAudio_isChannelIndexMask(config->channel_mask))); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 256 | |
Robert Wu | d559ba5 | 2023-06-29 00:08:51 +0000 | [diff] [blame] | 257 | setFormat(config->format); |
| 258 | setSampleRate(config->sample_rate); |
Robert Wu | 310037a | 2022-09-06 21:48:18 +0000 | [diff] [blame] | 259 | setHardwareSampleRate(getSampleRate()); |
| 260 | setHardwareFormat(getFormat()); |
| 261 | setHardwareSamplesPerFrame(AAudioConvert_channelMaskToCount(getChannelMask())); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 262 | |
jiabin | a5df87b | 2020-12-29 10:45:19 -0800 | [diff] [blame] | 263 | // If the position is not updated while the timestamp is updated for more than a certain amount, |
| 264 | // the timestamp reported from the HAL may not be accurate. Here, a timestamp grace period is |
| 265 | // set as 5 burst size. We may want to update this value if there is any report from OEMs saying |
| 266 | // that is too short. |
| 267 | static constexpr int kTimestampGraceBurstCount = 5; |
| 268 | mTimestampGracePeriodMs = ((int64_t) kTimestampGraceBurstCount * mFramesPerBurst |
| 269 | * AAUDIO_MILLIS_PER_SECOND) / getSampleRate(); |
| 270 | |
jiabin | fc791ee | 2023-02-15 19:43:40 +0000 | [diff] [blame] | 271 | mDataReportOffsetNanos = ((int64_t)mTimestampGracePeriodMs) * AAUDIO_NANOS_PER_MILLISECOND; |
| 272 | |
Robert Wu | b7f8edc | 2024-11-04 19:54:38 +0000 | [diff] [blame] | 273 | ALOGD("%s() got rate = %d, channels = %d channelMask = %#x, deviceIds = %s, capacity = %d\n", |
jiabin | a909409 | 2021-06-28 20:36:45 +0000 | [diff] [blame] | 274 | __func__, getSampleRate(), getSamplesPerFrame(), getChannelMask(), |
Robert Wu | b7f8edc | 2024-11-04 19:54:38 +0000 | [diff] [blame] | 275 | android::toString(deviceIds).c_str(), getBufferCapacity()); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 276 | |
Phil Burk | ed782c8 | 2022-02-08 21:43:53 +0000 | [diff] [blame] | 277 | ALOGD("%s() got format = 0x%X = %s, frame size = %d, burst size = %d", |
| 278 | __func__, getFormat(), audio_format_to_string(getFormat()), |
| 279 | calculateBytesPerFrame(), mFramesPerBurst); |
Phil Burk | 0127c1b | 2018-03-29 13:48:06 -0700 | [diff] [blame] | 280 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 281 | return result; |
| 282 | |
| 283 | error: |
Phil Burk | cffd50f | 2024-06-03 23:52:19 +0000 | [diff] [blame] | 284 | close_l(); |
Phil Burk | 7bc710b | 2022-09-01 16:57:00 +0000 | [diff] [blame] | 285 | // restore original requests |
Robert Wu | b7f8edc | 2024-11-04 19:54:38 +0000 | [diff] [blame] | 286 | android::DeviceIdVector requestedDeviceIds; |
| 287 | if (mRequestedDeviceId != AAUDIO_UNSPECIFIED) { |
| 288 | requestedDeviceIds.push_back(mRequestedDeviceId); |
| 289 | } |
| 290 | setDeviceIds(requestedDeviceIds); |
Phil Burk | 7bc710b | 2022-09-01 16:57:00 +0000 | [diff] [blame] | 291 | setSessionId(requestedSessionId); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 292 | return result; |
| 293 | } |
| 294 | |
Phil Burk | 320910f | 2020-08-12 14:29:10 +0000 | [diff] [blame] | 295 | void AAudioServiceEndpointMMAP::close() { |
Phil Burk | cffd50f | 2024-06-03 23:52:19 +0000 | [diff] [blame] | 296 | bool closedIt = false; |
| 297 | { |
| 298 | const std::lock_guard<std::mutex> lock(mMmapStreamLock); |
| 299 | closedIt = close_l(); |
| 300 | } |
| 301 | if (closedIt) { |
| 302 | // TODO Why is this needed? |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 303 | AudioClock::sleepForNanos(100 * AAUDIO_NANOS_PER_MILLISECOND); |
| 304 | } |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 305 | } |
| 306 | |
Phil Burk | cffd50f | 2024-06-03 23:52:19 +0000 | [diff] [blame] | 307 | bool AAudioServiceEndpointMMAP::close_l() { // requires mMmapStreamLock |
| 308 | bool closedIt = false; |
| 309 | if (mMmapStream != nullptr) { |
| 310 | // Needs to be explicitly cleared or CTS will fail but it is not clear why. |
| 311 | ALOGD("%s() clear mMmapStream", __func__); |
| 312 | mMmapStream.clear(); |
| 313 | closedIt = true; |
| 314 | } |
| 315 | return closedIt; |
| 316 | } |
| 317 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 318 | aaudio_result_t AAudioServiceEndpointMMAP::startStream(sp<AAudioServiceStreamBase> stream, |
Phil Burk | bbd5286 | 2018-04-13 11:37:42 -0700 | [diff] [blame] | 319 | audio_port_handle_t *clientHandle __unused) { |
Phil Burk | bcc3674 | 2017-08-31 17:24:51 -0700 | [diff] [blame] | 320 | // Start the client on behalf of the AAudio service. |
| 321 | // Use the port handle that was provided by openMmapStream(). |
Phil Burk | bbd5286 | 2018-04-13 11:37:42 -0700 | [diff] [blame] | 322 | audio_port_handle_t tempHandle = mPortHandle; |
jiabin | d1f1cb6 | 2020-03-24 11:57:57 -0700 | [diff] [blame] | 323 | audio_attributes_t attr = {}; |
| 324 | if (stream != nullptr) { |
| 325 | attr = getAudioAttributesFrom(stream.get()); |
| 326 | } |
jiabin | 613e6ae | 2022-12-21 20:20:11 +0000 | [diff] [blame] | 327 | const aaudio_result_t result = startClient( |
jiabin | d1f1cb6 | 2020-03-24 11:57:57 -0700 | [diff] [blame] | 328 | mMmapClient, stream == nullptr ? nullptr : &attr, &tempHandle); |
Phil Burk | bbd5286 | 2018-04-13 11:37:42 -0700 | [diff] [blame] | 329 | // When AudioFlinger is passed a valid port handle then it should not change it. |
| 330 | LOG_ALWAYS_FATAL_IF(tempHandle != mPortHandle, |
| 331 | "%s() port handle not expected to change from %d to %d", |
| 332 | __func__, mPortHandle, tempHandle); |
Phil Burk | 29ccc29 | 2019-04-15 08:58:08 -0700 | [diff] [blame] | 333 | ALOGV("%s() mPortHandle = %d", __func__, mPortHandle); |
Phil Burk | bbd5286 | 2018-04-13 11:37:42 -0700 | [diff] [blame] | 334 | return result; |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 335 | } |
| 336 | |
jiabin | 613e6ae | 2022-12-21 20:20:11 +0000 | [diff] [blame] | 337 | aaudio_result_t AAudioServiceEndpointMMAP::stopStream(sp<AAudioServiceStreamBase> /*stream*/, |
Phil Burk | cffd50f | 2024-06-03 23:52:19 +0000 | [diff] [blame] | 338 | audio_port_handle_t clientHandle) { |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 339 | mFramesTransferred.reset32(); |
Phil Burk | 73af62a | 2017-10-26 12:11:47 -0700 | [diff] [blame] | 340 | |
| 341 | // Round 64-bit counter up to a multiple of the buffer capacity. |
| 342 | // This is required because the 64-bit counter is used as an index |
| 343 | // into a circular buffer and the actual HW position is reset to zero |
| 344 | // when the stream is stopped. |
| 345 | mFramesTransferred.roundUp64(getBufferCapacity()); |
| 346 | |
Phil Burk | bbd5286 | 2018-04-13 11:37:42 -0700 | [diff] [blame] | 347 | // Use the port handle that was provided by openMmapStream(). |
Phil Burk | cffd50f | 2024-06-03 23:52:19 +0000 | [diff] [blame] | 348 | aaudio_result_t result = stopClient(mPortHandle); |
| 349 | ALOGD("%s(%d): called stopClient(%d=mPortHandle), returning %d", __func__, |
| 350 | (int)clientHandle, mPortHandle, result); |
| 351 | return result; |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | aaudio_result_t AAudioServiceEndpointMMAP::startClient(const android::AudioClient& client, |
jiabin | d1f1cb6 | 2020-03-24 11:57:57 -0700 | [diff] [blame] | 355 | const audio_attributes_t *attr, |
Phil Burk | cffd50f | 2024-06-03 23:52:19 +0000 | [diff] [blame] | 356 | audio_port_handle_t *portHandlePtr) { |
| 357 | const std::lock_guard<std::mutex> lock(mMmapStreamLock); |
| 358 | if (mMmapStream == nullptr) { |
| 359 | ALOGW("%s(): called after mMmapStream set to NULL", __func__); |
| 360 | return AAUDIO_ERROR_NULL; |
| 361 | } else if (!isConnected()) { |
| 362 | ALOGD("%s(): MMAP stream was disconnected", __func__); |
| 363 | return AAUDIO_ERROR_DISCONNECTED; |
| 364 | } else { |
| 365 | aaudio_result_t result = AAudioConvert_androidToAAudioResult( |
| 366 | mMmapStream->start(client, attr, portHandlePtr)); |
| 367 | if (!isConnected() && (portHandlePtr != nullptr)) { |
| 368 | ALOGD("%s(): MMAP stream DISCONNECTED after starting port %d, will stop it", |
| 369 | __func__, *portHandlePtr); |
| 370 | mMmapStream->stop(*portHandlePtr); |
| 371 | *portHandlePtr = AUDIO_PORT_HANDLE_NONE; |
| 372 | result = AAUDIO_ERROR_DISCONNECTED; |
| 373 | } |
| 374 | ALOGD("%s(): returning port %d, result %d", __func__, |
| 375 | (portHandlePtr == nullptr) ? -1 : *portHandlePtr, result); |
| 376 | return result; |
| 377 | } |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 378 | } |
| 379 | |
Phil Burk | cffd50f | 2024-06-03 23:52:19 +0000 | [diff] [blame] | 380 | aaudio_result_t AAudioServiceEndpointMMAP::stopClient(audio_port_handle_t portHandle) { |
| 381 | const std::lock_guard<std::mutex> lock(mMmapStreamLock); |
| 382 | if (mMmapStream == nullptr) { |
| 383 | ALOGE("%s(%d): called after mMmapStream set to NULL", __func__, (int)portHandle); |
| 384 | return AAUDIO_ERROR_NULL; |
| 385 | } else { |
| 386 | aaudio_result_t result = AAudioConvert_androidToAAudioResult( |
| 387 | mMmapStream->stop(portHandle)); |
| 388 | ALOGD("%s(%d): returning %d", __func__, (int)portHandle, result); |
| 389 | return result; |
| 390 | } |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 391 | } |
| 392 | |
jiabin | f7f0615 | 2021-11-22 18:10:14 +0000 | [diff] [blame] | 393 | aaudio_result_t AAudioServiceEndpointMMAP::standby() { |
Phil Burk | cffd50f | 2024-06-03 23:52:19 +0000 | [diff] [blame] | 394 | const std::lock_guard<std::mutex> lock(mMmapStreamLock); |
| 395 | if (mMmapStream == nullptr) { |
| 396 | ALOGW("%s(): called after mMmapStream set to NULL", __func__); |
| 397 | return AAUDIO_ERROR_NULL; |
| 398 | } else { |
| 399 | return AAudioConvert_androidToAAudioResult(mMmapStream->standby()); |
| 400 | } |
jiabin | f7f0615 | 2021-11-22 18:10:14 +0000 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | aaudio_result_t AAudioServiceEndpointMMAP::exitStandby(AudioEndpointParcelable* parcelable) { |
Phil Burk | cffd50f | 2024-06-03 23:52:19 +0000 | [diff] [blame] | 404 | const std::lock_guard<std::mutex> lock(mMmapStreamLock); |
jiabin | f7f0615 | 2021-11-22 18:10:14 +0000 | [diff] [blame] | 405 | if (mMmapStream == nullptr) { |
| 406 | return AAUDIO_ERROR_NULL; |
| 407 | } |
jiabin | fc791ee | 2023-02-15 19:43:40 +0000 | [diff] [blame] | 408 | mAudioDataWrapper->reset(); |
Phil Burk | cffd50f | 2024-06-03 23:52:19 +0000 | [diff] [blame] | 409 | const aaudio_result_t result = createMmapBuffer_l(); |
jiabin | f7f0615 | 2021-11-22 18:10:14 +0000 | [diff] [blame] | 410 | if (result == AAUDIO_OK) { |
jiabin | fc791ee | 2023-02-15 19:43:40 +0000 | [diff] [blame] | 411 | getDownDataDescription(parcelable); |
jiabin | f7f0615 | 2021-11-22 18:10:14 +0000 | [diff] [blame] | 412 | } |
| 413 | return result; |
| 414 | } |
| 415 | |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 416 | // Get free-running DSP or DMA hardware position from the HAL. |
| 417 | aaudio_result_t AAudioServiceEndpointMMAP::getFreeRunningPosition(int64_t *positionFrames, |
| 418 | int64_t *timeNanos) { |
Phil Burk | cffd50f | 2024-06-03 23:52:19 +0000 | [diff] [blame] | 419 | const std::lock_guard<std::mutex> lock(mMmapStreamLock); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 420 | if (mMmapStream == nullptr) { |
Phil Burk | cffd50f | 2024-06-03 23:52:19 +0000 | [diff] [blame] | 421 | ALOGW("%s(): called after mMmapStream set to NULL", __func__); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 422 | return AAUDIO_ERROR_NULL; |
| 423 | } |
Phil Burk | cffd50f | 2024-06-03 23:52:19 +0000 | [diff] [blame] | 424 | struct audio_mmap_position position; |
Phil Burk | ed89641 | 2024-11-05 06:23:06 +0000 | [diff] [blame] | 425 | status_t status = mMmapStream->getMmapPosition(&position); |
Phil Burk | 19e990e | 2018-03-22 13:59:34 -0700 | [diff] [blame] | 426 | ALOGV("%s() status= %d, pos = %d, nanos = %lld\n", |
| 427 | __func__, status, position.position_frames, (long long) position.time_nanoseconds); |
Phil Burk | ed89641 | 2024-11-05 06:23:06 +0000 | [diff] [blame] | 428 | if (status == INVALID_OPERATION) { |
| 429 | // The HAL can return INVALID_OPERATION when the position is UNKNOWN. |
| 430 | // That can cause SHARED MMAP to break. So coerce it to NOT_ENOUGH_DATA. |
| 431 | // That will get converted to AAUDIO_ERROR_UNAVAILABLE. |
| 432 | ALOGW("%s(): change INVALID_OPERATION to NOT_ENOUGH_DATA", __func__); |
| 433 | status = NOT_ENOUGH_DATA; // see b/376467258 |
| 434 | } |
| 435 | |
jiabin | 613e6ae | 2022-12-21 20:20:11 +0000 | [diff] [blame] | 436 | const aaudio_result_t result = AAudioConvert_androidToAAudioResult(status); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 437 | if (result == AAUDIO_ERROR_UNAVAILABLE) { |
Phil Burk | 19e990e | 2018-03-22 13:59:34 -0700 | [diff] [blame] | 438 | ALOGW("%s(): getMmapPosition() has no position data available", __func__); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 439 | } else if (result != AAUDIO_OK) { |
Phil Burk | 19e990e | 2018-03-22 13:59:34 -0700 | [diff] [blame] | 440 | ALOGE("%s(): getMmapPosition() returned status %d", __func__, status); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 441 | } else { |
| 442 | // Convert 32-bit position to 64-bit position. |
| 443 | mFramesTransferred.update32(position.position_frames); |
| 444 | *positionFrames = mFramesTransferred.get(); |
| 445 | *timeNanos = position.time_nanoseconds; |
| 446 | } |
| 447 | return result; |
| 448 | } |
| 449 | |
jiabin | 613e6ae | 2022-12-21 20:20:11 +0000 | [diff] [blame] | 450 | aaudio_result_t AAudioServiceEndpointMMAP::getTimestamp(int64_t* /*positionFrames*/, |
| 451 | int64_t* /*timeNanos*/) { |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 452 | return 0; // TODO |
| 453 | } |
| 454 | |
Phil Burk | a77869d | 2020-05-07 10:39:47 -0700 | [diff] [blame] | 455 | // This is called by onTearDown() in a separate thread to avoid deadlocks. |
| 456 | void AAudioServiceEndpointMMAP::handleTearDownAsync(audio_port_handle_t portHandle) { |
Phil Burk | bbd5286 | 2018-04-13 11:37:42 -0700 | [diff] [blame] | 457 | // Are we tearing down the EXCLUSIVE MMAP stream? |
| 458 | if (isStreamRegistered(portHandle)) { |
| 459 | ALOGD("%s(%d) tearing down this entire MMAP endpoint", __func__, portHandle); |
| 460 | disconnectRegisteredStreams(); |
| 461 | } else { |
| 462 | // Must be a SHARED stream? |
| 463 | ALOGD("%s(%d) disconnect a specific stream", __func__, portHandle); |
jiabin | 613e6ae | 2022-12-21 20:20:11 +0000 | [diff] [blame] | 464 | const aaudio_result_t result = mAAudioService.disconnectStreamByPortHandle(portHandle); |
Phil Burk | bbd5286 | 2018-04-13 11:37:42 -0700 | [diff] [blame] | 465 | ALOGD("%s(%d) disconnectStreamByPortHandle returned %d", __func__, portHandle, result); |
| 466 | } |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 467 | }; |
| 468 | |
Phil Burk | a77869d | 2020-05-07 10:39:47 -0700 | [diff] [blame] | 469 | // This is called by AudioFlinger when it wants to destroy a stream. |
| 470 | void AAudioServiceEndpointMMAP::onTearDown(audio_port_handle_t portHandle) { |
| 471 | ALOGD("%s(portHandle = %d) called", __func__, portHandle); |
jiabin | 613e6ae | 2022-12-21 20:20:11 +0000 | [diff] [blame] | 472 | const android::sp<AAudioServiceEndpointMMAP> holdEndpoint(this); |
Phil Burk | 3d20194 | 2021-04-08 23:27:04 +0000 | [diff] [blame] | 473 | std::thread asyncTask([holdEndpoint, portHandle]() { |
| 474 | holdEndpoint->handleTearDownAsync(portHandle); |
| 475 | }); |
Phil Burk | a77869d | 2020-05-07 10:39:47 -0700 | [diff] [blame] | 476 | asyncTask.detach(); |
| 477 | } |
| 478 | |
Robert Wu | 4389ae6 | 2022-02-17 18:39:41 +0000 | [diff] [blame] | 479 | void AAudioServiceEndpointMMAP::onVolumeChanged(float volume) { |
| 480 | ALOGD("%s() volume = %f", __func__, volume); |
jiabin | 613e6ae | 2022-12-21 20:20:11 +0000 | [diff] [blame] | 481 | const std::lock_guard<std::mutex> lock(mLockStreams); |
Chih-Hung Hsieh | 3ef324d | 2018-12-11 11:48:12 -0800 | [diff] [blame] | 482 | for(const auto& stream : mRegisteredStreams) { |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 483 | stream->onVolumeChanged(volume); |
| 484 | } |
| 485 | }; |
| 486 | |
Robert Wu | aeb1d00 | 2024-10-30 23:19:44 +0000 | [diff] [blame] | 487 | void AAudioServiceEndpointMMAP::onRoutingChanged(const android::DeviceIdVector& deviceIds) { |
Robert Wu | b7f8edc | 2024-11-04 19:54:38 +0000 | [diff] [blame] | 488 | ALOGD("%s() called with dev %s, old = %s", __func__, android::toString(deviceIds).c_str(), |
| 489 | android::toString(getDeviceIds()).c_str()); |
| 490 | if (!android::areDeviceIdsEqual(getDeviceIds(), deviceIds)) { |
| 491 | if (!getDeviceIds().empty()) { |
jiabin | d7ff88a | 2023-12-04 18:40:26 +0000 | [diff] [blame] | 492 | // When there is a routing changed, mmap stream should be disconnected. Set `mConnected` |
Robert Wu | b7f8edc | 2024-11-04 19:54:38 +0000 | [diff] [blame] | 493 | // as false here so that there won't be a new stream connected to this endpoint. |
jiabin | d7ff88a | 2023-12-04 18:40:26 +0000 | [diff] [blame] | 494 | mConnected.store(false); |
jiabin | 613e6ae | 2022-12-21 20:20:11 +0000 | [diff] [blame] | 495 | const android::sp<AAudioServiceEndpointMMAP> holdEndpoint(this); |
Robert Wu | b7f8edc | 2024-11-04 19:54:38 +0000 | [diff] [blame] | 496 | std::thread asyncTask([holdEndpoint, deviceIds]() { |
Phil Burk | 3d20194 | 2021-04-08 23:27:04 +0000 | [diff] [blame] | 497 | ALOGD("onRoutingChanged() asyncTask launched"); |
jiabin | d7ff88a | 2023-12-04 18:40:26 +0000 | [diff] [blame] | 498 | // When routing changed, the stream is disconnected and cannot be used except for |
| 499 | // closing. In that case, it should be safe to release all registered streams. |
| 500 | // This can help release service side resource in case the client doesn't close |
| 501 | // the stream after receiving disconnect event. |
| 502 | holdEndpoint->releaseRegisteredStreams(); |
Robert Wu | b7f8edc | 2024-11-04 19:54:38 +0000 | [diff] [blame] | 503 | holdEndpoint->setDeviceIds(deviceIds); |
Phil Burk | a77869d | 2020-05-07 10:39:47 -0700 | [diff] [blame] | 504 | }); |
| 505 | asyncTask.detach(); |
| 506 | } else { |
Robert Wu | b7f8edc | 2024-11-04 19:54:38 +0000 | [diff] [blame] | 507 | setDeviceIds(deviceIds); |
Phil Burk | a77869d | 2020-05-07 10:39:47 -0700 | [diff] [blame] | 508 | } |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 509 | } |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 510 | }; |
| 511 | |
| 512 | /** |
| 513 | * Get an immutable description of the data queue from the HAL. |
| 514 | */ |
jiabin | 2a59462 | 2021-10-14 00:32:25 +0000 | [diff] [blame] | 515 | aaudio_result_t AAudioServiceEndpointMMAP::getDownDataDescription( |
| 516 | AudioEndpointParcelable* parcelable) |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 517 | { |
jiabin | fc791ee | 2023-02-15 19:43:40 +0000 | [diff] [blame] | 518 | if (mAudioDataWrapper->setupFifoBuffer(calculateBytesPerFrame(), getBufferCapacity()) |
| 519 | != AAUDIO_OK) { |
| 520 | ALOGE("Failed to setup audio data wrapper, will not be able to " |
| 521 | "set data for sound dose computation"); |
| 522 | // This will not affect the audio processing capability |
| 523 | } |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 524 | // Gather information on the data queue based on HAL info. |
jiabin | fc791ee | 2023-02-15 19:43:40 +0000 | [diff] [blame] | 525 | mAudioDataWrapper->fillParcelable(parcelable, parcelable->mDownDataQueueParcelable, |
| 526 | calculateBytesPerFrame(), mFramesPerBurst, |
| 527 | getBufferCapacity(), |
| 528 | getDirection() == AAUDIO_DIRECTION_OUTPUT |
| 529 | ? SharedMemoryWrapper::WRITE |
| 530 | : SharedMemoryWrapper::NONE); |
Phil Burk | 39f02dd | 2017-08-04 09:13:31 -0700 | [diff] [blame] | 531 | return AAUDIO_OK; |
| 532 | } |
jiabin | b7d8c5a | 2020-08-26 17:24:52 -0700 | [diff] [blame] | 533 | |
| 534 | aaudio_result_t AAudioServiceEndpointMMAP::getExternalPosition(uint64_t *positionFrames, |
| 535 | int64_t *timeNanos) |
| 536 | { |
Phil Burk | cffd50f | 2024-06-03 23:52:19 +0000 | [diff] [blame] | 537 | const std::lock_guard<std::mutex> lock(mMmapStreamLock); |
jiabin | a5df87b | 2020-12-29 10:45:19 -0800 | [diff] [blame] | 538 | if (mHalExternalPositionStatus != AAUDIO_OK) { |
| 539 | return mHalExternalPositionStatus; |
jiabin | b7d8c5a | 2020-08-26 17:24:52 -0700 | [diff] [blame] | 540 | } |
Phil Burk | cffd50f | 2024-06-03 23:52:19 +0000 | [diff] [blame] | 541 | if (mMmapStream == nullptr) { |
| 542 | ALOGW("%s(): called after mMmapStream set to NULL", __func__); |
| 543 | return AAUDIO_ERROR_NULL; |
| 544 | } |
jiabin | a5df87b | 2020-12-29 10:45:19 -0800 | [diff] [blame] | 545 | uint64_t tempPositionFrames; |
| 546 | int64_t tempTimeNanos; |
jiabin | 613e6ae | 2022-12-21 20:20:11 +0000 | [diff] [blame] | 547 | const status_t status = mMmapStream->getExternalPosition(&tempPositionFrames, &tempTimeNanos); |
jiabin | a5df87b | 2020-12-29 10:45:19 -0800 | [diff] [blame] | 548 | if (status != OK) { |
| 549 | // getExternalPosition reports error. The HAL may not support the API. Cache the result |
jiabin | b7d8c5a | 2020-08-26 17:24:52 -0700 | [diff] [blame] | 550 | // so that the call will not go to the HAL next time. |
jiabin | a5df87b | 2020-12-29 10:45:19 -0800 | [diff] [blame] | 551 | mHalExternalPositionStatus = AAudioConvert_androidToAAudioResult(status); |
| 552 | return mHalExternalPositionStatus; |
jiabin | b7d8c5a | 2020-08-26 17:24:52 -0700 | [diff] [blame] | 553 | } |
jiabin | a5df87b | 2020-12-29 10:45:19 -0800 | [diff] [blame] | 554 | |
| 555 | // If the HAL keeps reporting the same position or timestamp, the HAL may be having some issues |
| 556 | // to report correct external position. In that case, we will not trust the values reported from |
| 557 | // the HAL. Ideally, we may want to stop querying external position if the HAL cannot report |
| 558 | // correct position within a period. But it may not be a good idea to get system time too often. |
| 559 | // In that case, a maximum number of frozen external position is defined so that if the |
| 560 | // count of the same timestamp or position is reported by the HAL continuously, the values from |
| 561 | // the HAL will no longer be trusted. |
| 562 | static constexpr int kMaxFrozenCount = 20; |
| 563 | // If the HAL version is less than 7.0, the getPresentationPosition is an optional API. |
| 564 | // If the HAL version is 7.0 or later, the getPresentationPosition is a mandatory API. |
| 565 | // In that case, even the returned status is NO_ERROR, it doesn't indicate the returned |
| 566 | // position is a valid one. Do a simple validation, which is checking if the position is |
| 567 | // forward within half a second or not, here so that this function can return error if |
| 568 | // the validation fails. Note that we don't only apply this validation logic to HAL API |
| 569 | // less than 7.0. The reason is that there is a chance the HAL is not reporting the |
| 570 | // timestamp and position correctly. |
| 571 | if (mLastPositionFrames > tempPositionFrames) { |
| 572 | // If the position is going backwards, there must be something wrong with the HAL. |
| 573 | // In that case, we do not trust the values reported by the HAL. |
| 574 | ALOGW("%s position is going backwards, last position(%jd) current position(%jd)", |
| 575 | __func__, mLastPositionFrames, tempPositionFrames); |
| 576 | mHalExternalPositionStatus = AAUDIO_ERROR_INTERNAL; |
| 577 | return mHalExternalPositionStatus; |
| 578 | } else if (mLastPositionFrames == tempPositionFrames) { |
| 579 | if (tempTimeNanos - mTimestampNanosForLastPosition > |
| 580 | AAUDIO_NANOS_PER_MILLISECOND * mTimestampGracePeriodMs) { |
| 581 | ALOGW("%s, the reported position is not changed within %d msec. " |
| 582 | "Set the external position as not supported", __func__, mTimestampGracePeriodMs); |
| 583 | mHalExternalPositionStatus = AAUDIO_ERROR_INTERNAL; |
| 584 | return mHalExternalPositionStatus; |
| 585 | } |
| 586 | mFrozenPositionCount++; |
| 587 | } else { |
| 588 | mFrozenPositionCount = 0; |
| 589 | } |
| 590 | |
| 591 | if (mTimestampNanosForLastPosition > tempTimeNanos) { |
| 592 | // If the timestamp is going backwards, there must be something wrong with the HAL. |
| 593 | // In that case, we do not trust the values reported by the HAL. |
| 594 | ALOGW("%s timestamp is going backwards, last timestamp(%jd), current timestamp(%jd)", |
| 595 | __func__, mTimestampNanosForLastPosition, tempTimeNanos); |
| 596 | mHalExternalPositionStatus = AAUDIO_ERROR_INTERNAL; |
| 597 | return mHalExternalPositionStatus; |
| 598 | } else if (mTimestampNanosForLastPosition == tempTimeNanos) { |
| 599 | mFrozenTimestampCount++; |
| 600 | } else { |
| 601 | mFrozenTimestampCount = 0; |
| 602 | } |
| 603 | |
| 604 | if (mFrozenTimestampCount + mFrozenPositionCount > kMaxFrozenCount) { |
| 605 | ALOGW("%s too many frozen external position from HAL.", __func__); |
| 606 | mHalExternalPositionStatus = AAUDIO_ERROR_INTERNAL; |
| 607 | return mHalExternalPositionStatus; |
| 608 | } |
| 609 | |
| 610 | mLastPositionFrames = tempPositionFrames; |
| 611 | mTimestampNanosForLastPosition = tempTimeNanos; |
| 612 | |
| 613 | // Only update the timestamp and position when they looks valid. |
| 614 | *positionFrames = tempPositionFrames; |
| 615 | *timeNanos = tempTimeNanos; |
| 616 | return mHalExternalPositionStatus; |
jiabin | b7d8c5a | 2020-08-26 17:24:52 -0700 | [diff] [blame] | 617 | } |
jiabin | f7f0615 | 2021-11-22 18:10:14 +0000 | [diff] [blame] | 618 | |
Phil Burk | cffd50f | 2024-06-03 23:52:19 +0000 | [diff] [blame] | 619 | // mMmapStreamLock should be held when calling this function. |
| 620 | aaudio_result_t AAudioServiceEndpointMMAP::createMmapBuffer_l() |
jiabin | f7f0615 | 2021-11-22 18:10:14 +0000 | [diff] [blame] | 621 | { |
| 622 | memset(&mMmapBufferinfo, 0, sizeof(struct audio_mmap_buffer_info)); |
| 623 | int32_t minSizeFrames = getBufferCapacity(); |
| 624 | if (minSizeFrames <= 0) { // zero will get rejected |
| 625 | minSizeFrames = AAUDIO_BUFFER_CAPACITY_MIN; |
| 626 | } |
Phil Burk | cffd50f | 2024-06-03 23:52:19 +0000 | [diff] [blame] | 627 | |
| 628 | if (mMmapStream == nullptr) { |
| 629 | ALOGW("%s(): called after mMmapStream set to NULL", __func__); |
| 630 | return AAUDIO_ERROR_NULL; |
| 631 | } |
| 632 | |
jiabin | 613e6ae | 2022-12-21 20:20:11 +0000 | [diff] [blame] | 633 | const status_t status = mMmapStream->createMmapBuffer(minSizeFrames, &mMmapBufferinfo); |
| 634 | const bool isBufferShareable = mMmapBufferinfo.flags & AUDIO_MMAP_APPLICATION_SHAREABLE; |
jiabin | f7f0615 | 2021-11-22 18:10:14 +0000 | [diff] [blame] | 635 | if (status != OK) { |
| 636 | ALOGE("%s() - createMmapBuffer() failed with status %d %s", |
| 637 | __func__, status, strerror(-status)); |
| 638 | return AAUDIO_ERROR_UNAVAILABLE; |
| 639 | } else { |
| 640 | ALOGD("%s() createMmapBuffer() buffer_size = %d fr, burst_size %d fr" |
| 641 | ", Sharable FD: %s", |
| 642 | __func__, |
| 643 | mMmapBufferinfo.buffer_size_frames, |
| 644 | mMmapBufferinfo.burst_size_frames, |
| 645 | isBufferShareable ? "Yes" : "No"); |
| 646 | } |
| 647 | |
| 648 | setBufferCapacity(mMmapBufferinfo.buffer_size_frames); |
| 649 | if (!isBufferShareable) { |
| 650 | // 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] | 651 | const int32_t audioServiceUid = |
jiabin | f7f0615 | 2021-11-22 18:10:14 +0000 | [diff] [blame] | 652 | VALUE_OR_FATAL(legacy2aidl_uid_t_int32_t(getuid())); |
| 653 | if ((mMmapClient.attributionSource.uid != audioServiceUid) && |
| 654 | getSharingMode() == AAUDIO_SHARING_MODE_EXCLUSIVE) { |
| 655 | ALOGW("%s() - exclusive FD cannot be used by client", __func__); |
| 656 | return AAUDIO_ERROR_UNAVAILABLE; |
| 657 | } |
| 658 | } |
| 659 | |
| 660 | // AAudio creates a copy of this FD and retains ownership of the copy. |
| 661 | // Assume that AudioFlinger will close the original shared_memory_fd. |
jiabin | fc791ee | 2023-02-15 19:43:40 +0000 | [diff] [blame] | 662 | |
| 663 | mAudioDataWrapper->getDataFileDescriptor().reset(dup(mMmapBufferinfo.shared_memory_fd)); |
| 664 | if (mAudioDataWrapper->getDataFileDescriptor().get() == -1) { |
jiabin | f7f0615 | 2021-11-22 18:10:14 +0000 | [diff] [blame] | 665 | ALOGE("%s() - could not dup shared_memory_fd", __func__); |
| 666 | return AAUDIO_ERROR_INTERNAL; |
| 667 | } |
| 668 | |
| 669 | // Call to HAL to make sure the transport FD was able to be closed by binder. |
| 670 | // This is a tricky workaround for a problem in Binder. |
| 671 | // TODO:[b/192048842] When that problem is fixed we may be able to remove or change this code. |
Phil Burk | cffd50f | 2024-06-03 23:52:19 +0000 | [diff] [blame] | 672 | ALOGD("%s() - call getMmapPosition() as a hack to clear FD stuck in Binder", __func__); |
jiabin | f7f0615 | 2021-11-22 18:10:14 +0000 | [diff] [blame] | 673 | struct audio_mmap_position position; |
| 674 | mMmapStream->getMmapPosition(&position); |
| 675 | |
| 676 | mFramesPerBurst = mMmapBufferinfo.burst_size_frames; |
| 677 | |
| 678 | return AAUDIO_OK; |
| 679 | } |
jiabin | fc791ee | 2023-02-15 19:43:40 +0000 | [diff] [blame] | 680 | |
| 681 | int64_t AAudioServiceEndpointMMAP::nextDataReportTime() { |
| 682 | return getDirection() == AAUDIO_DIRECTION_OUTPUT |
| 683 | ? AudioClock::getNanoseconds() + mDataReportOffsetNanos |
| 684 | : std::numeric_limits<int64_t>::max(); |
| 685 | } |
| 686 | |
| 687 | void AAudioServiceEndpointMMAP::reportData() { |
Phil Burk | cffd50f | 2024-06-03 23:52:19 +0000 | [diff] [blame] | 688 | const std::lock_guard<std::mutex> lock(mMmapStreamLock); |
| 689 | |
jiabin | fc791ee | 2023-02-15 19:43:40 +0000 | [diff] [blame] | 690 | if (mMmapStream == nullptr) { |
| 691 | // This must not happen |
| 692 | ALOGE("%s() invalid state, mmap stream is not initialized", __func__); |
| 693 | return; |
| 694 | } |
Phil Burk | cffd50f | 2024-06-03 23:52:19 +0000 | [diff] [blame] | 695 | |
jiabin | fc791ee | 2023-02-15 19:43:40 +0000 | [diff] [blame] | 696 | auto fifo = mAudioDataWrapper->getFifoBuffer(); |
| 697 | if (fifo == nullptr) { |
| 698 | ALOGE("%s() fifo buffer is not initialized, cannot report data", __func__); |
| 699 | return; |
| 700 | } |
| 701 | |
| 702 | WrappingBuffer wrappingBuffer; |
| 703 | fifo_frames_t framesAvailable = fifo->getFullDataAvailable(&wrappingBuffer); |
| 704 | for (size_t i = 0; i < WrappingBuffer::SIZE; ++i) { |
| 705 | if (wrappingBuffer.numFrames[i] > 0) { |
| 706 | mMmapStream->reportData(wrappingBuffer.data[i], wrappingBuffer.numFrames[i]); |
| 707 | } |
| 708 | } |
| 709 | fifo->advanceReadIndex(framesAvailable); |
| 710 | } |