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