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