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