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 | |
| 28 | #include "core-impl/Module.h" |
Vlad Popa | 943b7e2 | 2022-12-08 14:24:12 +0100 | [diff] [blame] | 29 | #include "core-impl/SoundDose.h" |
Mikhail Naganov | 3b125b7 | 2022-10-05 02:12:39 +0000 | [diff] [blame] | 30 | #include "core-impl/Telephony.h" |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 31 | #include "core-impl/utils.h" |
| 32 | |
| 33 | using aidl::android::hardware::audio::common::SinkMetadata; |
| 34 | using aidl::android::hardware::audio::common::SourceMetadata; |
Mikhail Naganov | 6a4872d | 2022-06-15 21:39:04 +0000 | [diff] [blame] | 35 | using aidl::android::media::audio::common::AudioChannelLayout; |
Mikhail Naganov | ef6bc74 | 2022-10-06 00:14:19 +0000 | [diff] [blame] | 36 | using aidl::android::media::audio::common::AudioDevice; |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 37 | using aidl::android::media::audio::common::AudioFormatDescription; |
Mikhail Naganov | 6a4872d | 2022-06-15 21:39:04 +0000 | [diff] [blame] | 38 | using aidl::android::media::audio::common::AudioFormatType; |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 39 | using aidl::android::media::audio::common::AudioInputFlags; |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 40 | using aidl::android::media::audio::common::AudioIoFlags; |
| 41 | using aidl::android::media::audio::common::AudioOffloadInfo; |
| 42 | using aidl::android::media::audio::common::AudioOutputFlags; |
| 43 | using aidl::android::media::audio::common::AudioPort; |
| 44 | using aidl::android::media::audio::common::AudioPortConfig; |
| 45 | using aidl::android::media::audio::common::AudioPortExt; |
| 46 | using aidl::android::media::audio::common::AudioProfile; |
| 47 | using aidl::android::media::audio::common::Int; |
Mikhail Naganov | 6a4872d | 2022-06-15 21:39:04 +0000 | [diff] [blame] | 48 | using aidl::android::media::audio::common::PcmType; |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 49 | using android::hardware::audio::common::getFrameSizeInBytes; |
Mikhail Naganov | a2c5ddf | 2022-09-12 22:57:14 +0000 | [diff] [blame] | 50 | using android::hardware::audio::common::isBitPositionFlagSet; |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 51 | |
| 52 | namespace aidl::android::hardware::audio::core { |
| 53 | |
| 54 | namespace { |
| 55 | |
| 56 | bool generateDefaultPortConfig(const AudioPort& port, AudioPortConfig* config) { |
| 57 | *config = {}; |
| 58 | config->portId = port.id; |
| 59 | if (port.profiles.empty()) { |
| 60 | LOG(ERROR) << __func__ << ": port " << port.id << " has no profiles"; |
| 61 | return false; |
| 62 | } |
| 63 | const auto& profile = port.profiles.begin(); |
| 64 | config->format = profile->format; |
| 65 | if (profile->channelMasks.empty()) { |
| 66 | LOG(ERROR) << __func__ << ": the first profile in port " << port.id |
| 67 | << " has no channel masks"; |
| 68 | return false; |
| 69 | } |
| 70 | config->channelMask = *profile->channelMasks.begin(); |
| 71 | if (profile->sampleRates.empty()) { |
| 72 | LOG(ERROR) << __func__ << ": the first profile in port " << port.id |
| 73 | << " has no sample rates"; |
| 74 | return false; |
| 75 | } |
| 76 | Int sampleRate; |
| 77 | sampleRate.value = *profile->sampleRates.begin(); |
| 78 | config->sampleRate = sampleRate; |
| 79 | config->flags = port.flags; |
| 80 | config->ext = port.ext; |
| 81 | return true; |
| 82 | } |
| 83 | |
| 84 | bool findAudioProfile(const AudioPort& port, const AudioFormatDescription& format, |
| 85 | AudioProfile* profile) { |
| 86 | if (auto profilesIt = |
| 87 | find_if(port.profiles.begin(), port.profiles.end(), |
| 88 | [&format](const auto& profile) { return profile.format == format; }); |
| 89 | profilesIt != port.profiles.end()) { |
| 90 | *profile = *profilesIt; |
| 91 | return true; |
| 92 | } |
| 93 | return false; |
| 94 | } |
Mikhail Naganov | 00603d1 | 2022-05-02 22:52:13 +0000 | [diff] [blame] | 95 | |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 96 | } // namespace |
| 97 | |
| 98 | void Module::cleanUpPatch(int32_t patchId) { |
| 99 | erase_all_values(mPatches, std::set<int32_t>{patchId}); |
| 100 | } |
| 101 | |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 102 | ndk::ScopedAStatus Module::createStreamContext(int32_t in_portConfigId, int64_t in_bufferSizeFrames, |
Mikhail Naganov | 30301a4 | 2022-09-13 01:20:45 +0000 | [diff] [blame] | 103 | std::shared_ptr<IStreamCallback> asyncCallback, |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 104 | StreamContext* out_context) { |
Mikhail Naganov | 6a4872d | 2022-06-15 21:39:04 +0000 | [diff] [blame] | 105 | if (in_bufferSizeFrames <= 0) { |
| 106 | LOG(ERROR) << __func__ << ": non-positive buffer size " << in_bufferSizeFrames; |
| 107 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 108 | } |
| 109 | if (in_bufferSizeFrames < kMinimumStreamBufferSizeFrames) { |
| 110 | LOG(ERROR) << __func__ << ": insufficient buffer size " << in_bufferSizeFrames |
| 111 | << ", must be at least " << kMinimumStreamBufferSizeFrames; |
| 112 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 113 | } |
| 114 | auto& configs = getConfig().portConfigs; |
| 115 | auto portConfigIt = findById<AudioPortConfig>(configs, in_portConfigId); |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 116 | // Since this is a private method, it is assumed that |
Mikhail Naganov | 6a4872d | 2022-06-15 21:39:04 +0000 | [diff] [blame] | 117 | // validity of the portConfigId has already been checked. |
| 118 | const size_t frameSize = |
| 119 | getFrameSizeInBytes(portConfigIt->format.value(), portConfigIt->channelMask.value()); |
| 120 | if (frameSize == 0) { |
| 121 | LOG(ERROR) << __func__ << ": could not calculate frame size for port config " |
| 122 | << portConfigIt->toString(); |
| 123 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 124 | } |
| 125 | LOG(DEBUG) << __func__ << ": frame size " << frameSize << " bytes"; |
| 126 | if (frameSize > kMaximumStreamBufferSizeBytes / in_bufferSizeFrames) { |
| 127 | LOG(ERROR) << __func__ << ": buffer size " << in_bufferSizeFrames |
| 128 | << " frames is too large, maximum size is " |
| 129 | << kMaximumStreamBufferSizeBytes / frameSize; |
| 130 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 131 | } |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 132 | const auto& flags = portConfigIt->flags.value(); |
| 133 | if ((flags.getTag() == AudioIoFlags::Tag::input && |
Mikhail Naganov | a2c5ddf | 2022-09-12 22:57:14 +0000 | [diff] [blame] | 134 | !isBitPositionFlagSet(flags.get<AudioIoFlags::Tag::input>(), |
| 135 | AudioInputFlags::MMAP_NOIRQ)) || |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 136 | (flags.getTag() == AudioIoFlags::Tag::output && |
Mikhail Naganov | a2c5ddf | 2022-09-12 22:57:14 +0000 | [diff] [blame] | 137 | !isBitPositionFlagSet(flags.get<AudioIoFlags::Tag::output>(), |
| 138 | AudioOutputFlags::MMAP_NOIRQ))) { |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 139 | StreamContext temp( |
| 140 | std::make_unique<StreamContext::CommandMQ>(1, true /*configureEventFlagWord*/), |
| 141 | std::make_unique<StreamContext::ReplyMQ>(1, true /*configureEventFlagWord*/), |
Mikhail Naganov | ef6bc74 | 2022-10-06 00:14:19 +0000 | [diff] [blame] | 142 | portConfigIt->format.value(), portConfigIt->channelMask.value(), |
| 143 | std::make_unique<StreamContext::DataMQ>(frameSize * in_bufferSizeFrames), |
Mikhail Naganov | 30301a4 | 2022-09-13 01:20:45 +0000 | [diff] [blame] | 144 | asyncCallback, mDebug.streamTransientStateDelayMs); |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 145 | if (temp.isValid()) { |
| 146 | *out_context = std::move(temp); |
| 147 | } else { |
| 148 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE); |
| 149 | } |
| 150 | } else { |
| 151 | // TODO: Implement simulation of MMAP buffer allocation |
| 152 | } |
Mikhail Naganov | 6a4872d | 2022-06-15 21:39:04 +0000 | [diff] [blame] | 153 | return ndk::ScopedAStatus::ok(); |
| 154 | } |
| 155 | |
Mikhail Naganov | ef6bc74 | 2022-10-06 00:14:19 +0000 | [diff] [blame] | 156 | std::vector<AudioDevice> Module::findConnectedDevices(int32_t portConfigId) { |
| 157 | std::vector<AudioDevice> result; |
| 158 | auto& ports = getConfig().ports; |
| 159 | auto portIds = portIdsFromPortConfigIds(findConnectedPortConfigIds(portConfigId)); |
| 160 | for (auto it = portIds.begin(); it != portIds.end(); ++it) { |
| 161 | auto portIt = findById<AudioPort>(ports, *it); |
| 162 | if (portIt != ports.end() && portIt->ext.getTag() == AudioPortExt::Tag::device) { |
| 163 | result.push_back(portIt->ext.template get<AudioPortExt::Tag::device>().device); |
| 164 | } |
| 165 | } |
| 166 | return result; |
| 167 | } |
| 168 | |
| 169 | std::set<int32_t> Module::findConnectedPortConfigIds(int32_t portConfigId) { |
| 170 | std::set<int32_t> result; |
| 171 | auto patchIdsRange = mPatches.equal_range(portConfigId); |
| 172 | auto& patches = getConfig().patches; |
| 173 | for (auto it = patchIdsRange.first; it != patchIdsRange.second; ++it) { |
| 174 | auto patchIt = findById<AudioPatch>(patches, it->second); |
| 175 | if (patchIt == patches.end()) { |
| 176 | LOG(FATAL) << __func__ << ": patch with id " << it->second << " taken from mPatches " |
| 177 | << "not found in the configuration"; |
| 178 | } |
| 179 | if (std::find(patchIt->sourcePortConfigIds.begin(), patchIt->sourcePortConfigIds.end(), |
| 180 | portConfigId) != patchIt->sourcePortConfigIds.end()) { |
| 181 | result.insert(patchIt->sinkPortConfigIds.begin(), patchIt->sinkPortConfigIds.end()); |
| 182 | } else { |
| 183 | result.insert(patchIt->sourcePortConfigIds.begin(), patchIt->sourcePortConfigIds.end()); |
| 184 | } |
| 185 | } |
| 186 | return result; |
| 187 | } |
| 188 | |
Mikhail Naganov | 6a4872d | 2022-06-15 21:39:04 +0000 | [diff] [blame] | 189 | ndk::ScopedAStatus Module::findPortIdForNewStream(int32_t in_portConfigId, AudioPort** port) { |
| 190 | auto& configs = getConfig().portConfigs; |
| 191 | auto portConfigIt = findById<AudioPortConfig>(configs, in_portConfigId); |
| 192 | if (portConfigIt == configs.end()) { |
| 193 | LOG(ERROR) << __func__ << ": existing port config id " << in_portConfigId << " not found"; |
| 194 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 195 | } |
| 196 | const int32_t portId = portConfigIt->portId; |
| 197 | // In our implementation, configs of mix ports always have unique IDs. |
| 198 | CHECK(portId != in_portConfigId); |
| 199 | auto& ports = getConfig().ports; |
| 200 | auto portIt = findById<AudioPort>(ports, portId); |
| 201 | if (portIt == ports.end()) { |
| 202 | LOG(ERROR) << __func__ << ": port id " << portId << " used by port config id " |
| 203 | << in_portConfigId << " not found"; |
| 204 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 205 | } |
| 206 | if (mStreams.count(in_portConfigId) != 0) { |
| 207 | LOG(ERROR) << __func__ << ": port config id " << in_portConfigId |
| 208 | << " already has a stream opened on it"; |
| 209 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE); |
| 210 | } |
| 211 | if (portIt->ext.getTag() != AudioPortExt::Tag::mix) { |
| 212 | LOG(ERROR) << __func__ << ": port config id " << in_portConfigId |
| 213 | << " does not correspond to a mix port"; |
| 214 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 215 | } |
| 216 | const int32_t maxOpenStreamCount = portIt->ext.get<AudioPortExt::Tag::mix>().maxOpenStreamCount; |
| 217 | if (maxOpenStreamCount != 0 && mStreams.count(portId) >= maxOpenStreamCount) { |
| 218 | LOG(ERROR) << __func__ << ": port id " << portId |
| 219 | << " has already reached maximum allowed opened stream count: " |
| 220 | << maxOpenStreamCount; |
| 221 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE); |
| 222 | } |
| 223 | *port = &(*portIt); |
| 224 | return ndk::ScopedAStatus::ok(); |
| 225 | } |
| 226 | |
Mikhail Naganov | ef6bc74 | 2022-10-06 00:14:19 +0000 | [diff] [blame] | 227 | template <typename C> |
| 228 | std::set<int32_t> Module::portIdsFromPortConfigIds(C portConfigIds) { |
| 229 | std::set<int32_t> result; |
| 230 | auto& portConfigs = getConfig().portConfigs; |
| 231 | for (auto it = portConfigIds.begin(); it != portConfigIds.end(); ++it) { |
| 232 | auto portConfigIt = findById<AudioPortConfig>(portConfigs, *it); |
| 233 | if (portConfigIt != portConfigs.end()) { |
| 234 | result.insert(portConfigIt->portId); |
| 235 | } |
| 236 | } |
| 237 | return result; |
| 238 | } |
| 239 | |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 240 | internal::Configuration& Module::getConfig() { |
| 241 | if (!mConfig) { |
Mikhail Naganov | c8e4312 | 2022-12-09 00:33:47 +0000 | [diff] [blame] | 242 | switch (mType) { |
| 243 | case Type::DEFAULT: |
| 244 | mConfig = std::move(internal::getPrimaryConfiguration()); |
| 245 | break; |
| 246 | case Type::R_SUBMIX: |
| 247 | mConfig = std::move(internal::getRSubmixConfiguration()); |
| 248 | break; |
| 249 | } |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 250 | } |
| 251 | return *mConfig; |
| 252 | } |
| 253 | |
| 254 | void Module::registerPatch(const AudioPatch& patch) { |
| 255 | auto& configs = getConfig().portConfigs; |
| 256 | auto do_insert = [&](const std::vector<int32_t>& portConfigIds) { |
| 257 | for (auto portConfigId : portConfigIds) { |
| 258 | auto configIt = findById<AudioPortConfig>(configs, portConfigId); |
| 259 | if (configIt != configs.end()) { |
| 260 | mPatches.insert(std::pair{portConfigId, patch.id}); |
| 261 | if (configIt->portId != portConfigId) { |
| 262 | mPatches.insert(std::pair{configIt->portId, patch.id}); |
| 263 | } |
| 264 | } |
| 265 | }; |
| 266 | }; |
| 267 | do_insert(patch.sourcePortConfigIds); |
| 268 | do_insert(patch.sinkPortConfigIds); |
| 269 | } |
| 270 | |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 271 | void Module::updateStreamsConnectedState(const AudioPatch& oldPatch, const AudioPatch& newPatch) { |
| 272 | // Streams from the old patch need to be disconnected, streams from the new |
| 273 | // patch need to be connected. If the stream belongs to both patches, no need |
| 274 | // to update it. |
| 275 | std::set<int32_t> idsToDisconnect, idsToConnect; |
| 276 | idsToDisconnect.insert(oldPatch.sourcePortConfigIds.begin(), |
| 277 | oldPatch.sourcePortConfigIds.end()); |
| 278 | idsToDisconnect.insert(oldPatch.sinkPortConfigIds.begin(), oldPatch.sinkPortConfigIds.end()); |
| 279 | idsToConnect.insert(newPatch.sourcePortConfigIds.begin(), newPatch.sourcePortConfigIds.end()); |
| 280 | idsToConnect.insert(newPatch.sinkPortConfigIds.begin(), newPatch.sinkPortConfigIds.end()); |
| 281 | std::for_each(idsToDisconnect.begin(), idsToDisconnect.end(), [&](const auto& portConfigId) { |
| 282 | if (idsToConnect.count(portConfigId) == 0) { |
Mikhail Naganov | ef6bc74 | 2022-10-06 00:14:19 +0000 | [diff] [blame] | 283 | LOG(DEBUG) << "The stream on port config id " << portConfigId << " is not connected"; |
| 284 | mStreams.setStreamIsConnected(portConfigId, {}); |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 285 | } |
| 286 | }); |
| 287 | std::for_each(idsToConnect.begin(), idsToConnect.end(), [&](const auto& portConfigId) { |
| 288 | if (idsToDisconnect.count(portConfigId) == 0) { |
Mikhail Naganov | ef6bc74 | 2022-10-06 00:14:19 +0000 | [diff] [blame] | 289 | const auto connectedDevices = findConnectedDevices(portConfigId); |
| 290 | LOG(DEBUG) << "The stream on port config id " << portConfigId |
| 291 | << " is connected to: " << ::android::internal::ToString(connectedDevices); |
| 292 | mStreams.setStreamIsConnected(portConfigId, connectedDevices); |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 293 | } |
| 294 | }); |
| 295 | } |
| 296 | |
Mikhail Naganov | 00603d1 | 2022-05-02 22:52:13 +0000 | [diff] [blame] | 297 | ndk::ScopedAStatus Module::setModuleDebug( |
| 298 | const ::aidl::android::hardware::audio::core::ModuleDebug& in_debug) { |
| 299 | LOG(DEBUG) << __func__ << ": old flags:" << mDebug.toString() |
| 300 | << ", new flags: " << in_debug.toString(); |
| 301 | if (mDebug.simulateDeviceConnections != in_debug.simulateDeviceConnections && |
| 302 | !mConnectedDevicePorts.empty()) { |
| 303 | LOG(ERROR) << __func__ << ": attempting to change device connections simulation " |
| 304 | << "while having external devices connected"; |
| 305 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE); |
| 306 | } |
Mikhail Naganov | bd483c0 | 2022-11-17 20:33:39 +0000 | [diff] [blame] | 307 | if (in_debug.streamTransientStateDelayMs < 0) { |
| 308 | LOG(ERROR) << __func__ << ": streamTransientStateDelayMs is negative: " |
| 309 | << in_debug.streamTransientStateDelayMs; |
| 310 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 311 | } |
Mikhail Naganov | 00603d1 | 2022-05-02 22:52:13 +0000 | [diff] [blame] | 312 | mDebug = in_debug; |
| 313 | return ndk::ScopedAStatus::ok(); |
| 314 | } |
| 315 | |
Mikhail Naganov | 3b125b7 | 2022-10-05 02:12:39 +0000 | [diff] [blame] | 316 | ndk::ScopedAStatus Module::getTelephony(std::shared_ptr<ITelephony>* _aidl_return) { |
| 317 | if (mTelephony == nullptr) { |
| 318 | mTelephony = ndk::SharedRefBase::make<Telephony>(); |
Shunkai Yao | 39bf2c3 | 2022-12-06 03:25:59 +0000 | [diff] [blame] | 319 | AIBinder_setMinSchedulerPolicy(mTelephony->asBinder().get(), SCHED_NORMAL, |
| 320 | ANDROID_PRIORITY_AUDIO); |
Mikhail Naganov | 3b125b7 | 2022-10-05 02:12:39 +0000 | [diff] [blame] | 321 | } |
| 322 | *_aidl_return = mTelephony; |
| 323 | LOG(DEBUG) << __func__ << ": returning instance of ITelephony: " << _aidl_return->get(); |
| 324 | return ndk::ScopedAStatus::ok(); |
| 325 | } |
| 326 | |
Mikhail Naganov | 00603d1 | 2022-05-02 22:52:13 +0000 | [diff] [blame] | 327 | ndk::ScopedAStatus Module::connectExternalDevice(const AudioPort& in_templateIdAndAdditionalData, |
| 328 | AudioPort* _aidl_return) { |
| 329 | const int32_t templateId = in_templateIdAndAdditionalData.id; |
| 330 | auto& ports = getConfig().ports; |
| 331 | AudioPort connectedPort; |
| 332 | { // Scope the template port so that we don't accidentally modify it. |
| 333 | auto templateIt = findById<AudioPort>(ports, templateId); |
| 334 | if (templateIt == ports.end()) { |
| 335 | LOG(ERROR) << __func__ << ": port id " << templateId << " not found"; |
| 336 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 337 | } |
| 338 | if (templateIt->ext.getTag() != AudioPortExt::Tag::device) { |
| 339 | LOG(ERROR) << __func__ << ": port id " << templateId << " is not a device port"; |
| 340 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 341 | } |
| 342 | if (!templateIt->profiles.empty()) { |
| 343 | LOG(ERROR) << __func__ << ": port id " << templateId |
| 344 | << " does not have dynamic profiles"; |
| 345 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 346 | } |
| 347 | auto& templateDevicePort = templateIt->ext.get<AudioPortExt::Tag::device>(); |
| 348 | if (templateDevicePort.device.type.connection.empty()) { |
| 349 | LOG(ERROR) << __func__ << ": port id " << templateId << " is permanently attached"; |
| 350 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 351 | } |
| 352 | // Postpone id allocation until we ensure that there are no client errors. |
| 353 | connectedPort = *templateIt; |
| 354 | connectedPort.extraAudioDescriptors = in_templateIdAndAdditionalData.extraAudioDescriptors; |
| 355 | const auto& inputDevicePort = |
| 356 | in_templateIdAndAdditionalData.ext.get<AudioPortExt::Tag::device>(); |
| 357 | auto& connectedDevicePort = connectedPort.ext.get<AudioPortExt::Tag::device>(); |
| 358 | connectedDevicePort.device.address = inputDevicePort.device.address; |
| 359 | LOG(DEBUG) << __func__ << ": device port " << connectedPort.id << " device set to " |
| 360 | << connectedDevicePort.device.toString(); |
| 361 | // Check if there is already a connected port with for the same external device. |
| 362 | for (auto connectedPortId : mConnectedDevicePorts) { |
| 363 | auto connectedPortIt = findById<AudioPort>(ports, connectedPortId); |
| 364 | if (connectedPortIt->ext.get<AudioPortExt::Tag::device>().device == |
| 365 | connectedDevicePort.device) { |
| 366 | LOG(ERROR) << __func__ << ": device " << connectedDevicePort.device.toString() |
| 367 | << " is already connected at the device port id " << connectedPortId; |
| 368 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE); |
| 369 | } |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | if (!mDebug.simulateDeviceConnections) { |
| 374 | // In a real HAL here we would attempt querying the profiles from the device. |
| 375 | LOG(ERROR) << __func__ << ": failed to query supported device profiles"; |
| 376 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE); |
| 377 | } |
| 378 | |
| 379 | connectedPort.id = ++getConfig().nextPortId; |
| 380 | mConnectedDevicePorts.insert(connectedPort.id); |
| 381 | LOG(DEBUG) << __func__ << ": template port " << templateId << " external device connected, " |
| 382 | << "connected port ID " << connectedPort.id; |
| 383 | auto& connectedProfiles = getConfig().connectedProfiles; |
| 384 | if (auto connectedProfilesIt = connectedProfiles.find(templateId); |
| 385 | connectedProfilesIt != connectedProfiles.end()) { |
| 386 | connectedPort.profiles = connectedProfilesIt->second; |
| 387 | } |
| 388 | ports.push_back(connectedPort); |
| 389 | *_aidl_return = std::move(connectedPort); |
| 390 | |
| 391 | std::vector<AudioRoute> newRoutes; |
| 392 | auto& routes = getConfig().routes; |
| 393 | for (auto& r : routes) { |
| 394 | if (r.sinkPortId == templateId) { |
| 395 | AudioRoute newRoute; |
| 396 | newRoute.sourcePortIds = r.sourcePortIds; |
| 397 | newRoute.sinkPortId = connectedPort.id; |
| 398 | newRoute.isExclusive = r.isExclusive; |
| 399 | newRoutes.push_back(std::move(newRoute)); |
| 400 | } else { |
| 401 | auto& srcs = r.sourcePortIds; |
| 402 | if (std::find(srcs.begin(), srcs.end(), templateId) != srcs.end()) { |
| 403 | srcs.push_back(connectedPort.id); |
| 404 | } |
| 405 | } |
| 406 | } |
| 407 | routes.insert(routes.end(), newRoutes.begin(), newRoutes.end()); |
| 408 | |
| 409 | return ndk::ScopedAStatus::ok(); |
| 410 | } |
| 411 | |
| 412 | ndk::ScopedAStatus Module::disconnectExternalDevice(int32_t in_portId) { |
| 413 | auto& ports = getConfig().ports; |
| 414 | auto portIt = findById<AudioPort>(ports, in_portId); |
| 415 | if (portIt == ports.end()) { |
| 416 | LOG(ERROR) << __func__ << ": port id " << in_portId << " not found"; |
| 417 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 418 | } |
| 419 | if (portIt->ext.getTag() != AudioPortExt::Tag::device) { |
| 420 | LOG(ERROR) << __func__ << ": port id " << in_portId << " is not a device port"; |
| 421 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 422 | } |
| 423 | if (mConnectedDevicePorts.count(in_portId) == 0) { |
| 424 | LOG(ERROR) << __func__ << ": port id " << in_portId << " is not a connected device port"; |
| 425 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 426 | } |
| 427 | auto& configs = getConfig().portConfigs; |
| 428 | auto& initials = getConfig().initialConfigs; |
| 429 | auto configIt = std::find_if(configs.begin(), configs.end(), [&](const auto& config) { |
| 430 | if (config.portId == in_portId) { |
| 431 | // Check if the configuration was provided by the client. |
| 432 | const auto& initialIt = findById<AudioPortConfig>(initials, config.id); |
| 433 | return initialIt == initials.end() || config != *initialIt; |
| 434 | } |
| 435 | return false; |
| 436 | }); |
| 437 | if (configIt != configs.end()) { |
| 438 | LOG(ERROR) << __func__ << ": port id " << in_portId << " has a non-default config with id " |
| 439 | << configIt->id; |
| 440 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE); |
| 441 | } |
| 442 | ports.erase(portIt); |
| 443 | mConnectedDevicePorts.erase(in_portId); |
| 444 | LOG(DEBUG) << __func__ << ": connected device port " << in_portId << " released"; |
| 445 | |
| 446 | auto& routes = getConfig().routes; |
| 447 | for (auto routesIt = routes.begin(); routesIt != routes.end();) { |
| 448 | if (routesIt->sinkPortId == in_portId) { |
| 449 | routesIt = routes.erase(routesIt); |
| 450 | } else { |
| 451 | // Note: the list of sourcePortIds can't become empty because there must |
| 452 | // be the id of the template port in the route. |
| 453 | erase_if(routesIt->sourcePortIds, [in_portId](auto src) { return src == in_portId; }); |
| 454 | ++routesIt; |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | return ndk::ScopedAStatus::ok(); |
| 459 | } |
| 460 | |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 461 | ndk::ScopedAStatus Module::getAudioPatches(std::vector<AudioPatch>* _aidl_return) { |
| 462 | *_aidl_return = getConfig().patches; |
| 463 | LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " patches"; |
| 464 | return ndk::ScopedAStatus::ok(); |
| 465 | } |
| 466 | |
| 467 | ndk::ScopedAStatus Module::getAudioPort(int32_t in_portId, AudioPort* _aidl_return) { |
| 468 | auto& ports = getConfig().ports; |
| 469 | auto portIt = findById<AudioPort>(ports, in_portId); |
| 470 | if (portIt != ports.end()) { |
| 471 | *_aidl_return = *portIt; |
| 472 | LOG(DEBUG) << __func__ << ": returning port by id " << in_portId; |
| 473 | return ndk::ScopedAStatus::ok(); |
| 474 | } |
| 475 | LOG(ERROR) << __func__ << ": port id " << in_portId << " not found"; |
| 476 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 477 | } |
| 478 | |
| 479 | ndk::ScopedAStatus Module::getAudioPortConfigs(std::vector<AudioPortConfig>* _aidl_return) { |
| 480 | *_aidl_return = getConfig().portConfigs; |
| 481 | LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " port configs"; |
| 482 | return ndk::ScopedAStatus::ok(); |
| 483 | } |
| 484 | |
| 485 | ndk::ScopedAStatus Module::getAudioPorts(std::vector<AudioPort>* _aidl_return) { |
| 486 | *_aidl_return = getConfig().ports; |
| 487 | LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " ports"; |
| 488 | return ndk::ScopedAStatus::ok(); |
| 489 | } |
| 490 | |
| 491 | ndk::ScopedAStatus Module::getAudioRoutes(std::vector<AudioRoute>* _aidl_return) { |
| 492 | *_aidl_return = getConfig().routes; |
| 493 | LOG(DEBUG) << __func__ << ": returning " << _aidl_return->size() << " routes"; |
| 494 | return ndk::ScopedAStatus::ok(); |
| 495 | } |
| 496 | |
Mikhail Naganov | 00603d1 | 2022-05-02 22:52:13 +0000 | [diff] [blame] | 497 | ndk::ScopedAStatus Module::getAudioRoutesForAudioPort(int32_t in_portId, |
| 498 | std::vector<AudioRoute>* _aidl_return) { |
| 499 | auto& ports = getConfig().ports; |
| 500 | if (auto portIt = findById<AudioPort>(ports, in_portId); portIt == ports.end()) { |
| 501 | LOG(ERROR) << __func__ << ": port id " << in_portId << " not found"; |
| 502 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 503 | } |
| 504 | auto& routes = getConfig().routes; |
| 505 | std::copy_if(routes.begin(), routes.end(), std::back_inserter(*_aidl_return), |
| 506 | [&](const auto& r) { |
| 507 | const auto& srcs = r.sourcePortIds; |
| 508 | return r.sinkPortId == in_portId || |
| 509 | std::find(srcs.begin(), srcs.end(), in_portId) != srcs.end(); |
| 510 | }); |
| 511 | return ndk::ScopedAStatus::ok(); |
| 512 | } |
| 513 | |
Mikhail Naganov | 6a4872d | 2022-06-15 21:39:04 +0000 | [diff] [blame] | 514 | ndk::ScopedAStatus Module::openInputStream(const OpenInputStreamArguments& in_args, |
| 515 | OpenInputStreamReturn* _aidl_return) { |
| 516 | LOG(DEBUG) << __func__ << ": port config id " << in_args.portConfigId << ", buffer size " |
| 517 | << in_args.bufferSizeFrames << " frames"; |
| 518 | AudioPort* port = nullptr; |
| 519 | if (auto status = findPortIdForNewStream(in_args.portConfigId, &port); !status.isOk()) { |
| 520 | return status; |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 521 | } |
Mikhail Naganov | 6a4872d | 2022-06-15 21:39:04 +0000 | [diff] [blame] | 522 | if (port->flags.getTag() != AudioIoFlags::Tag::input) { |
| 523 | LOG(ERROR) << __func__ << ": port config id " << in_args.portConfigId |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 524 | << " does not correspond to an input mix port"; |
| 525 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 526 | } |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 527 | StreamContext context; |
Mikhail Naganov | 30301a4 | 2022-09-13 01:20:45 +0000 | [diff] [blame] | 528 | if (auto status = createStreamContext(in_args.portConfigId, in_args.bufferSizeFrames, nullptr, |
| 529 | &context); |
Mikhail Naganov | 6a4872d | 2022-06-15 21:39:04 +0000 | [diff] [blame] | 530 | !status.isOk()) { |
| 531 | return status; |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 532 | } |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 533 | context.fillDescriptor(&_aidl_return->desc); |
Mikhail Naganov | ef6bc74 | 2022-10-06 00:14:19 +0000 | [diff] [blame] | 534 | auto stream = ndk::SharedRefBase::make<StreamIn>(in_args.sinkMetadata, std::move(context), |
| 535 | mConfig->microphones); |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 536 | if (auto status = stream->init(); !status.isOk()) { |
| 537 | return status; |
| 538 | } |
Shunkai Yao | 39bf2c3 | 2022-12-06 03:25:59 +0000 | [diff] [blame] | 539 | AIBinder_setMinSchedulerPolicy(stream->asBinder().get(), SCHED_NORMAL, ANDROID_PRIORITY_AUDIO); |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 540 | StreamWrapper streamWrapper(stream); |
| 541 | auto patchIt = mPatches.find(in_args.portConfigId); |
| 542 | if (patchIt != mPatches.end()) { |
Mikhail Naganov | ef6bc74 | 2022-10-06 00:14:19 +0000 | [diff] [blame] | 543 | streamWrapper.setStreamIsConnected(findConnectedDevices(in_args.portConfigId)); |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 544 | } |
| 545 | mStreams.insert(port->id, in_args.portConfigId, std::move(streamWrapper)); |
Mikhail Naganov | 6a4872d | 2022-06-15 21:39:04 +0000 | [diff] [blame] | 546 | _aidl_return->stream = std::move(stream); |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 547 | return ndk::ScopedAStatus::ok(); |
| 548 | } |
| 549 | |
Mikhail Naganov | 6a4872d | 2022-06-15 21:39:04 +0000 | [diff] [blame] | 550 | ndk::ScopedAStatus Module::openOutputStream(const OpenOutputStreamArguments& in_args, |
| 551 | OpenOutputStreamReturn* _aidl_return) { |
| 552 | LOG(DEBUG) << __func__ << ": port config id " << in_args.portConfigId << ", has offload info? " |
| 553 | << (in_args.offloadInfo.has_value()) << ", buffer size " << in_args.bufferSizeFrames |
| 554 | << " frames"; |
| 555 | AudioPort* port = nullptr; |
| 556 | if (auto status = findPortIdForNewStream(in_args.portConfigId, &port); !status.isOk()) { |
| 557 | return status; |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 558 | } |
Mikhail Naganov | 6a4872d | 2022-06-15 21:39:04 +0000 | [diff] [blame] | 559 | if (port->flags.getTag() != AudioIoFlags::Tag::output) { |
| 560 | LOG(ERROR) << __func__ << ": port config id " << in_args.portConfigId |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 561 | << " does not correspond to an output mix port"; |
| 562 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 563 | } |
Mikhail Naganov | a2c5ddf | 2022-09-12 22:57:14 +0000 | [diff] [blame] | 564 | const bool isOffload = isBitPositionFlagSet(port->flags.get<AudioIoFlags::Tag::output>(), |
| 565 | AudioOutputFlags::COMPRESS_OFFLOAD); |
| 566 | if (isOffload && !in_args.offloadInfo.has_value()) { |
Mikhail Naganov | 6a4872d | 2022-06-15 21:39:04 +0000 | [diff] [blame] | 567 | LOG(ERROR) << __func__ << ": port id " << port->id |
Mikhail Naganov | 111e0ce | 2022-06-17 21:41:19 +0000 | [diff] [blame] | 568 | << " has COMPRESS_OFFLOAD flag set, requires offload info"; |
| 569 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 570 | } |
Mikhail Naganov | 30301a4 | 2022-09-13 01:20:45 +0000 | [diff] [blame] | 571 | const bool isNonBlocking = isBitPositionFlagSet(port->flags.get<AudioIoFlags::Tag::output>(), |
| 572 | AudioOutputFlags::NON_BLOCKING); |
| 573 | if (isNonBlocking && in_args.callback == nullptr) { |
| 574 | LOG(ERROR) << __func__ << ": port id " << port->id |
| 575 | << " has NON_BLOCKING flag set, requires async callback"; |
| 576 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 577 | } |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 578 | StreamContext context; |
Mikhail Naganov | 30301a4 | 2022-09-13 01:20:45 +0000 | [diff] [blame] | 579 | if (auto status = createStreamContext(in_args.portConfigId, in_args.bufferSizeFrames, |
| 580 | isNonBlocking ? in_args.callback : nullptr, &context); |
Mikhail Naganov | 6a4872d | 2022-06-15 21:39:04 +0000 | [diff] [blame] | 581 | !status.isOk()) { |
| 582 | return status; |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 583 | } |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 584 | context.fillDescriptor(&_aidl_return->desc); |
| 585 | auto stream = ndk::SharedRefBase::make<StreamOut>(in_args.sourceMetadata, std::move(context), |
| 586 | in_args.offloadInfo); |
| 587 | if (auto status = stream->init(); !status.isOk()) { |
| 588 | return status; |
| 589 | } |
Shunkai Yao | 39bf2c3 | 2022-12-06 03:25:59 +0000 | [diff] [blame] | 590 | AIBinder_setMinSchedulerPolicy(stream->asBinder().get(), SCHED_NORMAL, ANDROID_PRIORITY_AUDIO); |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 591 | StreamWrapper streamWrapper(stream); |
| 592 | auto patchIt = mPatches.find(in_args.portConfigId); |
| 593 | if (patchIt != mPatches.end()) { |
Mikhail Naganov | ef6bc74 | 2022-10-06 00:14:19 +0000 | [diff] [blame] | 594 | streamWrapper.setStreamIsConnected(findConnectedDevices(in_args.portConfigId)); |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 595 | } |
| 596 | mStreams.insert(port->id, in_args.portConfigId, std::move(streamWrapper)); |
Mikhail Naganov | 6a4872d | 2022-06-15 21:39:04 +0000 | [diff] [blame] | 597 | _aidl_return->stream = std::move(stream); |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 598 | return ndk::ScopedAStatus::ok(); |
| 599 | } |
| 600 | |
| 601 | ndk::ScopedAStatus Module::setAudioPatch(const AudioPatch& in_requested, AudioPatch* _aidl_return) { |
Mikhail Naganov | 16db9b7 | 2022-06-17 21:36:18 +0000 | [diff] [blame] | 602 | LOG(DEBUG) << __func__ << ": requested patch " << in_requested.toString(); |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 603 | if (in_requested.sourcePortConfigIds.empty()) { |
| 604 | LOG(ERROR) << __func__ << ": requested patch has empty sources list"; |
| 605 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 606 | } |
| 607 | if (!all_unique<int32_t>(in_requested.sourcePortConfigIds)) { |
| 608 | LOG(ERROR) << __func__ << ": requested patch has duplicate ids in the sources list"; |
| 609 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 610 | } |
| 611 | if (in_requested.sinkPortConfigIds.empty()) { |
| 612 | LOG(ERROR) << __func__ << ": requested patch has empty sinks list"; |
| 613 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 614 | } |
| 615 | if (!all_unique<int32_t>(in_requested.sinkPortConfigIds)) { |
| 616 | LOG(ERROR) << __func__ << ": requested patch has duplicate ids in the sinks list"; |
| 617 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 618 | } |
| 619 | |
| 620 | auto& configs = getConfig().portConfigs; |
| 621 | std::vector<int32_t> missingIds; |
| 622 | auto sources = |
| 623 | selectByIds<AudioPortConfig>(configs, in_requested.sourcePortConfigIds, &missingIds); |
| 624 | if (!missingIds.empty()) { |
| 625 | LOG(ERROR) << __func__ << ": following source port config ids not found: " |
| 626 | << ::android::internal::ToString(missingIds); |
| 627 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 628 | } |
| 629 | auto sinks = selectByIds<AudioPortConfig>(configs, in_requested.sinkPortConfigIds, &missingIds); |
| 630 | if (!missingIds.empty()) { |
| 631 | LOG(ERROR) << __func__ << ": following sink port config ids not found: " |
| 632 | << ::android::internal::ToString(missingIds); |
| 633 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 634 | } |
| 635 | // bool indicates whether a non-exclusive route is available. |
| 636 | // If only an exclusive route is available, that means the patch can not be |
| 637 | // established if there is any other patch which currently uses the sink port. |
| 638 | std::map<int32_t, bool> allowedSinkPorts; |
| 639 | auto& routes = getConfig().routes; |
| 640 | for (auto src : sources) { |
| 641 | for (const auto& r : routes) { |
| 642 | const auto& srcs = r.sourcePortIds; |
| 643 | if (std::find(srcs.begin(), srcs.end(), src->portId) != srcs.end()) { |
| 644 | if (!allowedSinkPorts[r.sinkPortId]) { // prefer non-exclusive |
| 645 | allowedSinkPorts[r.sinkPortId] = !r.isExclusive; |
| 646 | } |
| 647 | } |
| 648 | } |
| 649 | } |
| 650 | for (auto sink : sinks) { |
| 651 | if (allowedSinkPorts.count(sink->portId) == 0) { |
| 652 | LOG(ERROR) << __func__ << ": there is no route to the sink port id " << sink->portId; |
| 653 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 654 | } |
| 655 | } |
| 656 | |
| 657 | auto& patches = getConfig().patches; |
| 658 | auto existing = patches.end(); |
| 659 | std::optional<decltype(mPatches)> patchesBackup; |
| 660 | if (in_requested.id != 0) { |
| 661 | existing = findById<AudioPatch>(patches, in_requested.id); |
| 662 | if (existing != patches.end()) { |
| 663 | patchesBackup = mPatches; |
| 664 | cleanUpPatch(existing->id); |
| 665 | } else { |
| 666 | LOG(ERROR) << __func__ << ": not found existing patch id " << in_requested.id; |
| 667 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 668 | } |
| 669 | } |
| 670 | // Validate the requested patch. |
| 671 | for (const auto& [sinkPortId, nonExclusive] : allowedSinkPorts) { |
| 672 | if (!nonExclusive && mPatches.count(sinkPortId) != 0) { |
| 673 | LOG(ERROR) << __func__ << ": sink port id " << sinkPortId |
| 674 | << "is exclusive and is already used by some other patch"; |
| 675 | if (patchesBackup.has_value()) { |
| 676 | mPatches = std::move(*patchesBackup); |
| 677 | } |
| 678 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE); |
| 679 | } |
| 680 | } |
| 681 | *_aidl_return = in_requested; |
Mikhail Naganov | 6a4872d | 2022-06-15 21:39:04 +0000 | [diff] [blame] | 682 | _aidl_return->minimumStreamBufferSizeFrames = kMinimumStreamBufferSizeFrames; |
| 683 | _aidl_return->latenciesMs.clear(); |
| 684 | _aidl_return->latenciesMs.insert(_aidl_return->latenciesMs.end(), |
| 685 | _aidl_return->sinkPortConfigIds.size(), kLatencyMs); |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 686 | AudioPatch oldPatch{}; |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 687 | if (existing == patches.end()) { |
| 688 | _aidl_return->id = getConfig().nextPatchId++; |
| 689 | patches.push_back(*_aidl_return); |
| 690 | existing = patches.begin() + (patches.size() - 1); |
| 691 | } else { |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 692 | oldPatch = *existing; |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 693 | *existing = *_aidl_return; |
| 694 | } |
| 695 | registerPatch(*existing); |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 696 | updateStreamsConnectedState(oldPatch, *_aidl_return); |
| 697 | |
| 698 | LOG(DEBUG) << __func__ << ": " << (oldPatch.id == 0 ? "created" : "updated") << " patch " |
| 699 | << _aidl_return->toString(); |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 700 | return ndk::ScopedAStatus::ok(); |
| 701 | } |
| 702 | |
| 703 | ndk::ScopedAStatus Module::setAudioPortConfig(const AudioPortConfig& in_requested, |
| 704 | AudioPortConfig* out_suggested, bool* _aidl_return) { |
| 705 | LOG(DEBUG) << __func__ << ": requested " << in_requested.toString(); |
| 706 | auto& configs = getConfig().portConfigs; |
| 707 | auto existing = configs.end(); |
| 708 | if (in_requested.id != 0) { |
| 709 | if (existing = findById<AudioPortConfig>(configs, in_requested.id); |
| 710 | existing == configs.end()) { |
| 711 | LOG(ERROR) << __func__ << ": existing port config id " << in_requested.id |
| 712 | << " not found"; |
| 713 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 714 | } |
| 715 | } |
| 716 | |
| 717 | const int portId = existing != configs.end() ? existing->portId : in_requested.portId; |
| 718 | if (portId == 0) { |
| 719 | LOG(ERROR) << __func__ << ": input port config does not specify portId"; |
| 720 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 721 | } |
| 722 | auto& ports = getConfig().ports; |
| 723 | auto portIt = findById<AudioPort>(ports, portId); |
| 724 | if (portIt == ports.end()) { |
| 725 | LOG(ERROR) << __func__ << ": input port config points to non-existent portId " << portId; |
| 726 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 727 | } |
| 728 | if (existing != configs.end()) { |
| 729 | *out_suggested = *existing; |
| 730 | } else { |
| 731 | AudioPortConfig newConfig; |
| 732 | if (generateDefaultPortConfig(*portIt, &newConfig)) { |
| 733 | *out_suggested = newConfig; |
| 734 | } else { |
| 735 | LOG(ERROR) << __func__ << ": unable generate a default config for port " << portId; |
| 736 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 737 | } |
| 738 | } |
| 739 | // From this moment, 'out_suggested' is either an existing port config, |
| 740 | // or a new generated config. Now attempt to update it according to the specified |
| 741 | // fields of 'in_requested'. |
| 742 | |
| 743 | bool requestedIsValid = true, requestedIsFullySpecified = true; |
| 744 | |
| 745 | AudioIoFlags portFlags = portIt->flags; |
| 746 | if (in_requested.flags.has_value()) { |
| 747 | if (in_requested.flags.value() != portFlags) { |
| 748 | LOG(WARNING) << __func__ << ": requested flags " |
| 749 | << in_requested.flags.value().toString() << " do not match port's " |
| 750 | << portId << " flags " << portFlags.toString(); |
| 751 | requestedIsValid = false; |
| 752 | } |
| 753 | } else { |
| 754 | requestedIsFullySpecified = false; |
| 755 | } |
| 756 | |
| 757 | AudioProfile portProfile; |
| 758 | if (in_requested.format.has_value()) { |
| 759 | const auto& format = in_requested.format.value(); |
| 760 | if (findAudioProfile(*portIt, format, &portProfile)) { |
| 761 | out_suggested->format = format; |
| 762 | } else { |
| 763 | LOG(WARNING) << __func__ << ": requested format " << format.toString() |
| 764 | << " is not found in port's " << portId << " profiles"; |
| 765 | requestedIsValid = false; |
| 766 | } |
| 767 | } else { |
| 768 | requestedIsFullySpecified = false; |
| 769 | } |
| 770 | if (!findAudioProfile(*portIt, out_suggested->format.value(), &portProfile)) { |
| 771 | LOG(ERROR) << __func__ << ": port " << portId << " does not support format " |
| 772 | << out_suggested->format.value().toString() << " anymore"; |
| 773 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 774 | } |
| 775 | |
| 776 | if (in_requested.channelMask.has_value()) { |
| 777 | const auto& channelMask = in_requested.channelMask.value(); |
| 778 | if (find(portProfile.channelMasks.begin(), portProfile.channelMasks.end(), channelMask) != |
| 779 | portProfile.channelMasks.end()) { |
| 780 | out_suggested->channelMask = channelMask; |
| 781 | } else { |
| 782 | LOG(WARNING) << __func__ << ": requested channel mask " << channelMask.toString() |
| 783 | << " is not supported for the format " << portProfile.format.toString() |
| 784 | << " by the port " << portId; |
| 785 | requestedIsValid = false; |
| 786 | } |
| 787 | } else { |
| 788 | requestedIsFullySpecified = false; |
| 789 | } |
| 790 | |
| 791 | if (in_requested.sampleRate.has_value()) { |
| 792 | const auto& sampleRate = in_requested.sampleRate.value(); |
| 793 | if (find(portProfile.sampleRates.begin(), portProfile.sampleRates.end(), |
| 794 | sampleRate.value) != portProfile.sampleRates.end()) { |
| 795 | out_suggested->sampleRate = sampleRate; |
| 796 | } else { |
| 797 | LOG(WARNING) << __func__ << ": requested sample rate " << sampleRate.value |
| 798 | << " is not supported for the format " << portProfile.format.toString() |
| 799 | << " by the port " << portId; |
| 800 | requestedIsValid = false; |
| 801 | } |
| 802 | } else { |
| 803 | requestedIsFullySpecified = false; |
| 804 | } |
| 805 | |
| 806 | if (in_requested.gain.has_value()) { |
| 807 | // Let's pretend that gain can always be applied. |
| 808 | out_suggested->gain = in_requested.gain.value(); |
| 809 | } |
| 810 | |
| 811 | if (existing == configs.end() && requestedIsValid && requestedIsFullySpecified) { |
| 812 | out_suggested->id = getConfig().nextPortId++; |
| 813 | configs.push_back(*out_suggested); |
| 814 | *_aidl_return = true; |
| 815 | LOG(DEBUG) << __func__ << ": created new port config " << out_suggested->toString(); |
| 816 | } else if (existing != configs.end() && requestedIsValid) { |
| 817 | *existing = *out_suggested; |
| 818 | *_aidl_return = true; |
| 819 | LOG(DEBUG) << __func__ << ": updated port config " << out_suggested->toString(); |
| 820 | } else { |
| 821 | LOG(DEBUG) << __func__ << ": not applied; existing config ? " << (existing != configs.end()) |
| 822 | << "; requested is valid? " << requestedIsValid << ", fully specified? " |
| 823 | << requestedIsFullySpecified; |
| 824 | *_aidl_return = false; |
| 825 | } |
| 826 | return ndk::ScopedAStatus::ok(); |
| 827 | } |
| 828 | |
| 829 | ndk::ScopedAStatus Module::resetAudioPatch(int32_t in_patchId) { |
| 830 | auto& patches = getConfig().patches; |
| 831 | auto patchIt = findById<AudioPatch>(patches, in_patchId); |
| 832 | if (patchIt != patches.end()) { |
| 833 | cleanUpPatch(patchIt->id); |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 834 | updateStreamsConnectedState(*patchIt, AudioPatch{}); |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 835 | patches.erase(patchIt); |
| 836 | LOG(DEBUG) << __func__ << ": erased patch " << in_patchId; |
| 837 | return ndk::ScopedAStatus::ok(); |
| 838 | } |
| 839 | LOG(ERROR) << __func__ << ": patch id " << in_patchId << " not found"; |
| 840 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 841 | } |
| 842 | |
| 843 | ndk::ScopedAStatus Module::resetAudioPortConfig(int32_t in_portConfigId) { |
| 844 | auto& configs = getConfig().portConfigs; |
| 845 | auto configIt = findById<AudioPortConfig>(configs, in_portConfigId); |
| 846 | if (configIt != configs.end()) { |
| 847 | if (mStreams.count(in_portConfigId) != 0) { |
| 848 | LOG(ERROR) << __func__ << ": port config id " << in_portConfigId |
| 849 | << " has a stream opened on it"; |
| 850 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE); |
| 851 | } |
| 852 | auto patchIt = mPatches.find(in_portConfigId); |
| 853 | if (patchIt != mPatches.end()) { |
| 854 | LOG(ERROR) << __func__ << ": port config id " << in_portConfigId |
| 855 | << " is used by the patch with id " << patchIt->second; |
| 856 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE); |
| 857 | } |
| 858 | auto& initials = getConfig().initialConfigs; |
| 859 | auto initialIt = findById<AudioPortConfig>(initials, in_portConfigId); |
| 860 | if (initialIt == initials.end()) { |
| 861 | configs.erase(configIt); |
| 862 | LOG(DEBUG) << __func__ << ": erased port config " << in_portConfigId; |
| 863 | } else if (*configIt != *initialIt) { |
| 864 | *configIt = *initialIt; |
| 865 | LOG(DEBUG) << __func__ << ": reset port config " << in_portConfigId; |
| 866 | } |
| 867 | return ndk::ScopedAStatus::ok(); |
| 868 | } |
| 869 | LOG(ERROR) << __func__ << ": port config id " << in_portConfigId << " not found"; |
| 870 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 871 | } |
| 872 | |
Mikhail Naganov | 3b125b7 | 2022-10-05 02:12:39 +0000 | [diff] [blame] | 873 | ndk::ScopedAStatus Module::getMasterMute(bool* _aidl_return) { |
| 874 | *_aidl_return = mMasterMute; |
| 875 | LOG(DEBUG) << __func__ << ": returning " << *_aidl_return; |
| 876 | return ndk::ScopedAStatus::ok(); |
| 877 | } |
| 878 | |
| 879 | ndk::ScopedAStatus Module::setMasterMute(bool in_mute) { |
| 880 | LOG(DEBUG) << __func__ << ": " << in_mute; |
| 881 | mMasterMute = in_mute; |
| 882 | return ndk::ScopedAStatus::ok(); |
| 883 | } |
| 884 | |
| 885 | ndk::ScopedAStatus Module::getMasterVolume(float* _aidl_return) { |
| 886 | *_aidl_return = mMasterVolume; |
| 887 | LOG(DEBUG) << __func__ << ": returning " << *_aidl_return; |
| 888 | return ndk::ScopedAStatus::ok(); |
| 889 | } |
| 890 | |
| 891 | ndk::ScopedAStatus Module::setMasterVolume(float in_volume) { |
| 892 | LOG(DEBUG) << __func__ << ": " << in_volume; |
| 893 | if (in_volume >= 0.0f && in_volume <= 1.0f) { |
| 894 | mMasterVolume = in_volume; |
| 895 | return ndk::ScopedAStatus::ok(); |
| 896 | } |
| 897 | LOG(ERROR) << __func__ << ": invalid master volume value: " << in_volume; |
| 898 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 899 | } |
| 900 | |
| 901 | ndk::ScopedAStatus Module::getMicMute(bool* _aidl_return) { |
| 902 | *_aidl_return = mMicMute; |
| 903 | LOG(DEBUG) << __func__ << ": returning " << *_aidl_return; |
| 904 | return ndk::ScopedAStatus::ok(); |
| 905 | } |
| 906 | |
| 907 | ndk::ScopedAStatus Module::setMicMute(bool in_mute) { |
| 908 | LOG(DEBUG) << __func__ << ": " << in_mute; |
| 909 | mMicMute = in_mute; |
| 910 | return ndk::ScopedAStatus::ok(); |
| 911 | } |
| 912 | |
Mikhail Naganov | ef6bc74 | 2022-10-06 00:14:19 +0000 | [diff] [blame] | 913 | ndk::ScopedAStatus Module::getMicrophones(std::vector<MicrophoneInfo>* _aidl_return) { |
| 914 | *_aidl_return = mConfig->microphones; |
| 915 | LOG(DEBUG) << __func__ << ": returning " << ::android::internal::ToString(*_aidl_return); |
| 916 | return ndk::ScopedAStatus::ok(); |
| 917 | } |
| 918 | |
Mikhail Naganov | 3b125b7 | 2022-10-05 02:12:39 +0000 | [diff] [blame] | 919 | ndk::ScopedAStatus Module::updateAudioMode(AudioMode in_mode) { |
| 920 | // No checks for supported audio modes here, it's an informative notification. |
| 921 | LOG(DEBUG) << __func__ << ": " << toString(in_mode); |
| 922 | return ndk::ScopedAStatus::ok(); |
| 923 | } |
| 924 | |
| 925 | ndk::ScopedAStatus Module::updateScreenRotation(ScreenRotation in_rotation) { |
| 926 | LOG(DEBUG) << __func__ << ": " << toString(in_rotation); |
| 927 | return ndk::ScopedAStatus::ok(); |
| 928 | } |
| 929 | |
| 930 | ndk::ScopedAStatus Module::updateScreenState(bool in_isTurnedOn) { |
| 931 | LOG(DEBUG) << __func__ << ": " << in_isTurnedOn; |
| 932 | return ndk::ScopedAStatus::ok(); |
| 933 | } |
| 934 | |
Vlad Popa | 83a6d82 | 2022-11-07 13:53:57 +0100 | [diff] [blame] | 935 | ndk::ScopedAStatus Module::getSoundDose(std::shared_ptr<ISoundDose>* _aidl_return) { |
Vlad Popa | 943b7e2 | 2022-12-08 14:24:12 +0100 | [diff] [blame] | 936 | if (mSoundDose == nullptr) { |
| 937 | mSoundDose = ndk::SharedRefBase::make<SoundDose>(); |
| 938 | } |
| 939 | *_aidl_return = mSoundDose; |
| 940 | LOG(DEBUG) << __func__ << ": returning instance of ISoundDose: " << _aidl_return->get(); |
Vlad Popa | 83a6d82 | 2022-11-07 13:53:57 +0100 | [diff] [blame] | 941 | return ndk::ScopedAStatus::ok(); |
| 942 | } |
| 943 | |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 944 | } // namespace aidl::android::hardware::audio::core |