Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +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 | #define LOG_TAG "DeviceHalAidl" |
Mikhail Naganov | 89a9f74 | 2023-01-30 12:33:18 -0800 | [diff] [blame] | 18 | // #define LOG_NDEBUG 0 |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 19 | |
Mikhail Naganov | 5b1eed1 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 20 | #include <algorithm> |
| 21 | #include <forward_list> |
| 22 | |
Mikhail Naganov | dfd594e | 2023-02-08 16:59:41 -0800 | [diff] [blame] | 23 | #include <aidl/android/hardware/audio/core/BnStreamCallback.h> |
| 24 | #include <aidl/android/hardware/audio/core/BnStreamOutEventCallback.h> |
Mikhail Naganov | fab697c | 2023-01-11 19:33:13 +0000 | [diff] [blame] | 25 | #include <aidl/android/hardware/audio/core/StreamDescriptor.h> |
| 26 | #include <error/expected_utils.h> |
| 27 | #include <media/AidlConversionCppNdk.h> |
Mikhail Naganov | 25bc9a2 | 2023-04-21 18:48:16 -0700 | [diff] [blame] | 28 | #include <media/AidlConversionNdkCpp.h> |
Mikhail Naganov | fab697c | 2023-01-11 19:33:13 +0000 | [diff] [blame] | 29 | #include <media/AidlConversionUtil.h> |
Mikhail Naganov | 31d4665 | 2023-01-10 18:29:25 +0000 | [diff] [blame] | 30 | #include <mediautils/TimeCheck.h> |
Mikhail Naganov | 89a9f74 | 2023-01-30 12:33:18 -0800 | [diff] [blame] | 31 | #include <Utils.h> |
Mikhail Naganov | 31d4665 | 2023-01-10 18:29:25 +0000 | [diff] [blame] | 32 | #include <utils/Log.h> |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 33 | |
Mikhail Naganov | 31d4665 | 2023-01-10 18:29:25 +0000 | [diff] [blame] | 34 | #include "DeviceHalAidl.h" |
| 35 | #include "StreamHalAidl.h" |
| 36 | |
Mikhail Naganov | fab697c | 2023-01-11 19:33:13 +0000 | [diff] [blame] | 37 | using aidl::android::aidl_utils::statusTFromBinderStatus; |
Mikhail Naganov | ccc8211 | 2023-04-27 18:14:15 -0700 | [diff] [blame] | 38 | using aidl::android::media::audio::common::Boolean; |
Mikhail Naganov | e93a086 | 2023-03-15 17:06:59 -0700 | [diff] [blame] | 39 | using aidl::android::media::audio::common::AudioChannelLayout; |
Mikhail Naganov | 5b1eed1 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 40 | using aidl::android::media::audio::common::AudioConfig; |
| 41 | using aidl::android::media::audio::common::AudioDevice; |
David Li | 9cf5e62 | 2023-03-21 00:51:10 +0800 | [diff] [blame] | 42 | using aidl::android::media::audio::common::AudioDeviceAddress; |
Mikhail Naganov | 89a9f74 | 2023-01-30 12:33:18 -0800 | [diff] [blame] | 43 | using aidl::android::media::audio::common::AudioDeviceType; |
Mikhail Naganov | e93a086 | 2023-03-15 17:06:59 -0700 | [diff] [blame] | 44 | using aidl::android::media::audio::common::AudioFormatType; |
Mikhail Naganov | 89a9f74 | 2023-01-30 12:33:18 -0800 | [diff] [blame] | 45 | using aidl::android::media::audio::common::AudioInputFlags; |
Mikhail Naganov | 5b1eed1 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 46 | using aidl::android::media::audio::common::AudioIoFlags; |
Mikhail Naganov | dfd594e | 2023-02-08 16:59:41 -0800 | [diff] [blame] | 47 | using aidl::android::media::audio::common::AudioLatencyMode; |
David Li | 9cf5e62 | 2023-03-21 00:51:10 +0800 | [diff] [blame] | 48 | using aidl::android::media::audio::common::AudioMMapPolicy; |
| 49 | using aidl::android::media::audio::common::AudioMMapPolicyInfo; |
| 50 | using aidl::android::media::audio::common::AudioMMapPolicyType; |
Mikhail Naganov | fab697c | 2023-01-11 19:33:13 +0000 | [diff] [blame] | 51 | using aidl::android::media::audio::common::AudioMode; |
Mikhail Naganov | 5b1eed1 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 52 | using aidl::android::media::audio::common::AudioOutputFlags; |
| 53 | using aidl::android::media::audio::common::AudioPort; |
| 54 | using aidl::android::media::audio::common::AudioPortConfig; |
Mikhail Naganov | 89a9f74 | 2023-01-30 12:33:18 -0800 | [diff] [blame] | 55 | using aidl::android::media::audio::common::AudioPortDeviceExt; |
David Li | 9cf5e62 | 2023-03-21 00:51:10 +0800 | [diff] [blame] | 56 | using aidl::android::media::audio::common::AudioPortExt; |
Mikhail Naganov | 1a44bc6 | 2023-02-16 17:35:06 -0800 | [diff] [blame] | 57 | using aidl::android::media::audio::common::AudioPortMixExt; |
Mikhail Naganov | d8d01f7 | 2023-03-09 16:24:40 -0800 | [diff] [blame] | 58 | using aidl::android::media::audio::common::AudioPortMixExtUseCase; |
Mikhail Naganov | e93a086 | 2023-03-15 17:06:59 -0700 | [diff] [blame] | 59 | using aidl::android::media::audio::common::AudioProfile; |
Mikhail Naganov | 5b1eed1 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 60 | using aidl::android::media::audio::common::AudioSource; |
Mikhail Naganov | fab697c | 2023-01-11 19:33:13 +0000 | [diff] [blame] | 61 | using aidl::android::media::audio::common::Float; |
David Li | 9cf5e62 | 2023-03-21 00:51:10 +0800 | [diff] [blame] | 62 | using aidl::android::media::audio::common::Int; |
| 63 | using aidl::android::media::audio::common::MicrophoneDynamicInfo; |
| 64 | using aidl::android::media::audio::common::MicrophoneInfo; |
Mikhail Naganov | e7a26ad | 2023-05-25 17:36:48 -0700 | [diff] [blame] | 65 | using aidl::android::media::audio::IHalAdapterVendorExtension; |
Mikhail Naganov | 6352e82 | 2023-03-09 18:22:36 -0800 | [diff] [blame] | 66 | using aidl::android::hardware::audio::common::getFrameSizeInBytes; |
| 67 | using aidl::android::hardware::audio::common::isBitPositionFlagSet; |
Mikhail Naganov | e93a086 | 2023-03-15 17:06:59 -0700 | [diff] [blame] | 68 | using aidl::android::hardware::audio::common::isDefaultAudioFormat; |
Mikhail Naganov | 6352e82 | 2023-03-09 18:22:36 -0800 | [diff] [blame] | 69 | using aidl::android::hardware::audio::common::makeBitPositionFlagMask; |
Mikhail Naganov | 5b1eed1 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 70 | using aidl::android::hardware::audio::common::RecordTrackMetadata; |
| 71 | using aidl::android::hardware::audio::core::AudioPatch; |
Mikhail Naganov | 289468a | 2023-03-29 10:06:15 -0700 | [diff] [blame] | 72 | using aidl::android::hardware::audio::core::AudioRoute; |
Mikhail Naganov | ccc8211 | 2023-04-27 18:14:15 -0700 | [diff] [blame] | 73 | using aidl::android::hardware::audio::core::IBluetooth; |
| 74 | using aidl::android::hardware::audio::core::IBluetoothA2dp; |
| 75 | using aidl::android::hardware::audio::core::IBluetoothLe; |
Mikhail Naganov | fab697c | 2023-01-11 19:33:13 +0000 | [diff] [blame] | 76 | using aidl::android::hardware::audio::core::IModule; |
| 77 | using aidl::android::hardware::audio::core::ITelephony; |
Mikhail Naganov | e93a086 | 2023-03-15 17:06:59 -0700 | [diff] [blame] | 78 | using aidl::android::hardware::audio::core::ModuleDebug; |
Mikhail Naganov | fab697c | 2023-01-11 19:33:13 +0000 | [diff] [blame] | 79 | using aidl::android::hardware::audio::core::StreamDescriptor; |
Mikhail Naganov | e7a26ad | 2023-05-25 17:36:48 -0700 | [diff] [blame] | 80 | using aidl::android::hardware::audio::core::VendorParameter; |
Mikhail Naganov | 31d4665 | 2023-01-10 18:29:25 +0000 | [diff] [blame] | 81 | |
| 82 | namespace android { |
| 83 | |
Mikhail Naganov | 5b1eed1 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 84 | namespace { |
| 85 | |
| 86 | bool isConfigEqualToPortConfig(const AudioConfig& config, const AudioPortConfig& portConfig) { |
| 87 | return portConfig.sampleRate.value().value == config.base.sampleRate && |
| 88 | portConfig.channelMask.value() == config.base.channelMask && |
| 89 | portConfig.format.value() == config.base.format; |
| 90 | } |
| 91 | |
| 92 | void setConfigFromPortConfig(AudioConfig* config, const AudioPortConfig& portConfig) { |
| 93 | config->base.sampleRate = portConfig.sampleRate.value().value; |
| 94 | config->base.channelMask = portConfig.channelMask.value(); |
| 95 | config->base.format = portConfig.format.value(); |
| 96 | } |
| 97 | |
| 98 | void setPortConfigFromConfig(AudioPortConfig* portConfig, const AudioConfig& config) { |
| 99 | portConfig->sampleRate = Int{ .value = config.base.sampleRate }; |
| 100 | portConfig->channelMask = config.base.channelMask; |
| 101 | portConfig->format = config.base.format; |
| 102 | } |
| 103 | |
Mikhail Naganov | f83b974 | 2023-04-24 13:06:04 -0700 | [diff] [blame] | 104 | // Note: these converters are for types defined in different AIDL files. Although these |
| 105 | // AIDL files are copies of each other, however formally these are different types |
| 106 | // thus we don't use a conversion via a parcelable. |
| 107 | ConversionResult<media::AudioRoute> ndk2cpp_AudioRoute(const AudioRoute& ndk) { |
| 108 | media::AudioRoute cpp; |
| 109 | cpp.sourcePortIds.insert( |
| 110 | cpp.sourcePortIds.end(), ndk.sourcePortIds.begin(), ndk.sourcePortIds.end()); |
| 111 | cpp.sinkPortId = ndk.sinkPortId; |
| 112 | cpp.isExclusive = ndk.isExclusive; |
David Li | 9cf5e62 | 2023-03-21 00:51:10 +0800 | [diff] [blame] | 113 | return cpp; |
| 114 | } |
| 115 | |
Mikhail Naganov | 1fba38c | 2023-05-03 17:45:36 -0700 | [diff] [blame] | 116 | template<typename T> |
| 117 | std::shared_ptr<T> retrieveSubInterface(const std::shared_ptr<IModule>& module, |
| 118 | ::ndk::ScopedAStatus (IModule::*getT)(std::shared_ptr<T>*)) { |
| 119 | if (module != nullptr) { |
| 120 | std::shared_ptr<T> instance; |
| 121 | if (auto status = (module.get()->*getT)(&instance); status.isOk()) { |
| 122 | return instance; |
| 123 | } |
| 124 | } |
| 125 | return nullptr; |
| 126 | } |
| 127 | |
Mikhail Naganov | 5b1eed1 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 128 | } // namespace |
| 129 | |
Mikhail Naganov | e7a26ad | 2023-05-25 17:36:48 -0700 | [diff] [blame] | 130 | DeviceHalAidl::DeviceHalAidl(const std::string& instance, const std::shared_ptr<IModule>& module, |
| 131 | const std::shared_ptr<IHalAdapterVendorExtension>& vext) |
Mikhail Naganov | 1fba38c | 2023-05-03 17:45:36 -0700 | [diff] [blame] | 132 | : ConversionHelperAidl("DeviceHalAidl"), |
Mikhail Naganov | e7a26ad | 2023-05-25 17:36:48 -0700 | [diff] [blame] | 133 | mInstance(instance), mModule(module), mVendorExt(vext), |
Mikhail Naganov | ccc8211 | 2023-04-27 18:14:15 -0700 | [diff] [blame] | 134 | mTelephony(retrieveSubInterface<ITelephony>(module, &IModule::getTelephony)), |
| 135 | mBluetooth(retrieveSubInterface<IBluetooth>(module, &IModule::getBluetooth)), |
| 136 | mBluetoothA2dp(retrieveSubInterface<IBluetoothA2dp>(module, &IModule::getBluetoothA2dp)), |
| 137 | mBluetoothLe(retrieveSubInterface<IBluetoothLe>(module, &IModule::getBluetoothLe)) { |
Mikhail Naganov | 1fba38c | 2023-05-03 17:45:36 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Mikhail Naganov | f83b974 | 2023-04-24 13:06:04 -0700 | [diff] [blame] | 140 | status_t DeviceHalAidl::getAudioPorts(std::vector<media::audio::common::AudioPort> *ports) { |
Mikhail Naganov | 9e459d7 | 2023-05-05 17:36:39 -0700 | [diff] [blame] | 141 | return ::aidl::android::convertContainer(mPorts, ports, |
| 142 | [](const Ports::value_type& pair) { return ndk2cpp_AudioPort(pair.second); }); |
Mikhail Naganov | f83b974 | 2023-04-24 13:06:04 -0700 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | status_t DeviceHalAidl::getAudioRoutes(std::vector<media::AudioRoute> *routes) { |
| 146 | *routes = VALUE_OR_RETURN_STATUS( |
| 147 | ::aidl::android::convertContainer<std::vector<media::AudioRoute>>( |
| 148 | mRoutes, ndk2cpp_AudioRoute)); |
| 149 | return OK; |
| 150 | } |
| 151 | |
Mikhail Naganov | 1fba38c | 2023-05-03 17:45:36 -0700 | [diff] [blame] | 152 | status_t DeviceHalAidl::getSupportedModes(std::vector<media::audio::common::AudioMode> *modes) { |
| 153 | TIME_CHECK(); |
| 154 | if (modes == nullptr) { |
| 155 | return BAD_VALUE; |
| 156 | } |
| 157 | if (mModule == nullptr) return NO_INIT; |
| 158 | if (mTelephony == nullptr) return INVALID_OPERATION; |
| 159 | std::vector<AudioMode> aidlModes; |
| 160 | RETURN_STATUS_IF_ERROR( |
| 161 | statusTFromBinderStatus(mTelephony->getSupportedAudioModes(&aidlModes))); |
| 162 | *modes = VALUE_OR_RETURN_STATUS( |
| 163 | ::aidl::android::convertContainer<std::vector<media::audio::common::AudioMode>>( |
| 164 | aidlModes, ndk2cpp_AudioMode)); |
| 165 | return OK; |
| 166 | } |
| 167 | |
Mikhail Naganov | 31d4665 | 2023-01-10 18:29:25 +0000 | [diff] [blame] | 168 | status_t DeviceHalAidl::getSupportedDevices(uint32_t*) { |
| 169 | // Obsolete. |
| 170 | return INVALID_OPERATION; |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | status_t DeviceHalAidl::initCheck() { |
Mikhail Naganov | 5b1eed1 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 174 | TIME_CHECK(); |
Mikhail Naganov | 31d4665 | 2023-01-10 18:29:25 +0000 | [diff] [blame] | 175 | if (mModule == nullptr) return NO_INIT; |
Mikhail Naganov | 5b1eed1 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 176 | std::vector<AudioPort> ports; |
Mikhail Naganov | f83b974 | 2023-04-24 13:06:04 -0700 | [diff] [blame] | 177 | RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(mModule->getAudioPorts(&ports))); |
Mikhail Naganov | 5b1eed1 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 178 | ALOGW_IF(ports.empty(), "%s: module %s returned an empty list of audio ports", |
| 179 | __func__, mInstance.c_str()); |
| 180 | std::transform(ports.begin(), ports.end(), std::inserter(mPorts, mPorts.end()), |
| 181 | [](const auto& p) { return std::make_pair(p.id, p); }); |
Mikhail Naganov | 89a9f74 | 2023-01-30 12:33:18 -0800 | [diff] [blame] | 182 | mDefaultInputPortId = mDefaultOutputPortId = -1; |
| 183 | const int defaultDeviceFlag = 1 << AudioPortDeviceExt::FLAG_INDEX_DEFAULT_DEVICE; |
| 184 | for (const auto& pair : mPorts) { |
| 185 | const auto& p = pair.second; |
| 186 | if (p.ext.getTag() == AudioPortExt::Tag::device && |
| 187 | (p.ext.get<AudioPortExt::Tag::device>().flags & defaultDeviceFlag) != 0) { |
| 188 | if (p.flags.getTag() == AudioIoFlags::Tag::input) { |
| 189 | mDefaultInputPortId = p.id; |
| 190 | } else if (p.flags.getTag() == AudioIoFlags::Tag::output) { |
| 191 | mDefaultOutputPortId = p.id; |
| 192 | } |
| 193 | } |
| 194 | } |
| 195 | ALOGI("%s: module %s default port ids: input %d, output %d", |
| 196 | __func__, mInstance.c_str(), mDefaultInputPortId, mDefaultOutputPortId); |
Mikhail Naganov | 289468a | 2023-03-29 10:06:15 -0700 | [diff] [blame] | 197 | RETURN_STATUS_IF_ERROR(updateRoutes()); |
Mikhail Naganov | 5b1eed1 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 198 | std::vector<AudioPortConfig> portConfigs; |
| 199 | RETURN_STATUS_IF_ERROR( |
| 200 | statusTFromBinderStatus(mModule->getAudioPortConfigs(&portConfigs))); // OK if empty |
| 201 | std::transform(portConfigs.begin(), portConfigs.end(), |
| 202 | std::inserter(mPortConfigs, mPortConfigs.end()), |
| 203 | [](const auto& p) { return std::make_pair(p.id, p); }); |
jiabin | 9c07faf | 2023-04-26 22:00:44 +0000 | [diff] [blame] | 204 | std::transform(mPortConfigs.begin(), mPortConfigs.end(), |
| 205 | std::inserter(mInitialPortConfigIds, mInitialPortConfigIds.end()), |
| 206 | [](const auto& pcPair) { return pcPair.first; }); |
Mikhail Naganov | 5b1eed1 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 207 | std::vector<AudioPatch> patches; |
| 208 | RETURN_STATUS_IF_ERROR( |
| 209 | statusTFromBinderStatus(mModule->getAudioPatches(&patches))); // OK if empty |
| 210 | std::transform(patches.begin(), patches.end(), |
| 211 | std::inserter(mPatches, mPatches.end()), |
| 212 | [](const auto& p) { return std::make_pair(p.id, p); }); |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 213 | return OK; |
| 214 | } |
| 215 | |
| 216 | status_t DeviceHalAidl::setVoiceVolume(float volume) { |
Mikhail Naganov | 31d4665 | 2023-01-10 18:29:25 +0000 | [diff] [blame] | 217 | TIME_CHECK(); |
Mikhail Naganov | fab697c | 2023-01-11 19:33:13 +0000 | [diff] [blame] | 218 | if (!mModule) return NO_INIT; |
Mikhail Naganov | 1fba38c | 2023-05-03 17:45:36 -0700 | [diff] [blame] | 219 | if (mTelephony == nullptr) return INVALID_OPERATION; |
| 220 | ITelephony::TelecomConfig inConfig{ .voiceVolume = Float{volume} }, outConfig; |
| 221 | RETURN_STATUS_IF_ERROR( |
| 222 | statusTFromBinderStatus(mTelephony->setTelecomConfig(inConfig, &outConfig))); |
| 223 | ALOGW_IF(outConfig.voiceVolume.has_value() && volume != outConfig.voiceVolume.value().value, |
| 224 | "%s: the resulting voice volume %f is not the same as requested %f", |
| 225 | __func__, outConfig.voiceVolume.value().value, volume); |
| 226 | return OK; |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | status_t DeviceHalAidl::setMasterVolume(float volume) { |
Mikhail Naganov | 31d4665 | 2023-01-10 18:29:25 +0000 | [diff] [blame] | 230 | TIME_CHECK(); |
Mikhail Naganov | fab697c | 2023-01-11 19:33:13 +0000 | [diff] [blame] | 231 | if (!mModule) return NO_INIT; |
| 232 | return statusTFromBinderStatus(mModule->setMasterVolume(volume)); |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | status_t DeviceHalAidl::getMasterVolume(float *volume) { |
Mikhail Naganov | 31d4665 | 2023-01-10 18:29:25 +0000 | [diff] [blame] | 236 | TIME_CHECK(); |
Mikhail Naganov | fab697c | 2023-01-11 19:33:13 +0000 | [diff] [blame] | 237 | if (!mModule) return NO_INIT; |
| 238 | return statusTFromBinderStatus(mModule->getMasterVolume(volume)); |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 239 | } |
| 240 | |
Mikhail Naganov | fab697c | 2023-01-11 19:33:13 +0000 | [diff] [blame] | 241 | status_t DeviceHalAidl::setMode(audio_mode_t mode) { |
Mikhail Naganov | 31d4665 | 2023-01-10 18:29:25 +0000 | [diff] [blame] | 242 | TIME_CHECK(); |
| 243 | if (!mModule) return NO_INIT; |
Mikhail Naganov | fab697c | 2023-01-11 19:33:13 +0000 | [diff] [blame] | 244 | AudioMode audioMode = VALUE_OR_FATAL(::aidl::android::legacy2aidl_audio_mode_t_AudioMode(mode)); |
Mikhail Naganov | 1fba38c | 2023-05-03 17:45:36 -0700 | [diff] [blame] | 245 | if (mTelephony != nullptr) { |
| 246 | RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(mTelephony->switchAudioMode(audioMode))); |
Mikhail Naganov | fab697c | 2023-01-11 19:33:13 +0000 | [diff] [blame] | 247 | } |
| 248 | return statusTFromBinderStatus(mModule->updateAudioMode(audioMode)); |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | status_t DeviceHalAidl::setMicMute(bool state) { |
Mikhail Naganov | 31d4665 | 2023-01-10 18:29:25 +0000 | [diff] [blame] | 252 | TIME_CHECK(); |
Mikhail Naganov | fab697c | 2023-01-11 19:33:13 +0000 | [diff] [blame] | 253 | if (!mModule) return NO_INIT; |
| 254 | return statusTFromBinderStatus(mModule->setMicMute(state)); |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 255 | } |
Mikhail Naganov | 31d4665 | 2023-01-10 18:29:25 +0000 | [diff] [blame] | 256 | |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 257 | status_t DeviceHalAidl::getMicMute(bool *state) { |
Mikhail Naganov | 31d4665 | 2023-01-10 18:29:25 +0000 | [diff] [blame] | 258 | TIME_CHECK(); |
Mikhail Naganov | fab697c | 2023-01-11 19:33:13 +0000 | [diff] [blame] | 259 | if (!mModule) return NO_INIT; |
| 260 | return statusTFromBinderStatus(mModule->getMicMute(state)); |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 261 | } |
Mikhail Naganov | 31d4665 | 2023-01-10 18:29:25 +0000 | [diff] [blame] | 262 | |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 263 | status_t DeviceHalAidl::setMasterMute(bool state) { |
Mikhail Naganov | 31d4665 | 2023-01-10 18:29:25 +0000 | [diff] [blame] | 264 | TIME_CHECK(); |
Mikhail Naganov | fab697c | 2023-01-11 19:33:13 +0000 | [diff] [blame] | 265 | if (!mModule) return NO_INIT; |
| 266 | return statusTFromBinderStatus(mModule->setMasterMute(state)); |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 267 | } |
Mikhail Naganov | 31d4665 | 2023-01-10 18:29:25 +0000 | [diff] [blame] | 268 | |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 269 | status_t DeviceHalAidl::getMasterMute(bool *state) { |
Mikhail Naganov | 31d4665 | 2023-01-10 18:29:25 +0000 | [diff] [blame] | 270 | TIME_CHECK(); |
Mikhail Naganov | fab697c | 2023-01-11 19:33:13 +0000 | [diff] [blame] | 271 | if (!mModule) return NO_INIT; |
| 272 | return statusTFromBinderStatus(mModule->getMasterMute(state)); |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 273 | } |
| 274 | |
Mikhail Naganov | ccc8211 | 2023-04-27 18:14:15 -0700 | [diff] [blame] | 275 | status_t DeviceHalAidl::setParameters(const String8& kvPairs) { |
Mikhail Naganov | 31d4665 | 2023-01-10 18:29:25 +0000 | [diff] [blame] | 276 | if (!mModule) return NO_INIT; |
Mikhail Naganov | ccc8211 | 2023-04-27 18:14:15 -0700 | [diff] [blame] | 277 | AudioParameter parameters(kvPairs); |
| 278 | ALOGD("%s: parameters: \"%s\"", __func__, parameters.toString().c_str()); |
| 279 | |
| 280 | if (status_t status = filterAndUpdateBtA2dpParameters(parameters); status != OK) { |
| 281 | ALOGW("%s: filtering or updating BT A2DP parameters failed: %d", __func__, status); |
| 282 | } |
| 283 | if (status_t status = filterAndUpdateBtHfpParameters(parameters); status != OK) { |
| 284 | ALOGW("%s: filtering or updating BT HFP parameters failed: %d", __func__, status); |
| 285 | } |
| 286 | if (status_t status = filterAndUpdateBtLeParameters(parameters); status != OK) { |
| 287 | ALOGW("%s: filtering or updating BT LE parameters failed: %d", __func__, status); |
| 288 | } |
| 289 | if (status_t status = filterAndUpdateBtScoParameters(parameters); status != OK) { |
| 290 | ALOGW("%s: filtering or updating BT SCO parameters failed: %d", __func__, status); |
| 291 | } |
Mikhail Naganov | e92c34b | 2023-05-31 14:24:48 -0700 | [diff] [blame] | 292 | if (status_t status = filterAndUpdateScreenParameters(parameters); status != OK) { |
| 293 | ALOGW("%s: filtering or updating screen parameters failed: %d", __func__, status); |
| 294 | } |
Mikhail Naganov | e7a26ad | 2023-05-25 17:36:48 -0700 | [diff] [blame] | 295 | return parseAndSetVendorParameters(mVendorExt, mModule, parameters); |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 296 | } |
| 297 | |
Mikhail Naganov | e7a26ad | 2023-05-25 17:36:48 -0700 | [diff] [blame] | 298 | status_t DeviceHalAidl::getParameters(const String8& keys, String8 *values) { |
Mikhail Naganov | 31d4665 | 2023-01-10 18:29:25 +0000 | [diff] [blame] | 299 | TIME_CHECK(); |
Mikhail Naganov | 31d4665 | 2023-01-10 18:29:25 +0000 | [diff] [blame] | 300 | if (!mModule) return NO_INIT; |
Mikhail Naganov | e7a26ad | 2023-05-25 17:36:48 -0700 | [diff] [blame] | 301 | if (values == nullptr) { |
| 302 | return BAD_VALUE; |
| 303 | } |
| 304 | AudioParameter parameterKeys(keys), result; |
| 305 | if (status_t status = filterAndRetrieveBtA2dpParameters(parameterKeys, &result); status != OK) { |
| 306 | ALOGW("%s: filtering or retrieving BT A2DP parameters failed: %d", __func__, status); |
| 307 | } |
| 308 | *values = result.toString(); |
| 309 | return parseAndGetVendorParameters(mVendorExt, mModule, parameterKeys, values); |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 310 | } |
| 311 | |
Mikhail Naganov | 5b1eed1 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 312 | namespace { |
| 313 | |
| 314 | class Cleanup { |
| 315 | public: |
| 316 | typedef void (DeviceHalAidl::*Cleaner)(int32_t); |
| 317 | |
| 318 | Cleanup(DeviceHalAidl* device, Cleaner cleaner, int32_t id) : |
| 319 | mDevice(device), mCleaner(cleaner), mId(id) {} |
| 320 | ~Cleanup() { clean(); } |
| 321 | void clean() { |
| 322 | if (mDevice != nullptr) (mDevice->*mCleaner)(mId); |
| 323 | disarm(); |
| 324 | } |
| 325 | void disarm() { mDevice = nullptr; } |
| 326 | |
| 327 | private: |
| 328 | DeviceHalAidl* mDevice; |
| 329 | const Cleaner mCleaner; |
| 330 | const int32_t mId; |
| 331 | }; |
| 332 | |
| 333 | } // namespace |
| 334 | |
| 335 | // Since the order of container elements destruction is unspecified, |
| 336 | // ensure that cleanups are performed from the most recent one and upwards. |
| 337 | // This is the same as if there were individual Cleanup instances on the stack, |
| 338 | // however the bonus is that we can disarm all of them with just one statement. |
| 339 | class DeviceHalAidl::Cleanups : public std::forward_list<Cleanup> { |
| 340 | public: |
| 341 | ~Cleanups() { for (auto& c : *this) c.clean(); } |
| 342 | void disarmAll() { for (auto& c : *this) c.disarm(); } |
| 343 | }; |
| 344 | |
Mikhail Naganov | 89a9f74 | 2023-01-30 12:33:18 -0800 | [diff] [blame] | 345 | status_t DeviceHalAidl::getInputBufferSize(const struct audio_config* config, size_t* size) { |
| 346 | ALOGD("%p %s::%s", this, getClassName().c_str(), __func__); |
| 347 | if (size == nullptr) return BAD_VALUE; |
| 348 | TIME_CHECK(); |
| 349 | if (!mModule) return NO_INIT; |
| 350 | AudioConfig aidlConfig = VALUE_OR_RETURN_STATUS( |
| 351 | ::aidl::android::legacy2aidl_audio_config_t_AudioConfig(*config, true /*isInput*/)); |
| 352 | AudioDevice aidlDevice; |
| 353 | aidlDevice.type.type = AudioDeviceType::IN_DEFAULT; |
Mikhail Naganov | d8d01f7 | 2023-03-09 16:24:40 -0800 | [diff] [blame] | 354 | AudioSource aidlSource = AudioSource::DEFAULT; |
Mikhail Naganov | 89a9f74 | 2023-01-30 12:33:18 -0800 | [diff] [blame] | 355 | AudioIoFlags aidlFlags = AudioIoFlags::make<AudioIoFlags::Tag::input>(0); |
| 356 | AudioPortConfig mixPortConfig; |
| 357 | Cleanups cleanups; |
| 358 | audio_config writableConfig = *config; |
Mikhail Naganov | e93a086 | 2023-03-15 17:06:59 -0700 | [diff] [blame] | 359 | AudioPatch aidlPatch; |
Mikhail Naganov | d8d01f7 | 2023-03-09 16:24:40 -0800 | [diff] [blame] | 360 | RETURN_STATUS_IF_ERROR(prepareToOpenStream(0 /*handle*/, aidlDevice, aidlFlags, aidlSource, |
Mikhail Naganov | e93a086 | 2023-03-15 17:06:59 -0700 | [diff] [blame] | 361 | &writableConfig, &cleanups, &aidlConfig, &mixPortConfig, &aidlPatch)); |
Mikhail Naganov | 89a9f74 | 2023-01-30 12:33:18 -0800 | [diff] [blame] | 362 | *size = aidlConfig.frameCount * |
| 363 | getFrameSizeInBytes(aidlConfig.base.format, aidlConfig.base.channelMask); |
| 364 | // Do not disarm cleanups to release temporary port configs. |
| 365 | return OK; |
| 366 | } |
| 367 | |
Mikhail Naganov | 5b1eed1 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 368 | status_t DeviceHalAidl::prepareToOpenStream( |
| 369 | int32_t aidlHandle, const AudioDevice& aidlDevice, const AudioIoFlags& aidlFlags, |
Mikhail Naganov | d8d01f7 | 2023-03-09 16:24:40 -0800 | [diff] [blame] | 370 | AudioSource aidlSource, struct audio_config* config, |
Mikhail Naganov | 89a9f74 | 2023-01-30 12:33:18 -0800 | [diff] [blame] | 371 | Cleanups* cleanups, AudioConfig* aidlConfig, AudioPortConfig* mixPortConfig, |
Mikhail Naganov | e93a086 | 2023-03-15 17:06:59 -0700 | [diff] [blame] | 372 | AudioPatch* aidlPatch) { |
| 373 | ALOGD("%p %s::%s: handle %d, device %s, flags %s, source %s, config %s, mix port config %s", |
| 374 | this, getClassName().c_str(), __func__, aidlHandle, aidlDevice.toString().c_str(), |
| 375 | aidlFlags.toString().c_str(), toString(aidlSource).c_str(), |
| 376 | aidlConfig->toString().c_str(), mixPortConfig->toString().c_str()); |
jiabin | 9c07faf | 2023-04-26 22:00:44 +0000 | [diff] [blame] | 377 | resetUnusedPatchesAndPortConfigs(); |
Mikhail Naganov | 5b1eed1 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 378 | const bool isInput = aidlFlags.getTag() == AudioIoFlags::Tag::input; |
| 379 | // Find / create AudioPortConfigs for the device port and the mix port, |
| 380 | // then find / create a patch between them, and open a stream on the mix port. |
| 381 | AudioPortConfig devicePortConfig; |
| 382 | bool created = false; |
jiabin | 9c07faf | 2023-04-26 22:00:44 +0000 | [diff] [blame] | 383 | RETURN_STATUS_IF_ERROR(findOrCreatePortConfig(aidlDevice, aidlConfig, |
| 384 | &devicePortConfig, &created)); |
Mikhail Naganov | 5b1eed1 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 385 | if (created) { |
| 386 | cleanups->emplace_front(this, &DeviceHalAidl::resetPortConfig, devicePortConfig.id); |
| 387 | } |
Mikhail Naganov | d8d01f7 | 2023-03-09 16:24:40 -0800 | [diff] [blame] | 388 | RETURN_STATUS_IF_ERROR(findOrCreatePortConfig(*aidlConfig, aidlFlags, aidlHandle, aidlSource, |
Mikhail Naganov | 289468a | 2023-03-29 10:06:15 -0700 | [diff] [blame] | 389 | std::set<int32_t>{devicePortConfig.portId}, mixPortConfig, &created)); |
Mikhail Naganov | 5b1eed1 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 390 | if (created) { |
| 391 | cleanups->emplace_front(this, &DeviceHalAidl::resetPortConfig, mixPortConfig->id); |
| 392 | } |
| 393 | setConfigFromPortConfig(aidlConfig, *mixPortConfig); |
Mikhail Naganov | 5b1eed1 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 394 | if (isInput) { |
| 395 | RETURN_STATUS_IF_ERROR(findOrCreatePatch( |
Mikhail Naganov | e93a086 | 2023-03-15 17:06:59 -0700 | [diff] [blame] | 396 | {devicePortConfig.id}, {mixPortConfig->id}, aidlPatch, &created)); |
Mikhail Naganov | 5b1eed1 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 397 | } else { |
| 398 | RETURN_STATUS_IF_ERROR(findOrCreatePatch( |
Mikhail Naganov | e93a086 | 2023-03-15 17:06:59 -0700 | [diff] [blame] | 399 | {mixPortConfig->id}, {devicePortConfig.id}, aidlPatch, &created)); |
Mikhail Naganov | 5b1eed1 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 400 | } |
| 401 | if (created) { |
Mikhail Naganov | e93a086 | 2023-03-15 17:06:59 -0700 | [diff] [blame] | 402 | cleanups->emplace_front(this, &DeviceHalAidl::resetPatch, aidlPatch->id); |
Mikhail Naganov | 5b1eed1 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 403 | } |
| 404 | if (aidlConfig->frameCount <= 0) { |
Mikhail Naganov | e93a086 | 2023-03-15 17:06:59 -0700 | [diff] [blame] | 405 | aidlConfig->frameCount = aidlPatch->minimumStreamBufferSizeFrames; |
Mikhail Naganov | 5b1eed1 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 406 | } |
| 407 | *config = VALUE_OR_RETURN_STATUS( |
| 408 | ::aidl::android::aidl2legacy_AudioConfig_audio_config_t(*aidlConfig, isInput)); |
| 409 | return OK; |
| 410 | } |
| 411 | |
Mikhail Naganov | dfd594e | 2023-02-08 16:59:41 -0800 | [diff] [blame] | 412 | namespace { |
| 413 | |
| 414 | class StreamCallbackBase { |
| 415 | protected: |
| 416 | explicit StreamCallbackBase(const sp<CallbackBroker>& broker) : mBroker(broker) {} |
| 417 | public: |
| 418 | void* getCookie() const { return mCookie; } |
| 419 | void setCookie(void* cookie) { mCookie = cookie; } |
| 420 | sp<CallbackBroker> getBroker() const { |
| 421 | if (void* cookie = mCookie; cookie != nullptr) return mBroker.promote(); |
| 422 | return nullptr; |
| 423 | } |
| 424 | private: |
| 425 | const wp<CallbackBroker> mBroker; |
| 426 | std::atomic<void*> mCookie; |
| 427 | }; |
| 428 | |
| 429 | template<class C> |
| 430 | class StreamCallbackBaseHelper { |
| 431 | protected: |
| 432 | explicit StreamCallbackBaseHelper(const StreamCallbackBase& base) : mBase(base) {} |
| 433 | sp<C> getCb(const sp<CallbackBroker>& broker, void* cookie); |
| 434 | using CbRef = const sp<C>&; |
| 435 | ndk::ScopedAStatus runCb(const std::function<void(CbRef cb)>& f) { |
| 436 | if (auto cb = getCb(mBase.getBroker(), mBase.getCookie()); cb != nullptr) f(cb); |
| 437 | return ndk::ScopedAStatus::ok(); |
| 438 | } |
| 439 | private: |
| 440 | const StreamCallbackBase& mBase; |
| 441 | }; |
| 442 | |
| 443 | template<> |
| 444 | sp<StreamOutHalInterfaceCallback> StreamCallbackBaseHelper<StreamOutHalInterfaceCallback>::getCb( |
| 445 | const sp<CallbackBroker>& broker, void* cookie) { |
| 446 | if (broker != nullptr) return broker->getStreamOutCallback(cookie); |
| 447 | return nullptr; |
| 448 | } |
| 449 | |
| 450 | template<> |
| 451 | sp<StreamOutHalInterfaceEventCallback> |
| 452 | StreamCallbackBaseHelper<StreamOutHalInterfaceEventCallback>::getCb( |
| 453 | const sp<CallbackBroker>& broker, void* cookie) { |
| 454 | if (broker != nullptr) return broker->getStreamOutEventCallback(cookie); |
| 455 | return nullptr; |
| 456 | } |
| 457 | |
| 458 | template<> |
| 459 | sp<StreamOutHalInterfaceLatencyModeCallback> |
| 460 | StreamCallbackBaseHelper<StreamOutHalInterfaceLatencyModeCallback>::getCb( |
| 461 | const sp<CallbackBroker>& broker, void* cookie) { |
| 462 | if (broker != nullptr) return broker->getStreamOutLatencyModeCallback(cookie); |
| 463 | return nullptr; |
| 464 | } |
| 465 | |
| 466 | /* |
| 467 | Note on the callback ownership. |
| 468 | |
| 469 | In the Binder ownership model, the server implementation is kept alive |
| 470 | as long as there is any client (proxy object) alive. This is done by |
| 471 | incrementing the refcount of the server-side object by the Binder framework. |
| 472 | When it detects that the last client is gone, it decrements the refcount back. |
| 473 | |
| 474 | Thus, it is not needed to keep any references to StreamCallback on our |
| 475 | side (after we have sent an instance to the client), because we are |
| 476 | the server-side. The callback object will be kept alive as long as the HAL server |
| 477 | holds a strong ref to IStreamCallback proxy. |
| 478 | */ |
| 479 | |
| 480 | class OutputStreamCallbackAidl : public StreamCallbackBase, |
| 481 | public StreamCallbackBaseHelper<StreamOutHalInterfaceCallback>, |
| 482 | public ::aidl::android::hardware::audio::core::BnStreamCallback { |
| 483 | public: |
| 484 | explicit OutputStreamCallbackAidl(const sp<CallbackBroker>& broker) |
| 485 | : StreamCallbackBase(broker), |
| 486 | StreamCallbackBaseHelper<StreamOutHalInterfaceCallback>( |
| 487 | *static_cast<StreamCallbackBase*>(this)) {} |
| 488 | ndk::ScopedAStatus onTransferReady() override { |
| 489 | return runCb([](CbRef cb) { cb->onWriteReady(); }); |
| 490 | } |
| 491 | ndk::ScopedAStatus onError() override { |
| 492 | return runCb([](CbRef cb) { cb->onError(); }); |
| 493 | } |
| 494 | ndk::ScopedAStatus onDrainReady() override { |
| 495 | return runCb([](CbRef cb) { cb->onDrainReady(); }); |
| 496 | } |
| 497 | }; |
| 498 | |
| 499 | class OutputStreamEventCallbackAidl : |
| 500 | public StreamCallbackBase, |
| 501 | public StreamCallbackBaseHelper<StreamOutHalInterfaceEventCallback>, |
| 502 | public StreamCallbackBaseHelper<StreamOutHalInterfaceLatencyModeCallback>, |
| 503 | public ::aidl::android::hardware::audio::core::BnStreamOutEventCallback { |
| 504 | public: |
| 505 | explicit OutputStreamEventCallbackAidl(const sp<CallbackBroker>& broker) |
| 506 | : StreamCallbackBase(broker), |
| 507 | StreamCallbackBaseHelper<StreamOutHalInterfaceEventCallback>( |
| 508 | *static_cast<StreamCallbackBase*>(this)), |
| 509 | StreamCallbackBaseHelper<StreamOutHalInterfaceLatencyModeCallback>( |
| 510 | *static_cast<StreamCallbackBase*>(this)) {} |
| 511 | ndk::ScopedAStatus onCodecFormatChanged(const std::vector<uint8_t>& in_audioMetadata) override { |
| 512 | std::basic_string<uint8_t> halMetadata(in_audioMetadata.begin(), in_audioMetadata.end()); |
| 513 | return StreamCallbackBaseHelper<StreamOutHalInterfaceEventCallback>::runCb( |
| 514 | [&halMetadata](auto cb) { cb->onCodecFormatChanged(halMetadata); }); |
| 515 | } |
| 516 | ndk::ScopedAStatus onRecommendedLatencyModeChanged( |
| 517 | const std::vector<AudioLatencyMode>& in_modes) override { |
| 518 | auto halModes = VALUE_OR_FATAL( |
| 519 | ::aidl::android::convertContainer<std::vector<audio_latency_mode_t>>( |
| 520 | in_modes, |
| 521 | ::aidl::android::aidl2legacy_AudioLatencyMode_audio_latency_mode_t)); |
| 522 | return StreamCallbackBaseHelper<StreamOutHalInterfaceLatencyModeCallback>::runCb( |
| 523 | [&halModes](auto cb) { cb->onRecommendedLatencyModeChanged(halModes); }); |
| 524 | } |
| 525 | }; |
| 526 | |
| 527 | } // namespace |
| 528 | |
Mikhail Naganov | 31d4665 | 2023-01-10 18:29:25 +0000 | [diff] [blame] | 529 | status_t DeviceHalAidl::openOutputStream( |
Mikhail Naganov | 5b1eed1 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 530 | audio_io_handle_t handle, audio_devices_t devices, |
| 531 | audio_output_flags_t flags, struct audio_config* config, |
| 532 | const char* address, |
Mikhail Naganov | 31d4665 | 2023-01-10 18:29:25 +0000 | [diff] [blame] | 533 | sp<StreamOutHalInterface>* outStream) { |
Mikhail Naganov | 89a9f74 | 2023-01-30 12:33:18 -0800 | [diff] [blame] | 534 | ALOGD("%p %s::%s", this, getClassName().c_str(), __func__); |
Mikhail Naganov | 31d4665 | 2023-01-10 18:29:25 +0000 | [diff] [blame] | 535 | if (!outStream || !config) { |
| 536 | return BAD_VALUE; |
| 537 | } |
| 538 | TIME_CHECK(); |
| 539 | if (!mModule) return NO_INIT; |
Mikhail Naganov | 5b1eed1 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 540 | int32_t aidlHandle = VALUE_OR_RETURN_STATUS( |
| 541 | ::aidl::android::legacy2aidl_audio_io_handle_t_int32_t(handle)); |
| 542 | AudioConfig aidlConfig = VALUE_OR_RETURN_STATUS( |
| 543 | ::aidl::android::legacy2aidl_audio_config_t_AudioConfig(*config, false /*isInput*/)); |
| 544 | AudioDevice aidlDevice = VALUE_OR_RETURN_STATUS( |
| 545 | ::aidl::android::legacy2aidl_audio_device_AudioDevice(devices, address)); |
| 546 | int32_t aidlOutputFlags = VALUE_OR_RETURN_STATUS( |
| 547 | ::aidl::android::legacy2aidl_audio_output_flags_t_int32_t_mask(flags)); |
| 548 | AudioIoFlags aidlFlags = AudioIoFlags::make<AudioIoFlags::Tag::output>(aidlOutputFlags); |
| 549 | AudioPortConfig mixPortConfig; |
| 550 | Cleanups cleanups; |
Mikhail Naganov | e93a086 | 2023-03-15 17:06:59 -0700 | [diff] [blame] | 551 | AudioPatch aidlPatch; |
Mikhail Naganov | d8d01f7 | 2023-03-09 16:24:40 -0800 | [diff] [blame] | 552 | RETURN_STATUS_IF_ERROR(prepareToOpenStream(aidlHandle, aidlDevice, aidlFlags, |
| 553 | AudioSource::SYS_RESERVED_INVALID /*only needed for input*/, |
Mikhail Naganov | e93a086 | 2023-03-15 17:06:59 -0700 | [diff] [blame] | 554 | config, &cleanups, &aidlConfig, &mixPortConfig, &aidlPatch)); |
Mikhail Naganov | 5b1eed1 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 555 | ::aidl::android::hardware::audio::core::IModule::OpenOutputStreamArguments args; |
| 556 | args.portConfigId = mixPortConfig.id; |
Mikhail Naganov | dfd594e | 2023-02-08 16:59:41 -0800 | [diff] [blame] | 557 | const bool isOffload = isBitPositionFlagSet( |
| 558 | aidlOutputFlags, AudioOutputFlags::COMPRESS_OFFLOAD); |
| 559 | std::shared_ptr<OutputStreamCallbackAidl> streamCb; |
| 560 | if (isOffload) { |
| 561 | streamCb = ndk::SharedRefBase::make<OutputStreamCallbackAidl>(this); |
| 562 | } |
| 563 | auto eventCb = ndk::SharedRefBase::make<OutputStreamEventCallbackAidl>(this); |
| 564 | if (isOffload) { |
| 565 | args.offloadInfo = aidlConfig.offloadInfo; |
| 566 | args.callback = streamCb; |
| 567 | } |
Mikhail Naganov | 5b1eed1 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 568 | args.bufferSizeFrames = aidlConfig.frameCount; |
Mikhail Naganov | dfd594e | 2023-02-08 16:59:41 -0800 | [diff] [blame] | 569 | args.eventCallback = eventCb; |
Mikhail Naganov | 5b1eed1 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 570 | ::aidl::android::hardware::audio::core::IModule::OpenOutputStreamReturn ret; |
| 571 | RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(mModule->openOutputStream(args, &ret))); |
Mikhail Naganov | 712d71b | 2023-02-23 17:57:16 -0800 | [diff] [blame] | 572 | StreamContextAidl context(ret.desc, isOffload); |
Mikhail Naganov | 89a9f74 | 2023-01-30 12:33:18 -0800 | [diff] [blame] | 573 | if (!context.isValid()) { |
| 574 | ALOGE("%s: Failed to created a valid stream context from the descriptor: %s", |
| 575 | __func__, ret.desc.toString().c_str()); |
| 576 | return NO_INIT; |
| 577 | } |
Mikhail Naganov | e93a086 | 2023-03-15 17:06:59 -0700 | [diff] [blame] | 578 | *outStream = sp<StreamOutHalAidl>::make(*config, std::move(context), aidlPatch.latenciesMs[0], |
Mikhail Naganov | e7a26ad | 2023-05-25 17:36:48 -0700 | [diff] [blame] | 579 | std::move(ret.stream), mVendorExt, this /*callbackBroker*/); |
Mikhail Naganov | e93a086 | 2023-03-15 17:06:59 -0700 | [diff] [blame] | 580 | mStreams.insert(std::pair(*outStream, aidlPatch.id)); |
Mikhail Naganov | dfd594e | 2023-02-08 16:59:41 -0800 | [diff] [blame] | 581 | void* cbCookie = (*outStream).get(); |
| 582 | { |
| 583 | std::lock_guard l(mLock); |
| 584 | mCallbacks.emplace(cbCookie, Callbacks{}); |
| 585 | } |
| 586 | if (streamCb) streamCb->setCookie(cbCookie); |
| 587 | eventCb->setCookie(cbCookie); |
Mikhail Naganov | 5b1eed1 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 588 | cleanups.disarmAll(); |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 589 | return OK; |
| 590 | } |
| 591 | |
Mikhail Naganov | 31d4665 | 2023-01-10 18:29:25 +0000 | [diff] [blame] | 592 | status_t DeviceHalAidl::openInputStream( |
Mikhail Naganov | 5b1eed1 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 593 | audio_io_handle_t handle, audio_devices_t devices, |
| 594 | struct audio_config* config, audio_input_flags_t flags, |
| 595 | const char* address, audio_source_t source, |
| 596 | audio_devices_t outputDevice, const char* outputDeviceAddress, |
Mikhail Naganov | 31d4665 | 2023-01-10 18:29:25 +0000 | [diff] [blame] | 597 | sp<StreamInHalInterface>* inStream) { |
Mikhail Naganov | 89a9f74 | 2023-01-30 12:33:18 -0800 | [diff] [blame] | 598 | ALOGD("%p %s::%s", this, getClassName().c_str(), __func__); |
Mikhail Naganov | 31d4665 | 2023-01-10 18:29:25 +0000 | [diff] [blame] | 599 | if (!inStream || !config) { |
| 600 | return BAD_VALUE; |
| 601 | } |
| 602 | TIME_CHECK(); |
| 603 | if (!mModule) return NO_INIT; |
Mikhail Naganov | 5b1eed1 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 604 | int32_t aidlHandle = VALUE_OR_RETURN_STATUS( |
| 605 | ::aidl::android::legacy2aidl_audio_io_handle_t_int32_t(handle)); |
| 606 | AudioConfig aidlConfig = VALUE_OR_RETURN_STATUS( |
| 607 | ::aidl::android::legacy2aidl_audio_config_t_AudioConfig(*config, true /*isInput*/)); |
| 608 | AudioDevice aidlDevice = VALUE_OR_RETURN_STATUS( |
| 609 | ::aidl::android::legacy2aidl_audio_device_AudioDevice(devices, address)); |
| 610 | int32_t aidlInputFlags = VALUE_OR_RETURN_STATUS( |
| 611 | ::aidl::android::legacy2aidl_audio_input_flags_t_int32_t_mask(flags)); |
| 612 | AudioIoFlags aidlFlags = AudioIoFlags::make<AudioIoFlags::Tag::input>(aidlInputFlags); |
| 613 | AudioSource aidlSource = VALUE_OR_RETURN_STATUS( |
| 614 | ::aidl::android::legacy2aidl_audio_source_t_AudioSource(source)); |
| 615 | AudioPortConfig mixPortConfig; |
| 616 | Cleanups cleanups; |
Mikhail Naganov | e93a086 | 2023-03-15 17:06:59 -0700 | [diff] [blame] | 617 | AudioPatch aidlPatch; |
Mikhail Naganov | d8d01f7 | 2023-03-09 16:24:40 -0800 | [diff] [blame] | 618 | RETURN_STATUS_IF_ERROR(prepareToOpenStream(aidlHandle, aidlDevice, aidlFlags, aidlSource, |
Mikhail Naganov | e93a086 | 2023-03-15 17:06:59 -0700 | [diff] [blame] | 619 | config, &cleanups, &aidlConfig, &mixPortConfig, &aidlPatch)); |
Mikhail Naganov | 5b1eed1 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 620 | ::aidl::android::hardware::audio::core::IModule::OpenInputStreamArguments args; |
| 621 | args.portConfigId = mixPortConfig.id; |
| 622 | RecordTrackMetadata aidlTrackMetadata{ |
| 623 | .source = aidlSource, .gain = 1, .channelMask = aidlConfig.base.channelMask }; |
| 624 | if (outputDevice != AUDIO_DEVICE_NONE) { |
| 625 | aidlTrackMetadata.destinationDevice = VALUE_OR_RETURN_STATUS( |
| 626 | ::aidl::android::legacy2aidl_audio_device_AudioDevice( |
| 627 | outputDevice, outputDeviceAddress)); |
| 628 | } |
| 629 | args.sinkMetadata.tracks.push_back(std::move(aidlTrackMetadata)); |
| 630 | args.bufferSizeFrames = aidlConfig.frameCount; |
| 631 | ::aidl::android::hardware::audio::core::IModule::OpenInputStreamReturn ret; |
| 632 | RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(mModule->openInputStream(args, &ret))); |
Mikhail Naganov | 712d71b | 2023-02-23 17:57:16 -0800 | [diff] [blame] | 633 | StreamContextAidl context(ret.desc, false /*isAsynchronous*/); |
Mikhail Naganov | 89a9f74 | 2023-01-30 12:33:18 -0800 | [diff] [blame] | 634 | if (!context.isValid()) { |
| 635 | ALOGE("%s: Failed to created a valid stream context from the descriptor: %s", |
| 636 | __func__, ret.desc.toString().c_str()); |
| 637 | return NO_INIT; |
| 638 | } |
Mikhail Naganov | e93a086 | 2023-03-15 17:06:59 -0700 | [diff] [blame] | 639 | *inStream = sp<StreamInHalAidl>::make(*config, std::move(context), aidlPatch.latenciesMs[0], |
Mikhail Naganov | e7a26ad | 2023-05-25 17:36:48 -0700 | [diff] [blame] | 640 | std::move(ret.stream), mVendorExt, this /*micInfoProvider*/); |
Mikhail Naganov | e93a086 | 2023-03-15 17:06:59 -0700 | [diff] [blame] | 641 | mStreams.insert(std::pair(*inStream, aidlPatch.id)); |
Mikhail Naganov | 5b1eed1 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 642 | cleanups.disarmAll(); |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 643 | return OK; |
| 644 | } |
| 645 | |
| 646 | status_t DeviceHalAidl::supportsAudioPatches(bool* supportsPatches) { |
| 647 | *supportsPatches = true; |
| 648 | return OK; |
| 649 | } |
| 650 | |
Mikhail Naganov | 5b1eed1 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 651 | status_t DeviceHalAidl::createAudioPatch(unsigned int num_sources, |
| 652 | const struct audio_port_config* sources, |
| 653 | unsigned int num_sinks, |
| 654 | const struct audio_port_config* sinks, |
| 655 | audio_patch_handle_t* patch) { |
Mikhail Naganov | 89a9f74 | 2023-01-30 12:33:18 -0800 | [diff] [blame] | 656 | ALOGD("%p %s::%s", this, getClassName().c_str(), __func__); |
Mikhail Naganov | 31d4665 | 2023-01-10 18:29:25 +0000 | [diff] [blame] | 657 | TIME_CHECK(); |
| 658 | if (!mModule) return NO_INIT; |
Mikhail Naganov | 5b1eed1 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 659 | if (num_sinks > AUDIO_PATCH_PORTS_MAX || num_sources > AUDIO_PATCH_PORTS_MAX || |
| 660 | sources == nullptr || sinks == nullptr || patch == nullptr) { |
| 661 | return BAD_VALUE; |
| 662 | } |
Mikhail Naganov | 47d1d73 | 2023-02-23 15:16:04 -0800 | [diff] [blame] | 663 | // When the patch handle (*patch) is AUDIO_PATCH_HANDLE_NONE, it means |
| 664 | // the framework wants to create a new patch. The handle has to be generated |
| 665 | // by the HAL. Since handles generated this way can only be unique within |
| 666 | // a HAL module, the framework generates a globally unique handle, and maps |
| 667 | // it on the <HAL module, patch handle> pair. |
| 668 | // When the patch handle is set, it meant the framework intends to update |
| 669 | // an existing patch. |
| 670 | // |
| 671 | // This behavior corresponds to HAL module behavior, with the only difference |
| 672 | // that the HAL module uses `int32_t` for patch IDs. The following assert ensures |
| 673 | // that both the framework and the HAL use the same value for "no ID": |
| 674 | static_assert(AUDIO_PATCH_HANDLE_NONE == 0); |
| 675 | int32_t halPatchId = static_cast<int32_t>(*patch); |
Mikhail Naganov | 5b1eed1 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 676 | |
| 677 | // Upon conversion, mix port configs contain audio configuration, while |
| 678 | // device port configs contain device address. This data is used to find |
| 679 | // or create HAL configs. |
| 680 | std::vector<AudioPortConfig> aidlSources, aidlSinks; |
| 681 | for (unsigned int i = 0; i < num_sources; ++i) { |
| 682 | bool isInput = VALUE_OR_RETURN_STATUS(::aidl::android::portDirection( |
| 683 | sources[i].role, sources[i].type)) == |
| 684 | ::aidl::android::AudioPortDirection::INPUT; |
| 685 | aidlSources.push_back(VALUE_OR_RETURN_STATUS( |
| 686 | ::aidl::android::legacy2aidl_audio_port_config_AudioPortConfig( |
| 687 | sources[i], isInput, 0))); |
| 688 | } |
| 689 | for (unsigned int i = 0; i < num_sinks; ++i) { |
| 690 | bool isInput = VALUE_OR_RETURN_STATUS(::aidl::android::portDirection( |
| 691 | sinks[i].role, sinks[i].type)) == |
| 692 | ::aidl::android::AudioPortDirection::INPUT; |
| 693 | aidlSinks.push_back(VALUE_OR_RETURN_STATUS( |
| 694 | ::aidl::android::legacy2aidl_audio_port_config_AudioPortConfig( |
| 695 | sinks[i], isInput, 0))); |
| 696 | } |
| 697 | Cleanups cleanups; |
Mikhail Naganov | 47d1d73 | 2023-02-23 15:16:04 -0800 | [diff] [blame] | 698 | auto existingPatchIt = halPatchId != 0 ? mPatches.find(halPatchId): mPatches.end(); |
Mikhail Naganov | 5b1eed1 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 699 | AudioPatch aidlPatch; |
Mikhail Naganov | 47d1d73 | 2023-02-23 15:16:04 -0800 | [diff] [blame] | 700 | if (existingPatchIt != mPatches.end()) { |
| 701 | aidlPatch = existingPatchIt->second; |
| 702 | aidlPatch.sourcePortConfigIds.clear(); |
| 703 | aidlPatch.sinkPortConfigIds.clear(); |
Mikhail Naganov | 5b1eed1 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 704 | } |
Mikhail Naganov | 89a9f74 | 2023-01-30 12:33:18 -0800 | [diff] [blame] | 705 | ALOGD("%s: sources: %s, sinks: %s", |
| 706 | __func__, ::android::internal::ToString(aidlSources).c_str(), |
| 707 | ::android::internal::ToString(aidlSinks).c_str()); |
Mikhail Naganov | 5b1eed1 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 708 | auto fillPortConfigs = [&]( |
Mikhail Naganov | 289468a | 2023-03-29 10:06:15 -0700 | [diff] [blame] | 709 | const std::vector<AudioPortConfig>& configs, |
| 710 | const std::set<int32_t>& destinationPortIds, |
| 711 | std::vector<int32_t>* ids, std::set<int32_t>* portIds) -> status_t { |
Mikhail Naganov | 5b1eed1 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 712 | for (const auto& s : configs) { |
| 713 | AudioPortConfig portConfig; |
| 714 | bool created = false; |
Mikhail Naganov | 289468a | 2023-03-29 10:06:15 -0700 | [diff] [blame] | 715 | RETURN_STATUS_IF_ERROR(findOrCreatePortConfig( |
| 716 | s, destinationPortIds, &portConfig, &created)); |
Mikhail Naganov | 5b1eed1 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 717 | if (created) { |
| 718 | cleanups.emplace_front(this, &DeviceHalAidl::resetPortConfig, portConfig.id); |
| 719 | } |
| 720 | ids->push_back(portConfig.id); |
Mikhail Naganov | 289468a | 2023-03-29 10:06:15 -0700 | [diff] [blame] | 721 | if (portIds != nullptr) { |
| 722 | portIds->insert(portConfig.portId); |
| 723 | } |
Mikhail Naganov | 5b1eed1 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 724 | } |
| 725 | return OK; |
| 726 | }; |
Mikhail Naganov | 289468a | 2023-03-29 10:06:15 -0700 | [diff] [blame] | 727 | // When looking up port configs, the destinationPortId is only used for mix ports. |
| 728 | // Thus, we process device port configs first, and look up the destination port ID from them. |
| 729 | bool sourceIsDevice = std::any_of(aidlSources.begin(), aidlSources.end(), |
| 730 | [](const auto& config) { return config.ext.getTag() == AudioPortExt::device; }); |
| 731 | const std::vector<AudioPortConfig>& devicePortConfigs = |
| 732 | sourceIsDevice ? aidlSources : aidlSinks; |
| 733 | std::vector<int32_t>* devicePortConfigIds = |
| 734 | sourceIsDevice ? &aidlPatch.sourcePortConfigIds : &aidlPatch.sinkPortConfigIds; |
| 735 | const std::vector<AudioPortConfig>& mixPortConfigs = |
| 736 | sourceIsDevice ? aidlSinks : aidlSources; |
| 737 | std::vector<int32_t>* mixPortConfigIds = |
| 738 | sourceIsDevice ? &aidlPatch.sinkPortConfigIds : &aidlPatch.sourcePortConfigIds; |
| 739 | std::set<int32_t> devicePortIds; |
| 740 | RETURN_STATUS_IF_ERROR(fillPortConfigs( |
| 741 | devicePortConfigs, std::set<int32_t>(), devicePortConfigIds, &devicePortIds)); |
| 742 | RETURN_STATUS_IF_ERROR(fillPortConfigs( |
| 743 | mixPortConfigs, devicePortIds, mixPortConfigIds, nullptr)); |
Mikhail Naganov | 5b1eed1 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 744 | if (existingPatchIt != mPatches.end()) { |
| 745 | RETURN_STATUS_IF_ERROR(statusTFromBinderStatus( |
| 746 | mModule->setAudioPatch(aidlPatch, &aidlPatch))); |
| 747 | existingPatchIt->second = aidlPatch; |
| 748 | } else { |
| 749 | bool created = false; |
| 750 | RETURN_STATUS_IF_ERROR(findOrCreatePatch(aidlPatch, &aidlPatch, &created)); |
| 751 | // Since no cleanup of the patch is needed, 'created' is ignored. |
Mikhail Naganov | 47d1d73 | 2023-02-23 15:16:04 -0800 | [diff] [blame] | 752 | halPatchId = aidlPatch.id; |
| 753 | *patch = static_cast<audio_patch_handle_t>(halPatchId); |
Mikhail Naganov | 5b1eed1 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 754 | } |
| 755 | cleanups.disarmAll(); |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 756 | return OK; |
| 757 | } |
| 758 | |
Mikhail Naganov | 5b1eed1 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 759 | status_t DeviceHalAidl::releaseAudioPatch(audio_patch_handle_t patch) { |
Mikhail Naganov | 89a9f74 | 2023-01-30 12:33:18 -0800 | [diff] [blame] | 760 | ALOGD("%p %s::%s", this, getClassName().c_str(), __func__); |
Mikhail Naganov | 31d4665 | 2023-01-10 18:29:25 +0000 | [diff] [blame] | 761 | TIME_CHECK(); |
| 762 | if (!mModule) return NO_INIT; |
Mikhail Naganov | 47d1d73 | 2023-02-23 15:16:04 -0800 | [diff] [blame] | 763 | static_assert(AUDIO_PATCH_HANDLE_NONE == 0); |
| 764 | if (patch == AUDIO_PATCH_HANDLE_NONE) { |
Mikhail Naganov | 5b1eed1 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 765 | return BAD_VALUE; |
| 766 | } |
Mikhail Naganov | 47d1d73 | 2023-02-23 15:16:04 -0800 | [diff] [blame] | 767 | int32_t halPatchId = static_cast<int32_t>(patch); |
| 768 | auto patchIt = mPatches.find(halPatchId); |
| 769 | if (patchIt == mPatches.end()) { |
| 770 | ALOGE("%s: patch with id %d not found", __func__, halPatchId); |
| 771 | return BAD_VALUE; |
| 772 | } |
| 773 | RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(mModule->resetAudioPatch(halPatchId))); |
| 774 | mPatches.erase(patchIt); |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 775 | return OK; |
| 776 | } |
| 777 | |
Mikhail Naganov | e93a086 | 2023-03-15 17:06:59 -0700 | [diff] [blame] | 778 | status_t DeviceHalAidl::getAudioPort(struct audio_port* port) { |
| 779 | ALOGD("%p %s::%s", this, getClassName().c_str(), __func__); |
Mikhail Naganov | 31d4665 | 2023-01-10 18:29:25 +0000 | [diff] [blame] | 780 | TIME_CHECK(); |
| 781 | if (!mModule) return NO_INIT; |
Mikhail Naganov | e93a086 | 2023-03-15 17:06:59 -0700 | [diff] [blame] | 782 | if (port == nullptr) { |
| 783 | return BAD_VALUE; |
| 784 | } |
| 785 | audio_port_v7 portV7; |
| 786 | audio_populate_audio_port_v7(port, &portV7); |
| 787 | RETURN_STATUS_IF_ERROR(getAudioPort(&portV7)); |
| 788 | return audio_populate_audio_port(&portV7, port) ? OK : BAD_VALUE; |
| 789 | } |
| 790 | |
| 791 | status_t DeviceHalAidl::getAudioPort(struct audio_port_v7 *port) { |
| 792 | ALOGD("%p %s::%s", this, getClassName().c_str(), __func__); |
| 793 | TIME_CHECK(); |
| 794 | if (!mModule) return NO_INIT; |
| 795 | if (port == nullptr) { |
| 796 | return BAD_VALUE; |
| 797 | } |
| 798 | bool isInput = VALUE_OR_RETURN_STATUS(::aidl::android::portDirection(port->role, port->type)) == |
| 799 | ::aidl::android::AudioPortDirection::INPUT; |
| 800 | auto aidlPort = VALUE_OR_RETURN_STATUS( |
| 801 | ::aidl::android::legacy2aidl_audio_port_v7_AudioPort(*port, isInput)); |
| 802 | if (aidlPort.ext.getTag() != AudioPortExt::device) { |
| 803 | ALOGE("%s: provided port is not a device port (module %s): %s", |
| 804 | __func__, mInstance.c_str(), aidlPort.toString().c_str()); |
| 805 | return BAD_VALUE; |
| 806 | } |
| 807 | const auto& matchDevice = aidlPort.ext.get<AudioPortExt::device>().device; |
| 808 | // It seems that we don't have to call HAL since all valid ports have been added either |
| 809 | // during initialization, or while handling connection of an external device. |
| 810 | auto portsIt = findPort(matchDevice); |
| 811 | if (portsIt == mPorts.end()) { |
| 812 | ALOGE("%s: device port for device %s is not found in the module %s", |
| 813 | __func__, matchDevice.toString().c_str(), mInstance.c_str()); |
| 814 | return BAD_VALUE; |
| 815 | } |
| 816 | const int32_t fwkId = aidlPort.id; |
| 817 | aidlPort = portsIt->second; |
| 818 | aidlPort.id = fwkId; |
| 819 | *port = VALUE_OR_RETURN_STATUS(::aidl::android::aidl2legacy_AudioPort_audio_port_v7( |
| 820 | aidlPort, isInput)); |
| 821 | return OK; |
| 822 | } |
| 823 | |
| 824 | status_t DeviceHalAidl::setAudioPortConfig(const struct audio_port_config* config) { |
| 825 | ALOGD("%p %s::%s", this, getClassName().c_str(), __func__); |
| 826 | TIME_CHECK(); |
| 827 | if (!mModule) return NO_INIT; |
| 828 | if (config == nullptr) { |
| 829 | return BAD_VALUE; |
| 830 | } |
| 831 | bool isInput = VALUE_OR_RETURN_STATUS(::aidl::android::portDirection( |
| 832 | config->role, config->type)) == ::aidl::android::AudioPortDirection::INPUT; |
| 833 | AudioPortConfig requestedPortConfig = VALUE_OR_RETURN_STATUS( |
| 834 | ::aidl::android::legacy2aidl_audio_port_config_AudioPortConfig( |
| 835 | *config, isInput, 0 /*portId*/)); |
| 836 | AudioPortConfig portConfig; |
| 837 | bool created = false; |
Mikhail Naganov | 289468a | 2023-03-29 10:06:15 -0700 | [diff] [blame] | 838 | RETURN_STATUS_IF_ERROR(findOrCreatePortConfig( |
| 839 | requestedPortConfig, std::set<int32_t>(), &portConfig, &created)); |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 840 | return OK; |
| 841 | } |
| 842 | |
Mikhail Naganov | cad0afe | 2023-03-10 14:25:57 -0800 | [diff] [blame] | 843 | MicrophoneInfoProvider::Info const* DeviceHalAidl::getMicrophoneInfo() { |
| 844 | if (mMicrophones.status == Microphones::Status::UNKNOWN) { |
| 845 | TIME_CHECK(); |
| 846 | std::vector<MicrophoneInfo> aidlInfo; |
| 847 | status_t status = statusTFromBinderStatus(mModule->getMicrophones(&aidlInfo)); |
| 848 | if (status == OK) { |
| 849 | mMicrophones.status = Microphones::Status::QUERIED; |
| 850 | mMicrophones.info = std::move(aidlInfo); |
| 851 | } else if (status == INVALID_OPERATION) { |
| 852 | mMicrophones.status = Microphones::Status::NOT_SUPPORTED; |
| 853 | } else { |
| 854 | ALOGE("%s: Unexpected status from 'IModule.getMicrophones': %d", __func__, status); |
| 855 | return {}; |
| 856 | } |
| 857 | } |
| 858 | if (mMicrophones.status == Microphones::Status::QUERIED) { |
| 859 | return &mMicrophones.info; |
| 860 | } |
| 861 | return {}; // NOT_SUPPORTED |
| 862 | } |
| 863 | |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 864 | status_t DeviceHalAidl::getMicrophones( |
Mikhail Naganov | cad0afe | 2023-03-10 14:25:57 -0800 | [diff] [blame] | 865 | std::vector<audio_microphone_characteristic_t>* microphones) { |
| 866 | if (!microphones) { |
| 867 | return BAD_VALUE; |
| 868 | } |
Mikhail Naganov | 31d4665 | 2023-01-10 18:29:25 +0000 | [diff] [blame] | 869 | TIME_CHECK(); |
| 870 | if (!mModule) return NO_INIT; |
Mikhail Naganov | cad0afe | 2023-03-10 14:25:57 -0800 | [diff] [blame] | 871 | auto staticInfo = getMicrophoneInfo(); |
| 872 | if (!staticInfo) return INVALID_OPERATION; |
| 873 | std::vector<MicrophoneDynamicInfo> emptyDynamicInfo; |
| 874 | emptyDynamicInfo.reserve(staticInfo->size()); |
| 875 | std::transform(staticInfo->begin(), staticInfo->end(), std::back_inserter(emptyDynamicInfo), |
| 876 | [](const auto& info) { return MicrophoneDynamicInfo{ .id = info.id }; }); |
| 877 | *microphones = VALUE_OR_RETURN_STATUS( |
| 878 | ::aidl::android::convertContainers<std::vector<audio_microphone_characteristic_t>>( |
| 879 | *staticInfo, emptyDynamicInfo, |
| 880 | ::aidl::android::aidl2legacy_MicrophoneInfos_audio_microphone_characteristic_t) |
| 881 | ); |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 882 | return OK; |
| 883 | } |
| 884 | |
Mikhail Naganov | d2c7f85 | 2023-06-14 18:00:13 -0700 | [diff] [blame] | 885 | status_t DeviceHalAidl::addDeviceEffect(const struct audio_port_config *device __unused, |
Mikhail Naganov | 31d4665 | 2023-01-10 18:29:25 +0000 | [diff] [blame] | 886 | sp<EffectHalInterface> effect) { |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 887 | if (!effect) { |
| 888 | return BAD_VALUE; |
| 889 | } |
Mikhail Naganov | 31d4665 | 2023-01-10 18:29:25 +0000 | [diff] [blame] | 890 | TIME_CHECK(); |
| 891 | if (!mModule) return NO_INIT; |
| 892 | ALOGE("%s not implemented yet", __func__); |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 893 | return OK; |
| 894 | } |
Mikhail Naganov | d2c7f85 | 2023-06-14 18:00:13 -0700 | [diff] [blame] | 895 | status_t DeviceHalAidl::removeDeviceEffect(const struct audio_port_config *device __unused, |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 896 | sp<EffectHalInterface> effect) { |
| 897 | if (!effect) { |
| 898 | return BAD_VALUE; |
| 899 | } |
Mikhail Naganov | 31d4665 | 2023-01-10 18:29:25 +0000 | [diff] [blame] | 900 | TIME_CHECK(); |
| 901 | if (!mModule) return NO_INIT; |
| 902 | ALOGE("%s not implemented yet", __func__); |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 903 | return OK; |
| 904 | } |
| 905 | |
| 906 | status_t DeviceHalAidl::getMmapPolicyInfos( |
David Li | 9cf5e62 | 2023-03-21 00:51:10 +0800 | [diff] [blame] | 907 | media::audio::common::AudioMMapPolicyType policyType, |
| 908 | std::vector<media::audio::common::AudioMMapPolicyInfo>* policyInfos) { |
Mikhail Naganov | 31d4665 | 2023-01-10 18:29:25 +0000 | [diff] [blame] | 909 | TIME_CHECK(); |
Mikhail Naganov | 25bc9a2 | 2023-04-21 18:48:16 -0700 | [diff] [blame] | 910 | AudioMMapPolicyType mmapPolicyType = VALUE_OR_RETURN_STATUS( |
| 911 | cpp2ndk_AudioMMapPolicyType(policyType)); |
David Li | 9cf5e62 | 2023-03-21 00:51:10 +0800 | [diff] [blame] | 912 | |
| 913 | std::vector<AudioMMapPolicyInfo> mmapPolicyInfos; |
| 914 | |
| 915 | if (status_t status = statusTFromBinderStatus( |
| 916 | mModule->getMmapPolicyInfos(mmapPolicyType, &mmapPolicyInfos)); status != OK) { |
| 917 | return status; |
| 918 | } |
| 919 | |
| 920 | *policyInfos = VALUE_OR_RETURN_STATUS( |
| 921 | convertContainer<std::vector<media::audio::common::AudioMMapPolicyInfo>>( |
| 922 | mmapPolicyInfos, ndk2cpp_AudioMMapPolicyInfo)); |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 923 | return OK; |
| 924 | } |
| 925 | |
| 926 | int32_t DeviceHalAidl::getAAudioMixerBurstCount() { |
Mikhail Naganov | 31d4665 | 2023-01-10 18:29:25 +0000 | [diff] [blame] | 927 | TIME_CHECK(); |
David Li | 9cf5e62 | 2023-03-21 00:51:10 +0800 | [diff] [blame] | 928 | int32_t mixerBurstCount = 0; |
| 929 | if (mModule->getAAudioMixerBurstCount(&mixerBurstCount).isOk()) { |
| 930 | return mixerBurstCount; |
| 931 | } |
| 932 | return 0; |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 933 | } |
| 934 | |
| 935 | int32_t DeviceHalAidl::getAAudioHardwareBurstMinUsec() { |
Mikhail Naganov | 31d4665 | 2023-01-10 18:29:25 +0000 | [diff] [blame] | 936 | TIME_CHECK(); |
David Li | 9cf5e62 | 2023-03-21 00:51:10 +0800 | [diff] [blame] | 937 | int32_t hardwareBurstMinUsec = 0; |
| 938 | if (mModule->getAAudioHardwareBurstMinUsec(&hardwareBurstMinUsec).isOk()) { |
| 939 | return hardwareBurstMinUsec; |
| 940 | } |
| 941 | return 0; |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 942 | } |
| 943 | |
| 944 | error::Result<audio_hw_sync_t> DeviceHalAidl::getHwAvSync() { |
Mikhail Naganov | 31d4665 | 2023-01-10 18:29:25 +0000 | [diff] [blame] | 945 | TIME_CHECK(); |
Mikhail Naganov | 08a62ab | 2023-03-14 17:11:51 -0700 | [diff] [blame] | 946 | if (!mModule) return NO_INIT; |
| 947 | int32_t aidlHwAvSync; |
| 948 | RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(mModule->generateHwAvSyncId(&aidlHwAvSync))); |
| 949 | return VALUE_OR_RETURN_STATUS( |
| 950 | ::aidl::android::aidl2legacy_int32_t_audio_hw_sync_t(aidlHwAvSync)); |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 951 | } |
| 952 | |
Mikhail Naganov | fab697c | 2023-01-11 19:33:13 +0000 | [diff] [blame] | 953 | status_t DeviceHalAidl::dump(int fd, const Vector<String16>& args) { |
| 954 | TIME_CHECK(); |
| 955 | if (!mModule) return NO_INIT; |
| 956 | return mModule->dump(fd, Args(args).args(), args.size()); |
David Li | 9cf5e62 | 2023-03-21 00:51:10 +0800 | [diff] [blame] | 957 | } |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 958 | |
Mikhail Naganov | 3ac95c9 | 2023-04-12 13:14:30 -0700 | [diff] [blame] | 959 | int32_t DeviceHalAidl::supportsBluetoothVariableLatency(bool* supports) { |
Mikhail Naganov | 31d4665 | 2023-01-10 18:29:25 +0000 | [diff] [blame] | 960 | TIME_CHECK(); |
Mikhail Naganov | 3ac95c9 | 2023-04-12 13:14:30 -0700 | [diff] [blame] | 961 | if (!mModule) return NO_INIT; |
| 962 | if (supports == nullptr) { |
| 963 | return BAD_VALUE; |
| 964 | } |
| 965 | return statusTFromBinderStatus(mModule->supportsVariableLatency(supports)); |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 966 | } |
Mikhail Naganov | 31d4665 | 2023-01-10 18:29:25 +0000 | [diff] [blame] | 967 | |
jiabin | 872de70 | 2023-04-27 22:04:31 +0000 | [diff] [blame] | 968 | |
| 969 | status_t DeviceHalAidl::prepareToDisconnectExternalDevice(const struct audio_port_v7* port) { |
| 970 | // There is not AIDL API defined for `prepareToDisconnectExternalDevice`. |
| 971 | // Call `setConnectedState` instead. |
| 972 | // TODO(b/279824103): call prepareToDisconnectExternalDevice when it is added. |
| 973 | const status_t status = setConnectedState(port, false /*connected*/); |
| 974 | if (status == NO_ERROR) { |
| 975 | mDeviceDisconnectionNotified.insert(port->id); |
| 976 | } |
| 977 | return status; |
| 978 | } |
| 979 | |
Mikhail Naganov | e93a086 | 2023-03-15 17:06:59 -0700 | [diff] [blame] | 980 | status_t DeviceHalAidl::setConnectedState(const struct audio_port_v7 *port, bool connected) { |
| 981 | TIME_CHECK(); |
| 982 | if (!mModule) return NO_INIT; |
| 983 | if (port == nullptr) { |
| 984 | return BAD_VALUE; |
| 985 | } |
jiabin | 872de70 | 2023-04-27 22:04:31 +0000 | [diff] [blame] | 986 | if (!connected && mDeviceDisconnectionNotified.erase(port->id) > 0) { |
| 987 | // For device disconnection, APM will first call `prepareToDisconnectExternalDevice` |
| 988 | // and then call `setConnectedState`. However, there is no API for |
| 989 | // `prepareToDisconnectExternalDevice` yet. In that case, `setConnectedState` will be |
| 990 | // called when calling `prepareToDisconnectExternalDevice`. Do not call to the HAL if |
| 991 | // previous call is successful. Also remove the cache here to avoid a large cache after |
| 992 | // a long run. |
| 993 | return NO_ERROR; |
| 994 | } |
Mikhail Naganov | e93a086 | 2023-03-15 17:06:59 -0700 | [diff] [blame] | 995 | bool isInput = VALUE_OR_RETURN_STATUS(::aidl::android::portDirection(port->role, port->type)) == |
| 996 | ::aidl::android::AudioPortDirection::INPUT; |
| 997 | AudioPort aidlPort = VALUE_OR_RETURN_STATUS( |
| 998 | ::aidl::android::legacy2aidl_audio_port_v7_AudioPort(*port, isInput)); |
| 999 | if (aidlPort.ext.getTag() != AudioPortExt::device) { |
| 1000 | ALOGE("%s: provided port is not a device port (module %s): %s", |
| 1001 | __func__, mInstance.c_str(), aidlPort.toString().c_str()); |
| 1002 | return BAD_VALUE; |
| 1003 | } |
| 1004 | if (connected) { |
| 1005 | AudioDevice matchDevice = aidlPort.ext.get<AudioPortExt::device>().device; |
| 1006 | // Reset the device address to find the "template" port. |
| 1007 | matchDevice.address = AudioDeviceAddress::make<AudioDeviceAddress::id>(); |
| 1008 | auto portsIt = findPort(matchDevice); |
| 1009 | if (portsIt == mPorts.end()) { |
| 1010 | ALOGW("%s: device port for device %s is not found in the module %s", |
| 1011 | __func__, matchDevice.toString().c_str(), mInstance.c_str()); |
| 1012 | return BAD_VALUE; |
| 1013 | } |
| 1014 | // Use the ID of the "template" port, use all the information from the provided port. |
| 1015 | aidlPort.id = portsIt->first; |
| 1016 | AudioPort connectedPort; |
| 1017 | RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(mModule->connectExternalDevice( |
| 1018 | aidlPort, &connectedPort))); |
| 1019 | const auto [it, inserted] = mPorts.insert(std::make_pair(connectedPort.id, connectedPort)); |
| 1020 | LOG_ALWAYS_FATAL_IF(!inserted, |
| 1021 | "%s: module %s, duplicate port ID received from HAL: %s, existing port: %s", |
| 1022 | __func__, mInstance.c_str(), connectedPort.toString().c_str(), |
| 1023 | it->second.toString().c_str()); |
| 1024 | } else { // !connected |
| 1025 | AudioDevice matchDevice = aidlPort.ext.get<AudioPortExt::device>().device; |
| 1026 | auto portsIt = findPort(matchDevice); |
| 1027 | if (portsIt == mPorts.end()) { |
| 1028 | ALOGW("%s: device port for device %s is not found in the module %s", |
| 1029 | __func__, matchDevice.toString().c_str(), mInstance.c_str()); |
| 1030 | return BAD_VALUE; |
| 1031 | } |
| 1032 | // Any streams opened on the external device must be closed by this time, |
| 1033 | // thus we can clean up patches and port configs that were created for them. |
| 1034 | resetUnusedPatchesAndPortConfigs(); |
| 1035 | RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(mModule->disconnectExternalDevice( |
| 1036 | portsIt->second.id))); |
| 1037 | mPorts.erase(portsIt); |
| 1038 | } |
Mikhail Naganov | 289468a | 2023-03-29 10:06:15 -0700 | [diff] [blame] | 1039 | return updateRoutes(); |
Mikhail Naganov | e93a086 | 2023-03-15 17:06:59 -0700 | [diff] [blame] | 1040 | } |
| 1041 | |
| 1042 | status_t DeviceHalAidl::setSimulateDeviceConnections(bool enabled) { |
| 1043 | TIME_CHECK(); |
| 1044 | if (!mModule) return NO_INIT; |
| 1045 | ModuleDebug debug{ .simulateDeviceConnections = enabled }; |
| 1046 | status_t status = statusTFromBinderStatus(mModule->setModuleDebug(debug)); |
| 1047 | // This is important to log as it affects HAL behavior. |
| 1048 | if (status == OK) { |
| 1049 | ALOGI("%s: set enabled: %d", __func__, enabled); |
| 1050 | } else { |
| 1051 | ALOGW("%s: set enabled to %d failed: %d", __func__, enabled, status); |
| 1052 | } |
| 1053 | return status; |
| 1054 | } |
| 1055 | |
Mikhail Naganov | 89a9f74 | 2023-01-30 12:33:18 -0800 | [diff] [blame] | 1056 | bool DeviceHalAidl::audioDeviceMatches(const AudioDevice& device, const AudioPort& p) { |
| 1057 | if (p.ext.getTag() != AudioPortExt::Tag::device) return false; |
| 1058 | return p.ext.get<AudioPortExt::Tag::device>().device == device; |
| 1059 | } |
| 1060 | |
| 1061 | bool DeviceHalAidl::audioDeviceMatches(const AudioDevice& device, const AudioPortConfig& p) { |
| 1062 | if (p.ext.getTag() != AudioPortExt::Tag::device) return false; |
| 1063 | if (device.type.type == AudioDeviceType::IN_DEFAULT) { |
| 1064 | return p.portId == mDefaultInputPortId; |
| 1065 | } else if (device.type.type == AudioDeviceType::OUT_DEFAULT) { |
| 1066 | return p.portId == mDefaultOutputPortId; |
| 1067 | } |
| 1068 | return p.ext.get<AudioPortExt::Tag::device>().device == device; |
| 1069 | } |
| 1070 | |
David Li | a8675d4 | 2023-03-30 21:08:06 +0800 | [diff] [blame] | 1071 | status_t DeviceHalAidl::createOrUpdatePortConfig( |
| 1072 | const AudioPortConfig& requestedPortConfig, PortConfigs::iterator* result, bool* created) { |
Mikhail Naganov | 5b1eed1 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 1073 | TIME_CHECK(); |
Mikhail Naganov | 1a44bc6 | 2023-02-16 17:35:06 -0800 | [diff] [blame] | 1074 | AudioPortConfig appliedPortConfig; |
Mikhail Naganov | 5b1eed1 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 1075 | bool applied = false; |
| 1076 | RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(mModule->setAudioPortConfig( |
Mikhail Naganov | 1a44bc6 | 2023-02-16 17:35:06 -0800 | [diff] [blame] | 1077 | requestedPortConfig, &appliedPortConfig, &applied))); |
Mikhail Naganov | 5b1eed1 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 1078 | if (!applied) { |
| 1079 | RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(mModule->setAudioPortConfig( |
Mikhail Naganov | 1a44bc6 | 2023-02-16 17:35:06 -0800 | [diff] [blame] | 1080 | appliedPortConfig, &appliedPortConfig, &applied))); |
Mikhail Naganov | 5b1eed1 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 1081 | if (!applied) { |
| 1082 | ALOGE("%s: module %s did not apply suggested config %s", |
Mikhail Naganov | 1a44bc6 | 2023-02-16 17:35:06 -0800 | [diff] [blame] | 1083 | __func__, mInstance.c_str(), appliedPortConfig.toString().c_str()); |
Mikhail Naganov | 5b1eed1 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 1084 | return NO_INIT; |
| 1085 | } |
| 1086 | } |
David Li | a8675d4 | 2023-03-30 21:08:06 +0800 | [diff] [blame] | 1087 | |
| 1088 | int32_t id = appliedPortConfig.id; |
| 1089 | if (requestedPortConfig.id != 0 && requestedPortConfig.id != id) { |
| 1090 | LOG_ALWAYS_FATAL("%s: requested port config id %d changed to %d", __func__, |
| 1091 | requestedPortConfig.id, id); |
| 1092 | } |
| 1093 | |
| 1094 | auto [it, inserted] = mPortConfigs.insert_or_assign(std::move(id), |
| 1095 | std::move(appliedPortConfig)); |
Mikhail Naganov | 1a44bc6 | 2023-02-16 17:35:06 -0800 | [diff] [blame] | 1096 | *result = it; |
David Li | a8675d4 | 2023-03-30 21:08:06 +0800 | [diff] [blame] | 1097 | *created = inserted; |
Mikhail Naganov | 5b1eed1 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 1098 | return OK; |
| 1099 | } |
| 1100 | |
Mikhail Naganov | e7a26ad | 2023-05-25 17:36:48 -0700 | [diff] [blame] | 1101 | status_t DeviceHalAidl::filterAndRetrieveBtA2dpParameters( |
| 1102 | AudioParameter &keys, AudioParameter *result) { |
| 1103 | TIME_CHECK(); |
| 1104 | if (String8 key = String8(AudioParameter::keyReconfigA2dpSupported); keys.containsKey(key)) { |
| 1105 | keys.remove(key); |
| 1106 | bool supports; |
| 1107 | RETURN_STATUS_IF_ERROR(statusTFromBinderStatus( |
| 1108 | mBluetoothA2dp->supportsOffloadReconfiguration(&supports))); |
| 1109 | result->addInt(key, supports ? 1 : 0); |
| 1110 | } |
| 1111 | return OK; |
| 1112 | } |
| 1113 | |
Mikhail Naganov | ccc8211 | 2023-04-27 18:14:15 -0700 | [diff] [blame] | 1114 | status_t DeviceHalAidl::filterAndUpdateBtA2dpParameters(AudioParameter ¶meters) { |
| 1115 | TIME_CHECK(); |
| 1116 | std::optional<bool> a2dpEnabled; |
Mikhail Naganov | e7a26ad | 2023-05-25 17:36:48 -0700 | [diff] [blame] | 1117 | std::optional<std::vector<VendorParameter>> reconfigureOffload; |
Mikhail Naganov | ccc8211 | 2023-04-27 18:14:15 -0700 | [diff] [blame] | 1118 | (void)VALUE_OR_RETURN_STATUS(filterOutAndProcessParameter<String8>( |
| 1119 | parameters, String8(AudioParameter::keyBtA2dpSuspended), |
| 1120 | [&a2dpEnabled](const String8& trueOrFalse) { |
| 1121 | if (trueOrFalse == AudioParameter::valueTrue) { |
| 1122 | a2dpEnabled = false; // 'suspended' == true |
| 1123 | return OK; |
| 1124 | } else if (trueOrFalse == AudioParameter::valueFalse) { |
| 1125 | a2dpEnabled = true; // 'suspended' == false |
| 1126 | return OK; |
| 1127 | } |
| 1128 | ALOGE("setParameters: parameter key \"%s\" has invalid value \"%s\"", |
| 1129 | AudioParameter::keyBtA2dpSuspended, trueOrFalse.c_str()); |
| 1130 | return BAD_VALUE; |
| 1131 | })); |
Mikhail Naganov | e7a26ad | 2023-05-25 17:36:48 -0700 | [diff] [blame] | 1132 | (void)VALUE_OR_RETURN_STATUS(filterOutAndProcessParameter<String8>( |
| 1133 | parameters, String8(AudioParameter::keyReconfigA2dp), |
| 1134 | [&](const String8& value) -> status_t { |
| 1135 | if (mVendorExt != nullptr) { |
| 1136 | std::vector<VendorParameter> result; |
| 1137 | RETURN_STATUS_IF_ERROR(statusTFromBinderStatus( |
| 1138 | mVendorExt->parseBluetoothA2dpReconfigureOffload( |
| 1139 | std::string(value.c_str()), &result))); |
| 1140 | reconfigureOffload = std::move(result); |
| 1141 | } else { |
| 1142 | reconfigureOffload = std::vector<VendorParameter>(); |
| 1143 | } |
| 1144 | return OK; |
| 1145 | })); |
Mikhail Naganov | ccc8211 | 2023-04-27 18:14:15 -0700 | [diff] [blame] | 1146 | if (mBluetoothA2dp != nullptr && a2dpEnabled.has_value()) { |
| 1147 | return statusTFromBinderStatus(mBluetoothA2dp->setEnabled(a2dpEnabled.value())); |
| 1148 | } |
Mikhail Naganov | e7a26ad | 2023-05-25 17:36:48 -0700 | [diff] [blame] | 1149 | if (mBluetoothA2dp != nullptr && reconfigureOffload.has_value()) { |
| 1150 | return statusTFromBinderStatus(mBluetoothA2dp->reconfigureOffload( |
| 1151 | reconfigureOffload.value())); |
| 1152 | } |
Mikhail Naganov | ccc8211 | 2023-04-27 18:14:15 -0700 | [diff] [blame] | 1153 | return OK; |
| 1154 | } |
| 1155 | |
| 1156 | status_t DeviceHalAidl::filterAndUpdateBtHfpParameters(AudioParameter ¶meters) { |
| 1157 | TIME_CHECK(); |
| 1158 | IBluetooth::HfpConfig hfpConfig; |
| 1159 | (void)VALUE_OR_RETURN_STATUS(filterOutAndProcessParameter<String8>( |
| 1160 | parameters, String8(AudioParameter::keyBtHfpEnable), |
| 1161 | [&hfpConfig](const String8& trueOrFalse) { |
| 1162 | if (trueOrFalse == AudioParameter::valueTrue) { |
| 1163 | hfpConfig.isEnabled = Boolean{ .value = true }; |
| 1164 | return OK; |
| 1165 | } else if (trueOrFalse == AudioParameter::valueFalse) { |
| 1166 | hfpConfig.isEnabled = Boolean{ .value = false }; |
| 1167 | return OK; |
| 1168 | } |
| 1169 | ALOGE("setParameters: parameter key \"%s\" has invalid value \"%s\"", |
| 1170 | AudioParameter::keyBtHfpEnable, trueOrFalse.c_str()); |
| 1171 | return BAD_VALUE; |
| 1172 | })); |
| 1173 | (void)VALUE_OR_RETURN_STATUS(filterOutAndProcessParameter<int>( |
| 1174 | parameters, String8(AudioParameter::keyBtHfpSamplingRate), |
| 1175 | [&hfpConfig](int sampleRate) { |
| 1176 | return sampleRate > 0 ? |
| 1177 | hfpConfig.sampleRate = Int{ .value = sampleRate }, OK : BAD_VALUE; |
| 1178 | })); |
| 1179 | (void)VALUE_OR_RETURN_STATUS(filterOutAndProcessParameter<int>( |
| 1180 | parameters, String8(AudioParameter::keyBtHfpVolume), |
| 1181 | [&hfpConfig](int volume0to15) { |
| 1182 | if (volume0to15 >= 0 && volume0to15 <= 15) { |
| 1183 | hfpConfig.volume = Float{ .value = volume0to15 / 15.0f }; |
| 1184 | return OK; |
| 1185 | } |
| 1186 | return BAD_VALUE; |
| 1187 | })); |
| 1188 | if (mBluetooth != nullptr && hfpConfig != IBluetooth::HfpConfig{}) { |
| 1189 | IBluetooth::HfpConfig newHfpConfig; |
| 1190 | return statusTFromBinderStatus(mBluetooth->setHfpConfig(hfpConfig, &newHfpConfig)); |
| 1191 | } |
| 1192 | return OK; |
| 1193 | } |
| 1194 | |
| 1195 | status_t DeviceHalAidl::filterAndUpdateBtLeParameters(AudioParameter ¶meters) { |
| 1196 | TIME_CHECK(); |
| 1197 | std::optional<bool> leEnabled; |
| 1198 | (void)VALUE_OR_RETURN_STATUS(filterOutAndProcessParameter<String8>( |
| 1199 | parameters, String8(AudioParameter::keyBtLeSuspended), |
| 1200 | [&leEnabled](const String8& trueOrFalse) { |
| 1201 | if (trueOrFalse == AudioParameter::valueTrue) { |
| 1202 | leEnabled = false; // 'suspended' == true |
| 1203 | return OK; |
| 1204 | } else if (trueOrFalse == AudioParameter::valueFalse) { |
| 1205 | leEnabled = true; // 'suspended' == false |
| 1206 | return OK; |
| 1207 | } |
| 1208 | ALOGE("setParameters: parameter key \"%s\" has invalid value \"%s\"", |
| 1209 | AudioParameter::keyBtLeSuspended, trueOrFalse.c_str()); |
| 1210 | return BAD_VALUE; |
| 1211 | })); |
| 1212 | if (mBluetoothLe != nullptr && leEnabled.has_value()) { |
| 1213 | return statusTFromBinderStatus(mBluetoothLe->setEnabled(leEnabled.value())); |
| 1214 | } |
| 1215 | return OK; |
| 1216 | } |
| 1217 | |
| 1218 | status_t DeviceHalAidl::filterAndUpdateBtScoParameters(AudioParameter ¶meters) { |
| 1219 | TIME_CHECK(); |
| 1220 | IBluetooth::ScoConfig scoConfig; |
| 1221 | (void)VALUE_OR_RETURN_STATUS(filterOutAndProcessParameter<String8>( |
| 1222 | parameters, String8(AudioParameter::keyBtSco), |
| 1223 | [&scoConfig](const String8& onOrOff) { |
| 1224 | if (onOrOff == AudioParameter::valueOn) { |
| 1225 | scoConfig.isEnabled = Boolean{ .value = true }; |
| 1226 | return OK; |
| 1227 | } else if (onOrOff == AudioParameter::valueOff) { |
| 1228 | scoConfig.isEnabled = Boolean{ .value = false }; |
| 1229 | return OK; |
| 1230 | } |
| 1231 | ALOGE("setParameters: parameter key \"%s\" has invalid value \"%s\"", |
| 1232 | AudioParameter::keyBtSco, onOrOff.c_str()); |
| 1233 | return BAD_VALUE; |
| 1234 | })); |
| 1235 | (void)VALUE_OR_RETURN_STATUS(filterOutAndProcessParameter<String8>( |
| 1236 | parameters, String8(AudioParameter::keyBtScoHeadsetName), |
| 1237 | [&scoConfig](const String8& name) { |
| 1238 | scoConfig.debugName = name; |
| 1239 | return OK; |
| 1240 | })); |
| 1241 | (void)VALUE_OR_RETURN_STATUS(filterOutAndProcessParameter<String8>( |
| 1242 | parameters, String8(AudioParameter::keyBtNrec), |
| 1243 | [&scoConfig](const String8& onOrOff) { |
| 1244 | if (onOrOff == AudioParameter::valueOn) { |
| 1245 | scoConfig.isNrecEnabled = Boolean{ .value = true }; |
| 1246 | return OK; |
| 1247 | } else if (onOrOff == AudioParameter::valueOff) { |
| 1248 | scoConfig.isNrecEnabled = Boolean{ .value = false }; |
| 1249 | return OK; |
| 1250 | } |
| 1251 | ALOGE("setParameters: parameter key \"%s\" has invalid value \"%s\"", |
| 1252 | AudioParameter::keyBtNrec, onOrOff.c_str()); |
| 1253 | return BAD_VALUE; |
| 1254 | })); |
| 1255 | (void)VALUE_OR_RETURN_STATUS(filterOutAndProcessParameter<String8>( |
| 1256 | parameters, String8(AudioParameter::keyBtScoWb), |
| 1257 | [&scoConfig](const String8& onOrOff) { |
| 1258 | if (onOrOff == AudioParameter::valueOn) { |
| 1259 | scoConfig.mode = IBluetooth::ScoConfig::Mode::SCO_WB; |
| 1260 | return OK; |
| 1261 | } else if (onOrOff == AudioParameter::valueOff) { |
| 1262 | scoConfig.mode = IBluetooth::ScoConfig::Mode::SCO; |
| 1263 | return OK; |
| 1264 | } |
| 1265 | ALOGE("setParameters: parameter key \"%s\" has invalid value \"%s\"", |
| 1266 | AudioParameter::keyBtScoWb, onOrOff.c_str()); |
| 1267 | return BAD_VALUE; |
| 1268 | })); |
| 1269 | if (mBluetooth != nullptr && scoConfig != IBluetooth::ScoConfig{}) { |
| 1270 | IBluetooth::ScoConfig newScoConfig; |
| 1271 | return statusTFromBinderStatus(mBluetooth->setScoConfig(scoConfig, &newScoConfig)); |
| 1272 | } |
| 1273 | return OK; |
| 1274 | } |
| 1275 | |
Mikhail Naganov | e92c34b | 2023-05-31 14:24:48 -0700 | [diff] [blame] | 1276 | status_t DeviceHalAidl::filterAndUpdateScreenParameters(AudioParameter ¶meters) { |
| 1277 | TIME_CHECK(); |
| 1278 | (void)VALUE_OR_RETURN_STATUS(filterOutAndProcessParameter<String8>( |
| 1279 | parameters, String8(AudioParameter::keyScreenState), |
| 1280 | [&](const String8& onOrOff) -> status_t { |
| 1281 | std::optional<bool> isTurnedOn; |
| 1282 | if (onOrOff == AudioParameter::valueOn) { |
| 1283 | isTurnedOn = true; |
| 1284 | } else if (onOrOff == AudioParameter::valueOff) { |
| 1285 | isTurnedOn = false; |
| 1286 | } |
| 1287 | if (!isTurnedOn.has_value()) { |
| 1288 | ALOGE("setParameters: parameter key \"%s\" has invalid value \"%s\"", |
| 1289 | AudioParameter::keyScreenState, onOrOff.c_str()); |
| 1290 | return BAD_VALUE; |
| 1291 | } |
| 1292 | return statusTFromBinderStatus( |
| 1293 | mModule->updateScreenState(isTurnedOn.value())); |
| 1294 | })); |
| 1295 | (void)VALUE_OR_RETURN_STATUS(filterOutAndProcessParameter<int>( |
| 1296 | parameters, String8(AudioParameter::keyScreenRotation), |
| 1297 | [&](int rotationDegrees) -> status_t { |
| 1298 | IModule::ScreenRotation rotation; |
| 1299 | switch (rotationDegrees) { |
| 1300 | case 0: rotation = IModule::ScreenRotation::DEG_0; break; |
| 1301 | case 90: rotation = IModule::ScreenRotation::DEG_90; break; |
| 1302 | case 180: rotation = IModule::ScreenRotation::DEG_180; break; |
| 1303 | case 270: rotation = IModule::ScreenRotation::DEG_270; break; |
| 1304 | default: |
| 1305 | ALOGE("setParameters: parameter key \"%s\" has invalid value %d", |
| 1306 | AudioParameter::keyScreenRotation, rotationDegrees); |
| 1307 | return BAD_VALUE; |
| 1308 | } |
| 1309 | return statusTFromBinderStatus(mModule->updateScreenRotation(rotation)); |
| 1310 | })); |
| 1311 | return OK; |
| 1312 | } |
| 1313 | |
Mikhail Naganov | 5b1eed1 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 1314 | status_t DeviceHalAidl::findOrCreatePatch( |
| 1315 | const AudioPatch& requestedPatch, AudioPatch* patch, bool* created) { |
| 1316 | std::set<int32_t> sourcePortConfigIds(requestedPatch.sourcePortConfigIds.begin(), |
| 1317 | requestedPatch.sourcePortConfigIds.end()); |
| 1318 | std::set<int32_t> sinkPortConfigIds(requestedPatch.sinkPortConfigIds.begin(), |
| 1319 | requestedPatch.sinkPortConfigIds.end()); |
| 1320 | return findOrCreatePatch(sourcePortConfigIds, sinkPortConfigIds, patch, created); |
| 1321 | } |
| 1322 | |
| 1323 | status_t DeviceHalAidl::findOrCreatePatch( |
| 1324 | const std::set<int32_t>& sourcePortConfigIds, const std::set<int32_t>& sinkPortConfigIds, |
| 1325 | AudioPatch* patch, bool* created) { |
| 1326 | auto patchIt = findPatch(sourcePortConfigIds, sinkPortConfigIds); |
| 1327 | if (patchIt == mPatches.end()) { |
| 1328 | TIME_CHECK(); |
| 1329 | AudioPatch requestedPatch, appliedPatch; |
| 1330 | requestedPatch.sourcePortConfigIds.insert(requestedPatch.sourcePortConfigIds.end(), |
| 1331 | sourcePortConfigIds.begin(), sourcePortConfigIds.end()); |
| 1332 | requestedPatch.sinkPortConfigIds.insert(requestedPatch.sinkPortConfigIds.end(), |
| 1333 | sinkPortConfigIds.begin(), sinkPortConfigIds.end()); |
| 1334 | RETURN_STATUS_IF_ERROR(statusTFromBinderStatus(mModule->setAudioPatch( |
| 1335 | requestedPatch, &appliedPatch))); |
| 1336 | patchIt = mPatches.insert(mPatches.end(), std::make_pair(appliedPatch.id, appliedPatch)); |
| 1337 | *created = true; |
| 1338 | } else { |
| 1339 | *created = false; |
| 1340 | } |
| 1341 | *patch = patchIt->second; |
| 1342 | return OK; |
| 1343 | } |
| 1344 | |
jiabin | 9c07faf | 2023-04-26 22:00:44 +0000 | [diff] [blame] | 1345 | status_t DeviceHalAidl::findOrCreatePortConfig(const AudioDevice& device, const AudioConfig* config, |
Mikhail Naganov | 5b1eed1 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 1346 | AudioPortConfig* portConfig, bool* created) { |
| 1347 | auto portConfigIt = findPortConfig(device); |
| 1348 | if (portConfigIt == mPortConfigs.end()) { |
| 1349 | auto portsIt = findPort(device); |
| 1350 | if (portsIt == mPorts.end()) { |
| 1351 | ALOGE("%s: device port for device %s is not found in the module %s", |
| 1352 | __func__, device.toString().c_str(), mInstance.c_str()); |
| 1353 | return BAD_VALUE; |
| 1354 | } |
| 1355 | AudioPortConfig requestedPortConfig; |
| 1356 | requestedPortConfig.portId = portsIt->first; |
jiabin | 9c07faf | 2023-04-26 22:00:44 +0000 | [diff] [blame] | 1357 | if (config != nullptr) { |
| 1358 | setPortConfigFromConfig(&requestedPortConfig, *config); |
| 1359 | } |
David Li | a8675d4 | 2023-03-30 21:08:06 +0800 | [diff] [blame] | 1360 | RETURN_STATUS_IF_ERROR(createOrUpdatePortConfig(requestedPortConfig, &portConfigIt, |
| 1361 | created)); |
Mikhail Naganov | 5b1eed1 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 1362 | } else { |
| 1363 | *created = false; |
| 1364 | } |
| 1365 | *portConfig = portConfigIt->second; |
| 1366 | return OK; |
| 1367 | } |
| 1368 | |
| 1369 | status_t DeviceHalAidl::findOrCreatePortConfig( |
Mikhail Naganov | 89a9f74 | 2023-01-30 12:33:18 -0800 | [diff] [blame] | 1370 | const AudioConfig& config, const std::optional<AudioIoFlags>& flags, int32_t ioHandle, |
Mikhail Naganov | 289468a | 2023-03-29 10:06:15 -0700 | [diff] [blame] | 1371 | AudioSource source, const std::set<int32_t>& destinationPortIds, |
| 1372 | AudioPortConfig* portConfig, bool* created) { |
Mikhail Naganov | 1a44bc6 | 2023-02-16 17:35:06 -0800 | [diff] [blame] | 1373 | // These flags get removed one by one in this order when retrying port finding. |
| 1374 | static const std::vector<AudioInputFlags> kOptionalInputFlags{ |
| 1375 | AudioInputFlags::FAST, AudioInputFlags::RAW }; |
Mikhail Naganov | 5b1eed1 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 1376 | auto portConfigIt = findPortConfig(config, flags, ioHandle); |
Mikhail Naganov | 89a9f74 | 2023-01-30 12:33:18 -0800 | [diff] [blame] | 1377 | if (portConfigIt == mPortConfigs.end() && flags.has_value()) { |
Mikhail Naganov | 1a44bc6 | 2023-02-16 17:35:06 -0800 | [diff] [blame] | 1378 | auto optionalInputFlagsIt = kOptionalInputFlags.begin(); |
| 1379 | AudioIoFlags matchFlags = flags.value(); |
Mikhail Naganov | 289468a | 2023-03-29 10:06:15 -0700 | [diff] [blame] | 1380 | auto portsIt = findPort(config, matchFlags, destinationPortIds); |
Mikhail Naganov | 1a44bc6 | 2023-02-16 17:35:06 -0800 | [diff] [blame] | 1381 | while (portsIt == mPorts.end() && matchFlags.getTag() == AudioIoFlags::Tag::input |
| 1382 | && optionalInputFlagsIt != kOptionalInputFlags.end()) { |
| 1383 | if (!isBitPositionFlagSet( |
| 1384 | matchFlags.get<AudioIoFlags::Tag::input>(), *optionalInputFlagsIt)) { |
| 1385 | ++optionalInputFlagsIt; |
| 1386 | continue; |
| 1387 | } |
| 1388 | matchFlags.set<AudioIoFlags::Tag::input>(matchFlags.get<AudioIoFlags::Tag::input>() & |
| 1389 | ~makeBitPositionFlagMask(*optionalInputFlagsIt++)); |
Mikhail Naganov | 289468a | 2023-03-29 10:06:15 -0700 | [diff] [blame] | 1390 | portsIt = findPort(config, matchFlags, destinationPortIds); |
Mikhail Naganov | 1a44bc6 | 2023-02-16 17:35:06 -0800 | [diff] [blame] | 1391 | ALOGI("%s: mix port for config %s, flags %s was not found in the module %s, " |
| 1392 | "retried with flags %s", __func__, config.toString().c_str(), |
| 1393 | flags.value().toString().c_str(), mInstance.c_str(), |
| 1394 | matchFlags.toString().c_str()); |
| 1395 | } |
Mikhail Naganov | 5b1eed1 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 1396 | if (portsIt == mPorts.end()) { |
| 1397 | ALOGE("%s: mix port for config %s, flags %s is not found in the module %s", |
Mikhail Naganov | 1a44bc6 | 2023-02-16 17:35:06 -0800 | [diff] [blame] | 1398 | __func__, config.toString().c_str(), matchFlags.toString().c_str(), |
Mikhail Naganov | 5b1eed1 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 1399 | mInstance.c_str()); |
| 1400 | return BAD_VALUE; |
| 1401 | } |
| 1402 | AudioPortConfig requestedPortConfig; |
| 1403 | requestedPortConfig.portId = portsIt->first; |
| 1404 | setPortConfigFromConfig(&requestedPortConfig, config); |
Mikhail Naganov | 1a44bc6 | 2023-02-16 17:35:06 -0800 | [diff] [blame] | 1405 | requestedPortConfig.ext = AudioPortMixExt{ .handle = ioHandle }; |
Mikhail Naganov | d8d01f7 | 2023-03-09 16:24:40 -0800 | [diff] [blame] | 1406 | if (matchFlags.getTag() == AudioIoFlags::Tag::input |
| 1407 | && source != AudioSource::SYS_RESERVED_INVALID) { |
| 1408 | requestedPortConfig.ext.get<AudioPortExt::Tag::mix>().usecase = |
| 1409 | AudioPortMixExtUseCase::make<AudioPortMixExtUseCase::Tag::source>(source); |
| 1410 | } |
David Li | a8675d4 | 2023-03-30 21:08:06 +0800 | [diff] [blame] | 1411 | RETURN_STATUS_IF_ERROR(createOrUpdatePortConfig(requestedPortConfig, &portConfigIt, |
| 1412 | created)); |
Mikhail Naganov | 89a9f74 | 2023-01-30 12:33:18 -0800 | [diff] [blame] | 1413 | } else if (!flags.has_value()) { |
| 1414 | ALOGW("%s: mix port config for %s, handle %d not found in the module %s, " |
| 1415 | "and was not created as flags are not specified", |
| 1416 | __func__, config.toString().c_str(), ioHandle, mInstance.c_str()); |
| 1417 | return BAD_VALUE; |
Mikhail Naganov | 5b1eed1 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 1418 | } else { |
David Li | a8675d4 | 2023-03-30 21:08:06 +0800 | [diff] [blame] | 1419 | AudioPortConfig requestedPortConfig = portConfigIt->second; |
| 1420 | if (requestedPortConfig.ext.getTag() == AudioPortExt::Tag::mix) { |
| 1421 | AudioPortMixExt& mixExt = requestedPortConfig.ext.get<AudioPortExt::Tag::mix>(); |
| 1422 | if (mixExt.usecase.getTag() == AudioPortMixExtUseCase::Tag::source && |
| 1423 | source != AudioSource::SYS_RESERVED_INVALID) { |
| 1424 | mixExt.usecase.get<AudioPortMixExtUseCase::Tag::source>() = source; |
| 1425 | } |
| 1426 | } |
| 1427 | |
| 1428 | if (requestedPortConfig != portConfigIt->second) { |
| 1429 | RETURN_STATUS_IF_ERROR(createOrUpdatePortConfig(requestedPortConfig, &portConfigIt, |
| 1430 | created)); |
| 1431 | } else { |
| 1432 | *created = false; |
| 1433 | } |
Mikhail Naganov | 5b1eed1 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 1434 | } |
| 1435 | *portConfig = portConfigIt->second; |
| 1436 | return OK; |
| 1437 | } |
| 1438 | |
| 1439 | status_t DeviceHalAidl::findOrCreatePortConfig( |
Mikhail Naganov | 289468a | 2023-03-29 10:06:15 -0700 | [diff] [blame] | 1440 | const AudioPortConfig& requestedPortConfig, const std::set<int32_t>& destinationPortIds, |
| 1441 | AudioPortConfig* portConfig, bool* created) { |
Mikhail Naganov | 5b1eed1 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 1442 | using Tag = AudioPortExt::Tag; |
| 1443 | if (requestedPortConfig.ext.getTag() == Tag::mix) { |
| 1444 | if (const auto& p = requestedPortConfig; |
| 1445 | !p.sampleRate.has_value() || !p.channelMask.has_value() || |
Mikhail Naganov | 89a9f74 | 2023-01-30 12:33:18 -0800 | [diff] [blame] | 1446 | !p.format.has_value()) { |
Mikhail Naganov | 5b1eed1 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 1447 | ALOGW("%s: provided mix port config is not fully specified: %s", |
| 1448 | __func__, p.toString().c_str()); |
| 1449 | return BAD_VALUE; |
| 1450 | } |
| 1451 | AudioConfig config; |
| 1452 | setConfigFromPortConfig(&config, requestedPortConfig); |
Mikhail Naganov | d8d01f7 | 2023-03-09 16:24:40 -0800 | [diff] [blame] | 1453 | AudioSource source = requestedPortConfig.ext.get<Tag::mix>().usecase.getTag() == |
| 1454 | AudioPortMixExtUseCase::Tag::source ? |
| 1455 | requestedPortConfig.ext.get<Tag::mix>().usecase. |
| 1456 | get<AudioPortMixExtUseCase::Tag::source>() : AudioSource::SYS_RESERVED_INVALID; |
Mikhail Naganov | 89a9f74 | 2023-01-30 12:33:18 -0800 | [diff] [blame] | 1457 | return findOrCreatePortConfig(config, requestedPortConfig.flags, |
Mikhail Naganov | 289468a | 2023-03-29 10:06:15 -0700 | [diff] [blame] | 1458 | requestedPortConfig.ext.get<Tag::mix>().handle, source, destinationPortIds, |
| 1459 | portConfig, created); |
Mikhail Naganov | 5b1eed1 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 1460 | } else if (requestedPortConfig.ext.getTag() == Tag::device) { |
| 1461 | return findOrCreatePortConfig( |
jiabin | 9c07faf | 2023-04-26 22:00:44 +0000 | [diff] [blame] | 1462 | requestedPortConfig.ext.get<Tag::device>().device, nullptr /*config*/, |
| 1463 | portConfig, created); |
Mikhail Naganov | 5b1eed1 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 1464 | } |
| 1465 | ALOGW("%s: unsupported audio port config: %s", |
| 1466 | __func__, requestedPortConfig.toString().c_str()); |
| 1467 | return BAD_VALUE; |
| 1468 | } |
| 1469 | |
| 1470 | DeviceHalAidl::Patches::iterator DeviceHalAidl::findPatch( |
| 1471 | const std::set<int32_t>& sourcePortConfigIds, const std::set<int32_t>& sinkPortConfigIds) { |
| 1472 | return std::find_if(mPatches.begin(), mPatches.end(), |
| 1473 | [&](const auto& pair) { |
| 1474 | const auto& p = pair.second; |
| 1475 | std::set<int32_t> patchSrcs( |
| 1476 | p.sourcePortConfigIds.begin(), p.sourcePortConfigIds.end()); |
| 1477 | std::set<int32_t> patchSinks( |
| 1478 | p.sinkPortConfigIds.begin(), p.sinkPortConfigIds.end()); |
| 1479 | return sourcePortConfigIds == patchSrcs && sinkPortConfigIds == patchSinks; }); |
| 1480 | } |
| 1481 | |
| 1482 | DeviceHalAidl::Ports::iterator DeviceHalAidl::findPort(const AudioDevice& device) { |
Mikhail Naganov | 89a9f74 | 2023-01-30 12:33:18 -0800 | [diff] [blame] | 1483 | if (device.type.type == AudioDeviceType::IN_DEFAULT) { |
| 1484 | return mPorts.find(mDefaultInputPortId); |
| 1485 | } else if (device.type.type == AudioDeviceType::OUT_DEFAULT) { |
| 1486 | return mPorts.find(mDefaultOutputPortId); |
| 1487 | } |
Mikhail Naganov | 5b1eed1 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 1488 | return std::find_if(mPorts.begin(), mPorts.end(), |
Mikhail Naganov | 89a9f74 | 2023-01-30 12:33:18 -0800 | [diff] [blame] | 1489 | [&](const auto& pair) { return audioDeviceMatches(device, pair.second); }); |
Mikhail Naganov | 5b1eed1 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 1490 | } |
| 1491 | |
| 1492 | DeviceHalAidl::Ports::iterator DeviceHalAidl::findPort( |
Mikhail Naganov | 289468a | 2023-03-29 10:06:15 -0700 | [diff] [blame] | 1493 | const AudioConfig& config, const AudioIoFlags& flags, |
| 1494 | const std::set<int32_t>& destinationPortIds) { |
Mikhail Naganov | e93a086 | 2023-03-15 17:06:59 -0700 | [diff] [blame] | 1495 | auto belongsToProfile = [&config](const AudioProfile& prof) { |
| 1496 | return (isDefaultAudioFormat(config.base.format) || prof.format == config.base.format) && |
| 1497 | (config.base.channelMask.getTag() == AudioChannelLayout::none || |
| 1498 | std::find(prof.channelMasks.begin(), prof.channelMasks.end(), |
| 1499 | config.base.channelMask) != prof.channelMasks.end()) && |
| 1500 | (config.base.sampleRate == 0 || |
| 1501 | std::find(prof.sampleRates.begin(), prof.sampleRates.end(), |
| 1502 | config.base.sampleRate) != prof.sampleRates.end()); |
| 1503 | }; |
jiabin | 9c07faf | 2023-04-26 22:00:44 +0000 | [diff] [blame] | 1504 | static const std::vector<AudioOutputFlags> kOptionalOutputFlags{AudioOutputFlags::BIT_PERFECT}; |
| 1505 | int optionalFlags = 0; |
| 1506 | auto flagMatches = [&flags, &optionalFlags](const AudioIoFlags& portFlags) { |
| 1507 | // Ports should be able to match if the optional flags are not requested. |
| 1508 | return portFlags == flags || |
| 1509 | (portFlags.getTag() == AudioIoFlags::Tag::output && |
| 1510 | AudioIoFlags::make<AudioIoFlags::Tag::output>( |
| 1511 | portFlags.get<AudioIoFlags::Tag::output>() & |
| 1512 | ~optionalFlags) == flags); |
| 1513 | }; |
Mikhail Naganov | 89a9f74 | 2023-01-30 12:33:18 -0800 | [diff] [blame] | 1514 | auto matcher = [&](const auto& pair) { |
| 1515 | const auto& p = pair.second; |
Mikhail Naganov | 1a44bc6 | 2023-02-16 17:35:06 -0800 | [diff] [blame] | 1516 | return p.ext.getTag() == AudioPortExt::Tag::mix && |
jiabin | 9c07faf | 2023-04-26 22:00:44 +0000 | [diff] [blame] | 1517 | flagMatches(p.flags) && |
Mikhail Naganov | 289468a | 2023-03-29 10:06:15 -0700 | [diff] [blame] | 1518 | (destinationPortIds.empty() || |
| 1519 | std::any_of(destinationPortIds.begin(), destinationPortIds.end(), |
| 1520 | [&](const int32_t destId) { return mRoutingMatrix.count( |
| 1521 | std::make_pair(p.id, destId)) != 0; })) && |
Mikhail Naganov | e93a086 | 2023-03-15 17:06:59 -0700 | [diff] [blame] | 1522 | (p.profiles.empty() || |
| 1523 | std::find_if(p.profiles.begin(), p.profiles.end(), belongsToProfile) != |
| 1524 | p.profiles.end()); }; |
jiabin | 9c07faf | 2023-04-26 22:00:44 +0000 | [diff] [blame] | 1525 | auto result = std::find_if(mPorts.begin(), mPorts.end(), matcher); |
| 1526 | if (result == mPorts.end() && flags.getTag() == AudioIoFlags::Tag::output) { |
| 1527 | auto optionalOutputFlagsIt = kOptionalOutputFlags.begin(); |
| 1528 | while (result == mPorts.end() && optionalOutputFlagsIt != kOptionalOutputFlags.end()) { |
| 1529 | if (isBitPositionFlagSet( |
| 1530 | flags.get<AudioIoFlags::Tag::output>(), *optionalOutputFlagsIt)) { |
| 1531 | // If the flag is set by the request, it must be matched. |
| 1532 | ++optionalOutputFlagsIt; |
| 1533 | continue; |
| 1534 | } |
| 1535 | optionalFlags |= makeBitPositionFlagMask(*optionalOutputFlagsIt++); |
| 1536 | result = std::find_if(mPorts.begin(), mPorts.end(), matcher); |
| 1537 | ALOGI("%s: port for config %s, flags %s was not found in the module %s, " |
| 1538 | "retried with excluding optional flags %#x", __func__, config.toString().c_str(), |
| 1539 | flags.toString().c_str(), mInstance.c_str(), optionalFlags); |
| 1540 | } |
| 1541 | } |
| 1542 | return result; |
Mikhail Naganov | 5b1eed1 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 1543 | } |
| 1544 | |
| 1545 | DeviceHalAidl::PortConfigs::iterator DeviceHalAidl::findPortConfig(const AudioDevice& device) { |
Mikhail Naganov | 5b1eed1 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 1546 | return std::find_if(mPortConfigs.begin(), mPortConfigs.end(), |
Mikhail Naganov | 89a9f74 | 2023-01-30 12:33:18 -0800 | [diff] [blame] | 1547 | [&](const auto& pair) { return audioDeviceMatches(device, pair.second); }); |
Mikhail Naganov | 5b1eed1 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 1548 | } |
| 1549 | |
| 1550 | DeviceHalAidl::PortConfigs::iterator DeviceHalAidl::findPortConfig( |
Mikhail Naganov | 89a9f74 | 2023-01-30 12:33:18 -0800 | [diff] [blame] | 1551 | const AudioConfig& config, const std::optional<AudioIoFlags>& flags, int32_t ioHandle) { |
Mikhail Naganov | 5b1eed1 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 1552 | using Tag = AudioPortExt::Tag; |
| 1553 | return std::find_if(mPortConfigs.begin(), mPortConfigs.end(), |
| 1554 | [&](const auto& pair) { |
| 1555 | const auto& p = pair.second; |
| 1556 | LOG_ALWAYS_FATAL_IF(p.ext.getTag() == Tag::mix && |
| 1557 | !p.sampleRate.has_value() || !p.channelMask.has_value() || |
| 1558 | !p.format.has_value() || !p.flags.has_value(), |
| 1559 | "%s: stored mix port config is not fully specified: %s", |
| 1560 | __func__, p.toString().c_str()); |
| 1561 | return p.ext.getTag() == Tag::mix && |
| 1562 | isConfigEqualToPortConfig(config, p) && |
Mikhail Naganov | 89a9f74 | 2023-01-30 12:33:18 -0800 | [diff] [blame] | 1563 | (!flags.has_value() || p.flags.value() == flags.value()) && |
Mikhail Naganov | 5b1eed1 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 1564 | p.ext.template get<Tag::mix>().handle == ioHandle; }); |
| 1565 | } |
Mikhail Naganov | e93a086 | 2023-03-15 17:06:59 -0700 | [diff] [blame] | 1566 | |
Mikhail Naganov | 5b1eed1 | 2023-01-25 11:29:11 -0800 | [diff] [blame] | 1567 | void DeviceHalAidl::resetPatch(int32_t patchId) { |
| 1568 | if (auto it = mPatches.find(patchId); it != mPatches.end()) { |
| 1569 | mPatches.erase(it); |
| 1570 | TIME_CHECK(); |
| 1571 | if (ndk::ScopedAStatus status = mModule->resetAudioPatch(patchId); !status.isOk()) { |
| 1572 | ALOGE("%s: error while resetting patch %d: %s", |
| 1573 | __func__, patchId, status.getDescription().c_str()); |
| 1574 | } |
| 1575 | return; |
| 1576 | } |
| 1577 | ALOGE("%s: patch id %d not found", __func__, patchId); |
| 1578 | } |
| 1579 | |
| 1580 | void DeviceHalAidl::resetPortConfig(int32_t portConfigId) { |
| 1581 | if (auto it = mPortConfigs.find(portConfigId); it != mPortConfigs.end()) { |
| 1582 | mPortConfigs.erase(it); |
| 1583 | TIME_CHECK(); |
| 1584 | if (ndk::ScopedAStatus status = mModule->resetAudioPortConfig(portConfigId); |
| 1585 | !status.isOk()) { |
| 1586 | ALOGE("%s: error while resetting port config %d: %s", |
| 1587 | __func__, portConfigId, status.getDescription().c_str()); |
| 1588 | } |
| 1589 | return; |
| 1590 | } |
| 1591 | ALOGE("%s: port config id %d not found", __func__, portConfigId); |
| 1592 | } |
| 1593 | |
Mikhail Naganov | e93a086 | 2023-03-15 17:06:59 -0700 | [diff] [blame] | 1594 | void DeviceHalAidl::resetUnusedPatches() { |
| 1595 | // Since patches can be created independently of streams via 'createAudioPatch', |
| 1596 | // here we only clean up patches for released streams. |
| 1597 | for (auto it = mStreams.begin(); it != mStreams.end(); ) { |
| 1598 | if (auto streamSp = it->first.promote(); streamSp) { |
| 1599 | ++it; |
| 1600 | } else { |
| 1601 | resetPatch(it->second); |
| 1602 | it = mStreams.erase(it); |
| 1603 | } |
| 1604 | } |
| 1605 | } |
| 1606 | |
| 1607 | void DeviceHalAidl::resetUnusedPatchesAndPortConfigs() { |
| 1608 | resetUnusedPatches(); |
| 1609 | resetUnusedPortConfigs(); |
| 1610 | } |
| 1611 | |
| 1612 | void DeviceHalAidl::resetUnusedPortConfigs() { |
| 1613 | // The assumption is that port configs are used to create patches |
| 1614 | // (or to open streams, but that involves creation of patches, too). Thus, |
| 1615 | // orphaned port configs can and should be reset. |
| 1616 | std::set<int32_t> portConfigIds; |
| 1617 | std::transform(mPortConfigs.begin(), mPortConfigs.end(), |
| 1618 | std::inserter(portConfigIds, portConfigIds.end()), |
| 1619 | [](const auto& pcPair) { return pcPair.first; }); |
| 1620 | for (const auto& p : mPatches) { |
| 1621 | for (int32_t id : p.second.sourcePortConfigIds) portConfigIds.erase(id); |
| 1622 | for (int32_t id : p.second.sinkPortConfigIds) portConfigIds.erase(id); |
| 1623 | } |
jiabin | 9c07faf | 2023-04-26 22:00:44 +0000 | [diff] [blame] | 1624 | for (int32_t id : mInitialPortConfigIds) { |
| 1625 | portConfigIds.erase(id); |
| 1626 | } |
Mikhail Naganov | e93a086 | 2023-03-15 17:06:59 -0700 | [diff] [blame] | 1627 | for (int32_t id : portConfigIds) resetPortConfig(id); |
| 1628 | } |
| 1629 | |
Mikhail Naganov | 289468a | 2023-03-29 10:06:15 -0700 | [diff] [blame] | 1630 | status_t DeviceHalAidl::updateRoutes() { |
| 1631 | TIME_CHECK(); |
Mikhail Naganov | 289468a | 2023-03-29 10:06:15 -0700 | [diff] [blame] | 1632 | RETURN_STATUS_IF_ERROR( |
Mikhail Naganov | f83b974 | 2023-04-24 13:06:04 -0700 | [diff] [blame] | 1633 | statusTFromBinderStatus(mModule->getAudioRoutes(&mRoutes))); |
| 1634 | ALOGW_IF(mRoutes.empty(), "%s: module %s returned an empty list of audio routes", |
Mikhail Naganov | 289468a | 2023-03-29 10:06:15 -0700 | [diff] [blame] | 1635 | __func__, mInstance.c_str()); |
| 1636 | mRoutingMatrix.clear(); |
Mikhail Naganov | f83b974 | 2023-04-24 13:06:04 -0700 | [diff] [blame] | 1637 | for (const auto& r : mRoutes) { |
Mikhail Naganov | 289468a | 2023-03-29 10:06:15 -0700 | [diff] [blame] | 1638 | for (auto portId : r.sourcePortIds) { |
| 1639 | mRoutingMatrix.emplace(r.sinkPortId, portId); |
| 1640 | mRoutingMatrix.emplace(portId, r.sinkPortId); |
| 1641 | } |
| 1642 | } |
| 1643 | return OK; |
| 1644 | } |
| 1645 | |
Mikhail Naganov | dfd594e | 2023-02-08 16:59:41 -0800 | [diff] [blame] | 1646 | void DeviceHalAidl::clearCallbacks(void* cookie) { |
| 1647 | std::lock_guard l(mLock); |
| 1648 | mCallbacks.erase(cookie); |
| 1649 | } |
| 1650 | |
| 1651 | sp<StreamOutHalInterfaceCallback> DeviceHalAidl::getStreamOutCallback(void* cookie) { |
| 1652 | return getCallbackImpl(cookie, &Callbacks::out); |
| 1653 | } |
| 1654 | |
| 1655 | void DeviceHalAidl::setStreamOutCallback( |
| 1656 | void* cookie, const sp<StreamOutHalInterfaceCallback>& cb) { |
| 1657 | setCallbackImpl(cookie, &Callbacks::out, cb); |
| 1658 | } |
| 1659 | |
| 1660 | sp<StreamOutHalInterfaceEventCallback> DeviceHalAidl::getStreamOutEventCallback( |
| 1661 | void* cookie) { |
| 1662 | return getCallbackImpl(cookie, &Callbacks::event); |
| 1663 | } |
| 1664 | |
| 1665 | void DeviceHalAidl::setStreamOutEventCallback( |
| 1666 | void* cookie, const sp<StreamOutHalInterfaceEventCallback>& cb) { |
| 1667 | setCallbackImpl(cookie, &Callbacks::event, cb); |
| 1668 | } |
| 1669 | |
| 1670 | sp<StreamOutHalInterfaceLatencyModeCallback> DeviceHalAidl::getStreamOutLatencyModeCallback( |
| 1671 | void* cookie) { |
| 1672 | return getCallbackImpl(cookie, &Callbacks::latency); |
| 1673 | } |
| 1674 | |
| 1675 | void DeviceHalAidl::setStreamOutLatencyModeCallback( |
| 1676 | void* cookie, const sp<StreamOutHalInterfaceLatencyModeCallback>& cb) { |
| 1677 | setCallbackImpl(cookie, &Callbacks::latency, cb); |
| 1678 | } |
| 1679 | |
| 1680 | template<class C> |
| 1681 | sp<C> DeviceHalAidl::getCallbackImpl(void* cookie, wp<C> DeviceHalAidl::Callbacks::* field) { |
| 1682 | std::lock_guard l(mLock); |
| 1683 | if (auto it = mCallbacks.find(cookie); it != mCallbacks.end()) { |
| 1684 | return ((it->second).*field).promote(); |
| 1685 | } |
| 1686 | return nullptr; |
| 1687 | } |
| 1688 | template<class C> |
| 1689 | void DeviceHalAidl::setCallbackImpl( |
| 1690 | void* cookie, wp<C> DeviceHalAidl::Callbacks::* field, const sp<C>& cb) { |
| 1691 | std::lock_guard l(mLock); |
| 1692 | if (auto it = mCallbacks.find(cookie); it != mCallbacks.end()) { |
| 1693 | (it->second).*field = cb; |
| 1694 | } |
| 1695 | } |
| 1696 | |
Mikhail Naganov | 31d4665 | 2023-01-10 18:29:25 +0000 | [diff] [blame] | 1697 | } // namespace android |