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