Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <stdio.h> |
| 18 | |
| 19 | #define LOG_TAG "DeviceHalHidl" |
Vlad Popa | 03bd5bc | 2023-01-17 16:16:51 +0100 | [diff] [blame] | 20 | // #define LOG_NDEBUG 0 |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 21 | |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 22 | #include <cutils/native_handle.h> |
Jiabin Huang | ebe6410 | 2021-09-07 20:01:07 +0000 | [diff] [blame] | 23 | #include <cutils/properties.h> |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 24 | #include <hwbinder/IPCThreadState.h> |
jiabin | daf4995 | 2019-11-22 14:10:57 -0800 | [diff] [blame] | 25 | #include <media/AudioContainers.h> |
Andy Hung | 224f82f | 2022-03-22 00:00:49 -0700 | [diff] [blame] | 26 | #include <mediautils/TimeCheck.h> |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 27 | #include <utils/Log.h> |
| 28 | |
Mikhail Naganov | 247b5f9 | 2021-01-15 19:16:12 +0000 | [diff] [blame] | 29 | #include PATH(android/hardware/audio/FILE_VERSION/IPrimaryDevice.h) |
| 30 | #include <HidlUtils.h> |
Kevin Rocard | b9cfbf1 | 2018-02-23 19:11:06 -0800 | [diff] [blame] | 31 | #include <common/all-versions/VersionUtils.h> |
Mikhail Naganov | 247b5f9 | 2021-01-15 19:16:12 +0000 | [diff] [blame] | 32 | #include <util/CoreUtils.h> |
Kevin Rocard | b9cfbf1 | 2018-02-23 19:11:06 -0800 | [diff] [blame] | 33 | |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 34 | #include "DeviceHalHidl.h" |
Mikhail Naganov | 89c22e4 | 2023-06-14 15:49:59 -0700 | [diff] [blame] | 35 | #include "EffectHalHidl.h" |
Mikhail Naganov | 247b5f9 | 2021-01-15 19:16:12 +0000 | [diff] [blame] | 36 | #include "ParameterUtils.h" |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 37 | #include "StreamHalHidl.h" |
| 38 | |
Vlad Popa | 03bd5bc | 2023-01-17 16:16:51 +0100 | [diff] [blame] | 39 | #if MAJOR_VERSION == 7 && MINOR_VERSION == 1 |
| 40 | #include <aidl/android/hardware/audio/core/sounddose/BpSoundDose.h> |
| 41 | #include <aidl/android/hardware/audio/sounddose/BpSoundDoseFactory.h> |
| 42 | #include <android/binder_manager.h> |
| 43 | |
| 44 | constexpr std::string_view kSoundDoseInterfaceModule = "/default"; |
| 45 | |
| 46 | using aidl::android::hardware::audio::core::sounddose::ISoundDose; |
| 47 | using aidl::android::hardware::audio::sounddose::ISoundDoseFactory; |
| 48 | #endif |
| 49 | |
Mikhail Naganov | 6718c39 | 2022-01-27 22:17:21 +0000 | [diff] [blame] | 50 | using ::android::hardware::audio::common::COMMON_TYPES_CPP_VERSION::implementation::HidlUtils; |
Kevin Rocard | 7a9f05a | 2018-11-28 16:52:25 -0800 | [diff] [blame] | 51 | using ::android::hardware::audio::common::utils::EnumBitfield; |
Mikhail Naganov | 6718c39 | 2022-01-27 22:17:21 +0000 | [diff] [blame] | 52 | using ::android::hardware::audio::CORE_TYPES_CPP_VERSION::implementation::CoreUtils; |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 53 | using ::android::hardware::hidl_string; |
| 54 | using ::android::hardware::hidl_vec; |
| 55 | |
| 56 | namespace android { |
| 57 | |
Mikhail Naganov | 6718c39 | 2022-01-27 22:17:21 +0000 | [diff] [blame] | 58 | using namespace ::android::hardware::audio::common::COMMON_TYPES_CPP_VERSION; |
| 59 | using namespace ::android::hardware::audio::CORE_TYPES_CPP_VERSION; |
Mikhail Naganov | 9ccaa16 | 2018-12-12 10:27:29 -0800 | [diff] [blame] | 60 | |
Vlad Popa | 03bd5bc | 2023-01-17 16:16:51 +0100 | [diff] [blame] | 61 | class DeviceHalHidl::SoundDoseWrapper { |
| 62 | public: |
| 63 | SoundDoseWrapper() = default; |
| 64 | ~SoundDoseWrapper() = default; |
| 65 | |
| 66 | #if MAJOR_VERSION == 7 && MINOR_VERSION == 1 |
| 67 | std::shared_ptr<ISoundDoseFactory> mSoundDoseFactory; |
| 68 | std::shared_ptr<ISoundDose> mSoundDose; |
| 69 | #endif |
| 70 | }; |
| 71 | |
Mikhail Naganov | 6718c39 | 2022-01-27 22:17:21 +0000 | [diff] [blame] | 72 | DeviceHalHidl::DeviceHalHidl(const sp<::android::hardware::audio::CPP_VERSION::IDevice>& device) |
Vlad Popa | 03bd5bc | 2023-01-17 16:16:51 +0100 | [diff] [blame] | 73 | : CoreConversionHelperHidl("DeviceHalHidl"), |
| 74 | mDevice(device), |
| 75 | mSoundDoseWrapper(std::make_unique<DeviceHalHidl::SoundDoseWrapper>()) { |
Mikhail Naganov | 6718c39 | 2022-01-27 22:17:21 +0000 | [diff] [blame] | 76 | } |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 77 | |
Mikhail Naganov | 6718c39 | 2022-01-27 22:17:21 +0000 | [diff] [blame] | 78 | DeviceHalHidl::DeviceHalHidl( |
| 79 | const sp<::android::hardware::audio::CPP_VERSION::IPrimaryDevice>& device) |
Mikhail Naganov | 288a343 | 2022-03-25 00:29:56 +0000 | [diff] [blame] | 80 | : CoreConversionHelperHidl("DeviceHalHidl"), |
Mikhail Naganov | 6718c39 | 2022-01-27 22:17:21 +0000 | [diff] [blame] | 81 | #if MAJOR_VERSION <= 6 || (MAJOR_VERSION == 7 && MINOR_VERSION == 0) |
| 82 | mDevice(device), |
| 83 | #endif |
Vlad Popa | 03bd5bc | 2023-01-17 16:16:51 +0100 | [diff] [blame] | 84 | mPrimaryDevice(device), |
| 85 | mSoundDoseWrapper(std::make_unique<DeviceHalHidl::SoundDoseWrapper>()) { |
Mikhail Naganov | 6718c39 | 2022-01-27 22:17:21 +0000 | [diff] [blame] | 86 | #if MAJOR_VERSION == 7 && MINOR_VERSION == 1 |
| 87 | auto getDeviceRet = mPrimaryDevice->getDevice(); |
| 88 | if (getDeviceRet.isOk()) { |
| 89 | mDevice = getDeviceRet; |
| 90 | } else { |
| 91 | ALOGE("Call to IPrimaryDevice.getDevice has failed: %s", |
| 92 | getDeviceRet.description().c_str()); |
| 93 | } |
| 94 | #endif |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | DeviceHalHidl::~DeviceHalHidl() { |
| 98 | if (mDevice != 0) { |
Mikhail Naganov | 3355e44 | 2019-11-20 14:20:01 -0800 | [diff] [blame] | 99 | #if MAJOR_VERSION <= 5 |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 100 | mDevice.clear(); |
| 101 | hardware::IPCThreadState::self()->flushCommands(); |
Mikhail Naganov | 3355e44 | 2019-11-20 14:20:01 -0800 | [diff] [blame] | 102 | #elif MAJOR_VERSION >= 6 |
| 103 | mDevice->close(); |
| 104 | #endif |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 105 | } |
| 106 | } |
| 107 | |
Mikhail Naganov | 2d81489 | 2023-04-24 13:06:04 -0700 | [diff] [blame] | 108 | status_t DeviceHalHidl::getAudioPorts( |
| 109 | std::vector<media::audio::common::AudioPort> *ports __unused) { |
| 110 | return INVALID_OPERATION; |
| 111 | } |
| 112 | |
| 113 | status_t DeviceHalHidl::getAudioRoutes(std::vector<media::AudioRoute> *routes __unused) { |
| 114 | return INVALID_OPERATION; |
| 115 | } |
| 116 | |
Mikhail Naganov | ffd9771 | 2023-05-03 17:45:36 -0700 | [diff] [blame] | 117 | status_t DeviceHalHidl::getSupportedModes( |
| 118 | std::vector<media::audio::common::AudioMode> *modes __unused) { |
| 119 | return INVALID_OPERATION; |
| 120 | } |
| 121 | |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 122 | status_t DeviceHalHidl::getSupportedDevices(uint32_t*) { |
| 123 | // Obsolete. |
| 124 | return INVALID_OPERATION; |
| 125 | } |
| 126 | |
| 127 | status_t DeviceHalHidl::initCheck() { |
Andy Hung | 224f82f | 2022-03-22 00:00:49 -0700 | [diff] [blame] | 128 | TIME_CHECK(); |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 129 | if (mDevice == 0) return NO_INIT; |
| 130 | return processReturn("initCheck", mDevice->initCheck()); |
| 131 | } |
| 132 | |
| 133 | status_t DeviceHalHidl::setVoiceVolume(float volume) { |
Andy Hung | 224f82f | 2022-03-22 00:00:49 -0700 | [diff] [blame] | 134 | TIME_CHECK(); |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 135 | if (mDevice == 0) return NO_INIT; |
| 136 | if (mPrimaryDevice == 0) return INVALID_OPERATION; |
| 137 | return processReturn("setVoiceVolume", mPrimaryDevice->setVoiceVolume(volume)); |
| 138 | } |
| 139 | |
| 140 | status_t DeviceHalHidl::setMasterVolume(float volume) { |
Andy Hung | 224f82f | 2022-03-22 00:00:49 -0700 | [diff] [blame] | 141 | TIME_CHECK(); |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 142 | if (mDevice == 0) return NO_INIT; |
Mikhail Naganov | ae1f662 | 2019-02-21 15:20:05 -0800 | [diff] [blame] | 143 | return processReturn("setMasterVolume", mDevice->setMasterVolume(volume)); |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | status_t DeviceHalHidl::getMasterVolume(float *volume) { |
Andy Hung | 224f82f | 2022-03-22 00:00:49 -0700 | [diff] [blame] | 147 | TIME_CHECK(); |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 148 | if (mDevice == 0) return NO_INIT; |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 149 | Result retval; |
Mikhail Naganov | ae1f662 | 2019-02-21 15:20:05 -0800 | [diff] [blame] | 150 | Return<void> ret = mDevice->getMasterVolume( |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 151 | [&](Result r, float v) { |
| 152 | retval = r; |
| 153 | if (retval == Result::OK) { |
| 154 | *volume = v; |
| 155 | } |
| 156 | }); |
| 157 | return processReturn("getMasterVolume", ret, retval); |
| 158 | } |
| 159 | |
| 160 | status_t DeviceHalHidl::setMode(audio_mode_t mode) { |
Andy Hung | 224f82f | 2022-03-22 00:00:49 -0700 | [diff] [blame] | 161 | TIME_CHECK(); |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 162 | if (mDevice == 0) return NO_INIT; |
| 163 | if (mPrimaryDevice == 0) return INVALID_OPERATION; |
| 164 | return processReturn("setMode", mPrimaryDevice->setMode(AudioMode(mode))); |
| 165 | } |
| 166 | |
| 167 | status_t DeviceHalHidl::setMicMute(bool state) { |
Andy Hung | 224f82f | 2022-03-22 00:00:49 -0700 | [diff] [blame] | 168 | TIME_CHECK(); |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 169 | if (mDevice == 0) return NO_INIT; |
| 170 | return processReturn("setMicMute", mDevice->setMicMute(state)); |
| 171 | } |
| 172 | |
| 173 | status_t DeviceHalHidl::getMicMute(bool *state) { |
Andy Hung | 224f82f | 2022-03-22 00:00:49 -0700 | [diff] [blame] | 174 | TIME_CHECK(); |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 175 | if (mDevice == 0) return NO_INIT; |
| 176 | Result retval; |
| 177 | Return<void> ret = mDevice->getMicMute( |
| 178 | [&](Result r, bool mute) { |
| 179 | retval = r; |
| 180 | if (retval == Result::OK) { |
| 181 | *state = mute; |
| 182 | } |
| 183 | }); |
| 184 | return processReturn("getMicMute", ret, retval); |
| 185 | } |
| 186 | |
| 187 | status_t DeviceHalHidl::setMasterMute(bool state) { |
Andy Hung | 224f82f | 2022-03-22 00:00:49 -0700 | [diff] [blame] | 188 | TIME_CHECK(); |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 189 | if (mDevice == 0) return NO_INIT; |
| 190 | return processReturn("setMasterMute", mDevice->setMasterMute(state)); |
| 191 | } |
| 192 | |
| 193 | status_t DeviceHalHidl::getMasterMute(bool *state) { |
Andy Hung | 224f82f | 2022-03-22 00:00:49 -0700 | [diff] [blame] | 194 | TIME_CHECK(); |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 195 | if (mDevice == 0) return NO_INIT; |
| 196 | Result retval; |
| 197 | Return<void> ret = mDevice->getMasterMute( |
| 198 | [&](Result r, bool mute) { |
| 199 | retval = r; |
| 200 | if (retval == Result::OK) { |
| 201 | *state = mute; |
| 202 | } |
| 203 | }); |
| 204 | return processReturn("getMasterMute", ret, retval); |
| 205 | } |
| 206 | |
| 207 | status_t DeviceHalHidl::setParameters(const String8& kvPairs) { |
Andy Hung | 224f82f | 2022-03-22 00:00:49 -0700 | [diff] [blame] | 208 | TIME_CHECK(); |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 209 | if (mDevice == 0) return NO_INIT; |
| 210 | hidl_vec<ParameterValue> hidlParams; |
| 211 | status_t status = parametersFromHal(kvPairs, &hidlParams); |
| 212 | if (status != OK) return status; |
Kevin Rocard | b9cfbf1 | 2018-02-23 19:11:06 -0800 | [diff] [blame] | 213 | // TODO: change the API so that context and kvPairs are separated |
| 214 | return processReturn("setParameters", |
| 215 | utils::setParameters(mDevice, {} /* context */, hidlParams)); |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | status_t DeviceHalHidl::getParameters(const String8& keys, String8 *values) { |
Andy Hung | 224f82f | 2022-03-22 00:00:49 -0700 | [diff] [blame] | 219 | TIME_CHECK(); |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 220 | values->clear(); |
| 221 | if (mDevice == 0) return NO_INIT; |
| 222 | hidl_vec<hidl_string> hidlKeys; |
| 223 | status_t status = keysFromHal(keys, &hidlKeys); |
| 224 | if (status != OK) return status; |
| 225 | Result retval; |
Kevin Rocard | b9cfbf1 | 2018-02-23 19:11:06 -0800 | [diff] [blame] | 226 | Return<void> ret = utils::getParameters(mDevice, |
| 227 | {} /* context */, |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 228 | hidlKeys, |
| 229 | [&](Result r, const hidl_vec<ParameterValue>& parameters) { |
| 230 | retval = r; |
| 231 | if (retval == Result::OK) { |
| 232 | parametersToHal(parameters, values); |
| 233 | } |
| 234 | }); |
| 235 | return processReturn("getParameters", ret, retval); |
| 236 | } |
| 237 | |
| 238 | status_t DeviceHalHidl::getInputBufferSize( |
Mikhail Naganov | d5b643f | 2024-02-15 11:51:26 -0800 | [diff] [blame] | 239 | struct audio_config *config, size_t *size) { |
Andy Hung | 224f82f | 2022-03-22 00:00:49 -0700 | [diff] [blame] | 240 | TIME_CHECK(); |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 241 | if (mDevice == 0) return NO_INIT; |
| 242 | AudioConfig hidlConfig; |
Mikhail Naganov | 05e0319 | 2020-12-14 23:19:54 +0000 | [diff] [blame] | 243 | HidlUtils::audioConfigFromHal(*config, true /*isInput*/, &hidlConfig); |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 244 | Result retval; |
| 245 | Return<void> ret = mDevice->getInputBufferSize( |
| 246 | hidlConfig, |
| 247 | [&](Result r, uint64_t bufferSize) { |
| 248 | retval = r; |
| 249 | if (retval == Result::OK) { |
| 250 | *size = static_cast<size_t>(bufferSize); |
| 251 | } |
| 252 | }); |
| 253 | return processReturn("getInputBufferSize", ret, retval); |
| 254 | } |
| 255 | |
| 256 | status_t DeviceHalHidl::openOutputStream( |
| 257 | audio_io_handle_t handle, |
jiabin | 4381040 | 2019-10-24 14:58:31 -0700 | [diff] [blame] | 258 | audio_devices_t deviceType, |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 259 | audio_output_flags_t flags, |
| 260 | struct audio_config *config, |
| 261 | const char *address, |
Haofan Wang | bf44e09 | 2024-07-07 15:29:38 -0700 | [diff] [blame] | 262 | sp<StreamOutHalInterface> *outStream, |
| 263 | const std::vector<playback_track_metadata_v7_t>& sourceMetadata) { |
Andy Hung | 224f82f | 2022-03-22 00:00:49 -0700 | [diff] [blame] | 264 | TIME_CHECK(); |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 265 | if (mDevice == 0) return NO_INIT; |
| 266 | DeviceAddress hidlDevice; |
Mikhail Naganov | 247b5f9 | 2021-01-15 19:16:12 +0000 | [diff] [blame] | 267 | if (status_t status = CoreUtils::deviceAddressFromHal(deviceType, address, &hidlDevice); |
| 268 | status != OK) { |
| 269 | return status; |
| 270 | } |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 271 | AudioConfig hidlConfig; |
Mikhail Naganov | 247b5f9 | 2021-01-15 19:16:12 +0000 | [diff] [blame] | 272 | if (status_t status = HidlUtils::audioConfigFromHal(*config, false /*isInput*/, &hidlConfig); |
| 273 | status != OK) { |
| 274 | return status; |
| 275 | } |
Eric Laurent | e28c66d | 2022-01-21 13:40:41 +0100 | [diff] [blame] | 276 | |
Haofan Wang | bf44e09 | 2024-07-07 15:29:38 -0700 | [diff] [blame] | 277 | #if MAJOR_VERSION == 4 |
| 278 | ::android::hardware::audio::CORE_TYPES_CPP_VERSION::SourceMetadata hidlMetadata; |
| 279 | #else |
| 280 | ::android::hardware::audio::common::COMMON_TYPES_CPP_VERSION::SourceMetadata hidlMetadata; |
| 281 | #endif |
| 282 | |
Haofan Wang | 9b39086 | 2024-08-22 17:44:59 +0000 | [diff] [blame] | 283 | RETURN_STATUS_IF_ERROR(CoreUtils::sourceMetadataFromHalV7( |
| 284 | sourceMetadata, true /*ignoreNonVendorTags*/, &hidlMetadata |
| 285 | )); |
Haofan Wang | bf44e09 | 2024-07-07 15:29:38 -0700 | [diff] [blame] | 286 | |
Eric Laurent | e28c66d | 2022-01-21 13:40:41 +0100 | [diff] [blame] | 287 | #if !(MAJOR_VERSION == 7 && MINOR_VERSION == 1) |
| 288 | //TODO: b/193496180 use spatializer flag at audio HAL when available |
| 289 | if ((flags & AUDIO_OUTPUT_FLAG_SPATIALIZER) != 0) { |
| 290 | flags = (audio_output_flags_t)(flags & ~AUDIO_OUTPUT_FLAG_SPATIALIZER); |
| 291 | flags = (audio_output_flags_t) |
| 292 | (flags | AUDIO_OUTPUT_FLAG_FAST | AUDIO_OUTPUT_FLAG_DEEP_BUFFER); |
| 293 | } |
| 294 | #endif |
| 295 | |
Mikhail Naganov | 247b5f9 | 2021-01-15 19:16:12 +0000 | [diff] [blame] | 296 | CoreUtils::AudioOutputFlags hidlFlags; |
| 297 | if (status_t status = CoreUtils::audioOutputFlagsFromHal(flags, &hidlFlags); status != OK) { |
| 298 | return status; |
| 299 | } |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 300 | Result retval = Result::NOT_INITIALIZED; |
Mikhail Naganov | 6718c39 | 2022-01-27 22:17:21 +0000 | [diff] [blame] | 301 | #if MAJOR_VERSION == 7 && MINOR_VERSION == 1 |
| 302 | Return<void> ret = mDevice->openOutputStream_7_1( |
| 303 | #else |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 304 | Return<void> ret = mDevice->openOutputStream( |
Mikhail Naganov | 6718c39 | 2022-01-27 22:17:21 +0000 | [diff] [blame] | 305 | #endif |
Mikhail Naganov | 247b5f9 | 2021-01-15 19:16:12 +0000 | [diff] [blame] | 306 | handle, hidlDevice, hidlConfig, hidlFlags, |
Kevin Rocard | 3d48dce | 2018-11-08 17:16:57 -0800 | [diff] [blame] | 307 | #if MAJOR_VERSION >= 4 |
Haofan Wang | bf44e09 | 2024-07-07 15:29:38 -0700 | [diff] [blame] | 308 | hidlMetadata /* metadata */, |
Kevin Rocard | 070e751 | 2018-05-22 09:29:13 -0700 | [diff] [blame] | 309 | #endif |
Mikhail Naganov | 6718c39 | 2022-01-27 22:17:21 +0000 | [diff] [blame] | 310 | [&](Result r, const sp<::android::hardware::audio::CPP_VERSION::IStreamOut>& result, |
| 311 | const AudioConfig& suggestedConfig) { |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 312 | retval = r; |
| 313 | if (retval == Result::OK) { |
| 314 | *outStream = new StreamOutHalHidl(result); |
| 315 | } |
| 316 | HidlUtils::audioConfigToHal(suggestedConfig, config); |
| 317 | }); |
jiabin | 12537fc | 2023-10-12 17:56:08 +0000 | [diff] [blame] | 318 | const status_t status = processReturn("openOutputStream", ret, retval); |
| 319 | cleanupStreams(); |
| 320 | if (status == NO_ERROR) { |
| 321 | mStreams.insert({handle, *outStream}); |
| 322 | } |
| 323 | return status; |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | status_t DeviceHalHidl::openInputStream( |
| 327 | audio_io_handle_t handle, |
| 328 | audio_devices_t devices, |
| 329 | struct audio_config *config, |
| 330 | audio_input_flags_t flags, |
| 331 | const char *address, |
| 332 | audio_source_t source, |
Mikhail Naganov | b4e037e | 2019-01-14 15:56:33 -0800 | [diff] [blame] | 333 | audio_devices_t outputDevice, |
| 334 | const char *outputDeviceAddress, |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 335 | sp<StreamInHalInterface> *inStream) { |
Andy Hung | 224f82f | 2022-03-22 00:00:49 -0700 | [diff] [blame] | 336 | TIME_CHECK(); |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 337 | if (mDevice == 0) return NO_INIT; |
| 338 | DeviceAddress hidlDevice; |
Mikhail Naganov | 247b5f9 | 2021-01-15 19:16:12 +0000 | [diff] [blame] | 339 | if (status_t status = CoreUtils::deviceAddressFromHal(devices, address, &hidlDevice); |
| 340 | status != OK) { |
| 341 | return status; |
| 342 | } |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 343 | AudioConfig hidlConfig; |
Mikhail Naganov | 247b5f9 | 2021-01-15 19:16:12 +0000 | [diff] [blame] | 344 | if (status_t status = HidlUtils::audioConfigFromHal(*config, true /*isInput*/, &hidlConfig); |
| 345 | status != OK) { |
| 346 | return status; |
| 347 | } |
| 348 | CoreUtils::AudioInputFlags hidlFlags; |
Dean Wheatley | ea18634 | 2021-03-12 16:49:47 +1100 | [diff] [blame] | 349 | #if MAJOR_VERSION <= 5 |
| 350 | // Some flags were specific to framework and must not leak to the HAL. |
| 351 | flags = static_cast<audio_input_flags_t>(flags & ~AUDIO_INPUT_FLAG_DIRECT); |
| 352 | #endif |
Mikhail Naganov | 247b5f9 | 2021-01-15 19:16:12 +0000 | [diff] [blame] | 353 | if (status_t status = CoreUtils::audioInputFlagsFromHal(flags, &hidlFlags); status != OK) { |
| 354 | return status; |
| 355 | } |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 356 | Result retval = Result::NOT_INITIALIZED; |
Kevin Rocard | 070e751 | 2018-05-22 09:29:13 -0700 | [diff] [blame] | 357 | #if MAJOR_VERSION == 2 |
Mikhail Naganov | d9499eb | 2018-12-17 16:23:22 -0800 | [diff] [blame] | 358 | auto sinkMetadata = AudioSource(source); |
Kevin Rocard | 3d48dce | 2018-11-08 17:16:57 -0800 | [diff] [blame] | 359 | #elif MAJOR_VERSION >= 4 |
Kevin Rocard | b9cfbf1 | 2018-02-23 19:11:06 -0800 | [diff] [blame] | 360 | // TODO: correctly propagate the tracks sources and volume |
| 361 | // for now, only send the main source at 1dbfs |
Mikhail Naganov | 247b5f9 | 2021-01-15 19:16:12 +0000 | [diff] [blame] | 362 | AudioSource hidlSource; |
| 363 | if (status_t status = HidlUtils::audioSourceFromHal(source, &hidlSource); status != OK) { |
| 364 | return status; |
| 365 | } |
| 366 | SinkMetadata sinkMetadata = {{{ .source = std::move(hidlSource), .gain = 1 }}}; |
Kevin Rocard | 070e751 | 2018-05-22 09:29:13 -0700 | [diff] [blame] | 367 | #endif |
Mikhail Naganov | b4e037e | 2019-01-14 15:56:33 -0800 | [diff] [blame] | 368 | #if MAJOR_VERSION < 5 |
| 369 | (void)outputDevice; |
| 370 | (void)outputDeviceAddress; |
| 371 | #else |
Mikhail Naganov | c425880 | 2021-02-08 17:14:17 -0800 | [diff] [blame] | 372 | #if MAJOR_VERSION >= 7 |
| 373 | (void)HidlUtils::audioChannelMaskFromHal( |
| 374 | AUDIO_CHANNEL_NONE, true /*isInput*/, &sinkMetadata.tracks[0].channelMask); |
| 375 | #endif |
Mikhail Naganov | b4e037e | 2019-01-14 15:56:33 -0800 | [diff] [blame] | 376 | if (outputDevice != AUDIO_DEVICE_NONE) { |
| 377 | DeviceAddress hidlOutputDevice; |
Mikhail Naganov | 247b5f9 | 2021-01-15 19:16:12 +0000 | [diff] [blame] | 378 | if (status_t status = CoreUtils::deviceAddressFromHal( |
| 379 | outputDevice, outputDeviceAddress, &hidlOutputDevice); status != OK) { |
| 380 | return status; |
| 381 | } |
Mikhail Naganov | b4e037e | 2019-01-14 15:56:33 -0800 | [diff] [blame] | 382 | sinkMetadata.tracks[0].destination.device(std::move(hidlOutputDevice)); |
| 383 | } |
| 384 | #endif |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 385 | Return<void> ret = mDevice->openInputStream( |
Mikhail Naganov | 247b5f9 | 2021-01-15 19:16:12 +0000 | [diff] [blame] | 386 | handle, hidlDevice, hidlConfig, hidlFlags, sinkMetadata, |
Mikhail Naganov | accbe8a | 2022-02-03 23:45:36 +0000 | [diff] [blame] | 387 | [&](Result r, |
| 388 | const sp<::android::hardware::audio::CORE_TYPES_CPP_VERSION::IStreamIn>& result, |
Mikhail Naganov | 6718c39 | 2022-01-27 22:17:21 +0000 | [diff] [blame] | 389 | const AudioConfig& suggestedConfig) { |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 390 | retval = r; |
| 391 | if (retval == Result::OK) { |
| 392 | *inStream = new StreamInHalHidl(result); |
| 393 | } |
| 394 | HidlUtils::audioConfigToHal(suggestedConfig, config); |
| 395 | }); |
jiabin | 12537fc | 2023-10-12 17:56:08 +0000 | [diff] [blame] | 396 | const status_t status = processReturn("openInputStream", ret, retval); |
| 397 | cleanupStreams(); |
| 398 | if (status == NO_ERROR) { |
| 399 | mStreams.insert({handle, *inStream}); |
| 400 | } |
| 401 | return status; |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | status_t DeviceHalHidl::supportsAudioPatches(bool *supportsPatches) { |
Andy Hung | 224f82f | 2022-03-22 00:00:49 -0700 | [diff] [blame] | 405 | TIME_CHECK(); |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 406 | if (mDevice == 0) return NO_INIT; |
| 407 | return processReturn("supportsAudioPatches", mDevice->supportsAudioPatches(), supportsPatches); |
| 408 | } |
| 409 | |
| 410 | status_t DeviceHalHidl::createAudioPatch( |
| 411 | unsigned int num_sources, |
| 412 | const struct audio_port_config *sources, |
| 413 | unsigned int num_sinks, |
| 414 | const struct audio_port_config *sinks, |
| 415 | audio_patch_handle_t *patch) { |
Andy Hung | 224f82f | 2022-03-22 00:00:49 -0700 | [diff] [blame] | 416 | TIME_CHECK(); |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 417 | if (mDevice == 0) return NO_INIT; |
Eric Laurent | 8711cfd | 2019-06-10 18:06:33 -0700 | [diff] [blame] | 418 | if (patch == nullptr) return BAD_VALUE; |
| 419 | |
Mikhail Naganov | 73bdf57 | 2019-12-11 12:34:15 -0800 | [diff] [blame] | 420 | #if MAJOR_VERSION < 6 |
Eric Laurent | 8711cfd | 2019-06-10 18:06:33 -0700 | [diff] [blame] | 421 | if (*patch != AUDIO_PATCH_HANDLE_NONE) { |
| 422 | status_t status = releaseAudioPatch(*patch); |
| 423 | ALOGW_IF(status != NO_ERROR, "%s error %d releasing patch handle %d", |
| 424 | __func__, status, *patch); |
Mikhail Naganov | 73bdf57 | 2019-12-11 12:34:15 -0800 | [diff] [blame] | 425 | *patch = AUDIO_PATCH_HANDLE_NONE; |
Eric Laurent | 8711cfd | 2019-06-10 18:06:33 -0700 | [diff] [blame] | 426 | } |
Mikhail Naganov | 73bdf57 | 2019-12-11 12:34:15 -0800 | [diff] [blame] | 427 | #endif |
Eric Laurent | 8711cfd | 2019-06-10 18:06:33 -0700 | [diff] [blame] | 428 | |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 429 | hidl_vec<AudioPortConfig> hidlSources, hidlSinks; |
| 430 | HidlUtils::audioPortConfigsFromHal(num_sources, sources, &hidlSources); |
| 431 | HidlUtils::audioPortConfigsFromHal(num_sinks, sinks, &hidlSinks); |
Mikhail Naganov | 73bdf57 | 2019-12-11 12:34:15 -0800 | [diff] [blame] | 432 | Result retval = Result::OK; |
| 433 | Return<void> ret; |
| 434 | std::string methodName = "createAudioPatch"; |
| 435 | if (*patch == AUDIO_PATCH_HANDLE_NONE) { // always true for MAJOR_VERSION < 6 |
| 436 | ret = mDevice->createAudioPatch( |
| 437 | hidlSources, hidlSinks, |
| 438 | [&](Result r, AudioPatchHandle hidlPatch) { |
| 439 | retval = r; |
| 440 | if (retval == Result::OK) { |
| 441 | *patch = static_cast<audio_patch_handle_t>(hidlPatch); |
| 442 | } |
| 443 | }); |
| 444 | } else { |
| 445 | #if MAJOR_VERSION >= 6 |
| 446 | ret = mDevice->updateAudioPatch( |
| 447 | *patch, |
| 448 | hidlSources, hidlSinks, |
| 449 | [&](Result r, AudioPatchHandle hidlPatch) { |
| 450 | retval = r; |
| 451 | if (retval == Result::OK) { |
| 452 | *patch = static_cast<audio_patch_handle_t>(hidlPatch); |
| 453 | } |
| 454 | }); |
| 455 | methodName = "updateAudioPatch"; |
| 456 | #endif |
| 457 | } |
| 458 | return processReturn(methodName.c_str(), ret, retval); |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | status_t DeviceHalHidl::releaseAudioPatch(audio_patch_handle_t patch) { |
Andy Hung | 224f82f | 2022-03-22 00:00:49 -0700 | [diff] [blame] | 462 | TIME_CHECK(); |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 463 | if (mDevice == 0) return NO_INIT; |
| 464 | return processReturn("releaseAudioPatch", mDevice->releaseAudioPatch(patch)); |
| 465 | } |
| 466 | |
Mikhail Naganov | 720cc43 | 2021-03-24 20:42:49 -0700 | [diff] [blame] | 467 | template <typename HalPort> |
| 468 | status_t DeviceHalHidl::getAudioPortImpl(HalPort *port) { |
Mikhail Naganov | 2d81489 | 2023-04-24 13:06:04 -0700 | [diff] [blame] | 469 | using ::android::hardware::audio::common::COMMON_TYPES_CPP_VERSION::AudioPort; |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 470 | if (mDevice == 0) return NO_INIT; |
| 471 | AudioPort hidlPort; |
| 472 | HidlUtils::audioPortFromHal(*port, &hidlPort); |
| 473 | Result retval; |
| 474 | Return<void> ret = mDevice->getAudioPort( |
| 475 | hidlPort, |
| 476 | [&](Result r, const AudioPort& p) { |
| 477 | retval = r; |
| 478 | if (retval == Result::OK) { |
| 479 | HidlUtils::audioPortToHal(p, port); |
| 480 | } |
| 481 | }); |
| 482 | return processReturn("getAudioPort", ret, retval); |
| 483 | } |
| 484 | |
Mikhail Naganov | 720cc43 | 2021-03-24 20:42:49 -0700 | [diff] [blame] | 485 | status_t DeviceHalHidl::getAudioPort(struct audio_port *port) { |
Andy Hung | 224f82f | 2022-03-22 00:00:49 -0700 | [diff] [blame] | 486 | TIME_CHECK(); |
Mikhail Naganov | 720cc43 | 2021-03-24 20:42:49 -0700 | [diff] [blame] | 487 | return getAudioPortImpl(port); |
| 488 | } |
| 489 | |
jiabin | b4fed19 | 2020-09-22 14:45:40 -0700 | [diff] [blame] | 490 | status_t DeviceHalHidl::getAudioPort(struct audio_port_v7 *port) { |
Andy Hung | 224f82f | 2022-03-22 00:00:49 -0700 | [diff] [blame] | 491 | TIME_CHECK(); |
jiabin | b4fed19 | 2020-09-22 14:45:40 -0700 | [diff] [blame] | 492 | #if MAJOR_VERSION >= 7 |
Mikhail Naganov | 720cc43 | 2021-03-24 20:42:49 -0700 | [diff] [blame] | 493 | return getAudioPortImpl(port); |
jiabin | b4fed19 | 2020-09-22 14:45:40 -0700 | [diff] [blame] | 494 | #else |
| 495 | struct audio_port audioPort = {}; |
Mikhail Naganov | 720cc43 | 2021-03-24 20:42:49 -0700 | [diff] [blame] | 496 | status_t result = NO_ERROR; |
| 497 | if (!audio_populate_audio_port(port, &audioPort)) { |
| 498 | ALOGE("Failed to populate legacy audio port from audio_port_v7"); |
| 499 | result = BAD_VALUE; |
| 500 | } |
| 501 | status_t status = getAudioPort(&audioPort); |
jiabin | b4fed19 | 2020-09-22 14:45:40 -0700 | [diff] [blame] | 502 | if (status == NO_ERROR) { |
| 503 | audio_populate_audio_port_v7(&audioPort, port); |
Mikhail Naganov | 720cc43 | 2021-03-24 20:42:49 -0700 | [diff] [blame] | 504 | } else { |
| 505 | result = status; |
jiabin | b4fed19 | 2020-09-22 14:45:40 -0700 | [diff] [blame] | 506 | } |
Mikhail Naganov | 720cc43 | 2021-03-24 20:42:49 -0700 | [diff] [blame] | 507 | return result; |
jiabin | b4fed19 | 2020-09-22 14:45:40 -0700 | [diff] [blame] | 508 | #endif |
jiabin | b4fed19 | 2020-09-22 14:45:40 -0700 | [diff] [blame] | 509 | } |
| 510 | |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 511 | status_t DeviceHalHidl::setAudioPortConfig(const struct audio_port_config *config) { |
Mikhail Naganov | 2d81489 | 2023-04-24 13:06:04 -0700 | [diff] [blame] | 512 | using ::android::hardware::audio::common::COMMON_TYPES_CPP_VERSION::AudioPortConfig; |
Andy Hung | 224f82f | 2022-03-22 00:00:49 -0700 | [diff] [blame] | 513 | TIME_CHECK(); |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 514 | if (mDevice == 0) return NO_INIT; |
| 515 | AudioPortConfig hidlConfig; |
| 516 | HidlUtils::audioPortConfigFromHal(*config, &hidlConfig); |
| 517 | return processReturn("setAudioPortConfig", mDevice->setAudioPortConfig(hidlConfig)); |
| 518 | } |
| 519 | |
Kevin Rocard | 070e751 | 2018-05-22 09:29:13 -0700 | [diff] [blame] | 520 | #if MAJOR_VERSION == 2 |
| 521 | status_t DeviceHalHidl::getMicrophones( |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 522 | std::vector<audio_microphone_characteristic_t> *microphonesInfo __unused) { |
Kevin Rocard | 070e751 | 2018-05-22 09:29:13 -0700 | [diff] [blame] | 523 | if (mDevice == 0) return NO_INIT; |
| 524 | return INVALID_OPERATION; |
| 525 | } |
Kevin Rocard | 3d48dce | 2018-11-08 17:16:57 -0800 | [diff] [blame] | 526 | #elif MAJOR_VERSION >= 4 |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 527 | status_t DeviceHalHidl::getMicrophones( |
| 528 | std::vector<audio_microphone_characteristic_t> *microphonesInfo) { |
Andy Hung | 224f82f | 2022-03-22 00:00:49 -0700 | [diff] [blame] | 529 | TIME_CHECK(); |
jiabin | 9ff780e | 2018-03-19 18:19:52 -0700 | [diff] [blame] | 530 | if (mDevice == 0) return NO_INIT; |
| 531 | Result retval; |
| 532 | Return<void> ret = mDevice->getMicrophones( |
| 533 | [&](Result r, hidl_vec<MicrophoneInfo> micArrayHal) { |
| 534 | retval = r; |
| 535 | for (size_t k = 0; k < micArrayHal.size(); k++) { |
| 536 | audio_microphone_characteristic_t dst; |
| 537 | //convert |
Mikhail Naganov | 247b5f9 | 2021-01-15 19:16:12 +0000 | [diff] [blame] | 538 | (void)CoreUtils::microphoneInfoToHal(micArrayHal[k], &dst); |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 539 | microphonesInfo->push_back(dst); |
jiabin | 9ff780e | 2018-03-19 18:19:52 -0700 | [diff] [blame] | 540 | } |
| 541 | }); |
| 542 | return processReturn("getMicrophones", ret, retval); |
| 543 | } |
Kevin Rocard | 070e751 | 2018-05-22 09:29:13 -0700 | [diff] [blame] | 544 | #endif |
jiabin | 9ff780e | 2018-03-19 18:19:52 -0700 | [diff] [blame] | 545 | |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 546 | #if MAJOR_VERSION >= 6 |
| 547 | status_t DeviceHalHidl::addDeviceEffect( |
Mikhail Naganov | d2c7f85 | 2023-06-14 18:00:13 -0700 | [diff] [blame] | 548 | const struct audio_port_config *device, sp<EffectHalInterface> effect) { |
Andy Hung | 224f82f | 2022-03-22 00:00:49 -0700 | [diff] [blame] | 549 | TIME_CHECK(); |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 550 | if (mDevice == 0) return NO_INIT; |
Mikhail Naganov | 89c22e4 | 2023-06-14 15:49:59 -0700 | [diff] [blame] | 551 | auto hidlEffect = sp<effect::EffectHalHidl>::cast(effect); |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 552 | return processReturn("addDeviceEffect", mDevice->addDeviceEffect( |
Mikhail Naganov | d2c7f85 | 2023-06-14 18:00:13 -0700 | [diff] [blame] | 553 | static_cast<AudioPortHandle>(device->id), hidlEffect->effectId())); |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 554 | } |
| 555 | #else |
| 556 | status_t DeviceHalHidl::addDeviceEffect( |
Mikhail Naganov | d2c7f85 | 2023-06-14 18:00:13 -0700 | [diff] [blame] | 557 | const struct audio_port_config *device __unused, sp<EffectHalInterface> effect __unused) { |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 558 | return INVALID_OPERATION; |
| 559 | } |
| 560 | #endif |
| 561 | |
| 562 | #if MAJOR_VERSION >= 6 |
| 563 | status_t DeviceHalHidl::removeDeviceEffect( |
Mikhail Naganov | d2c7f85 | 2023-06-14 18:00:13 -0700 | [diff] [blame] | 564 | const struct audio_port_config *device, sp<EffectHalInterface> effect) { |
Andy Hung | 224f82f | 2022-03-22 00:00:49 -0700 | [diff] [blame] | 565 | TIME_CHECK(); |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 566 | if (mDevice == 0) return NO_INIT; |
Mikhail Naganov | 89c22e4 | 2023-06-14 15:49:59 -0700 | [diff] [blame] | 567 | auto hidlEffect = sp<effect::EffectHalHidl>::cast(effect); |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 568 | return processReturn("removeDeviceEffect", mDevice->removeDeviceEffect( |
Mikhail Naganov | d2c7f85 | 2023-06-14 18:00:13 -0700 | [diff] [blame] | 569 | static_cast<AudioPortHandle>(device->id), hidlEffect->effectId())); |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 570 | } |
| 571 | #else |
| 572 | status_t DeviceHalHidl::removeDeviceEffect( |
Mikhail Naganov | d2c7f85 | 2023-06-14 18:00:13 -0700 | [diff] [blame] | 573 | const struct audio_port_config *device __unused, sp<EffectHalInterface> effect __unused) { |
Eric Laurent | b82e6b7 | 2019-11-22 17:25:04 -0800 | [diff] [blame] | 574 | return INVALID_OPERATION; |
| 575 | } |
| 576 | #endif |
| 577 | |
jiabin | c004863 | 2023-04-27 22:04:31 +0000 | [diff] [blame] | 578 | status_t DeviceHalHidl::prepareToDisconnectExternalDevice(const struct audio_port_v7* port) { |
| 579 | // For HIDL HAL, there is not API to call notify the HAL to prepare for device connected |
| 580 | // state changed. Call `setConnectedState` directly. |
| 581 | const status_t status = setConnectedState(port, false /*connected*/); |
| 582 | if (status == NO_ERROR) { |
| 583 | // Cache the port id so that it won't disconnect twice. |
| 584 | mDeviceDisconnectionNotified.insert(port->id); |
| 585 | } |
| 586 | return status; |
| 587 | } |
| 588 | |
Mikhail Naganov | 516d398 | 2022-02-01 23:53:59 +0000 | [diff] [blame] | 589 | status_t DeviceHalHidl::setConnectedState(const struct audio_port_v7 *port, bool connected) { |
Mikhail Naganov | 2d81489 | 2023-04-24 13:06:04 -0700 | [diff] [blame] | 590 | using ::android::hardware::audio::common::COMMON_TYPES_CPP_VERSION::AudioPort; |
Andy Hung | 224f82f | 2022-03-22 00:00:49 -0700 | [diff] [blame] | 591 | TIME_CHECK(); |
Mikhail Naganov | 516d398 | 2022-02-01 23:53:59 +0000 | [diff] [blame] | 592 | if (mDevice == 0) return NO_INIT; |
jiabin | c004863 | 2023-04-27 22:04:31 +0000 | [diff] [blame] | 593 | if (!connected && mDeviceDisconnectionNotified.erase(port->id) > 0) { |
| 594 | // For device disconnection, APM will first call `prepareToDisconnectExternalDevice` and |
| 595 | // then call `setConnectedState`. However, in HIDL HAL, there is no API for |
| 596 | // `prepareToDisconnectExternalDevice`. In that case, HIDL HAL will call `setConnectedState` |
| 597 | // when calling `prepareToDisconnectExternalDevice`. Do not call to the HAL if previous |
| 598 | // call is successful. Also remove the cache here to avoid a large cache after a long run. |
| 599 | return NO_ERROR; |
| 600 | } |
Mikhail Naganov | 516d398 | 2022-02-01 23:53:59 +0000 | [diff] [blame] | 601 | #if MAJOR_VERSION == 7 && MINOR_VERSION == 1 |
| 602 | if (supportsSetConnectedState7_1) { |
| 603 | AudioPort hidlPort; |
| 604 | if (status_t result = HidlUtils::audioPortFromHal(*port, &hidlPort); result != NO_ERROR) { |
| 605 | return result; |
| 606 | } |
| 607 | Return<Result> ret = mDevice->setConnectedState_7_1(hidlPort, connected); |
| 608 | if (!ret.isOk() || ret != Result::NOT_SUPPORTED) { |
| 609 | return processReturn("setConnectedState_7_1", ret); |
| 610 | } else if (ret == Result::OK) { |
| 611 | return NO_ERROR; |
| 612 | } |
| 613 | supportsSetConnectedState7_1 = false; |
| 614 | } |
| 615 | #endif |
| 616 | DeviceAddress hidlAddress; |
| 617 | if (status_t result = CoreUtils::deviceAddressFromHal( |
| 618 | port->ext.device.type, port->ext.device.address, &hidlAddress); |
| 619 | result != NO_ERROR) { |
| 620 | return result; |
| 621 | } |
Ping Tsai | 2a5a524 | 2024-08-16 13:39:10 +0800 | [diff] [blame] | 622 | Return<Result> ret = mDevice->setConnectedState(hidlAddress, connected); |
| 623 | if (ret.isOk() || ret == Result::NOT_SUPPORTED) { |
| 624 | // The framework is only interested in errors occurring due to connection state handling, |
| 625 | // so it can decide whether retrying is needed. If the HAL does not support this operation, |
| 626 | // it's not an error. |
| 627 | return NO_ERROR; |
| 628 | } |
| 629 | return processReturn("setConnectedState", ret); |
Mikhail Naganov | 516d398 | 2022-02-01 23:53:59 +0000 | [diff] [blame] | 630 | } |
| 631 | |
Ytai Ben-Tsvi | 48287b5 | 2021-12-01 15:07:11 -0800 | [diff] [blame] | 632 | error::Result<audio_hw_sync_t> DeviceHalHidl::getHwAvSync() { |
Andy Hung | 224f82f | 2022-03-22 00:00:49 -0700 | [diff] [blame] | 633 | TIME_CHECK(); |
Ytai Ben-Tsvi | 48287b5 | 2021-12-01 15:07:11 -0800 | [diff] [blame] | 634 | if (mDevice == 0) return NO_INIT; |
| 635 | audio_hw_sync_t value; |
| 636 | Result result; |
| 637 | Return<void> ret = mDevice->getHwAvSync([&value, &result](Result r, audio_hw_sync_t v) { |
| 638 | value = v; |
| 639 | result = r; |
| 640 | }); |
| 641 | RETURN_IF_ERROR(processReturn("getHwAvSync", ret, result)); |
| 642 | return value; |
| 643 | } |
| 644 | |
Andy Hung | 61589a4 | 2021-06-16 09:37:53 -0700 | [diff] [blame] | 645 | status_t DeviceHalHidl::dump(int fd, const Vector<String16>& args) { |
Andy Hung | 224f82f | 2022-03-22 00:00:49 -0700 | [diff] [blame] | 646 | TIME_CHECK(); |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 647 | if (mDevice == 0) return NO_INIT; |
| 648 | native_handle_t* hidlHandle = native_handle_create(1, 0); |
| 649 | hidlHandle->data[0] = fd; |
Andy Hung | 61589a4 | 2021-06-16 09:37:53 -0700 | [diff] [blame] | 650 | hidl_vec<hidl_string> hidlArgs; |
| 651 | argsFromHal(args, &hidlArgs); |
| 652 | Return<void> ret = mDevice->debug(hidlHandle, hidlArgs); |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 653 | native_handle_delete(hidlHandle); |
Andy Hung | e72ff02 | 2021-08-16 10:16:15 -0700 | [diff] [blame] | 654 | |
| 655 | // TODO(b/111997867, b/177271958) Workaround - remove when fixed. |
| 656 | // A Binder transmitted fd may not close immediately due to a race condition b/111997867 |
| 657 | // when the remote binder thread removes the last refcount to the fd blocks in the |
| 658 | // kernel for binder activity. We send a Binder ping() command to unblock the thread |
| 659 | // and complete the fd close / release. |
| 660 | // |
| 661 | // See DeviceHalHidl::dump(), EffectHalHidl::dump(), StreamHalHidl::dump(), |
| 662 | // EffectsFactoryHalHidl::dumpEffects(). |
| 663 | |
| 664 | (void)mDevice->ping(); // synchronous Binder call |
| 665 | |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 666 | return processReturn("dump", ret); |
| 667 | } |
| 668 | |
Vlad Popa | 03bd5bc | 2023-01-17 16:16:51 +0100 | [diff] [blame] | 669 | #if MAJOR_VERSION == 7 && MINOR_VERSION == 1 |
| 670 | status_t DeviceHalHidl::getSoundDoseInterface(const std::string& module, |
| 671 | ::ndk::SpAIBinder* soundDoseBinder) { |
| 672 | if (mSoundDoseWrapper->mSoundDose != nullptr) { |
| 673 | *soundDoseBinder = mSoundDoseWrapper->mSoundDose->asBinder(); |
| 674 | return OK; |
| 675 | } |
| 676 | |
| 677 | if (mSoundDoseWrapper->mSoundDoseFactory == nullptr) { |
| 678 | std::string interface = |
| 679 | std::string(ISoundDoseFactory::descriptor) + kSoundDoseInterfaceModule.data(); |
| 680 | AIBinder* binder = AServiceManager_checkService(interface.c_str()); |
| 681 | if (binder == nullptr) { |
| 682 | ALOGW("%s service %s doesn't exist", __func__, interface.c_str()); |
| 683 | return NO_INIT; |
| 684 | } |
| 685 | mSoundDoseWrapper->mSoundDoseFactory = |
| 686 | ISoundDoseFactory::fromBinder(ndk::SpAIBinder(binder)); |
| 687 | } |
| 688 | |
| 689 | auto result = mSoundDoseWrapper->mSoundDoseFactory->getSoundDose( |
| 690 | module, &mSoundDoseWrapper->mSoundDose); |
| 691 | if (!result.isOk()) { |
| 692 | ALOGW("%s could not get sound dose interface: %s", __func__, result.getMessage()); |
| 693 | return BAD_VALUE; |
| 694 | } |
| 695 | |
| 696 | if (mSoundDoseWrapper->mSoundDose == nullptr) { |
| 697 | ALOGW("%s standalone sound dose interface is not implemented", __func__); |
| 698 | *soundDoseBinder = nullptr; |
| 699 | return OK; |
| 700 | } |
| 701 | |
| 702 | *soundDoseBinder = mSoundDoseWrapper->mSoundDose->asBinder(); |
| 703 | ALOGI("%s using standalone sound dose interface", __func__); |
| 704 | return OK; |
| 705 | } |
| 706 | #else |
| 707 | status_t DeviceHalHidl::getSoundDoseInterface(const std::string& module, |
| 708 | ::ndk::SpAIBinder* soundDoseBinder) { |
| 709 | (void)module; // avoid unused param |
| 710 | (void)soundDoseBinder; // avoid unused param |
| 711 | return INVALID_OPERATION; |
| 712 | } |
| 713 | #endif |
| 714 | |
Eric Laurent | 7af6ee7 | 2023-06-29 11:44:54 +0200 | [diff] [blame] | 715 | status_t DeviceHalHidl::supportsBluetoothVariableLatency(bool* supports) { |
| 716 | if (supports == nullptr) { |
| 717 | return BAD_VALUE; |
| 718 | } |
| 719 | *supports = false; |
| 720 | |
| 721 | String8 reply; |
| 722 | status_t status = getParameters( |
| 723 | String8(AUDIO_PARAMETER_BT_VARIABLE_LATENCY_SUPPORTED), &reply); |
| 724 | if (status != NO_ERROR) { |
| 725 | return status; |
| 726 | } |
| 727 | AudioParameter replyParams(reply); |
| 728 | String8 trueOrFalse; |
| 729 | status = replyParams.get( |
| 730 | String8(AUDIO_PARAMETER_BT_VARIABLE_LATENCY_SUPPORTED), trueOrFalse); |
| 731 | if (status != NO_ERROR) { |
| 732 | return status; |
| 733 | } |
| 734 | *supports = trueOrFalse == AudioParameter::valueTrue; |
| 735 | return NO_ERROR; |
| 736 | } |
jiabin | 12537fc | 2023-10-12 17:56:08 +0000 | [diff] [blame] | 737 | |
| 738 | namespace { |
| 739 | |
| 740 | status_t getParametersFromStream( |
| 741 | sp<StreamHalInterface> stream, |
| 742 | const char* parameters, |
| 743 | const char* extraParameters, |
| 744 | String8* reply) { |
| 745 | String8 request(parameters); |
| 746 | if (extraParameters != nullptr) { |
| 747 | request.append(";"); |
| 748 | request.append(extraParameters); |
| 749 | } |
| 750 | status_t status = stream->getParameters(request, reply); |
| 751 | if (status != NO_ERROR) { |
| 752 | ALOGW("%s, failed to query %s, status=%d", __func__, parameters, status); |
| 753 | return status; |
| 754 | } |
| 755 | AudioParameter repliedParameters(*reply); |
| 756 | status = repliedParameters.get(String8(parameters), *reply); |
| 757 | if (status != NO_ERROR) { |
| 758 | ALOGW("%s: failed to retrieve %s, bailing out", __func__, parameters); |
| 759 | } |
| 760 | return status; |
| 761 | } |
| 762 | |
| 763 | } // namespace |
| 764 | |
| 765 | status_t DeviceHalHidl::getAudioMixPort(const struct audio_port_v7 *devicePort, |
| 766 | struct audio_port_v7 *mixPort) { |
| 767 | // For HIDL HAL, querying mix port information is not supported. If the HAL supports |
| 768 | // `getAudioPort` API to query the device port attributes, use the structured audio profiles |
| 769 | // that have the same attributes reported by the `getParameters` API. Otherwise, only use |
| 770 | // the attributes reported by `getParameters` API. |
| 771 | struct audio_port_v7 temp = *devicePort; |
| 772 | AudioProfileAttributesMultimap attrsFromDevice; |
jiabin | 19a3d72 | 2024-03-15 16:09:36 +0000 | [diff] [blame] | 773 | bool supportsPatches; |
| 774 | if (supportsAudioPatches(&supportsPatches) == OK && supportsPatches) { |
| 775 | // The audio patches are supported since HAL 3.0, which is the same HAL version |
| 776 | // requirement for 'getAudioPort' API. |
| 777 | if (getAudioPort(&temp) == NO_ERROR) { |
| 778 | attrsFromDevice = createAudioProfilesAttrMap(temp.audio_profiles, 0 /*first*/, |
| 779 | temp.num_audio_profiles); |
| 780 | } |
jiabin | 12537fc | 2023-10-12 17:56:08 +0000 | [diff] [blame] | 781 | } |
| 782 | auto streamIt = mStreams.find(mixPort->ext.mix.handle); |
| 783 | if (streamIt == mStreams.end()) { |
| 784 | return BAD_VALUE; |
| 785 | } |
| 786 | auto stream = streamIt->second.promote(); |
| 787 | if (stream == nullptr) { |
| 788 | return BAD_VALUE; |
| 789 | } |
| 790 | |
| 791 | String8 formatsStr; |
jiabin | 19a3d72 | 2024-03-15 16:09:36 +0000 | [diff] [blame] | 792 | status_t status = getParametersFromStream( |
jiabin | 12537fc | 2023-10-12 17:56:08 +0000 | [diff] [blame] | 793 | stream, AudioParameter::keyStreamSupportedFormats, nullptr /*extraParameters*/, |
| 794 | &formatsStr); |
| 795 | if (status != NO_ERROR) { |
| 796 | return status; |
| 797 | } |
| 798 | FormatVector formats = formatsFromString(formatsStr.c_str()); |
| 799 | |
| 800 | mixPort->num_audio_profiles = 0; |
| 801 | for (audio_format_t format : formats) { |
| 802 | if (mixPort->num_audio_profiles >= AUDIO_PORT_MAX_AUDIO_PROFILES) { |
| 803 | ALOGW("%s, too many audio profiles", __func__); |
| 804 | break; |
| 805 | } |
| 806 | AudioParameter formatParameter; |
| 807 | formatParameter.addInt(String8(AudioParameter::keyFormat), format); |
| 808 | |
| 809 | String8 samplingRatesStr; |
| 810 | status = getParametersFromStream( |
| 811 | stream, AudioParameter::keyStreamSupportedSamplingRates, |
| 812 | formatParameter.toString(), &samplingRatesStr); |
| 813 | if (status != NO_ERROR) { |
| 814 | // Failed to query supported sample rate for current format, may succeed with |
| 815 | // other formats. |
| 816 | ALOGW("Skip adding format=%#x, status=%d", format, status); |
| 817 | continue; |
| 818 | } |
| 819 | SampleRateSet sampleRatesFromStream = samplingRatesFromString(samplingRatesStr.c_str()); |
| 820 | if (sampleRatesFromStream.empty()) { |
| 821 | ALOGW("Skip adding format=%#x as the returned sampling rates are empty", format); |
| 822 | continue; |
| 823 | } |
| 824 | String8 channelMasksStr; |
| 825 | status = getParametersFromStream( |
| 826 | stream, AudioParameter::keyStreamSupportedChannels, |
| 827 | formatParameter.toString(), &channelMasksStr); |
| 828 | if (status != NO_ERROR) { |
| 829 | // Failed to query supported channel masks for current format, may succeed with |
| 830 | // other formats. |
| 831 | ALOGW("Skip adding format=%#x, status=%d", format, status); |
| 832 | continue; |
| 833 | } |
| 834 | ChannelMaskSet channelMasksFromStream = channelMasksFromString(channelMasksStr.c_str()); |
| 835 | if (channelMasksFromStream.empty()) { |
| 836 | ALOGW("Skip adding format=%#x as the returned channel masks are empty", format); |
| 837 | continue; |
| 838 | } |
| 839 | |
| 840 | // For an audio format, all audio profiles from the device port with the same format will |
| 841 | // be added to mix port after filtering sample rates, channel masks according to the reply |
| 842 | // of getParameters API. If there is any sample rate or channel mask reported by |
| 843 | // getParameters API but not reported by the device, additional audio profiles will be |
| 844 | // added. |
| 845 | populateAudioProfiles(attrsFromDevice, format, channelMasksFromStream, |
| 846 | sampleRatesFromStream, mixPort->audio_profiles, |
| 847 | &mixPort->num_audio_profiles); |
| 848 | } |
| 849 | |
| 850 | return NO_ERROR; |
| 851 | } |
| 852 | |
| 853 | void DeviceHalHidl::cleanupStreams() { |
| 854 | for (auto it = mStreams.begin(); it != mStreams.end();) { |
| 855 | if (it->second.promote() == nullptr) { |
| 856 | it = mStreams.erase(it); |
| 857 | } else { |
| 858 | ++it; |
| 859 | } |
| 860 | } |
| 861 | } |
| 862 | |
Kevin Rocard | 4bcd67f | 2018-02-28 14:33:38 -0800 | [diff] [blame] | 863 | } // namespace android |