Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2022 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 | #include <algorithm> |
| 18 | #include <set> |
| 19 | |
| 20 | #define LOG_TAG "AHAL_Module" |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 21 | #include <android-base/logging.h> |
Shunkai Yao | 39bf2c3 | 2022-12-06 03:25:59 +0000 | [diff] [blame] | 22 | #include <android/binder_ibinder_platform.h> |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 23 | |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 24 | #include <Utils.h> |
| 25 | #include <aidl/android/media/audio/common/AudioInputFlags.h> |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 26 | #include <aidl/android/media/audio/common/AudioOutputFlags.h> |
| 27 | |
Mikhail Naganov | 10c6fe2 | 2022-09-30 23:49:17 +0000 | [diff] [blame] | 28 | #include "core-impl/Bluetooth.h" |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 29 | #include "core-impl/Module.h" |
jiabin | 253bd32 | 2023-01-25 23:57:31 +0000 | [diff] [blame] | 30 | #include "core-impl/ModuleUsb.h" |
Vlad Popa | 943b7e2 | 2022-12-08 14:24:12 +0100 | [diff] [blame] | 31 | #include "core-impl/SoundDose.h" |
Mikhail Naganov | f429c03 | 2023-01-07 00:24:50 +0000 | [diff] [blame] | 32 | #include "core-impl/StreamStub.h" |
jiabin | 253bd32 | 2023-01-25 23:57:31 +0000 | [diff] [blame] | 33 | #include "core-impl/StreamUsb.h" |
Mikhail Naganov | 3b125b7 | 2022-10-05 02:12:39 +0000 | [diff] [blame] | 34 | #include "core-impl/Telephony.h" |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 35 | #include "core-impl/utils.h" |
| 36 | |
| 37 | using aidl::android::hardware::audio::common::SinkMetadata; |
| 38 | using aidl::android::hardware::audio::common::SourceMetadata; |
Vlad Popa | 2afbd1e | 2022-12-28 17:04:58 +0100 | [diff] [blame] | 39 | using aidl::android::hardware::audio::core::sounddose::ISoundDose; |
Mikhail Naganov | 6a4872d | 2022-06-15 21:39:04 +0000 | [diff] [blame] | 40 | using aidl::android::media::audio::common::AudioChannelLayout; |
Mikhail Naganov | ef6bc74 | 2022-10-06 00:14:19 +0000 | [diff] [blame] | 41 | using aidl::android::media::audio::common::AudioDevice; |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 42 | using aidl::android::media::audio::common::AudioFormatDescription; |
Mikhail Naganov | 6a4872d | 2022-06-15 21:39:04 +0000 | [diff] [blame] | 43 | using aidl::android::media::audio::common::AudioFormatType; |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 44 | using aidl::android::media::audio::common::AudioInputFlags; |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 45 | using aidl::android::media::audio::common::AudioIoFlags; |
jiabin | 9a8e686 | 2023-01-12 23:06:37 +0000 | [diff] [blame] | 46 | using aidl::android::media::audio::common::AudioMMapPolicy; |
| 47 | using aidl::android::media::audio::common::AudioMMapPolicyInfo; |
| 48 | using aidl::android::media::audio::common::AudioMMapPolicyType; |
Mikhail Naganov | 04ae822 | 2023-01-11 15:48:10 -0800 | [diff] [blame] | 49 | using aidl::android::media::audio::common::AudioMode; |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 50 | using aidl::android::media::audio::common::AudioOffloadInfo; |
| 51 | using aidl::android::media::audio::common::AudioOutputFlags; |
| 52 | using aidl::android::media::audio::common::AudioPort; |
| 53 | using aidl::android::media::audio::common::AudioPortConfig; |
| 54 | using aidl::android::media::audio::common::AudioPortExt; |
| 55 | using aidl::android::media::audio::common::AudioProfile; |
Mikhail Naganov | 20047bc | 2023-01-05 20:16:07 +0000 | [diff] [blame] | 56 | using aidl::android::media::audio::common::Boolean; |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 57 | using aidl::android::media::audio::common::Int; |
Mikhail Naganov | 6725ef5 | 2023-02-09 17:52:50 -0800 | [diff] [blame] | 58 | using aidl::android::media::audio::common::MicrophoneInfo; |
Mikhail Naganov | 6a4872d | 2022-06-15 21:39:04 +0000 | [diff] [blame] | 59 | using aidl::android::media::audio::common::PcmType; |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 60 | using android::hardware::audio::common::getFrameSizeInBytes; |
Mikhail Naganov | a2c5ddf | 2022-09-12 22:57:14 +0000 | [diff] [blame] | 61 | using android::hardware::audio::common::isBitPositionFlagSet; |
Mikhail Naganov | 04ae822 | 2023-01-11 15:48:10 -0800 | [diff] [blame] | 62 | using android::hardware::audio::common::isValidAudioMode; |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 63 | |
| 64 | namespace aidl::android::hardware::audio::core { |
| 65 | |
| 66 | namespace { |
| 67 | |
| 68 | bool generateDefaultPortConfig(const AudioPort& port, AudioPortConfig* config) { |
| 69 | *config = {}; |
| 70 | config->portId = port.id; |
| 71 | if (port.profiles.empty()) { |
| 72 | LOG(ERROR) << __func__ << ": port " << port.id << " has no profiles"; |
| 73 | return false; |
| 74 | } |
| 75 | const auto& profile = port.profiles.begin(); |
| 76 | config->format = profile->format; |
| 77 | if (profile->channelMasks.empty()) { |
| 78 | LOG(ERROR) << __func__ << ": the first profile in port " << port.id |
| 79 | << " has no channel masks"; |
| 80 | return false; |
| 81 | } |
| 82 | config->channelMask = *profile->channelMasks.begin(); |
| 83 | if (profile->sampleRates.empty()) { |
| 84 | LOG(ERROR) << __func__ << ": the first profile in port " << port.id |
| 85 | << " has no sample rates"; |
| 86 | return false; |
| 87 | } |
| 88 | Int sampleRate; |
| 89 | sampleRate.value = *profile->sampleRates.begin(); |
| 90 | config->sampleRate = sampleRate; |
| 91 | config->flags = port.flags; |
| 92 | config->ext = port.ext; |
| 93 | return true; |
| 94 | } |
| 95 | |
| 96 | bool findAudioProfile(const AudioPort& port, const AudioFormatDescription& format, |
| 97 | AudioProfile* profile) { |
| 98 | if (auto profilesIt = |
| 99 | find_if(port.profiles.begin(), port.profiles.end(), |
| 100 | [&format](const auto& profile) { return profile.format == format; }); |
| 101 | profilesIt != port.profiles.end()) { |
| 102 | *profile = *profilesIt; |
| 103 | return true; |
| 104 | } |
| 105 | return false; |
| 106 | } |
Mikhail Naganov | 00603d1 | 2022-05-02 22:52:13 +0000 | [diff] [blame] | 107 | |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 108 | } // namespace |
| 109 | |
jiabin | 253bd32 | 2023-01-25 23:57:31 +0000 | [diff] [blame] | 110 | // static |
| 111 | std::shared_ptr<Module> Module::createInstance(Type type) { |
| 112 | switch (type) { |
| 113 | case Module::Type::USB: |
| 114 | return ndk::SharedRefBase::make<ModuleUsb>(type); |
| 115 | case Type::DEFAULT: |
| 116 | case Type::R_SUBMIX: |
| 117 | default: |
| 118 | return ndk::SharedRefBase::make<Module>(type); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | // static |
| 123 | StreamIn::CreateInstance Module::getStreamInCreator(Type type) { |
| 124 | switch (type) { |
| 125 | case Type::USB: |
| 126 | return StreamInUsb::createInstance; |
| 127 | case Type::DEFAULT: |
| 128 | case Type::R_SUBMIX: |
| 129 | default: |
| 130 | return StreamInStub::createInstance; |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | // static |
| 135 | StreamOut::CreateInstance Module::getStreamOutCreator(Type type) { |
| 136 | switch (type) { |
| 137 | case Type::USB: |
| 138 | return StreamOutUsb::createInstance; |
| 139 | case Type::DEFAULT: |
| 140 | case Type::R_SUBMIX: |
| 141 | default: |
| 142 | return StreamOutStub::createInstance; |
| 143 | } |
| 144 | } |
| 145 | |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 146 | void Module::cleanUpPatch(int32_t patchId) { |
| 147 | erase_all_values(mPatches, std::set<int32_t>{patchId}); |
| 148 | } |
| 149 | |
Mikhail Naganov | 8651b36 | 2023-01-06 23:15:27 +0000 | [diff] [blame] | 150 | ndk::ScopedAStatus Module::createStreamContext( |
| 151 | int32_t in_portConfigId, int64_t in_bufferSizeFrames, |
| 152 | std::shared_ptr<IStreamCallback> asyncCallback, |
| 153 | std::shared_ptr<IStreamOutEventCallback> outEventCallback, StreamContext* out_context) { |
Mikhail Naganov | 6a4872d | 2022-06-15 21:39:04 +0000 | [diff] [blame] | 154 | if (in_bufferSizeFrames <= 0) { |
| 155 | LOG(ERROR) << __func__ << ": non-positive buffer size " << in_bufferSizeFrames; |
| 156 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 157 | } |
| 158 | if (in_bufferSizeFrames < kMinimumStreamBufferSizeFrames) { |
| 159 | LOG(ERROR) << __func__ << ": insufficient buffer size " << in_bufferSizeFrames |
| 160 | << ", must be at least " << kMinimumStreamBufferSizeFrames; |
| 161 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 162 | } |
| 163 | auto& configs = getConfig().portConfigs; |
| 164 | auto portConfigIt = findById<AudioPortConfig>(configs, in_portConfigId); |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 165 | // Since this is a private method, it is assumed that |
Mikhail Naganov | 6a4872d | 2022-06-15 21:39:04 +0000 | [diff] [blame] | 166 | // validity of the portConfigId has already been checked. |
| 167 | const size_t frameSize = |
| 168 | getFrameSizeInBytes(portConfigIt->format.value(), portConfigIt->channelMask.value()); |
| 169 | if (frameSize == 0) { |
| 170 | LOG(ERROR) << __func__ << ": could not calculate frame size for port config " |
| 171 | << portConfigIt->toString(); |
| 172 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 173 | } |
| 174 | LOG(DEBUG) << __func__ << ": frame size " << frameSize << " bytes"; |
| 175 | if (frameSize > kMaximumStreamBufferSizeBytes / in_bufferSizeFrames) { |
| 176 | LOG(ERROR) << __func__ << ": buffer size " << in_bufferSizeFrames |
| 177 | << " frames is too large, maximum size is " |
| 178 | << kMaximumStreamBufferSizeBytes / frameSize; |
| 179 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 180 | } |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 181 | const auto& flags = portConfigIt->flags.value(); |
| 182 | if ((flags.getTag() == AudioIoFlags::Tag::input && |
Mikhail Naganov | a2c5ddf | 2022-09-12 22:57:14 +0000 | [diff] [blame] | 183 | !isBitPositionFlagSet(flags.get<AudioIoFlags::Tag::input>(), |
| 184 | AudioInputFlags::MMAP_NOIRQ)) || |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 185 | (flags.getTag() == AudioIoFlags::Tag::output && |
Mikhail Naganov | a2c5ddf | 2022-09-12 22:57:14 +0000 | [diff] [blame] | 186 | !isBitPositionFlagSet(flags.get<AudioIoFlags::Tag::output>(), |
| 187 | AudioOutputFlags::MMAP_NOIRQ))) { |
Mikhail Naganov | 20047bc | 2023-01-05 20:16:07 +0000 | [diff] [blame] | 188 | StreamContext::DebugParameters params{mDebug.streamTransientStateDelayMs, |
Mikhail Naganov | 194daaa | 2023-01-05 22:34:20 +0000 | [diff] [blame] | 189 | mVendorDebug.forceTransientBurst, |
| 190 | mVendorDebug.forceSynchronousDrain}; |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 191 | StreamContext temp( |
| 192 | std::make_unique<StreamContext::CommandMQ>(1, true /*configureEventFlagWord*/), |
| 193 | std::make_unique<StreamContext::ReplyMQ>(1, true /*configureEventFlagWord*/), |
Mikhail Naganov | ef6bc74 | 2022-10-06 00:14:19 +0000 | [diff] [blame] | 194 | portConfigIt->format.value(), portConfigIt->channelMask.value(), |
jiabin | 253bd32 | 2023-01-25 23:57:31 +0000 | [diff] [blame] | 195 | portConfigIt->sampleRate.value().value, |
Mikhail Naganov | ef6bc74 | 2022-10-06 00:14:19 +0000 | [diff] [blame] | 196 | std::make_unique<StreamContext::DataMQ>(frameSize * in_bufferSizeFrames), |
Mikhail Naganov | 8651b36 | 2023-01-06 23:15:27 +0000 | [diff] [blame] | 197 | asyncCallback, outEventCallback, params); |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 198 | if (temp.isValid()) { |
| 199 | *out_context = std::move(temp); |
| 200 | } else { |
| 201 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE); |
| 202 | } |
| 203 | } else { |
| 204 | // TODO: Implement simulation of MMAP buffer allocation |
| 205 | } |
Mikhail Naganov | 6a4872d | 2022-06-15 21:39:04 +0000 | [diff] [blame] | 206 | return ndk::ScopedAStatus::ok(); |
| 207 | } |
| 208 | |
Mikhail Naganov | ef6bc74 | 2022-10-06 00:14:19 +0000 | [diff] [blame] | 209 | std::vector<AudioDevice> Module::findConnectedDevices(int32_t portConfigId) { |
| 210 | std::vector<AudioDevice> result; |
| 211 | auto& ports = getConfig().ports; |
| 212 | auto portIds = portIdsFromPortConfigIds(findConnectedPortConfigIds(portConfigId)); |
| 213 | for (auto it = portIds.begin(); it != portIds.end(); ++it) { |
| 214 | auto portIt = findById<AudioPort>(ports, *it); |
| 215 | if (portIt != ports.end() && portIt->ext.getTag() == AudioPortExt::Tag::device) { |
| 216 | result.push_back(portIt->ext.template get<AudioPortExt::Tag::device>().device); |
| 217 | } |
| 218 | } |
| 219 | return result; |
| 220 | } |
| 221 | |
| 222 | std::set<int32_t> Module::findConnectedPortConfigIds(int32_t portConfigId) { |
| 223 | std::set<int32_t> result; |
| 224 | auto patchIdsRange = mPatches.equal_range(portConfigId); |
| 225 | auto& patches = getConfig().patches; |
| 226 | for (auto it = patchIdsRange.first; it != patchIdsRange.second; ++it) { |
| 227 | auto patchIt = findById<AudioPatch>(patches, it->second); |
| 228 | if (patchIt == patches.end()) { |
| 229 | LOG(FATAL) << __func__ << ": patch with id " << it->second << " taken from mPatches " |
| 230 | << "not found in the configuration"; |
| 231 | } |
| 232 | if (std::find(patchIt->sourcePortConfigIds.begin(), patchIt->sourcePortConfigIds.end(), |
| 233 | portConfigId) != patchIt->sourcePortConfigIds.end()) { |
| 234 | result.insert(patchIt->sinkPortConfigIds.begin(), patchIt->sinkPortConfigIds.end()); |
| 235 | } else { |
| 236 | result.insert(patchIt->sourcePortConfigIds.begin(), patchIt->sourcePortConfigIds.end()); |
| 237 | } |
| 238 | } |
| 239 | return result; |
| 240 | } |
| 241 | |
Mikhail Naganov | 6a4872d | 2022-06-15 21:39:04 +0000 | [diff] [blame] | 242 | ndk::ScopedAStatus Module::findPortIdForNewStream(int32_t in_portConfigId, AudioPort** port) { |
| 243 | auto& configs = getConfig().portConfigs; |
| 244 | auto portConfigIt = findById<AudioPortConfig>(configs, in_portConfigId); |
| 245 | if (portConfigIt == configs.end()) { |
| 246 | LOG(ERROR) << __func__ << ": existing port config id " << in_portConfigId << " not found"; |
| 247 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 248 | } |
| 249 | const int32_t portId = portConfigIt->portId; |
| 250 | // In our implementation, configs of mix ports always have unique IDs. |
| 251 | CHECK(portId != in_portConfigId); |
| 252 | auto& ports = getConfig().ports; |
| 253 | auto portIt = findById<AudioPort>(ports, portId); |
| 254 | if (portIt == ports.end()) { |
| 255 | LOG(ERROR) << __func__ << ": port id " << portId << " used by port config id " |
| 256 | << in_portConfigId << " not found"; |
| 257 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 258 | } |
| 259 | if (mStreams.count(in_portConfigId) != 0) { |
| 260 | LOG(ERROR) << __func__ << ": port config id " << in_portConfigId |
| 261 | << " already has a stream opened on it"; |
| 262 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE); |
| 263 | } |
| 264 | if (portIt->ext.getTag() != AudioPortExt::Tag::mix) { |
| 265 | LOG(ERROR) << __func__ << ": port config id " << in_portConfigId |
| 266 | << " does not correspond to a mix port"; |
| 267 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 268 | } |
| 269 | const int32_t maxOpenStreamCount = portIt->ext.get<AudioPortExt::Tag::mix>().maxOpenStreamCount; |
| 270 | if (maxOpenStreamCount != 0 && mStreams.count(portId) >= maxOpenStreamCount) { |
| 271 | LOG(ERROR) << __func__ << ": port id " << portId |
| 272 | << " has already reached maximum allowed opened stream count: " |
| 273 | << maxOpenStreamCount; |
| 274 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE); |
| 275 | } |
| 276 | *port = &(*portIt); |
| 277 | return ndk::ScopedAStatus::ok(); |
| 278 | } |
| 279 | |
Mikhail Naganov | ef6bc74 | 2022-10-06 00:14:19 +0000 | [diff] [blame] | 280 | template <typename C> |
| 281 | std::set<int32_t> Module::portIdsFromPortConfigIds(C portConfigIds) { |
| 282 | std::set<int32_t> result; |
| 283 | auto& portConfigs = getConfig().portConfigs; |
| 284 | for (auto it = portConfigIds.begin(); it != portConfigIds.end(); ++it) { |
| 285 | auto portConfigIt = findById<AudioPortConfig>(portConfigs, *it); |
| 286 | if (portConfigIt != portConfigs.end()) { |
| 287 | result.insert(portConfigIt->portId); |
| 288 | } |
| 289 | } |
| 290 | return result; |
| 291 | } |
| 292 | |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 293 | internal::Configuration& Module::getConfig() { |
| 294 | if (!mConfig) { |
Mikhail Naganov | c8e4312 | 2022-12-09 00:33:47 +0000 | [diff] [blame] | 295 | switch (mType) { |
| 296 | case Type::DEFAULT: |
| 297 | mConfig = std::move(internal::getPrimaryConfiguration()); |
| 298 | break; |
| 299 | case Type::R_SUBMIX: |
| 300 | mConfig = std::move(internal::getRSubmixConfiguration()); |
| 301 | break; |
jiabin | b309d8d | 2023-01-20 19:07:15 +0000 | [diff] [blame] | 302 | case Type::USB: |
| 303 | mConfig = std::move(internal::getUsbConfiguration()); |
jiabin | 253bd32 | 2023-01-25 23:57:31 +0000 | [diff] [blame] | 304 | break; |
Mikhail Naganov | c8e4312 | 2022-12-09 00:33:47 +0000 | [diff] [blame] | 305 | } |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 306 | } |
| 307 | return *mConfig; |
| 308 | } |
| 309 | |
| 310 | void Module::registerPatch(const AudioPatch& patch) { |
| 311 | auto& configs = getConfig().portConfigs; |
| 312 | auto do_insert = [&](const std::vector<int32_t>& portConfigIds) { |
| 313 | for (auto portConfigId : portConfigIds) { |
| 314 | auto configIt = findById<AudioPortConfig>(configs, portConfigId); |
| 315 | if (configIt != configs.end()) { |
| 316 | mPatches.insert(std::pair{portConfigId, patch.id}); |
| 317 | if (configIt->portId != portConfigId) { |
| 318 | mPatches.insert(std::pair{configIt->portId, patch.id}); |
| 319 | } |
| 320 | } |
| 321 | }; |
| 322 | }; |
| 323 | do_insert(patch.sourcePortConfigIds); |
| 324 | do_insert(patch.sinkPortConfigIds); |
| 325 | } |
| 326 | |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 327 | void Module::updateStreamsConnectedState(const AudioPatch& oldPatch, const AudioPatch& newPatch) { |
| 328 | // Streams from the old patch need to be disconnected, streams from the new |
| 329 | // patch need to be connected. If the stream belongs to both patches, no need |
| 330 | // to update it. |
| 331 | std::set<int32_t> idsToDisconnect, idsToConnect; |
| 332 | idsToDisconnect.insert(oldPatch.sourcePortConfigIds.begin(), |
| 333 | oldPatch.sourcePortConfigIds.end()); |
| 334 | idsToDisconnect.insert(oldPatch.sinkPortConfigIds.begin(), oldPatch.sinkPortConfigIds.end()); |
| 335 | idsToConnect.insert(newPatch.sourcePortConfigIds.begin(), newPatch.sourcePortConfigIds.end()); |
| 336 | idsToConnect.insert(newPatch.sinkPortConfigIds.begin(), newPatch.sinkPortConfigIds.end()); |
| 337 | std::for_each(idsToDisconnect.begin(), idsToDisconnect.end(), [&](const auto& portConfigId) { |
| 338 | if (idsToConnect.count(portConfigId) == 0) { |
Mikhail Naganov | ef6bc74 | 2022-10-06 00:14:19 +0000 | [diff] [blame] | 339 | LOG(DEBUG) << "The stream on port config id " << portConfigId << " is not connected"; |
| 340 | mStreams.setStreamIsConnected(portConfigId, {}); |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 341 | } |
| 342 | }); |
| 343 | std::for_each(idsToConnect.begin(), idsToConnect.end(), [&](const auto& portConfigId) { |
| 344 | if (idsToDisconnect.count(portConfigId) == 0) { |
Mikhail Naganov | ef6bc74 | 2022-10-06 00:14:19 +0000 | [diff] [blame] | 345 | const auto connectedDevices = findConnectedDevices(portConfigId); |
| 346 | LOG(DEBUG) << "The stream on port config id " << portConfigId |
| 347 | << " is connected to: " << ::android::internal::ToString(connectedDevices); |
| 348 | mStreams.setStreamIsConnected(portConfigId, connectedDevices); |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 349 | } |
| 350 | }); |
| 351 | } |
| 352 | |
Mikhail Naganov | 00603d1 | 2022-05-02 22:52:13 +0000 | [diff] [blame] | 353 | ndk::ScopedAStatus Module::setModuleDebug( |
| 354 | const ::aidl::android::hardware::audio::core::ModuleDebug& in_debug) { |
| 355 | LOG(DEBUG) << __func__ << ": old flags:" << mDebug.toString() |
| 356 | << ", new flags: " << in_debug.toString(); |
| 357 | if (mDebug.simulateDeviceConnections != in_debug.simulateDeviceConnections && |
| 358 | !mConnectedDevicePorts.empty()) { |
| 359 | LOG(ERROR) << __func__ << ": attempting to change device connections simulation " |
| 360 | << "while having external devices connected"; |
| 361 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE); |
| 362 | } |
Mikhail Naganov | bd483c0 | 2022-11-17 20:33:39 +0000 | [diff] [blame] | 363 | if (in_debug.streamTransientStateDelayMs < 0) { |
| 364 | LOG(ERROR) << __func__ << ": streamTransientStateDelayMs is negative: " |
| 365 | << in_debug.streamTransientStateDelayMs; |
| 366 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 367 | } |
Mikhail Naganov | 00603d1 | 2022-05-02 22:52:13 +0000 | [diff] [blame] | 368 | mDebug = in_debug; |
| 369 | return ndk::ScopedAStatus::ok(); |
| 370 | } |
| 371 | |
Mikhail Naganov | 3b125b7 | 2022-10-05 02:12:39 +0000 | [diff] [blame] | 372 | ndk::ScopedAStatus Module::getTelephony(std::shared_ptr<ITelephony>* _aidl_return) { |
Mikhail Naganov | 7499a00 | 2023-02-27 18:51:44 -0800 | [diff] [blame] | 373 | if (!mTelephony) { |
Mikhail Naganov | 3b125b7 | 2022-10-05 02:12:39 +0000 | [diff] [blame] | 374 | mTelephony = ndk::SharedRefBase::make<Telephony>(); |
| 375 | } |
Mikhail Naganov | 7499a00 | 2023-02-27 18:51:44 -0800 | [diff] [blame] | 376 | *_aidl_return = mTelephony.getPtr(); |
Mikhail Naganov | 3b125b7 | 2022-10-05 02:12:39 +0000 | [diff] [blame] | 377 | LOG(DEBUG) << __func__ << ": returning instance of ITelephony: " << _aidl_return->get(); |
| 378 | return ndk::ScopedAStatus::ok(); |
| 379 | } |
| 380 | |
Mikhail Naganov | 10c6fe2 | 2022-09-30 23:49:17 +0000 | [diff] [blame] | 381 | ndk::ScopedAStatus Module::getBluetooth(std::shared_ptr<IBluetooth>* _aidl_return) { |
Mikhail Naganov | 7499a00 | 2023-02-27 18:51:44 -0800 | [diff] [blame] | 382 | if (!mBluetooth) { |
Mikhail Naganov | 10c6fe2 | 2022-09-30 23:49:17 +0000 | [diff] [blame] | 383 | mBluetooth = ndk::SharedRefBase::make<Bluetooth>(); |
Mikhail Naganov | 10c6fe2 | 2022-09-30 23:49:17 +0000 | [diff] [blame] | 384 | } |
Mikhail Naganov | 7499a00 | 2023-02-27 18:51:44 -0800 | [diff] [blame] | 385 | *_aidl_return = mBluetooth.getPtr(); |
Mikhail Naganov | 10c6fe2 | 2022-09-30 23:49:17 +0000 | [diff] [blame] | 386 | LOG(DEBUG) << __func__ << ": returning instance of IBluetooth: " << _aidl_return->get(); |
| 387 | return ndk::ScopedAStatus::ok(); |
| 388 | } |
| 389 | |
Mikhail Naganov | 7499a00 | 2023-02-27 18:51:44 -0800 | [diff] [blame] | 390 | ndk::ScopedAStatus Module::getBluetoothA2dp(std::shared_ptr<IBluetoothA2dp>* _aidl_return) { |
| 391 | if (!mBluetoothA2dp) { |
| 392 | mBluetoothA2dp = ndk::SharedRefBase::make<BluetoothA2dp>(); |
| 393 | } |
| 394 | *_aidl_return = mBluetoothA2dp.getPtr(); |
| 395 | LOG(DEBUG) << __func__ << ": returning instance of IBluetoothA2dp: " << _aidl_return->get(); |
| 396 | return ndk::ScopedAStatus::ok(); |
| 397 | } |
| 398 | |
Mikhail Naganov | 00603d1 | 2022-05-02 22:52:13 +0000 | [diff] [blame] | 399 | ndk::ScopedAStatus Module::connectExternalDevice(const AudioPort& in_templateIdAndAdditionalData, |
| 400 | AudioPort* _aidl_return) { |
| 401 | const int32_t templateId = in_templateIdAndAdditionalData.id; |
| 402 | auto& ports = getConfig().ports; |
| 403 | AudioPort connectedPort; |
| 404 | { // Scope the template port so that we don't accidentally modify it. |
| 405 | auto templateIt = findById<AudioPort>(ports, templateId); |
| 406 | if (templateIt == ports.end()) { |
| 407 | LOG(ERROR) << __func__ << ": port id " << templateId << " not found"; |
| 408 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 409 | } |
| 410 | if (templateIt->ext.getTag() != AudioPortExt::Tag::device) { |
| 411 | LOG(ERROR) << __func__ << ": port id " << templateId << " is not a device port"; |
| 412 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 413 | } |
| 414 | if (!templateIt->profiles.empty()) { |
| 415 | LOG(ERROR) << __func__ << ": port id " << templateId |
| 416 | << " does not have dynamic profiles"; |
| 417 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 418 | } |
| 419 | auto& templateDevicePort = templateIt->ext.get<AudioPortExt::Tag::device>(); |
| 420 | if (templateDevicePort.device.type.connection.empty()) { |
| 421 | LOG(ERROR) << __func__ << ": port id " << templateId << " is permanently attached"; |
| 422 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 423 | } |
| 424 | // Postpone id allocation until we ensure that there are no client errors. |
| 425 | connectedPort = *templateIt; |
| 426 | connectedPort.extraAudioDescriptors = in_templateIdAndAdditionalData.extraAudioDescriptors; |
| 427 | const auto& inputDevicePort = |
| 428 | in_templateIdAndAdditionalData.ext.get<AudioPortExt::Tag::device>(); |
| 429 | auto& connectedDevicePort = connectedPort.ext.get<AudioPortExt::Tag::device>(); |
| 430 | connectedDevicePort.device.address = inputDevicePort.device.address; |
| 431 | LOG(DEBUG) << __func__ << ": device port " << connectedPort.id << " device set to " |
| 432 | << connectedDevicePort.device.toString(); |
| 433 | // Check if there is already a connected port with for the same external device. |
| 434 | for (auto connectedPortId : mConnectedDevicePorts) { |
| 435 | auto connectedPortIt = findById<AudioPort>(ports, connectedPortId); |
| 436 | if (connectedPortIt->ext.get<AudioPortExt::Tag::device>().device == |
| 437 | connectedDevicePort.device) { |
| 438 | LOG(ERROR) << __func__ << ": device " << connectedDevicePort.device.toString() |
| 439 | << " is already connected at the device port id " << connectedPortId; |
| 440 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE); |
| 441 | } |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | if (!mDebug.simulateDeviceConnections) { |
| 446 | // In a real HAL here we would attempt querying the profiles from the device. |
| 447 | LOG(ERROR) << __func__ << ": failed to query supported device profiles"; |
jiabin | 253bd32 | 2023-01-25 23:57:31 +0000 | [diff] [blame] | 448 | // TODO: Check the return value when it is ready for actual devices. |
| 449 | populateConnectedDevicePort(&connectedPort); |
Mikhail Naganov | 00603d1 | 2022-05-02 22:52:13 +0000 | [diff] [blame] | 450 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE); |
| 451 | } |
| 452 | |
| 453 | connectedPort.id = ++getConfig().nextPortId; |
| 454 | mConnectedDevicePorts.insert(connectedPort.id); |
| 455 | LOG(DEBUG) << __func__ << ": template port " << templateId << " external device connected, " |
| 456 | << "connected port ID " << connectedPort.id; |
| 457 | auto& connectedProfiles = getConfig().connectedProfiles; |
| 458 | if (auto connectedProfilesIt = connectedProfiles.find(templateId); |
| 459 | connectedProfilesIt != connectedProfiles.end()) { |
| 460 | connectedPort.profiles = connectedProfilesIt->second; |
| 461 | } |
| 462 | ports.push_back(connectedPort); |
jiabin | 783c48b | 2023-02-28 18:28:06 +0000 | [diff] [blame] | 463 | onExternalDeviceConnectionChanged(connectedPort, true /*connected*/); |
Mikhail Naganov | 00603d1 | 2022-05-02 22:52:13 +0000 | [diff] [blame] | 464 | *_aidl_return = std::move(connectedPort); |
| 465 | |
| 466 | std::vector<AudioRoute> newRoutes; |
| 467 | auto& routes = getConfig().routes; |
| 468 | for (auto& r : routes) { |
| 469 | if (r.sinkPortId == templateId) { |
| 470 | AudioRoute newRoute; |
| 471 | newRoute.sourcePortIds = r.sourcePortIds; |
| 472 | newRoute.sinkPortId = connectedPort.id; |
| 473 | newRoute.isExclusive = r.isExclusive; |
| 474 | newRoutes.push_back(std::move(newRoute)); |
| 475 | } else { |
| 476 | auto& srcs = r.sourcePortIds; |
| 477 | if (std::find(srcs.begin(), srcs.end(), templateId) != srcs.end()) { |
| 478 | srcs.push_back(connectedPort.id); |
| 479 | } |
| 480 | } |
| 481 | } |
| 482 | routes.insert(routes.end(), newRoutes.begin(), newRoutes.end()); |
| 483 | |
| 484 | return ndk::ScopedAStatus::ok(); |
| 485 | } |
| 486 | |
| 487 | ndk::ScopedAStatus Module::disconnectExternalDevice(int32_t in_portId) { |
| 488 | auto& ports = getConfig().ports; |
| 489 | auto portIt = findById<AudioPort>(ports, in_portId); |
| 490 | if (portIt == ports.end()) { |
| 491 | LOG(ERROR) << __func__ << ": port id " << in_portId << " not found"; |
| 492 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 493 | } |
| 494 | if (portIt->ext.getTag() != AudioPortExt::Tag::device) { |
| 495 | LOG(ERROR) << __func__ << ": port id " << in_portId << " is not a device port"; |
| 496 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 497 | } |
| 498 | if (mConnectedDevicePorts.count(in_portId) == 0) { |
| 499 | LOG(ERROR) << __func__ << ": port id " << in_portId << " is not a connected device port"; |
| 500 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 501 | } |
| 502 | auto& configs = getConfig().portConfigs; |
| 503 | auto& initials = getConfig().initialConfigs; |
| 504 | auto configIt = std::find_if(configs.begin(), configs.end(), [&](const auto& config) { |
| 505 | if (config.portId == in_portId) { |
| 506 | // Check if the configuration was provided by the client. |
| 507 | const auto& initialIt = findById<AudioPortConfig>(initials, config.id); |
| 508 | return initialIt == initials.end() || config != *initialIt; |
| 509 | } |
| 510 | return false; |
| 511 | }); |
| 512 | if (configIt != configs.end()) { |
| 513 | LOG(ERROR) << __func__ << ": port id " << in_portId << " has a non-default config with id " |
| 514 | << configIt->id; |
| 515 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE); |
| 516 | } |
jiabin | 783c48b | 2023-02-28 18:28:06 +0000 | [diff] [blame] | 517 | onExternalDeviceConnectionChanged(*portIt, false /*connected*/); |
Mikhail Naganov | 00603d1 | 2022-05-02 22:52:13 +0000 | [diff] [blame] | 518 | ports.erase(portIt); |
| 519 | mConnectedDevicePorts.erase(in_portId); |
| 520 | LOG(DEBUG) << __func__ << ": connected device port " << in_portId << " released"; |
| 521 | |
| 522 | auto& routes = getConfig().routes; |
| 523 | for (auto routesIt = routes.begin(); routesIt != routes.end();) { |
| 524 | if (routesIt->sinkPortId == in_portId) { |
| 525 | routesIt = routes.erase(routesIt); |
| 526 | } else { |
| 527 | // Note: the list of sourcePortIds can't become empty because there must |
| 528 | // be the id of the template port in the route. |
| 529 | erase_if(routesIt->sourcePortIds, [in_portId](auto src) { return src == in_portId; }); |
| 530 | ++routesIt; |
| 531 | } |
| 532 | } |
| 533 | |
| 534 | return ndk::ScopedAStatus::ok(); |
| 535 | } |
| 536 | |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 537 | ndk::ScopedAStatus Module::getAudioPatches(std::vector<AudioPatch>* _aidl_return) { |
| 538 | *_aidl_return = getConfig().patches; |
| 539 | LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " patches"; |
| 540 | return ndk::ScopedAStatus::ok(); |
| 541 | } |
| 542 | |
| 543 | ndk::ScopedAStatus Module::getAudioPort(int32_t in_portId, AudioPort* _aidl_return) { |
| 544 | auto& ports = getConfig().ports; |
| 545 | auto portIt = findById<AudioPort>(ports, in_portId); |
| 546 | if (portIt != ports.end()) { |
| 547 | *_aidl_return = *portIt; |
| 548 | LOG(DEBUG) << __func__ << ": returning port by id " << in_portId; |
| 549 | return ndk::ScopedAStatus::ok(); |
| 550 | } |
| 551 | LOG(ERROR) << __func__ << ": port id " << in_portId << " not found"; |
| 552 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 553 | } |
| 554 | |
| 555 | ndk::ScopedAStatus Module::getAudioPortConfigs(std::vector<AudioPortConfig>* _aidl_return) { |
| 556 | *_aidl_return = getConfig().portConfigs; |
| 557 | LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " port configs"; |
| 558 | return ndk::ScopedAStatus::ok(); |
| 559 | } |
| 560 | |
| 561 | ndk::ScopedAStatus Module::getAudioPorts(std::vector<AudioPort>* _aidl_return) { |
| 562 | *_aidl_return = getConfig().ports; |
| 563 | LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " ports"; |
| 564 | return ndk::ScopedAStatus::ok(); |
| 565 | } |
| 566 | |
| 567 | ndk::ScopedAStatus Module::getAudioRoutes(std::vector<AudioRoute>* _aidl_return) { |
| 568 | *_aidl_return = getConfig().routes; |
| 569 | LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " routes"; |
| 570 | return ndk::ScopedAStatus::ok(); |
| 571 | } |
| 572 | |
Mikhail Naganov | 00603d1 | 2022-05-02 22:52:13 +0000 | [diff] [blame] | 573 | ndk::ScopedAStatus Module::getAudioRoutesForAudioPort(int32_t in_portId, |
| 574 | std::vector<AudioRoute>* _aidl_return) { |
| 575 | auto& ports = getConfig().ports; |
| 576 | if (auto portIt = findById<AudioPort>(ports, in_portId); portIt == ports.end()) { |
| 577 | LOG(ERROR) << __func__ << ": port id " << in_portId << " not found"; |
| 578 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 579 | } |
| 580 | auto& routes = getConfig().routes; |
| 581 | std::copy_if(routes.begin(), routes.end(), std::back_inserter(*_aidl_return), |
| 582 | [&](const auto& r) { |
| 583 | const auto& srcs = r.sourcePortIds; |
| 584 | return r.sinkPortId == in_portId || |
| 585 | std::find(srcs.begin(), srcs.end(), in_portId) != srcs.end(); |
| 586 | }); |
| 587 | return ndk::ScopedAStatus::ok(); |
| 588 | } |
| 589 | |
Mikhail Naganov | 6a4872d | 2022-06-15 21:39:04 +0000 | [diff] [blame] | 590 | ndk::ScopedAStatus Module::openInputStream(const OpenInputStreamArguments& in_args, |
| 591 | OpenInputStreamReturn* _aidl_return) { |
| 592 | LOG(DEBUG) << __func__ << ": port config id " << in_args.portConfigId << ", buffer size " |
| 593 | << in_args.bufferSizeFrames << " frames"; |
| 594 | AudioPort* port = nullptr; |
| 595 | if (auto status = findPortIdForNewStream(in_args.portConfigId, &port); !status.isOk()) { |
| 596 | return status; |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 597 | } |
Mikhail Naganov | 6a4872d | 2022-06-15 21:39:04 +0000 | [diff] [blame] | 598 | if (port->flags.getTag() != AudioIoFlags::Tag::input) { |
| 599 | LOG(ERROR) << __func__ << ": port config id " << in_args.portConfigId |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 600 | << " does not correspond to an input mix port"; |
| 601 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 602 | } |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 603 | StreamContext context; |
Mikhail Naganov | 30301a4 | 2022-09-13 01:20:45 +0000 | [diff] [blame] | 604 | if (auto status = createStreamContext(in_args.portConfigId, in_args.bufferSizeFrames, nullptr, |
Mikhail Naganov | 8651b36 | 2023-01-06 23:15:27 +0000 | [diff] [blame] | 605 | nullptr, &context); |
Mikhail Naganov | 6a4872d | 2022-06-15 21:39:04 +0000 | [diff] [blame] | 606 | !status.isOk()) { |
| 607 | return status; |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 608 | } |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 609 | context.fillDescriptor(&_aidl_return->desc); |
Mikhail Naganov | e9f10fc | 2022-10-14 23:31:52 +0000 | [diff] [blame] | 610 | std::shared_ptr<StreamIn> stream; |
jiabin | 253bd32 | 2023-01-25 23:57:31 +0000 | [diff] [blame] | 611 | ndk::ScopedAStatus status = getStreamInCreator(mType)(in_args.sinkMetadata, std::move(context), |
| 612 | mConfig->microphones, &stream); |
| 613 | if (!status.isOk()) { |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 614 | return status; |
| 615 | } |
| 616 | StreamWrapper streamWrapper(stream); |
Mikhail Naganov | df5feba | 2022-12-15 00:11:14 +0000 | [diff] [blame] | 617 | AIBinder_setMinSchedulerPolicy(streamWrapper.getBinder().get(), SCHED_NORMAL, |
| 618 | ANDROID_PRIORITY_AUDIO); |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 619 | auto patchIt = mPatches.find(in_args.portConfigId); |
| 620 | if (patchIt != mPatches.end()) { |
Mikhail Naganov | ef6bc74 | 2022-10-06 00:14:19 +0000 | [diff] [blame] | 621 | streamWrapper.setStreamIsConnected(findConnectedDevices(in_args.portConfigId)); |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 622 | } |
| 623 | mStreams.insert(port->id, in_args.portConfigId, std::move(streamWrapper)); |
Mikhail Naganov | 6a4872d | 2022-06-15 21:39:04 +0000 | [diff] [blame] | 624 | _aidl_return->stream = std::move(stream); |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 625 | return ndk::ScopedAStatus::ok(); |
| 626 | } |
| 627 | |
Mikhail Naganov | 6a4872d | 2022-06-15 21:39:04 +0000 | [diff] [blame] | 628 | ndk::ScopedAStatus Module::openOutputStream(const OpenOutputStreamArguments& in_args, |
| 629 | OpenOutputStreamReturn* _aidl_return) { |
| 630 | LOG(DEBUG) << __func__ << ": port config id " << in_args.portConfigId << ", has offload info? " |
| 631 | << (in_args.offloadInfo.has_value()) << ", buffer size " << in_args.bufferSizeFrames |
| 632 | << " frames"; |
| 633 | AudioPort* port = nullptr; |
| 634 | if (auto status = findPortIdForNewStream(in_args.portConfigId, &port); !status.isOk()) { |
| 635 | return status; |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 636 | } |
Mikhail Naganov | 6a4872d | 2022-06-15 21:39:04 +0000 | [diff] [blame] | 637 | if (port->flags.getTag() != AudioIoFlags::Tag::output) { |
| 638 | LOG(ERROR) << __func__ << ": port config id " << in_args.portConfigId |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 639 | << " does not correspond to an output mix port"; |
| 640 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 641 | } |
Mikhail Naganov | a2c5ddf | 2022-09-12 22:57:14 +0000 | [diff] [blame] | 642 | const bool isOffload = isBitPositionFlagSet(port->flags.get<AudioIoFlags::Tag::output>(), |
| 643 | AudioOutputFlags::COMPRESS_OFFLOAD); |
| 644 | if (isOffload && !in_args.offloadInfo.has_value()) { |
Mikhail Naganov | 6a4872d | 2022-06-15 21:39:04 +0000 | [diff] [blame] | 645 | LOG(ERROR) << __func__ << ": port id " << port->id |
Mikhail Naganov | 111e0ce | 2022-06-17 21:41:19 +0000 | [diff] [blame] | 646 | << " has COMPRESS_OFFLOAD flag set, requires offload info"; |
| 647 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 648 | } |
Mikhail Naganov | 30301a4 | 2022-09-13 01:20:45 +0000 | [diff] [blame] | 649 | const bool isNonBlocking = isBitPositionFlagSet(port->flags.get<AudioIoFlags::Tag::output>(), |
| 650 | AudioOutputFlags::NON_BLOCKING); |
| 651 | if (isNonBlocking && in_args.callback == nullptr) { |
| 652 | LOG(ERROR) << __func__ << ": port id " << port->id |
| 653 | << " has NON_BLOCKING flag set, requires async callback"; |
| 654 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 655 | } |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 656 | StreamContext context; |
Mikhail Naganov | 30301a4 | 2022-09-13 01:20:45 +0000 | [diff] [blame] | 657 | if (auto status = createStreamContext(in_args.portConfigId, in_args.bufferSizeFrames, |
Mikhail Naganov | 8651b36 | 2023-01-06 23:15:27 +0000 | [diff] [blame] | 658 | isNonBlocking ? in_args.callback : nullptr, |
| 659 | in_args.eventCallback, &context); |
Mikhail Naganov | 6a4872d | 2022-06-15 21:39:04 +0000 | [diff] [blame] | 660 | !status.isOk()) { |
| 661 | return status; |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 662 | } |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 663 | context.fillDescriptor(&_aidl_return->desc); |
Mikhail Naganov | e9f10fc | 2022-10-14 23:31:52 +0000 | [diff] [blame] | 664 | std::shared_ptr<StreamOut> stream; |
jiabin | 253bd32 | 2023-01-25 23:57:31 +0000 | [diff] [blame] | 665 | ndk::ScopedAStatus status = getStreamOutCreator(mType)( |
| 666 | in_args.sourceMetadata, std::move(context), in_args.offloadInfo, &stream); |
| 667 | if (!status.isOk()) { |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 668 | return status; |
| 669 | } |
| 670 | StreamWrapper streamWrapper(stream); |
Mikhail Naganov | df5feba | 2022-12-15 00:11:14 +0000 | [diff] [blame] | 671 | AIBinder_setMinSchedulerPolicy(streamWrapper.getBinder().get(), SCHED_NORMAL, |
| 672 | ANDROID_PRIORITY_AUDIO); |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 673 | auto patchIt = mPatches.find(in_args.portConfigId); |
| 674 | if (patchIt != mPatches.end()) { |
Mikhail Naganov | ef6bc74 | 2022-10-06 00:14:19 +0000 | [diff] [blame] | 675 | streamWrapper.setStreamIsConnected(findConnectedDevices(in_args.portConfigId)); |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 676 | } |
| 677 | mStreams.insert(port->id, in_args.portConfigId, std::move(streamWrapper)); |
Mikhail Naganov | 6a4872d | 2022-06-15 21:39:04 +0000 | [diff] [blame] | 678 | _aidl_return->stream = std::move(stream); |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 679 | return ndk::ScopedAStatus::ok(); |
| 680 | } |
| 681 | |
Mikhail Naganov | 7492720 | 2022-12-19 16:37:14 +0000 | [diff] [blame] | 682 | ndk::ScopedAStatus Module::getSupportedPlaybackRateFactors( |
| 683 | SupportedPlaybackRateFactors* _aidl_return) { |
| 684 | LOG(DEBUG) << __func__; |
| 685 | (void)_aidl_return; |
| 686 | return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION); |
| 687 | } |
| 688 | |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 689 | ndk::ScopedAStatus Module::setAudioPatch(const AudioPatch& in_requested, AudioPatch* _aidl_return) { |
Mikhail Naganov | 16db9b7 | 2022-06-17 21:36:18 +0000 | [diff] [blame] | 690 | LOG(DEBUG) << __func__ << ": requested patch " << in_requested.toString(); |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 691 | if (in_requested.sourcePortConfigIds.empty()) { |
| 692 | LOG(ERROR) << __func__ << ": requested patch has empty sources list"; |
| 693 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 694 | } |
| 695 | if (!all_unique<int32_t>(in_requested.sourcePortConfigIds)) { |
| 696 | LOG(ERROR) << __func__ << ": requested patch has duplicate ids in the sources list"; |
| 697 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 698 | } |
| 699 | if (in_requested.sinkPortConfigIds.empty()) { |
| 700 | LOG(ERROR) << __func__ << ": requested patch has empty sinks list"; |
| 701 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 702 | } |
| 703 | if (!all_unique<int32_t>(in_requested.sinkPortConfigIds)) { |
| 704 | LOG(ERROR) << __func__ << ": requested patch has duplicate ids in the sinks list"; |
| 705 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 706 | } |
| 707 | |
| 708 | auto& configs = getConfig().portConfigs; |
| 709 | std::vector<int32_t> missingIds; |
| 710 | auto sources = |
| 711 | selectByIds<AudioPortConfig>(configs, in_requested.sourcePortConfigIds, &missingIds); |
| 712 | if (!missingIds.empty()) { |
| 713 | LOG(ERROR) << __func__ << ": following source port config ids not found: " |
| 714 | << ::android::internal::ToString(missingIds); |
| 715 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 716 | } |
| 717 | auto sinks = selectByIds<AudioPortConfig>(configs, in_requested.sinkPortConfigIds, &missingIds); |
| 718 | if (!missingIds.empty()) { |
| 719 | LOG(ERROR) << __func__ << ": following sink port config ids not found: " |
| 720 | << ::android::internal::ToString(missingIds); |
| 721 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 722 | } |
| 723 | // bool indicates whether a non-exclusive route is available. |
| 724 | // If only an exclusive route is available, that means the patch can not be |
| 725 | // established if there is any other patch which currently uses the sink port. |
| 726 | std::map<int32_t, bool> allowedSinkPorts; |
| 727 | auto& routes = getConfig().routes; |
| 728 | for (auto src : sources) { |
| 729 | for (const auto& r : routes) { |
| 730 | const auto& srcs = r.sourcePortIds; |
| 731 | if (std::find(srcs.begin(), srcs.end(), src->portId) != srcs.end()) { |
| 732 | if (!allowedSinkPorts[r.sinkPortId]) { // prefer non-exclusive |
| 733 | allowedSinkPorts[r.sinkPortId] = !r.isExclusive; |
| 734 | } |
| 735 | } |
| 736 | } |
| 737 | } |
| 738 | for (auto sink : sinks) { |
| 739 | if (allowedSinkPorts.count(sink->portId) == 0) { |
| 740 | LOG(ERROR) << __func__ << ": there is no route to the sink port id " << sink->portId; |
| 741 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 742 | } |
| 743 | } |
| 744 | |
jiabin | 253bd32 | 2023-01-25 23:57:31 +0000 | [diff] [blame] | 745 | if (auto status = checkAudioPatchEndpointsMatch(sources, sinks); !status.isOk()) { |
| 746 | return status; |
| 747 | } |
| 748 | |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 749 | auto& patches = getConfig().patches; |
| 750 | auto existing = patches.end(); |
| 751 | std::optional<decltype(mPatches)> patchesBackup; |
| 752 | if (in_requested.id != 0) { |
| 753 | existing = findById<AudioPatch>(patches, in_requested.id); |
| 754 | if (existing != patches.end()) { |
| 755 | patchesBackup = mPatches; |
| 756 | cleanUpPatch(existing->id); |
| 757 | } else { |
| 758 | LOG(ERROR) << __func__ << ": not found existing patch id " << in_requested.id; |
| 759 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 760 | } |
| 761 | } |
| 762 | // Validate the requested patch. |
| 763 | for (const auto& [sinkPortId, nonExclusive] : allowedSinkPorts) { |
| 764 | if (!nonExclusive && mPatches.count(sinkPortId) != 0) { |
| 765 | LOG(ERROR) << __func__ << ": sink port id " << sinkPortId |
| 766 | << "is exclusive and is already used by some other patch"; |
| 767 | if (patchesBackup.has_value()) { |
| 768 | mPatches = std::move(*patchesBackup); |
| 769 | } |
| 770 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE); |
| 771 | } |
| 772 | } |
| 773 | *_aidl_return = in_requested; |
Mikhail Naganov | 6a4872d | 2022-06-15 21:39:04 +0000 | [diff] [blame] | 774 | _aidl_return->minimumStreamBufferSizeFrames = kMinimumStreamBufferSizeFrames; |
| 775 | _aidl_return->latenciesMs.clear(); |
| 776 | _aidl_return->latenciesMs.insert(_aidl_return->latenciesMs.end(), |
| 777 | _aidl_return->sinkPortConfigIds.size(), kLatencyMs); |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 778 | AudioPatch oldPatch{}; |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 779 | if (existing == patches.end()) { |
| 780 | _aidl_return->id = getConfig().nextPatchId++; |
| 781 | patches.push_back(*_aidl_return); |
| 782 | existing = patches.begin() + (patches.size() - 1); |
| 783 | } else { |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 784 | oldPatch = *existing; |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 785 | *existing = *_aidl_return; |
| 786 | } |
| 787 | registerPatch(*existing); |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 788 | updateStreamsConnectedState(oldPatch, *_aidl_return); |
| 789 | |
| 790 | LOG(DEBUG) << __func__ << ": " << (oldPatch.id == 0 ? "created" : "updated") << " patch " |
| 791 | << _aidl_return->toString(); |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 792 | return ndk::ScopedAStatus::ok(); |
| 793 | } |
| 794 | |
| 795 | ndk::ScopedAStatus Module::setAudioPortConfig(const AudioPortConfig& in_requested, |
| 796 | AudioPortConfig* out_suggested, bool* _aidl_return) { |
| 797 | LOG(DEBUG) << __func__ << ": requested " << in_requested.toString(); |
| 798 | auto& configs = getConfig().portConfigs; |
| 799 | auto existing = configs.end(); |
| 800 | if (in_requested.id != 0) { |
| 801 | if (existing = findById<AudioPortConfig>(configs, in_requested.id); |
| 802 | existing == configs.end()) { |
| 803 | LOG(ERROR) << __func__ << ": existing port config id " << in_requested.id |
| 804 | << " not found"; |
| 805 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 806 | } |
| 807 | } |
| 808 | |
| 809 | const int portId = existing != configs.end() ? existing->portId : in_requested.portId; |
| 810 | if (portId == 0) { |
| 811 | LOG(ERROR) << __func__ << ": input port config does not specify portId"; |
| 812 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 813 | } |
| 814 | auto& ports = getConfig().ports; |
| 815 | auto portIt = findById<AudioPort>(ports, portId); |
| 816 | if (portIt == ports.end()) { |
| 817 | LOG(ERROR) << __func__ << ": input port config points to non-existent portId " << portId; |
| 818 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 819 | } |
| 820 | if (existing != configs.end()) { |
| 821 | *out_suggested = *existing; |
| 822 | } else { |
| 823 | AudioPortConfig newConfig; |
| 824 | if (generateDefaultPortConfig(*portIt, &newConfig)) { |
| 825 | *out_suggested = newConfig; |
| 826 | } else { |
| 827 | LOG(ERROR) << __func__ << ": unable generate a default config for port " << portId; |
| 828 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 829 | } |
| 830 | } |
| 831 | // From this moment, 'out_suggested' is either an existing port config, |
| 832 | // or a new generated config. Now attempt to update it according to the specified |
| 833 | // fields of 'in_requested'. |
| 834 | |
| 835 | bool requestedIsValid = true, requestedIsFullySpecified = true; |
| 836 | |
| 837 | AudioIoFlags portFlags = portIt->flags; |
| 838 | if (in_requested.flags.has_value()) { |
| 839 | if (in_requested.flags.value() != portFlags) { |
| 840 | LOG(WARNING) << __func__ << ": requested flags " |
| 841 | << in_requested.flags.value().toString() << " do not match port's " |
| 842 | << portId << " flags " << portFlags.toString(); |
| 843 | requestedIsValid = false; |
| 844 | } |
| 845 | } else { |
| 846 | requestedIsFullySpecified = false; |
| 847 | } |
| 848 | |
| 849 | AudioProfile portProfile; |
| 850 | if (in_requested.format.has_value()) { |
| 851 | const auto& format = in_requested.format.value(); |
| 852 | if (findAudioProfile(*portIt, format, &portProfile)) { |
| 853 | out_suggested->format = format; |
| 854 | } else { |
| 855 | LOG(WARNING) << __func__ << ": requested format " << format.toString() |
| 856 | << " is not found in port's " << portId << " profiles"; |
| 857 | requestedIsValid = false; |
| 858 | } |
| 859 | } else { |
| 860 | requestedIsFullySpecified = false; |
| 861 | } |
| 862 | if (!findAudioProfile(*portIt, out_suggested->format.value(), &portProfile)) { |
| 863 | LOG(ERROR) << __func__ << ": port " << portId << " does not support format " |
| 864 | << out_suggested->format.value().toString() << " anymore"; |
| 865 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 866 | } |
| 867 | |
| 868 | if (in_requested.channelMask.has_value()) { |
| 869 | const auto& channelMask = in_requested.channelMask.value(); |
| 870 | if (find(portProfile.channelMasks.begin(), portProfile.channelMasks.end(), channelMask) != |
| 871 | portProfile.channelMasks.end()) { |
| 872 | out_suggested->channelMask = channelMask; |
| 873 | } else { |
| 874 | LOG(WARNING) << __func__ << ": requested channel mask " << channelMask.toString() |
| 875 | << " is not supported for the format " << portProfile.format.toString() |
| 876 | << " by the port " << portId; |
| 877 | requestedIsValid = false; |
| 878 | } |
| 879 | } else { |
| 880 | requestedIsFullySpecified = false; |
| 881 | } |
| 882 | |
| 883 | if (in_requested.sampleRate.has_value()) { |
| 884 | const auto& sampleRate = in_requested.sampleRate.value(); |
| 885 | if (find(portProfile.sampleRates.begin(), portProfile.sampleRates.end(), |
| 886 | sampleRate.value) != portProfile.sampleRates.end()) { |
| 887 | out_suggested->sampleRate = sampleRate; |
| 888 | } else { |
| 889 | LOG(WARNING) << __func__ << ": requested sample rate " << sampleRate.value |
| 890 | << " is not supported for the format " << portProfile.format.toString() |
| 891 | << " by the port " << portId; |
| 892 | requestedIsValid = false; |
| 893 | } |
| 894 | } else { |
| 895 | requestedIsFullySpecified = false; |
| 896 | } |
| 897 | |
| 898 | if (in_requested.gain.has_value()) { |
| 899 | // Let's pretend that gain can always be applied. |
| 900 | out_suggested->gain = in_requested.gain.value(); |
| 901 | } |
| 902 | |
Mikhail Naganov | 248e950 | 2023-02-21 16:32:40 -0800 | [diff] [blame] | 903 | if (in_requested.ext.getTag() != AudioPortExt::Tag::unspecified) { |
| 904 | if (in_requested.ext.getTag() == out_suggested->ext.getTag()) { |
| 905 | if (out_suggested->ext.getTag() == AudioPortExt::Tag::mix) { |
| 906 | // 'AudioMixPortExt.handle' is set by the client, copy from in_requested |
| 907 | out_suggested->ext.get<AudioPortExt::Tag::mix>().handle = |
| 908 | in_requested.ext.get<AudioPortExt::Tag::mix>().handle; |
| 909 | } |
| 910 | } else { |
| 911 | LOG(WARNING) << __func__ << ": requested ext tag " |
| 912 | << toString(in_requested.ext.getTag()) << " do not match port's tag " |
| 913 | << toString(out_suggested->ext.getTag()); |
| 914 | requestedIsValid = false; |
| 915 | } |
| 916 | } |
| 917 | |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 918 | if (existing == configs.end() && requestedIsValid && requestedIsFullySpecified) { |
| 919 | out_suggested->id = getConfig().nextPortId++; |
| 920 | configs.push_back(*out_suggested); |
| 921 | *_aidl_return = true; |
| 922 | LOG(DEBUG) << __func__ << ": created new port config " << out_suggested->toString(); |
| 923 | } else if (existing != configs.end() && requestedIsValid) { |
| 924 | *existing = *out_suggested; |
| 925 | *_aidl_return = true; |
| 926 | LOG(DEBUG) << __func__ << ": updated port config " << out_suggested->toString(); |
| 927 | } else { |
| 928 | LOG(DEBUG) << __func__ << ": not applied; existing config ? " << (existing != configs.end()) |
| 929 | << "; requested is valid? " << requestedIsValid << ", fully specified? " |
| 930 | << requestedIsFullySpecified; |
| 931 | *_aidl_return = false; |
| 932 | } |
| 933 | return ndk::ScopedAStatus::ok(); |
| 934 | } |
| 935 | |
| 936 | ndk::ScopedAStatus Module::resetAudioPatch(int32_t in_patchId) { |
| 937 | auto& patches = getConfig().patches; |
| 938 | auto patchIt = findById<AudioPatch>(patches, in_patchId); |
| 939 | if (patchIt != patches.end()) { |
| 940 | cleanUpPatch(patchIt->id); |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 941 | updateStreamsConnectedState(*patchIt, AudioPatch{}); |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 942 | patches.erase(patchIt); |
| 943 | LOG(DEBUG) << __func__ << ": erased patch " << in_patchId; |
| 944 | return ndk::ScopedAStatus::ok(); |
| 945 | } |
| 946 | LOG(ERROR) << __func__ << ": patch id " << in_patchId << " not found"; |
| 947 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 948 | } |
| 949 | |
| 950 | ndk::ScopedAStatus Module::resetAudioPortConfig(int32_t in_portConfigId) { |
| 951 | auto& configs = getConfig().portConfigs; |
| 952 | auto configIt = findById<AudioPortConfig>(configs, in_portConfigId); |
| 953 | if (configIt != configs.end()) { |
| 954 | if (mStreams.count(in_portConfigId) != 0) { |
| 955 | LOG(ERROR) << __func__ << ": port config id " << in_portConfigId |
| 956 | << " has a stream opened on it"; |
| 957 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE); |
| 958 | } |
| 959 | auto patchIt = mPatches.find(in_portConfigId); |
| 960 | if (patchIt != mPatches.end()) { |
| 961 | LOG(ERROR) << __func__ << ": port config id " << in_portConfigId |
| 962 | << " is used by the patch with id " << patchIt->second; |
| 963 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE); |
| 964 | } |
| 965 | auto& initials = getConfig().initialConfigs; |
| 966 | auto initialIt = findById<AudioPortConfig>(initials, in_portConfigId); |
| 967 | if (initialIt == initials.end()) { |
| 968 | configs.erase(configIt); |
| 969 | LOG(DEBUG) << __func__ << ": erased port config " << in_portConfigId; |
| 970 | } else if (*configIt != *initialIt) { |
| 971 | *configIt = *initialIt; |
| 972 | LOG(DEBUG) << __func__ << ": reset port config " << in_portConfigId; |
| 973 | } |
| 974 | return ndk::ScopedAStatus::ok(); |
| 975 | } |
| 976 | LOG(ERROR) << __func__ << ": port config id " << in_portConfigId << " not found"; |
| 977 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 978 | } |
| 979 | |
Mikhail Naganov | 3b125b7 | 2022-10-05 02:12:39 +0000 | [diff] [blame] | 980 | ndk::ScopedAStatus Module::getMasterMute(bool* _aidl_return) { |
| 981 | *_aidl_return = mMasterMute; |
| 982 | LOG(DEBUG) << __func__ << ": returning " << *_aidl_return; |
| 983 | return ndk::ScopedAStatus::ok(); |
| 984 | } |
| 985 | |
| 986 | ndk::ScopedAStatus Module::setMasterMute(bool in_mute) { |
| 987 | LOG(DEBUG) << __func__ << ": " << in_mute; |
jiabin | 783c48b | 2023-02-28 18:28:06 +0000 | [diff] [blame] | 988 | auto result = mDebug.simulateDeviceConnections ? ndk::ScopedAStatus::ok() |
| 989 | : onMasterMuteChanged(in_mute); |
| 990 | if (result.isOk()) { |
| 991 | mMasterMute = in_mute; |
| 992 | } else { |
| 993 | LOG(ERROR) << __func__ << ": failed calling onMasterMuteChanged(" << in_mute |
| 994 | << "), error=" << result; |
| 995 | // Reset master mute if it failed. |
| 996 | onMasterMuteChanged(mMasterMute); |
| 997 | } |
| 998 | return std::move(result); |
Mikhail Naganov | 3b125b7 | 2022-10-05 02:12:39 +0000 | [diff] [blame] | 999 | } |
| 1000 | |
| 1001 | ndk::ScopedAStatus Module::getMasterVolume(float* _aidl_return) { |
| 1002 | *_aidl_return = mMasterVolume; |
| 1003 | LOG(DEBUG) << __func__ << ": returning " << *_aidl_return; |
| 1004 | return ndk::ScopedAStatus::ok(); |
| 1005 | } |
| 1006 | |
| 1007 | ndk::ScopedAStatus Module::setMasterVolume(float in_volume) { |
| 1008 | LOG(DEBUG) << __func__ << ": " << in_volume; |
| 1009 | if (in_volume >= 0.0f && in_volume <= 1.0f) { |
jiabin | 783c48b | 2023-02-28 18:28:06 +0000 | [diff] [blame] | 1010 | auto result = mDebug.simulateDeviceConnections ? ndk::ScopedAStatus::ok() |
| 1011 | : onMasterVolumeChanged(in_volume); |
| 1012 | if (result.isOk()) { |
| 1013 | mMasterVolume = in_volume; |
| 1014 | } else { |
| 1015 | // Reset master volume if it failed. |
| 1016 | LOG(ERROR) << __func__ << ": failed calling onMasterVolumeChanged(" << in_volume |
| 1017 | << "), error=" << result; |
| 1018 | onMasterVolumeChanged(mMasterVolume); |
| 1019 | } |
| 1020 | return std::move(result); |
Mikhail Naganov | 3b125b7 | 2022-10-05 02:12:39 +0000 | [diff] [blame] | 1021 | } |
| 1022 | LOG(ERROR) << __func__ << ": invalid master volume value: " << in_volume; |
| 1023 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 1024 | } |
| 1025 | |
| 1026 | ndk::ScopedAStatus Module::getMicMute(bool* _aidl_return) { |
| 1027 | *_aidl_return = mMicMute; |
| 1028 | LOG(DEBUG) << __func__ << ": returning " << *_aidl_return; |
| 1029 | return ndk::ScopedAStatus::ok(); |
| 1030 | } |
| 1031 | |
| 1032 | ndk::ScopedAStatus Module::setMicMute(bool in_mute) { |
| 1033 | LOG(DEBUG) << __func__ << ": " << in_mute; |
| 1034 | mMicMute = in_mute; |
| 1035 | return ndk::ScopedAStatus::ok(); |
| 1036 | } |
| 1037 | |
Mikhail Naganov | ef6bc74 | 2022-10-06 00:14:19 +0000 | [diff] [blame] | 1038 | ndk::ScopedAStatus Module::getMicrophones(std::vector<MicrophoneInfo>* _aidl_return) { |
Pawan Wagh | 6f57cd9 | 2023-02-01 21:14:34 +0000 | [diff] [blame] | 1039 | *_aidl_return = getConfig().microphones; |
Mikhail Naganov | ef6bc74 | 2022-10-06 00:14:19 +0000 | [diff] [blame] | 1040 | LOG(DEBUG) << __func__ << ": returning " << ::android::internal::ToString(*_aidl_return); |
| 1041 | return ndk::ScopedAStatus::ok(); |
| 1042 | } |
| 1043 | |
Mikhail Naganov | 3b125b7 | 2022-10-05 02:12:39 +0000 | [diff] [blame] | 1044 | ndk::ScopedAStatus Module::updateAudioMode(AudioMode in_mode) { |
Mikhail Naganov | 04ae822 | 2023-01-11 15:48:10 -0800 | [diff] [blame] | 1045 | if (!isValidAudioMode(in_mode)) { |
| 1046 | LOG(ERROR) << __func__ << ": invalid mode " << toString(in_mode); |
| 1047 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 1048 | } |
Mikhail Naganov | 3b125b7 | 2022-10-05 02:12:39 +0000 | [diff] [blame] | 1049 | // No checks for supported audio modes here, it's an informative notification. |
| 1050 | LOG(DEBUG) << __func__ << ": " << toString(in_mode); |
| 1051 | return ndk::ScopedAStatus::ok(); |
| 1052 | } |
| 1053 | |
| 1054 | ndk::ScopedAStatus Module::updateScreenRotation(ScreenRotation in_rotation) { |
| 1055 | LOG(DEBUG) << __func__ << ": " << toString(in_rotation); |
| 1056 | return ndk::ScopedAStatus::ok(); |
| 1057 | } |
| 1058 | |
| 1059 | ndk::ScopedAStatus Module::updateScreenState(bool in_isTurnedOn) { |
| 1060 | LOG(DEBUG) << __func__ << ": " << in_isTurnedOn; |
| 1061 | return ndk::ScopedAStatus::ok(); |
| 1062 | } |
| 1063 | |
Vlad Popa | 83a6d82 | 2022-11-07 13:53:57 +0100 | [diff] [blame] | 1064 | ndk::ScopedAStatus Module::getSoundDose(std::shared_ptr<ISoundDose>* _aidl_return) { |
Mikhail Naganov | 7499a00 | 2023-02-27 18:51:44 -0800 | [diff] [blame] | 1065 | if (!mSoundDose) { |
Vlad Popa | 2afbd1e | 2022-12-28 17:04:58 +0100 | [diff] [blame] | 1066 | mSoundDose = ndk::SharedRefBase::make<sounddose::SoundDose>(); |
Vlad Popa | 943b7e2 | 2022-12-08 14:24:12 +0100 | [diff] [blame] | 1067 | } |
Mikhail Naganov | 7499a00 | 2023-02-27 18:51:44 -0800 | [diff] [blame] | 1068 | *_aidl_return = mSoundDose.getPtr(); |
Vlad Popa | 943b7e2 | 2022-12-08 14:24:12 +0100 | [diff] [blame] | 1069 | LOG(DEBUG) << __func__ << ": returning instance of ISoundDose: " << _aidl_return->get(); |
Vlad Popa | 83a6d82 | 2022-11-07 13:53:57 +0100 | [diff] [blame] | 1070 | return ndk::ScopedAStatus::ok(); |
| 1071 | } |
| 1072 | |
Mikhail Naganov | e9f10fc | 2022-10-14 23:31:52 +0000 | [diff] [blame] | 1073 | ndk::ScopedAStatus Module::generateHwAvSyncId(int32_t* _aidl_return) { |
| 1074 | LOG(DEBUG) << __func__; |
| 1075 | (void)_aidl_return; |
| 1076 | return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION); |
| 1077 | } |
| 1078 | |
Mikhail Naganov | 20047bc | 2023-01-05 20:16:07 +0000 | [diff] [blame] | 1079 | const std::string Module::VendorDebug::kForceTransientBurstName = "aosp.forceTransientBurst"; |
Mikhail Naganov | 194daaa | 2023-01-05 22:34:20 +0000 | [diff] [blame] | 1080 | const std::string Module::VendorDebug::kForceSynchronousDrainName = "aosp.forceSynchronousDrain"; |
Mikhail Naganov | 20047bc | 2023-01-05 20:16:07 +0000 | [diff] [blame] | 1081 | |
Mikhail Naganov | e9f10fc | 2022-10-14 23:31:52 +0000 | [diff] [blame] | 1082 | ndk::ScopedAStatus Module::getVendorParameters(const std::vector<std::string>& in_ids, |
| 1083 | std::vector<VendorParameter>* _aidl_return) { |
| 1084 | LOG(DEBUG) << __func__ << ": id count: " << in_ids.size(); |
Mikhail Naganov | 20047bc | 2023-01-05 20:16:07 +0000 | [diff] [blame] | 1085 | bool allParametersKnown = true; |
| 1086 | for (const auto& id : in_ids) { |
| 1087 | if (id == VendorDebug::kForceTransientBurstName) { |
| 1088 | VendorParameter forceTransientBurst{.id = id}; |
| 1089 | forceTransientBurst.ext.setParcelable(Boolean{mVendorDebug.forceTransientBurst}); |
| 1090 | _aidl_return->push_back(std::move(forceTransientBurst)); |
Mikhail Naganov | 194daaa | 2023-01-05 22:34:20 +0000 | [diff] [blame] | 1091 | } else if (id == VendorDebug::kForceSynchronousDrainName) { |
| 1092 | VendorParameter forceSynchronousDrain{.id = id}; |
| 1093 | forceSynchronousDrain.ext.setParcelable(Boolean{mVendorDebug.forceSynchronousDrain}); |
| 1094 | _aidl_return->push_back(std::move(forceSynchronousDrain)); |
Mikhail Naganov | 20047bc | 2023-01-05 20:16:07 +0000 | [diff] [blame] | 1095 | } else { |
| 1096 | allParametersKnown = false; |
| 1097 | LOG(ERROR) << __func__ << ": unrecognized parameter \"" << id << "\""; |
| 1098 | } |
| 1099 | } |
| 1100 | if (allParametersKnown) return ndk::ScopedAStatus::ok(); |
| 1101 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
Mikhail Naganov | e9f10fc | 2022-10-14 23:31:52 +0000 | [diff] [blame] | 1102 | } |
| 1103 | |
Mikhail Naganov | 194daaa | 2023-01-05 22:34:20 +0000 | [diff] [blame] | 1104 | namespace { |
| 1105 | |
| 1106 | template <typename W> |
| 1107 | bool extractParameter(const VendorParameter& p, decltype(W::value)* v) { |
| 1108 | std::optional<W> value; |
| 1109 | binder_status_t result = p.ext.getParcelable(&value); |
| 1110 | if (result == STATUS_OK && value.has_value()) { |
| 1111 | *v = value.value().value; |
| 1112 | return true; |
| 1113 | } |
| 1114 | LOG(ERROR) << __func__ << ": failed to read the value of the parameter \"" << p.id |
| 1115 | << "\": " << result; |
| 1116 | return false; |
| 1117 | } |
| 1118 | |
| 1119 | } // namespace |
| 1120 | |
Mikhail Naganov | e9f10fc | 2022-10-14 23:31:52 +0000 | [diff] [blame] | 1121 | ndk::ScopedAStatus Module::setVendorParameters(const std::vector<VendorParameter>& in_parameters, |
| 1122 | bool in_async) { |
| 1123 | LOG(DEBUG) << __func__ << ": parameter count " << in_parameters.size() |
| 1124 | << ", async: " << in_async; |
Mikhail Naganov | 20047bc | 2023-01-05 20:16:07 +0000 | [diff] [blame] | 1125 | bool allParametersKnown = true; |
| 1126 | for (const auto& p : in_parameters) { |
| 1127 | if (p.id == VendorDebug::kForceTransientBurstName) { |
Mikhail Naganov | 194daaa | 2023-01-05 22:34:20 +0000 | [diff] [blame] | 1128 | if (!extractParameter<Boolean>(p, &mVendorDebug.forceTransientBurst)) { |
| 1129 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 1130 | } |
| 1131 | } else if (p.id == VendorDebug::kForceSynchronousDrainName) { |
| 1132 | if (!extractParameter<Boolean>(p, &mVendorDebug.forceSynchronousDrain)) { |
Mikhail Naganov | 20047bc | 2023-01-05 20:16:07 +0000 | [diff] [blame] | 1133 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 1134 | } |
| 1135 | } else { |
| 1136 | allParametersKnown = false; |
| 1137 | LOG(ERROR) << __func__ << ": unrecognized parameter \"" << p.id << "\""; |
| 1138 | } |
| 1139 | } |
| 1140 | if (allParametersKnown) return ndk::ScopedAStatus::ok(); |
| 1141 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
Mikhail Naganov | e9f10fc | 2022-10-14 23:31:52 +0000 | [diff] [blame] | 1142 | } |
| 1143 | |
Mikhail Naganov | fb1acde | 2022-12-12 18:57:36 +0000 | [diff] [blame] | 1144 | ndk::ScopedAStatus Module::addDeviceEffect( |
| 1145 | int32_t in_portConfigId, |
| 1146 | const std::shared_ptr<::aidl::android::hardware::audio::effect::IEffect>& in_effect) { |
| 1147 | if (in_effect == nullptr) { |
| 1148 | LOG(DEBUG) << __func__ << ": port id " << in_portConfigId << ", null effect"; |
| 1149 | } else { |
| 1150 | LOG(DEBUG) << __func__ << ": port id " << in_portConfigId << ", effect Binder " |
| 1151 | << in_effect->asBinder().get(); |
| 1152 | } |
| 1153 | return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION); |
| 1154 | } |
| 1155 | |
| 1156 | ndk::ScopedAStatus Module::removeDeviceEffect( |
| 1157 | int32_t in_portConfigId, |
| 1158 | const std::shared_ptr<::aidl::android::hardware::audio::effect::IEffect>& in_effect) { |
| 1159 | if (in_effect == nullptr) { |
| 1160 | LOG(DEBUG) << __func__ << ": port id " << in_portConfigId << ", null effect"; |
| 1161 | } else { |
| 1162 | LOG(DEBUG) << __func__ << ": port id " << in_portConfigId << ", effect Binder " |
| 1163 | << in_effect->asBinder().get(); |
| 1164 | } |
| 1165 | return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION); |
| 1166 | } |
| 1167 | |
jiabin | 9a8e686 | 2023-01-12 23:06:37 +0000 | [diff] [blame] | 1168 | ndk::ScopedAStatus Module::getMmapPolicyInfos(AudioMMapPolicyType mmapPolicyType, |
| 1169 | std::vector<AudioMMapPolicyInfo>* _aidl_return) { |
| 1170 | LOG(DEBUG) << __func__ << ": mmap policy type " << toString(mmapPolicyType); |
| 1171 | std::set<int32_t> mmapSinks; |
| 1172 | std::set<int32_t> mmapSources; |
| 1173 | auto& ports = getConfig().ports; |
| 1174 | for (const auto& port : ports) { |
| 1175 | if (port.flags.getTag() == AudioIoFlags::Tag::input && |
| 1176 | isBitPositionFlagSet(port.flags.get<AudioIoFlags::Tag::input>(), |
| 1177 | AudioInputFlags::MMAP_NOIRQ)) { |
| 1178 | mmapSinks.insert(port.id); |
| 1179 | } else if (port.flags.getTag() == AudioIoFlags::Tag::output && |
| 1180 | isBitPositionFlagSet(port.flags.get<AudioIoFlags::Tag::output>(), |
| 1181 | AudioOutputFlags::MMAP_NOIRQ)) { |
| 1182 | mmapSources.insert(port.id); |
| 1183 | } |
| 1184 | } |
| 1185 | for (const auto& route : getConfig().routes) { |
| 1186 | if (mmapSinks.count(route.sinkPortId) != 0) { |
| 1187 | // The sink is a mix port, add the sources if they are device ports. |
| 1188 | for (int sourcePortId : route.sourcePortIds) { |
| 1189 | auto sourcePortIt = findById<AudioPort>(ports, sourcePortId); |
| 1190 | if (sourcePortIt == ports.end()) { |
| 1191 | // This must not happen |
| 1192 | LOG(ERROR) << __func__ << ": port id " << sourcePortId << " cannot be found"; |
| 1193 | continue; |
| 1194 | } |
| 1195 | if (sourcePortIt->ext.getTag() != AudioPortExt::Tag::device) { |
| 1196 | // The source is not a device port, skip |
| 1197 | continue; |
| 1198 | } |
| 1199 | AudioMMapPolicyInfo policyInfo; |
| 1200 | policyInfo.device = sourcePortIt->ext.get<AudioPortExt::Tag::device>().device; |
| 1201 | // Always return AudioMMapPolicy.AUTO if the device supports mmap for |
| 1202 | // default implementation. |
| 1203 | policyInfo.mmapPolicy = AudioMMapPolicy::AUTO; |
| 1204 | _aidl_return->push_back(policyInfo); |
| 1205 | } |
| 1206 | } else { |
| 1207 | auto sinkPortIt = findById<AudioPort>(ports, route.sinkPortId); |
| 1208 | if (sinkPortIt == ports.end()) { |
| 1209 | // This must not happen |
| 1210 | LOG(ERROR) << __func__ << ": port id " << route.sinkPortId << " cannot be found"; |
| 1211 | continue; |
| 1212 | } |
| 1213 | if (sinkPortIt->ext.getTag() != AudioPortExt::Tag::device) { |
| 1214 | // The sink is not a device port, skip |
| 1215 | continue; |
| 1216 | } |
| 1217 | if (count_any(mmapSources, route.sourcePortIds)) { |
| 1218 | AudioMMapPolicyInfo policyInfo; |
| 1219 | policyInfo.device = sinkPortIt->ext.get<AudioPortExt::Tag::device>().device; |
| 1220 | // Always return AudioMMapPolicy.AUTO if the device supports mmap for |
| 1221 | // default implementation. |
| 1222 | policyInfo.mmapPolicy = AudioMMapPolicy::AUTO; |
| 1223 | _aidl_return->push_back(policyInfo); |
| 1224 | } |
| 1225 | } |
| 1226 | } |
| 1227 | return ndk::ScopedAStatus::ok(); |
| 1228 | } |
| 1229 | |
Eric Laurent | e2432ea | 2023-01-12 17:47:31 +0100 | [diff] [blame] | 1230 | ndk::ScopedAStatus Module::supportsVariableLatency(bool* _aidl_return) { |
| 1231 | LOG(DEBUG) << __func__; |
| 1232 | *_aidl_return = false; |
| 1233 | return ndk::ScopedAStatus::ok(); |
| 1234 | } |
| 1235 | |
jiabin | b76981e | 2023-01-18 00:58:30 +0000 | [diff] [blame] | 1236 | ndk::ScopedAStatus Module::getAAudioMixerBurstCount(int32_t* _aidl_return) { |
| 1237 | if (!isMmapSupported()) { |
| 1238 | LOG(DEBUG) << __func__ << ": mmap is not supported "; |
| 1239 | return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION); |
| 1240 | } |
| 1241 | *_aidl_return = DEFAULT_AAUDIO_MIXER_BURST_COUNT; |
| 1242 | LOG(DEBUG) << __func__ << ": returning " << *_aidl_return; |
| 1243 | return ndk::ScopedAStatus::ok(); |
| 1244 | } |
| 1245 | |
| 1246 | ndk::ScopedAStatus Module::getAAudioHardwareBurstMinUsec(int32_t* _aidl_return) { |
| 1247 | if (!isMmapSupported()) { |
| 1248 | LOG(DEBUG) << __func__ << ": mmap is not supported "; |
| 1249 | return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION); |
| 1250 | } |
| 1251 | *_aidl_return = DEFAULT_AAUDIO_HARDWARE_BURST_MIN_DURATION_US; |
| 1252 | LOG(DEBUG) << __func__ << ": returning " << *_aidl_return; |
| 1253 | return ndk::ScopedAStatus::ok(); |
| 1254 | } |
| 1255 | |
| 1256 | bool Module::isMmapSupported() { |
| 1257 | if (mIsMmapSupported.has_value()) { |
| 1258 | return mIsMmapSupported.value(); |
| 1259 | } |
| 1260 | std::vector<AudioMMapPolicyInfo> mmapPolicyInfos; |
| 1261 | if (!getMmapPolicyInfos(AudioMMapPolicyType::DEFAULT, &mmapPolicyInfos).isOk()) { |
| 1262 | mIsMmapSupported = false; |
| 1263 | } else { |
| 1264 | mIsMmapSupported = |
| 1265 | std::find_if(mmapPolicyInfos.begin(), mmapPolicyInfos.end(), [](const auto& info) { |
| 1266 | return info.mmapPolicy == AudioMMapPolicy::AUTO || |
| 1267 | info.mmapPolicy == AudioMMapPolicy::ALWAYS; |
| 1268 | }) != mmapPolicyInfos.end(); |
| 1269 | } |
| 1270 | return mIsMmapSupported.value(); |
| 1271 | } |
| 1272 | |
jiabin | 253bd32 | 2023-01-25 23:57:31 +0000 | [diff] [blame] | 1273 | ndk::ScopedAStatus Module::populateConnectedDevicePort(AudioPort* audioPort __unused) { |
jiabin | 116d839 | 2023-03-01 22:52:57 +0000 | [diff] [blame] | 1274 | LOG(VERBOSE) << __func__ << ": do nothing and return ok"; |
jiabin | 253bd32 | 2023-01-25 23:57:31 +0000 | [diff] [blame] | 1275 | return ndk::ScopedAStatus::ok(); |
| 1276 | } |
| 1277 | |
| 1278 | ndk::ScopedAStatus Module::checkAudioPatchEndpointsMatch( |
| 1279 | const std::vector<AudioPortConfig*>& sources __unused, |
| 1280 | const std::vector<AudioPortConfig*>& sinks __unused) { |
jiabin | 116d839 | 2023-03-01 22:52:57 +0000 | [diff] [blame] | 1281 | LOG(VERBOSE) << __func__ << ": do nothing and return ok"; |
jiabin | 253bd32 | 2023-01-25 23:57:31 +0000 | [diff] [blame] | 1282 | return ndk::ScopedAStatus::ok(); |
| 1283 | } |
| 1284 | |
jiabin | 783c48b | 2023-02-28 18:28:06 +0000 | [diff] [blame] | 1285 | void Module::onExternalDeviceConnectionChanged( |
| 1286 | const ::aidl::android::media::audio::common::AudioPort& audioPort __unused, |
| 1287 | bool connected __unused) { |
| 1288 | LOG(DEBUG) << __func__ << ": do nothing and return"; |
| 1289 | } |
| 1290 | |
| 1291 | ndk::ScopedAStatus Module::onMasterMuteChanged(bool mute __unused) { |
| 1292 | LOG(VERBOSE) << __func__ << ": do nothing and return ok"; |
| 1293 | return ndk::ScopedAStatus::ok(); |
| 1294 | } |
| 1295 | |
| 1296 | ndk::ScopedAStatus Module::onMasterVolumeChanged(float volume __unused) { |
| 1297 | LOG(VERBOSE) << __func__ << ": do nothing and return ok"; |
| 1298 | return ndk::ScopedAStatus::ok(); |
| 1299 | } |
| 1300 | |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 1301 | } // namespace aidl::android::hardware::audio::core |